Php monitors the status of the varnish cache server

Source: Internet
Author: User
Tags varnish
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.