Projet

Général

Profil

Révision b1ce664e

IDb1ce664ec782253fae2deea4cc10bc38dcf178b3
Parent dc892da5
Enfant 635d1284

Ajouté par mutante il y a presque 14 ans

Initial version

Voir les différences:

plugins/other/mediawiki
1
#!/usr/bin/php
2
<?php
3
# mediawiki plugin for munin v0.2
4
# Reads number of edits,views,articles,pages,users,admins and images from a Mediawiki
5
# http://www.mediawiki.org/wiki/MediaWiki | http://munin.projects.linpro.no/wiki
6
# by mutante of S23 | http://s23.org/wiki | greets to hundfred
7
# 2007-02-12 | v0.1 first version, didnt really work yet
8
# 2007-02-16 | v0.2 introduced different retrieval methods, seperate graphs for the different values that can be symlinked..
9

  
10
# What you need to config:
11
# - a getmethod
12
# -- if its the mysql method, a valid dbhost,dbname,dbuser and dbpass
13
# -- if its an URL grabbing method, the URL to your Special:Statistics?action=raw page
14
# - the name of your wiki
15
# Read below for details
16

  
17
### Get Method - There are different ways to get the stats from Mediawiki:
18

  
19
## A - Database
20
# getmethod="mysql"
21

  
22
# reads directly from the Mediawiki MySQL database from table "site_stats"
23
# Note that this may not give you accurate numbers depending on your Mediawiki version.
24
# Mediawiki is currently not using this table itself to generate (all) numbers for Special:Statistics?action=raw
25
# but this may change in the near future
26

  
27
# The database method needs a valid mysql user to connect to the wiki database
28
# Comment this out, if you use an URL method or supply the database credentials inside this script
29

  
30
# require_once("/home/mutante/wiki_mysql_conf.php");
31

  
32
# I include the database settings from elsewhere, so i dont have to show the password in /usr/share/..
33
# I also set "[mediawiki] user mutante" in plugin-conf.d/ so that my user can read the config
34
# alternatively set them in here like:
35

  
36
# $dbhost="localhost";
37
# $dbname="wikidb";
38
# $dbuser="wikiuser";
39
# $dbpass="yourpassword";
40

  
41

  
42
## B - URL reading
43
# These methods all retrieve the Special:Statistics?action=raw URL from Mediawiki via the webserver
44
# This is the preferred method to get accurate stats currently, because Mediawiki doesnt use site_stats correctly atm
45

  
46
# getmethod="curl"
47
# uses curl via libcurl from PHP, should be fastest but you need the lib installed. if it works, use this.
48
# if it fails you may try one of the following ones and test what works for you
49

  
50
# getmethod="file_get_contents"
51
# uses the PHP function file_get_contents() read the Special:Statistics?action=raw URL from the webserver
52

  
53
# getmethod="fgets"
54
# uses the PHP function fgets() read the Special:Statistics?action=raw URL from the webserver
55

  
56
# getmethod="fopen"
57
# uses the PHP function fopen() to read the Special:Statistics?action=raw URL from the webserver
58

  
59
# getmethod="lynx"
60
# uses "lynx -dump" to read the Special:Statistics?action=raw URL from the webserver
61
# probably slow, if all others fail...
62

  
63

  
64

  
65
## CONFIG HERE
66

  
67
$getmethod="curl";
68

  
69
# IF you use one of the URL methods you need to supply your Special:Statistics?action=raw URL
70

  
71
$statsurl="http://s23.org/wiki/Special:Statistics?action=raw";
72

  
73
# Name of your wiki
74

  
75
$wikiname="S23-Wiki";
76

  
77
## END CONFIG
78

  
79

  
80

  
81
# Parsing function for the URL retrieving methods
82

  
83
function parsebuffer($buffer)
84
{
85
$pieces = explode(";",$buffer);
86
$total = explode("=",$pieces[0]);
87
$total = $total[1];
88
$good = explode("=",$pieces[1]);
89
$good = $good[1];
90
$views = explode("=",$pieces[2]);
91
$views = $views[1];
92
$edits = explode("=",$pieces[3]);
93
$edits = $edits[1];
94
$users = explode("=",$pieces[4]);
95
$users = $users[1];
96
$images = explode("=",$pieces[6]);
97
$images = $images[1];
98
$images = explode("<",$images);
99
$images = $images[0];
100
$admins = explode("=",$pieces[5]);
101
$admins = $admins[1];
102
$admins = trim($admins);
103
return array ($total,$good,$views,$edits,$users,$images,$admins);
104
}
105

  
106
# Output
107

  
108
# Check the filename suffix  _ (from the symlink) to decide which value to output
109
# symlink the file for each value you want displayed
110
# example: ln -s /usr/share/munin/plugins/mediawiki /etc/munin/plugins/mediawiki_views
111

  
112
$basename = preg_replace( '/^.+[\\\\\\/]/', '', $_SERVER['PHP_SELF'] );
113
$suffix=explode("_",$basename);
114
$suffix=$suffix[1];
115

  
116

  
117
# Print the config if called as "mediawiki config"
118
switch ($argv[1]) {
119
case config:
120

  
121
print <<<CONFIG
122
graph_title $wikiname $suffix
123
graph_vlabel number
124
graph_category wiki
125
graph_scale no
126
graph_info Reads the total number of $suffix from $wikiname\n
127
CONFIG;
128

  
129

  
130
switch ($suffix) {
131

  
132
case views:
133
print <<<VIEWS
134
views.info Total number of page views
135
views.label views
136
views.type COUNTER\n
137
VIEWS;
138
break;
139

  
140
case edits:
141
print <<<EDITS
142
edits.info Total number of page edits
143
edits.label edits
144
edits.type COUNTER\n
145
EDITS;
146
break;
147

  
148
case articles:
149
print <<<ARTICLES
150
articles.info Total number of 'good' pages (articles)
151
articles.label articles
152
articles.type GAUGE\n
153
ARTICLES;
154
break;
155

  
156
case pages:
157
print <<<PAGES
158
pages.info Total number of all pages
159
pages.label pages
160
pages.type GAUGE\n
161
PAGES;
162
break;
163

  
164
case users:
165
print <<<USERS
166
users.info Total number of user accounts
167
users.label users
168
users.type GAUGE\n
169
USERS;
170
break;
171

  
172
case images:
173
print <<<IMAGES
174
images.info Total number of uploaded images
175
images.label images
176
images.type GAUGE\n
177
IMAGES;
178
break;
179

  
180
case admins:
181
print <<<ADMINS
182
admins.info Total number of admins (sysops)
183
admins.label admins
184
admins.type GAUGE\n
185
ADMINS;
186
break;
187

  
188
default:
189
print <<<ERROR
190
Error: link me as mediawiki_<type>, where type can be one of: views, edits, articles, pages, users, images or admins.\n
191
ERROR;
192
}
193

  
194

  
195
break;
196

  
197
default:
198

  
199
# Default Output
200

  
201
# The different methods to grab stats ..
202

  
203
switch ($getmethod) {
204

  
205
case mysql:
206

  
207
mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
208
mysql_select_db("$dbname") or die(mysql_error());
209

  
210
$query="select * from site_stats";
211
$result = mysql_query("$query") or die(mysql_error());
212
$row = mysql_fetch_array( $result );
213

  
214
$views=$row['ss_total_views'];
215
$edits=$row['ss_total_edits'];
216
$articles=$row['ss_good_articles'];
217
$pages=$row['ss_total_pages'];
218
$users=$row['ss_users'];
219
$images=$row['ss_images'];
220

  
221
break;
222

  
223
case curl:
224

  
225
$ch = curl_init();
226
curl_setopt($ch, CURLOPT_URL, "$statsurl");
227
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
228
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
229
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
230
curl_setopt($ch, CURLOPT_HEADER, false);
231
$buffer = curl_exec($ch);
232
curl_close($ch);
233
break;
234

  
235
case fopen:
236
$buffer = fopen($statsurl,"r");
237
break;
238

  
239
case file_get_contents:
240
$buffer = file_get_contents($statsurl);
241
break;
242

  
243
case fgets:
244
$buffer = fgets($statsurl);
245
break;
246

  
247
case lynx:
248
$buffer = `lynx -dump "$statsurl"`;
249
break;
250

  
251
default:
252
$buffer = file_get_contents($statsurl);
253
}
254

  
255
# Parse
256
$buffer=trim($buffer);
257
list($total,$good,$views,$edits,$users,$images,$admins) = parsebuffer($buffer);
258

  
259
# Output
260

  
261
# Check the filename suffix  _ (from the symlink) to decide which value to output
262
# symlink the file for each value you want displayed
263
# example: ln -s /usr/share/munin/plugins/mediawiki /etc/munin/plugins/mediawiki_views
264

  
265
$basename = preg_replace( '/^.+[\\\\\\/]/', '', $_SERVER['PHP_SELF'] );
266
$suffix=explode("_",$basename);
267
$suffix=$suffix[1];
268

  
269
switch ($suffix) {
270

  
271
case views:
272
print "views.value $views\n";
273
break;
274

  
275
case edits:
276
print "edits.value $edits\n";
277
break;
278

  
279
case articles:
280
print "articles.value $good\n";
281
break;
282

  
283
case pages:
284
print "pages.value $total\n";
285
break;
286

  
287
case users:
288
print "users.value $users\n";
289
break;
290

  
291
case images:
292
print "images.value $images\n";
293
break;
294

  
295
case admins:
296
print "admins.value $admins\n";
297

  
298
default:
299
print "link me as mediawiki_<type>, where type can be one of: views, edits, articles, pages, users, images or admins. \n";
300
}
301

  
302
}
303
?>
304

  

Formats disponibles : Unified diff