php实现监控varnish缓存服务器的状态

yipeiwu_com6年前服务器

当varnish和网站部署在同一台服务器上的时候,我们不可能随时登录上服务器去查看varnish的命中率,没想到有大神早就写了出来,今天就分享给大家,使用网页查看varnish命中率。

系统:centos 5.x
软件:varnish-3.0.x

ps:3.0以下的版本可以通过Socket连接到Varnish管理端口,通过stat命令查看,3.0以上没有stat命令,只能通过下面的方法解决。

复制代码 代码如下:

<?php
$outfile=shell_exec("/usr/bin/varnishstat -x");
$xml=simplexml_load_string($outfile);
echo $xml->getName() . "<br />";
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"] . "<br />";
  echo "client_req: ".$arys["client_req"] . "<br />";
  echo "cache_hit: ".$arys["cache_hit"] . "<br />";
  echo "cache_miss: ".$arys["cache_miss"] . "<br />";
  echo "Cache hit rate: ".round(($arys["cache_hit"]/$arys["client_req"])*100)." % <br/>";
  echo "LRU nuked objects: ".$arys[n_lru_nuked]."<br/>";
  echo " ".byteReduce($arys["s_bodybytes"]+$arys["s_hdrbytes"])." Acc Content (".byteReduce($arys["s_hdrbytes"])." header ".byteReduce($arys["s_bodybytes"])." Body)";
?>

效果如下:

ps:为了查看实时情况,可以在这监控页加个html定时刷新.
好了,这样就方便我们随时查看varnish的状态了.

相关文章

Linux系统中设置多版本PHP共存配合Nginx服务器使用

应用环境 LNMP的环境,当前PHP版本5.3.8,遇到一个应用需求只支持PHP 5.2.x,又希望保持现有应用还是用PHP 5.3.8。也就是说需要两个版本的PHP同时存在,供ngin...

在windows服务器开启php的gd库phpinfo中未发现

在windows服务器开启php的gd库时,使用cgi之后phpinfo()得到的结果中 Configure Command 中并没有出现gd. Configure Command 后显...

centos+nginx+uwsgi+Django实现IP+port访问服务器

centos+nginx+uwsgi+Django实现IP+port访问服务器

环境 MacBookAir 阿里云ESC: Centos7.0 nginx+1.16.1 uwsgi=2.0.18 django=2.2.7 服务器 进入阿里云...

Python实现简单http服务器

Python实现简单http服务器

写一个python脚本,实现简单的http服务器功能: 1.浏览器中输入网站地址:172.20.52.163:20014 2.server接到浏览器的请求后,读取本地的index.htm...

php实现在服务器上创建目录的方法

本文实例讲述了php实现在服务器上创建目录的方法。分享给大家供大家参考。具体分析如下: 下面的代码先判断目录是否存在,然后通过mkdir()函数在服务器上创建了一个目录 <&#...