Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / moinmoin / moinmoin_pages @ 73817ba8

Historique | Voir | Annoter | Télécharger (3,84 ko)

1
#! /usr/local/bin/python
2

    
3
# Overview
4
# --------
5
#
6
# this is a munin plugin that lists the number of pages (including ACL-protected pages) in all wikis of a MoinMoin wikifarm
7
#
8
# Installation
9
# ------------
10
#
11
# Put this in your munin plugins directory. You probably want to set the following block in plugin-conf.d/munin-node so that it runs under the riht user for your wiki:
12
#
13
# [moinmoin_*]
14
# user www
15
#
16
# Implementation notes
17
# --------------------
18
#
19
# it is quite koumbit-specific:
20
#  1. the wikifarm config is hardcoded
21
#  2. it relies on the "wikilist.py" file to contain the list of wiki -> url patterns
22
#  3. it assumes the url patterns are simple enough that they are decodable into an url
23
#
24
# also note that it reuses code from MoinMoin/wikimacro.py's SystemInfo macro
25
#
26
# finally, i tried using XMLRPC instead of native functions to fetch the data, but it ended up being slower. For the record, here is what the getPageList() call would have looked like:
27
# xmlrpclib.ServerProxy("http://wiki.koumbit.net/?action=xmlrpc2").getAllPages()
28
#
29
# the quick benchmark i did yieled those results for the getAllPages() vs getPageList() calls:
30
# xmlrpc:         2.35 real         0.12 user         0.04 sys
31
# native:         1.44 real         1.07 user         0.35 sys
32
#
33
# so the plugin is spending more time in the CPU (all time, actually), but it's doing in faster. it is highly possible that the CPU time spared in XMLRPC is in fact used by the server
34
#
35
# (C) Copyleft 2007, The Anarcat <anarcat@koumbit.org>
36
# Licensed under the GPLv2 or any later version
37

    
38
import sys, operator, os
39

    
40
os.chdir('/export/wiki/config')
41
sys.path.insert(0, '/export/wiki/config')
42

    
43
from MoinMoin import wikiutil
44
from MoinMoin.Page import Page
45
from farmconfig import wikis
46
from re import sub
47
import farmconfig
48

    
49
from MoinMoin.request import RequestCLI
50

    
51
def _formatInReadableUnits(size):
52
	size = float(size)
53
	unit = u' Byte'
54
	if size > 9999:
55
		unit = u' KiB'
56
		size /= 1024
57
	if size > 9999:
58
		unit = u' MiB'
59
		size /= 1024
60
	if size > 9999:
61
		unit = u' GiB'
62
		size /= 1024
63
	return u"%.1f %s" % (size, unit)
64

    
65
def _getDirectorySize(path):
66
            try:
67
                dirsize = 0
68
                for root, dirs, files in os.walk(path):
69
                    dirsize += sum([os.path.getsize(os.path.join(root, name)) for name in files])
70
            except EnvironmentError, e:
71
                dirsize = -1
72
            return dirsize
73

    
74
def main():
75
    for wiki in wikis:
76
        name = wiki[0]
77
        url = wiki[1]
78
        # XXX, hack: transform the regexp into a canonical url
79
        # we need canonical urls in the config for this to be clean
80
        # look for (foo|bar) and replace with foo
81
        url = sub('\(([^\|]*)(\|[^\)]*\))+', '\\1', url)
82
        # remove common regexp patterns and slap a protocol to make this a real url
83
        url = sub('[\^\$]|(\.\*)', '', url)
84

    
85
        mod = getattr(__import__(name), 'Config')
86
        #print "Upgradeing wiki %s (%s)" % (getattr(mod, 'sitename'), url)
87

    
88
        request = RequestCLI(url)
89
        pagelist = request.rootpage.getPageList(user='')
90

    
91
        systemPages = [page for page in pagelist
92
                           if wikiutil.isSystemPage(request, page)]
93
        print(name + '.value ' + str(len(pagelist)-len(systemPages)))
94
        #totalsize = reduce(operator.add, [Page(request, name).size() for name in pagelist])
95
        #print('Accumulated page sizes' + _formatInReadableUnits(totalsize))
96

    
97
def config():
98
    print("""graph_title Wiki size
99
graph_vlabel Number of pages
100
graph_args --base 1000 -l 0
101
graph_scale no
102
graph_category wiki
103
graph_info The number of pages excludes system pages but includes ACL-protected pages.""")
104
    for wiki in wikis:
105
        name = wiki[0]
106
        mod = getattr(__import__(name), 'Config')
107
	print(name + '.label ' + getattr(mod, 'sitename'))
108

    
109
if __name__ == "__main__":
110
    if len(sys.argv) > 1 and sys.argv[1] == 'config':
111
        config()
112
    else:
113
        main()