Home  >  Article  >  Backend Development  >  How to monitor Varnish cache server status in PHP?

How to monitor Varnish cache server status in PHP?

WBOY
WBOYOriginal
2016-07-25 08:52:10985browse
  1. $outfile=shell_exec("/usr/bin/varnishstat -x");
  2. $xml=simplexml_load_string($outfile);
  3. echo $xml->getName() . "
    ";
  4. foreach($xml->children() as $child)
  5. {
  6. //$tmpName="";
  7. foreach($child->children() as $subChild)
  8. {
  9. if ($subChild->getName() =="name" )
  10. {
  11. $tmpName=$subChild;
  12. }
  13. elseif ($subChild->getName() =="value" )
  14. {
  15. if ($tmpName!="")
  16. {
  17. $arys["$tmpName"]=$subChild;
  18. $tmpName="";
  19. }
  20. }
  21. else
  22. {
  23. continue;
  24. }
  25. }
  26. }
  27. function byteReduce($bytes)
  28. {
  29. if ($bytes>1099511627776)
  30. {
  31. return round($bytes/1099511627776)."TB";
  32. }
  33. else if ($bytes > 1073741824)
  34. {
  35. return round($bytes/1073741824)."GB";
  36. }
  37. else if ($bytes>1048576)
  38. {
  39. return round($bytes/1048576)."MB";
  40. }
  41. else if ($bytes>1024)
  42. {
  43. return round($bytes/1024)."KB";
  44. }
  45. else
  46. {
  47. return $bytes."B";
  48. }
  49. }
  50. echo "client_conn: ".$arys["client_conn"] . "
    ";
  51. echo "client_req: ".$arys["client_req"] . "
    ";
  52. echo "cache_hit: ".$arys["cache_hit"] . "
    ";
  53. echo "cache_miss: ".$arys["cache_miss"] . "
    ";
  54. echo "Cache hit rate: ".round(($arys["cache_hit"]/$arys["client_req"])*100)." %
    ";
  55. echo "LRU nuked objects: ".$arys[n_lru_nuked]."
    ";
  56. echo " ".byteReduce($arys["s_bodybytes"]+$arys["s_hdrbytes"])." Acc Content (".byteReduce($arys["s_hdrbytes"])." header ".byteReduce($arys["s_bodybytes"])." Body)";
  57. ?>
复制代码

效果,如下图: How to monitor Varnish cache server status in PHP?

备注,为了查看实时情况,可以在在监控页加个html定时刷新,可以随时查看varnish缓存服务器状态。



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn