Skip to content

Instantly share code, notes, and snippets.

@arekinath
Created August 18, 2012 06:31
Show Gist options
  • Save arekinath/3384869 to your computer and use it in GitHub Desktop.
Save arekinath/3384869 to your computer and use it in GitHub Desktop.
Observium minecraft monitor

Minecraft server monitoring plugin for Observium (http://www.observium.org/)

It uses the client "lobby ping" protocol to ping the server and get the number of connected players / max players and graphs these two numbers in Observium.

Some of us take our monitoring seriously for non-serious services, too! ;)

<?php
$rrd = $config['rrd_dir'] . "/" . $device['hostname'] . "/app-minecraft.rrd";
if (is_file($rrd))
{
$rrd_filename = $rrd;
}
include("includes/graphs/common.inc.php");
$scale_min = 0;
$colours = "mixed";
$nototal = 0;
$unit_text = "Players";
$array = array(
'max_players' => array('descr' => 'Capacity', 'colour' => '555555'),
'players' => array('descr' => 'Players', 'colour' => 'cc0000', 'areacolour' => 'ff999955'),
);
$i = 0;
if (is_file($rrd_filename))
{
foreach ($array as $ds => $vars)
{
$rrd_list[$i]['filename'] = $rrd_filename;
$rrd_list[$i]['descr'] = $vars['descr'];
$rrd_list[$i]['ds'] = $ds;
$rrd_list[$i]['colour'] = $vars['colour'];
if (!empty($vars['areacolour'])) { $rrd_list[$i]['areacolour'] = $vars['areacolour']; }
$i++;
}
} else {
echo("file missing: $file");
}
include("includes/graphs/generic_multi_line.inc.php");
?>
<?php
global $config;
$graphs = array('minecraft_players' => 'minecraft Players');
foreach ($graphs as $key => $text)
{
$graph_type = $key;
$graph_array['height'] = "100";
$graph_array['width'] = "215";
$graph_array['to'] = $config['time']['now'];
$graph_array['id'] = $app['app_id'];
$graph_array['type'] = "application_".$key;
echo('<h3>'.$text.'</h3>');
echo("<tr bgcolor='$row_colour'><td colspan=5>");
include("includes/print-graphrow.inc.php");
echo("</td></tr>");
}
?>
<?php
print " minecraft";
function minecraft_ping($host, $port=25565, $timeout=30) {
//Set up our socket
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$fp) return false;
//Send 0xFE: Server list ping
fwrite($fp, "\xFE");
//Read as much data as we can (max packet size: 241 bytes)
$d = fread($fp, 256);
//Check we've got a 0xFF Disconnect
if ($d[0] != "\xFF") return false;
//Remove the packet ident (0xFF) and the short containing the length of the string
$d = substr($d, 3);
//Decode UCS-2 string
$d = mb_convert_encoding($d, 'auto', 'UCS-2');
//Split into array
$d = explode("\xA7", $d);
//Return an associative array of values
return array(
'motd' => $d[0],
'players' => intval($d[1]),
'max_players' => intval($d[2]));
}
$minecraft = minecraft_ping($device['hostname']);
if ($minecraft) {
print "(ok: {$minecraft['players']}/{$minecraft['max_players']})";
$rrdfile = $config['rrd_dir'] . "/" . $device['hostname'] . "/app-minecraft.rrd";
if (!is_file($rrdfile)) {
rrdtool_create($rrdfile, "--step 300 \
DS:players:GAUGE:600:0:10000 \
DS:max_players:GAUGE:600:0:10000".$config['rrd_rra']);
}
rrdtool_update($rrdfile, "N:{$minecraft['players']}:{$minecraft['max_players']}");
} else {
print "(fail)";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment