#!/usr/bin/php
<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
/*
 ** REQUIRES PHP5-CLI **
 ---- DESCRIPTION ----
 wordpress plugin for munin
  it's a simple plugin to monitor users, comments, pingbacks
  and your posts from your wordpress homepage.

  Simply put your path of wp-config.php in your munin-node
  configuration and this plugin does the rest for you.
  Happy monitoring! :)

  This plugin was inspired by the existing wordpress plugin in bash/sh:
   https://github.com/munin-monitoring/contrib/blob/master/plugins/other/wordpress


 ---- CONFIGURATION ----
 You just need to provide the path to your
 wp-config.php of your wordpress installation.

 The configuration for munin-node is by default
 at: /etc/munin/plugin-conf.d/munin-node

 Example configuration:
 [wordpress]
 env.NAME Blog of Chuck Norris
 env.CONF /var/www/wordpress/wp-config.php

 or you can use:

 [wordpress]
 env.NAME Blog of Chuck Norris
 env.HOST 127.0.0.1
 env.USER blog
 env.PASS password
 env.DBNM wordpress
 env.TBPF wp_


 ---- More details ----
 @Author...: Patrik Kernstock
 @Version..: v1.1
 @Date.....: 26 November 2012
 @Web......: http://pkern.at
 @Support..: support@pkern.at
 @GitHub...: https://github.com/patschi/munin-plugins
 @License..: http://creativecommons.org/licenses/by-nc-sa/3.0/

*/
#%# family=auto
#%# capabilities=autoconf

// Get configuration variables
$d["name"] = getenv("NAME");
$d["conf"] = getenv("CONF");
$d["host"] = getenv("HOST");
$d["user"] = getenv("USER");
$d["pass"] = getenv("PASS");
$d["tbpf"] = getenv("TBPF");
$d["dbnm"] = getenv("DBNM");

if(!empty($d["conf"]) && !empty($d["name"])) {
  // INCLUDE wp-config.php
	define("ABSPATH", str_replace("wp-config.php", "", $d["conf"]));
	require_once($d["conf"]);
	$d["host"] = DB_HOST;
	$d["user"] = DB_USER;
	$d["pass"] = DB_PASSWORD;
	$d["dbnm"] = DB_NAME;
	$d["tbpf"] = $table_prefix;
}

if($argv[1] == "config") {
	echo 'graph_title wordpress statistic of '.$d["name"]."\n";
	echo 'graph_order users posts comments pingbacks'."\n";
	echo 'graph_vlabel Wordpress'."\n";
	echo 'graph_info some wordpress statistics of '.$d["name"]."\n";
	echo 'graph_category cms'."\n";
	echo 'users.label Users'."\n";
	echo 'posts.label Posts'."\n";
	echo 'posts.draw LINE3'."\n";
	echo 'comments.label Comments'."\n";
	echo 'pingbacks.label Pingbacks'."\n";
    exit(0);

}else if($argv[1] == "autoconf") {
	if(file_exists($d["conf"])) {
		echo "yes";
		exit(0);
	 }else{
		echo "no (config does not exist)";
		exit(1);
	}
}

if(!empty($d["conf"])) {
	if(!file_exists($d["conf"])) {
		echo "Error: config does not exist!";
		exit(1);
	}
}

// INIT VARIABLES
$users     = 0;
$posts     = 0;
$comments  = 0;
$pingbacks = 0;

// GET DATA
mysql_connect($d["host"], $d["user"], $d["pass"]) or die("Error: Failed to connect to the MySQL database!");
mysql_select_db($d["dbnm"]) or die("Error: Failed to select database!");

$users     = mysql_result(mysql_query("SELECT COUNT(*) FROM ".$d["tbpf"]."users;"), 0);
$posts     = mysql_result(mysql_query("SELECT COUNT(*) FROM ".$d["tbpf"]."posts    WHERE post_status='publish' AND post_password='' AND post_type='post';"), 0);
$comments  = mysql_result(mysql_query("SELECT COUNT(*) FROM ".$d["tbpf"]."comments WHERE comment_approved='1' AND comment_type='';"), 0);
$pingbacks = mysql_result(mysql_query("SELECT COUNT(*) FROM ".$d["tbpf"]."comments WHERE comment_approved='1' AND comment_type='pingback';"), 0);

// OUTPUT DATA
echo     "users.value ".$users    ."\n";
echo     "posts.value ".$posts    ."\n";
echo  "comments.value ".$comments ."\n";
echo "pingbacks.value ".$pingbacks."\n";

?>
