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

yipeiwu_com4年前服务器

当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的状态了.

相关文章

Go语言基于Socket编写服务器端与客户端通信的实例

Go语言基于Socket编写服务器端与客户端通信的实例

在golang中,网络协议已经被封装的非常完好了,想要写一个Socket的Server,我们并不用像其他语言那样需要为socket、bind、listen、receive等一系列操作头疼...

Python实现根据指定端口探测服务器/模块部署的方法

本文实例讲述了Python实现根据指定端口探测服务器/模块部署的方法,非常具有实用价值。分享给大家供大家参考借鉴。 有些时候,在维护过程中,服务器数量非常多。应用模块部署在不同服务器上。...

1 行 Python 代码快速实现 FTP 服务器

1 行 Python 代码快速实现 FTP 服务器

 摘要: 当你想快速共享一个目录的时候,这是特别有用的,只需要1行代码即可实现。 当你想快速共享一个目录的时候,这是特别有用的,只需要1行代码即可实现。 FTP 服务器,在此...

python实现tail实时查看服务器日志示例

我就废话不多说了,直接上代码吧! import paramiko from paramiko_expect import SSHClientInteraction host =...

Python实现简单http服务器

Python实现简单http服务器

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