#!/usr/bin/php
<?php
//error_reporting( E_ALL &!E_NOTICE); //to avoid of the crap generation

/*///////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
This file is a Munin plugin
Murmur users online grahpher PHP using ICE,
ver  alpha 0.1 2009.03.13 
author _KaszpiR_ kaszpir at gmail dot com
code is under GPL

Requirements:
- PHP installed in CLI (so you can run it in command line)
  Make sure the first line of this file points to the pworking php cli inrepreter
- ICE framework on server
- Murmur server with ICE enabled

/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
Configuration:
create a symlink to thie file in the following format:
    mumble-ice_ip_port_sid
where ip/port correspond to the ip and port on which the mumble ice interface listens to
sid is the server id we want to query (mumble can run multiple servers )

Script returns:
channels (num)
users_reg (num)
users_noreg )num)
//banlist (num)

///////////////////////////////////////////////////////////////////////////////////////////////////*/

$profile='Murmur11x';

// Define STDIN in case if it is not already defined by PHP for some reason 
if(!defined("STDIN"))
{
    define("STDIN", fopen('php://stdin','r'));
}

if(!defined("STDERR"))
{
    define("STDERR", fopen('php://stderr','w'));
}



list($dummy,$ip,$port,$serverid,$desc) = split("_",$argv[0],5);

if(!$ip) $ip="127.0.0.1";
if(!$port) $port=6502;
if(!$serverid) $serverid=1;

/////////////////////////////////////////////////////////////////////////////////////////////////////
//autoconf test
if(isset($argv[1]) && $argv[1] == "autoconf")
{
	if(FALSE)
	{
	}
	else 
	{
		fwrite(STDOUT, "No\n");
		fwrite(STDERR, "symlink ".$argv[0]." to somethilg like ".$argv[0]."_127.0.0.1_6502_1_description-here \n");
	}
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
//config, set rrd files
if(isset($argv[1]) && $argv[1] == "config")
{
	if(TRUE)
	{
	// yea dirty hack 
		echo "graph_title Mumble Users".($desc?" on ".str_replace("_"," ",$desc):"")."\n";
		echo "graph_vlabel Connected Users\n";
		echo "graph_category VoIP\n";
		echo "graph_info This graph shows the number of connected users on a murmur server\n";
		echo "channels.label Chans\n";
		echo "players.label Total Users\n";
		echo "registered.label Registerd\n";
		echo "unregistered.label Unregistered\n";
		echo "chanlinks.label Linked chans\n";
		echo "error.label Server status error\n";
		
		$arr=array("channels","players","registered","unregistered","chanlinks","error");
		foreach($arr as $field){
		    echo "".$field.".draw LINE1\n";
		    echo "".$field.".type GAUGE\n";
		}


		return 0;
		
	}else {
		echo "RTFM\n";
		return 1;
	}
	return 0;
}




$online_noreg=0;
$online_reg=0;
$channels=0;
$links=0;

Ice_loadProfile($profile);

try {
  $base = $ICE->stringToProxy("Meta:tcp -h ".$ip." -p ".$port);
  $meta = $base->ice_checkedCast("::Murmur::Meta");

  $servers = $meta->getBootedServers();
  $default = $meta->getDefaultConf();
  foreach($servers as $s) {
    $name = $s->getConf("registername");
    if (! $name) {
      $name =  $default["registername"];
    }

    if($s->id() !=$serverid) continue;

    $chanlist = $s->getChannels();
    $channels = count($chanlist);
    foreach($chanlist as $chan=>$c){
	$links+=count($c->links);
    }
    $players = $s->getPlayers();
    foreach($players as $id => $p) {
        if($p->playerid ==-1)
	    $online_noreg++;
        else
    	    $online_reg++;
    }
    }
} catch (Ice_LocalException $ex) {
    fwrite(STDERR,"ERROR: IP=".$ip.", Port=".$port.", Id=".$serverid."\n");
    fwrite(STDERR,$ex);
echo "channels.value 0\n";
echo "players.value 0\n";
echo "registered.value 0\n";
echo "unregistered.value 0\n";
echo "chanlinks.value ".$links."\n";
echo "error.value 1\n";

return 1;
}
echo "channels.value ".$channels."\n";
echo "players.value ".($online_noreg+$online_reg)."\n";
echo "registered.value ".$online_reg."\n";
echo "unregistered.value ".$online_noreg."\n";
echo "chanlinks.value ".$links."\n";
echo "error.value 0\n";
return 0;
?>