Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@winny-
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save winny-/9008512 to your computer and use it in GitHub Desktop.
Save winny-/9008512 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
// Config:
// [minecraft_users]
// env.host awesomeserver.com
// env.port 25566
error_reporting(E_ERROR | E_PARSE);
$host = getenv('host');
$host = $host ? $host : 'localhost';
$port = getenv('port');
$port = $port ? $port : '25565';
if ((count($argv) > 1) && ($argv[1] == 'config')) {
print("graph_title Connected players\n");
print("graph_vlabel players\n");
print("players.label Number of players\n");
print("max_players.label Max players\n");
print("graph_info Number of players connected to Minecraft\n");
print("graph_scale no\n");
print("graph_category minecraft\n");
exit();
}
/*
================
Server List Ping
================
An example of how to get a Minecraft server status's using a "Server List Ping" packet.
See details here: http://www.wiki.vg/Server_List_Ping
*/
function MC_packString($string)
{
return pack('n', strlen($string)) . mb_convert_encoding($string, 'UCS-2BE');
}
// This is needed since UCS-2 text rendered as UTF-8 contains unnecessary null bytes
// and could cause other components, especially string functions to blow up. Boom!
function MC_decodeUCS2BE($string)
{
return mb_convert_encoding($string, 'UTF-8', 'UCS-2BE');
}
function MC_serverListPing($hostname, $port=25565)
{
// 1. pack data to send
$request = pack('nc', 0xfe01, 0xfa) .
MC_packString('MC|PingHost') .
pack('nc', 7+2*strlen($hostname), 73) .
MC_packString($hostname) .
pack('N', 25565);
// 2. open communication socket and make transaction
$time = microtime(true);
$fp = stream_socket_client('tcp://' . $hostname . ':' . $port);
if (!$fp) {
return false;
}
fwrite($fp, $request);
$response = fread($fp, 2048);
fclose($fp);
$time = round((microtime(true)-$time)*1000);
// 3. unpack data and return
if (strpos($response, 0xFF) !== 0) {
return false;
}
$response = substr($response, 3);
$response = explode(pack('n', 0), $response);
return array(
'player_count' => MC_decodeUCS2BE($response[4]),
'player_max' => MC_decodeUCS2BE($response[5]),
'motd' => MC_decodeUCS2BE($response[3]),
'server_version' => MC_decodeUCS2BE($response[2]),
'protocol_version' => MC_decodeUCS2BE($response[1]),
'latency' => $time
);
}
// ============================================================
$reply = MC_serverListPing($host, $port);
print('players.value ' . $reply['player_count'] . "\n");
print('max_players.value ' . $reply['player_max'] . "\n");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment