"5", "usec" => "0"))) OR (!socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, Array("sec" => "5", "usec" => "0")))) {
die("Unable to set socket timeout");
}
if (@socket_connect($socket, $adminHost, $adminPort)) {
$data = "";
if (!$socket) {
die("Unable to open socket to " . $server . ":" . $port . "\n");
}
socket_write($socket, $command . "\n");
socket_recv($socket, $buffer, 65536, 0);
$data .= $buffer;
socket_close($socket);
return $data;
}
else {
return "Unable to connect: " . socket_strerror(socket_last_error()) . "\n";
}
}
// byteReduce(str) - this function converts a numeric value of bytes to a human readable format and returns the result
function byteReduce($bytes) {
// Terabytes
if ($bytes > 1099511627776) {
return round($bytes / 1099511627776) . "TB";
}
else if ($bytes > 1073741824) {
return round($bytes / 1073741824) . "GB";
}
else if ($bytes > 1048576) {
return round($bytes / 1048576) . "MB";
}
else if ($bytes > 1024) {
return round($bytes / 1024) . "KB";
}
else {
return $bytes . "B";
}
}
// This is where our main code starts
echo "
Statistics since last reset:
";
$stats = pollServer("stats");
if (substr($stats, 0, 3) == "200") { // If request was legitimate
// Clear all excessive white space and split by lines
$stats = preg_replace("/ {2,}/", "|", $stats);
$stats = preg_replace("/\n\|/", "\n", $stats);
$statsArray = explode("\n", $stats);
// Removes the first call return value and splits by pipe
array_shift($statsArray);
$statistics = array();
foreach ($statsArray as $stat) {
@$statVal = explode("|", $stat);
@$statistics[$statVal[1]] = $statVal[0];
}
unset($stats, $statsArray, $stat, $statVal);
// Start outputting statistics
echo "- " . $statistics["Client connections accepted"] . " clients served over " . $statistics["Client requests received"] . " requests";
echo "
- " . round(($statistics["Cache hits"] / $statistics["Client requests received"]) * 100) . "% of requests served from cache";
echo "
- " . byteReduce($statistics["Total header bytes"] + $statistics["Total body bytes"]) . " served (" . byteReduce($statistics["Total header bytes"]) . " headers, " . byteReduce($statistics["Total body bytes"]) . " content)";
// The following line is commented out because it only works when using file storage, I switched to malloc and this broke
// echo "
- " . byteReduce($statistics["bytes allocated"]) . " out of " . byteReduce($statistics["bytes allocated"] + $statistics["bytes free"]) . " used (" . round(($statistics["bytes allocated"] / ($statistics["bytes allocated"] + $statistics["bytes free"])) * 100) . "% usage)";
}
else {
echo "Unable to get stats, error was: \"" . $stats;
}
echo "
";
?>