munin-contrib/plugins/mediawiki/mediawiki

305 lines
7.5 KiB
PHP
Executable File

#!/usr/bin/php
<?php
# mediawiki plugin for munin v0.2
# Reads number of edits,views,articles,pages,users,admins and images from a Mediawiki
# http://www.mediawiki.org/wiki/MediaWiki | http://munin.projects.linpro.no/wiki
# by mutante of S23 | http://s23.org/wiki | greets to hundfred
# 2007-02-12 | v0.1 first version, didn't really work yet
# 2007-02-16 | v0.2 introduced different retrieval methods, separate graphs for the different values that can be symlinked..
# What you need to config:
# - a getmethod
# -- if its the mysql method, a valid dbhost,dbname,dbuser and dbpass
# -- if its an URL grabbing method, the URL to your Special:Statistics?action=raw page
# - the name of your wiki
# Read below for details
### Get Method - There are different ways to get the stats from Mediawiki:
## A - Database
# getmethod="mysql"
# reads directly from the Mediawiki MySQL database from table "site_stats"
# Note that this may not give you accurate numbers depending on your Mediawiki version.
# Mediawiki is currently not using this table itself to generate (all) numbers for Special:Statistics?action=raw
# but this may change in the near future
# The database method needs a valid mysql user to connect to the wiki database
# Comment this out, if you use an URL method or supply the database credentials inside this script
# require_once("/home/mutante/wiki_mysql_conf.php");
# I include the database settings from elsewhere, so i don't have to show the password in /usr/share/..
# I also set "[mediawiki] user mutante" in plugin-conf.d/ so that my user can read the config
# alternatively set them in here like:
# $dbhost="localhost";
# $dbname="wikidb";
# $dbuser="wikiuser";
# $dbpass="yourpassword";
## B - URL reading
# These methods all retrieve the Special:Statistics?action=raw URL from Mediawiki via the webserver
# This is the preferred method to get accurate stats currently, because Mediawiki does not use site_stats correctly atm
# getmethod="curl"
# uses curl via libcurl from PHP, should be fastest but you need the lib installed. if it works, use this.
# if it fails you may try one of the following ones and test what works for you
# getmethod="file_get_contents"
# uses the PHP function file_get_contents() read the Special:Statistics?action=raw URL from the webserver
# getmethod="fgets"
# uses the PHP function fgets() read the Special:Statistics?action=raw URL from the webserver
# getmethod="fopen"
# uses the PHP function fopen() to read the Special:Statistics?action=raw URL from the webserver
# getmethod="lynx"
# uses "lynx -dump" to read the Special:Statistics?action=raw URL from the webserver
# probably slow, if all others fail...
## CONFIG HERE
$getmethod="curl";
# IF you use one of the URL methods you need to supply your Special:Statistics?action=raw URL
$statsurl="http://s23.org/wiki/Special:Statistics?action=raw";
# Name of your wiki
$wikiname="S23-Wiki";
## END CONFIG
# Parsing function for the URL retrieving methods
function parsebuffer($buffer)
{
$pieces = explode(";",$buffer);
$total = explode("=",$pieces[0]);
$total = $total[1];
$good = explode("=",$pieces[1]);
$good = $good[1];
$views = explode("=",$pieces[2]);
$views = $views[1];
$edits = explode("=",$pieces[3]);
$edits = $edits[1];
$users = explode("=",$pieces[4]);
$users = $users[1];
$images = explode("=",$pieces[6]);
$images = $images[1];
$images = explode("<",$images);
$images = $images[0];
$admins = explode("=",$pieces[5]);
$admins = $admins[1];
$admins = trim($admins);
return array ($total,$good,$views,$edits,$users,$images,$admins);
}
# Output
# Check the filename suffix _ (from the symlink) to decide which value to output
# symlink the file for each value you want displayed
# example: ln -s /usr/share/munin/plugins/mediawiki /etc/munin/plugins/mediawiki_views
$basename = preg_replace( '/^.+[\\\\\\/]/', '', $_SERVER['PHP_SELF'] );
$suffix=explode("_",$basename);
$suffix=$suffix[1];
# Print the config if called as "mediawiki config"
switch ($argv[1]) {
case config:
print <<<CONFIG
graph_title $wikiname $suffix
graph_vlabel number
graph_category wiki
graph_scale no
graph_info Reads the total number of $suffix from $wikiname\n
CONFIG;
switch ($suffix) {
case views:
print <<<VIEWS
views.info Total number of page views
views.label views
views.type COUNTER\n
VIEWS;
break;
case edits:
print <<<EDITS
edits.info Total number of page edits
edits.label edits
edits.type COUNTER\n
EDITS;
break;
case articles:
print <<<ARTICLES
articles.info Total number of 'good' pages (articles)
articles.label articles
articles.type GAUGE\n
ARTICLES;
break;
case pages:
print <<<PAGES
pages.info Total number of all pages
pages.label pages
pages.type GAUGE\n
PAGES;
break;
case users:
print <<<USERS
users.info Total number of user accounts
users.label users
users.type GAUGE\n
USERS;
break;
case images:
print <<<IMAGES
images.info Total number of uploaded images
images.label images
images.type GAUGE\n
IMAGES;
break;
case admins:
print <<<ADMINS
admins.info Total number of admins (sysops)
admins.label admins
admins.type GAUGE\n
ADMINS;
break;
default:
print <<<ERROR
Error: link me as mediawiki_<type>, where type can be one of: views, edits, articles, pages, users, images or admins.\n
ERROR;
}
break;
default:
# Default Output
# The different methods to grab stats ..
switch ($getmethod) {
case mysql:
mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
mysql_select_db("$dbname") or die(mysql_error());
$query="select * from site_stats";
$result = mysql_query("$query") or die(mysql_error());
$row = mysql_fetch_array( $result );
$views=$row['ss_total_views'];
$edits=$row['ss_total_edits'];
$articles=$row['ss_good_articles'];
$pages=$row['ss_total_pages'];
$users=$row['ss_users'];
$images=$row['ss_images'];
break;
case curl:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$statsurl");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_HEADER, false);
$buffer = curl_exec($ch);
curl_close($ch);
break;
case fopen:
$buffer = fopen($statsurl,"r");
break;
case file_get_contents:
$buffer = file_get_contents($statsurl);
break;
case fgets:
$buffer = fgets($statsurl);
break;
case lynx:
$buffer = `lynx -dump "$statsurl"`;
break;
default:
$buffer = file_get_contents($statsurl);
}
# Parse
$buffer=trim($buffer);
list($total,$good,$views,$edits,$users,$images,$admins) = parsebuffer($buffer);
# Output
# Check the filename suffix _ (from the symlink) to decide which value to output
# symlink the file for each value you want displayed
# example: ln -s /usr/share/munin/plugins/mediawiki /etc/munin/plugins/mediawiki_views
$basename = preg_replace( '/^.+[\\\\\\/]/', '', $_SERVER['PHP_SELF'] );
$suffix=explode("_",$basename);
$suffix=$suffix[1];
switch ($suffix) {
case views:
print "views.value $views\n";
break;
case edits:
print "edits.value $edits\n";
break;
case articles:
print "articles.value $good\n";
break;
case pages:
print "pages.value $total\n";
break;
case users:
print "users.value $users\n";
break;
case images:
print "images.value $images\n";
break;
case admins:
print "admins.value $admins\n";
default:
print "link me as mediawiki_<type>, where type can be one of: views, edits, articles, pages, users, images or admins. \n";
}
}
?>