When varnish and the website are deployed on the same server, it is impossible for us to log in to the server at any time to check the hit rate of varnish. Unexpectedly, a master has written it long ago. Today I will share it with you, use the web page to check varnish Hit rate.
System: centos 5.x
Software: varnish-3.0.x
ps: Versions below 3.0 can connect to the Varnish management port through Socket and view it through the stat command. There is no stat command above 3.0 and can only be solved by the following method.
The code is as follows:
$outfile=shell_exec("/usr/bin/varnishstat -x");
$xml=simplexml_load_string($outfile);
echo $xml->getName() . "
";
foreach($xml->children() as $child)
{
//$tmpName="";
foreach($child->children() as $subChild)
{
if ($subChild->getName() == "name" )
{
$tmpName=$subChild;
}
else if ($subChild->getName() == "value" )
{
if ($tmpName!="")
{
$arys["$tmpName"]=$subChild;
$tmpName="";
}
}
else
{
continue;
}
}
}
function byteReduce($bytes)
{
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";
}
}
echo "client_conn: ".$arys["client_conn"] . "
";
echo "client_req: ".$arys["client_req"] . "
";
echo "cache_hit: ".$arys["cache_hit"] . "
";
echo "cache_miss: ".$arys["cache_miss"] . "
";
echo "Cache hit rate: ".round(($arys["cache_hit"]/$arys["client_req"])*100)." %
";
echo "LRU nuked objects: ".$arys[n_lru_nuked]."
";
echo " ".byteReduce($arys["s_bodybytes"] $arys["s_hdrbytes"])." Acc Content (".byteReduce($arys["s_hdrbytes"])." header ".byteReduce($arys["s_bodybytes "])." Body)";
?>
The effect is as follows:
ps: In order to check the real-time situation, you can add an html to this monitoring page to refresh regularly.
Okay, this will make it easier for us to check the status of varnish at any time.
http://www.bkjia.com/PHPjc/975881.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/975881.htmlTechArticlephp implements monitoring the status of the varnish cache server. This article mainly introduces the php implementation of monitoring the status of the varnish cache server, Varnish It is a high-performance open source HTTP accelerator that can replace...