This article describes how to monitor the status of varnish cache servers in php. Varnish is a high-performance open-source HTTP accelerator that can replace Squid, Nginx, and other servers, when varnish and the website are deployed on the same server, it is impossible for us to log on to the server at any time to check the varnish hit rate, today, we will share with you the varnish hit rate on the web page.
System: centos 5.x
Software: varnish-3.0.x
Ps: For versions earlier than 3.0, you can connect to the Varnish management Port through Socket. you can use the stat command to check whether the stat command is available in versions later than 3.0. you can only use the following method to solve this problem.
The code is as follows:
<? Php
$ 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: To view the real-time status, you can add an html to regularly refresh the page.
Now, we can check the varnish status at any time.