All pastes #1268381 Raw Edit

PHP Varnish Stats Generator

public php v1 · immutable
#1268381 ·published 2008-11-27 08:09 UTC
rendered paste body
<?php// This is just a code snippet written by Jason "Foxdie" Gaunt, its not meant to be executed, it may work as-is, it may not.// I freely acknowledge this code is unoptimised but it has worked in practice for 6 months :)// Lets define our connection details$adminHost = "127.0.0.1"; // IP address to connect to$adminPort = "2400"; // Port to connect to// pollServer(str) - this function connects to the management port, sends the command and returns the results, or an error on failurefunction pollServer($command) {	global $adminHost, $adminPort;		$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname("tcp"));	if ((!socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, Array("sec" => "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 resultfunction 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 startsecho "<div class=\"inner\"><br />Statistics since last reset:<ul>";$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 "<li>" . $statistics["Client connections accepted"] . " clients served over " . $statistics["Client requests received"] . " requests";	echo "<li>" . round(($statistics["Cache hits"] / $statistics["Client requests received"]) * 100) . "% of requests served from cache";	echo "<li>" . 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 "<li>" . 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 "</ul></div>";?>