downmovies 发表于 2015-9-3 07:35:47

squid命中率的监控办法(cacti-snmpd,squid-grash,cachemgr.cgi)

  ...
  https://forum.pfsense.org/index.php?topic=28835.0
  Correct - lightsquid parses the squid access log and generates a report on what sites have been visited. It lists access times per IP, how much bandwidth each IP has used and how much traffic has been served out of squid's cache.
The squid package is compiled with SNMP, so you could utilize another external tool, on another machine, to query squid and have it output the relevant info (cpu usage, mem info, cache hits, etc.) and have that tool graph the results.
To do this you will you need allow access, so add the following ACL snippet under Services->Proxy Server->General in the Custom Options field.



acl aclname snmp_community public
snmp_access allow aclname
  
This will enable SNMP and squid will accept snmp queries (read-only) on port 3401.
Otherwise you can use the cachemgr.cgi which also provides this info. Steps to set it up as follows:



cd /usr/local/www && ln -s /usr/local/libexec/squid/cachemgr.cgi
cd /usr/local/etc/squid && cp cachemgr.conf.default cachemgr.conf
  
edit cachemgr.conf and add the IP address of the interface your squid is listening on, for example:



# This file controls which servers may be managed by
# the cachemgr.cgi script
#
# The file consists of one server per line on the format
#   hostname:portdescription
#
# Specifying :port is optional. If not specified then
# the default proxy port is assumed. :* or :any matches
# any port on the target server.
#
# hostname is matched using shell filename matching, allowing
# * and other shell wildcards.
192.168.1.1
  
Then in the pfSense GUI go to Services->Proxy Server->Access Control and add the IP you inserted above, into the 'External Cache-Managers' field. Click save and then surf to http://gui_ip/cachemgr.cgi

Hope that helps!
  
  ===============================
  http://www.xuebuyuan.com/1559862.html

cacti监控squid的方法


2013年12月01日 ⁄ 综合 ⁄ 共 1127字 ⁄ 字号 小 中 大 ⁄ 评论关闭







cacti中监控squid的方法:

  1.在 /usr/local/squid/etc/squid.conf 中添加
      acl localhost src 127.0.0.1/32
      acl snmp snmp_community public
      snmp_access allow snmp localhost
      这点在咱们的代理服务器上添加需要注意所放的位置,不然会监控不到数据。
   2.在 /etc/snmp/snmpd.conf 中添加
      proxy -Cc -m /usr/local/squid/share/mib.txt -v 1 -c public 127.0.0.1:3401 .1.3.6.1.4.1.3495.1
   3.重新加载squid的配置文件
   /usr/local/squid/sbin/squid -k reconfigure
   4.重启snmpd
   /etc/init.d/snmpd restart
   这就可以用cacti 等snmp管理软件监控到了(收集snmp的squid信息还涉及mib),你可以使用snmpwalk -c public -v1 127.0.0.1:3401 .1.3.6.1.4.1.3495.1测试下是不是有数据输出,如果有数据输入那么则说明snmp设置成功。
   5、加载监控squid的模板,当然如果学习cacti比较深入的话也可以自己制作模板。cacti已经有前辈制作好模版我们直接导入就可以使用了
http://forums.cacti.net/about4142.html
下面2个文件放在cacti目录下
/resource/snmp_queries/
webcache_squid_core.xml
webcache_squid_median.xml,然后在http://cactihost/cacti/的页面中有一项Import Templates(模板导入)项,将cacti_host_template_webcache_squid_server_snmp.xml导入,则在添加host的模板中选择Web cache -squid server项,,配置的时候需要注意,设置:
      SNMP Version      Version1
      SNMP Community    public
      SNMP Port         3401(默认为161)。
如果配置没有问题的话,你就可以监控到squid的运行情况了,包括squid的缓存、文件描述符、Cache命中率等情况了。
  http://hi.baidu.com/apparitor/item/b34332c642a4bd11b67a2400
  1、编译squid
  编译Squid加上--enable-snmp支持snmp
  
  2、# vi squid.conf在配置文件加上。
  acl CactiHost src 10.168.1.215
  acl SNMP snmp_community
  Squid_snmpsnmp_port 3401
  snmp_access allow SNMP CactiHost
  snmp_access deny all
  ----------------------------
  注释:
  acl CactiHost src 10.168.1.215                     #定义允许snmp探测的机器IP
  acl SNMP snmp_community Squid_snmp   #设置snmp的 community
  snmp_port 3401                                             # 监听的端口
  snmp_access allow SNMP CactiHost
  snmp_access deny all
  
  
3、测试。再cacti上运行                # snmpwalk -v 1 -c passwd 10.168.1.48:3401 .1.3.6.1.4.1.3495.1
4、添加模板
  # wget http://forums.cacti.net/download.php?id=79
  # unzip SquidStats-0.1.zip
  # mv webcache_squid_*.xml cacti_Path/resource/snmp_queries/
  打开cacti的WEBConsole -> Import Templates -> 选择文件导入cacti_host_template_webcache_squid_server_snmp.xml 保存
  
5、添加监控
  Host Template 里选择 WebCache - Squid-Server(SNMP)
  在 Downed Device Detection 选项中不要选择带有snmp的选项。
  Ping Method   着个可以随便选择。
  SNMP Version选择 Version 1
  其他的按照你的配置填。
  http://www.91linux.cn/archives/281.html
  1、Squid准备工作:
在配置文件(squid.conf)中添加以下选项:
  aclCactiServersrc127.0.0.1               //127.0.0.1改为监控主机IP
aclsnmpcommunity snmp_community snmppublic   //snmppublic可改为public(cacti默认是这个)
snmp_port3401
snmp_accessallow snmpcommunity CactiServer
snmp_accessdeny all
  2、重启Squid
  3、在shell中输入以下指令
  export MIBFILES="/etc/squid/mib.txt"
snmpwalk -c snmppublic -v1 :3401 .1.3.6.1.4.1.3495.1//把“hostname”改为本机ip,snmppublic为snmpd使用的团体名也即密码,测试Squid的snmp工作是否正常
  4、Cacti配置和导入脚本
1)把以下文件拷贝到此目录 <cacti_path>/resource/snmp_queries:
webcache_squid_core.xml
webcache_squid_median.xml
  2)在Cacti的配置界面“Import Templates”中导入cacti_host_template_webcache_squid_server_snmp.xml
  3)在“Devices”选项中添加新主机,并注意填写以下内容:
  SNMP Community = snmppublic
SNMP Version   = 1
SNMP Port      = 3401
  最后可以在下面看到此内容
1) WebCache - Squid Statistics - Core(Verbose Query)Uptime Goes BackwardsSuccess       
2) WebCache - Squid Statistics - Median Based(Verbose Query)Uptime Goes BackwardsSuccess    
其中在顶部有提示说“SNMP error”,这个无关紧要的
  5、可能碰到的问题:
设置完成以后执行 poller 的时候总是无法产生 rrd 数据, 给 php 里面加 log 也没有看出来什么, google 换了很多关键词, 总算发现了原因:cacti 在进行 snmp 查询之前会先确定对方是否在运行, 他用的方法是查询 .1.3.6.1.2.1.1.3.0 这个 oid, 但是 squid 不支持这个 oid , 于是 cacti 就以为 squid down 了,不去真正查询. 临时解决方法是在 cacti 的 settings 里面, poller 页的 Downed Host Detection 选择 Ping, 不要选择带有 snmp 字样的.
  
  
  ...=============================
  http://clq1013.blog.hexun.com/36751811_d.html


今天上网无意间搜到一篇关于squid监控的文章,其中提到了一个软件,squid-graph,之前就一直在 关注squid监控方面的东西,cacti也装过了,用于squid的流量监控,webalizer也配过了,用于squid的日志分析,唯独关于 squid cache命中的没有,也一直想要找这么一个软件,尽管可以写脚本来获得相关的信息,不过还是不够直观吧
现在好了,找到了squid-graph这个软件,现在我就来简单介绍一下这个软件的安装配置
准备工作
1,下载:squid-graph
http://sourceforge.net/projects/squid-graph
   下载:GD-2.35.tar.gz
http://search.cpan.org/dist/GD/

2,安装:
GD的安装
1,tar zxvf GD-2.35.tar.gz
2,perl Makefile.PL
所有选择按默认就可以了
3,make
4,make install

1,解压缩,tar zxvf squid-graph-3.2.tar.gz
2,mv squid-graph /usr/local/
3,在apache目录下建立squid-graph文件夹
mkdir -p /usr/local/apache2/htdocs/squid-graph
使用以下命令就可以运行了
/usr/local/squid-graph/squid-graph -o=/usr/local/apache2/htdocs/squid-graph/ < /opt/squid/var/logs/access.log
配置:
编辑apache的配置文档http.conf
在最后加入
Alias /squid-graph /usr/local/apache2/htdocs/squid-graph
<Location /squid-graph>
    Order deny,allow
    Allow from all
    Deny from all
    Allow from 127.0.0.1
    Allow from ::1
    # Allow from .example.com
</Location>
保存退出
然后配置cron,每5分钟执行一次
crontab -e
*/5 * * * * /usr/local/squid-graph/squid-graph -o=/usr/local/apache2/htdocs/squid-graph/ < /opt/squid/var/logs/access.log

到此结束,然后通过浏览器访问http://ip/squid-graph/index.html就可以看到了
注:有一点需要主要的地方,就是squid的日志要使用squid原本的格式
#emulate_httpd_log on ,此行一定要注释掉,不然程序分析不出来
access_log /opt/squid/var/logs/access.log
最终结果图
http://blogimg.chinaunix.net/blog/upfile2/071210224814.gif
http://blogimg.chinaunix.net/blog/upfile2/071210224838.gif

问题与解决办法
1,如果出现下面错误信息,需要去http://www.libgd.org/Main_Page下载最新的libgd
gd-2.0.36的安装很容易就三步
1,./configure
2,make
3,make install
# make
/usr/bin/perl "-Iblib/arch" "-Iblib/lib" GD/Image.pm.PLS GD/Image.pm
Extracting Image.pm (with variable substitutions)
cp GD/Polyline.pm blib/lib/GD/Polyline.pm
cp qd.pl blib/lib/qd.pl
cp GD/Image.pm blib/lib/GD/Image.pm
cp GD.pm blib/lib/GD.pm
AutoSplitting blib/lib/GD.pm (blib/lib/auto/GD)
cp GD/Simple.pm blib/lib/GD/Simple.pm
cp GD/Polygon.pm blib/lib/GD/Polygon.pm
/usr/bin/perl /usr/lib/perl5/5.8.5/ExtUtils/xsubpp-typemap /usr/lib/perl5/5.8.5/ExtUtils/typemap -typemap typemapGD.xs > GD.xsc && mv GD.xsc GD.c
gcc -c-I/usr/include -I/usr/include/gd -D_REENTRANT -D_GNU_SOURCE -DDEBUGGING -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm -O2 -g -pipe -m32 -march=i386 -mtune=pentium4   -DVERSION=\"2.35\" -DXS_VERSION=\"2.35\" -fPIC "-I/usr/lib/perl5/5.8.5/i386-linux-thread-multi/CORE"-DHAVE_JPEG -DHAVE_FT -DHAVE_XPM -DHAVE_GIF -DHAVE_PNG -DHAVE_ANIMGIF GD.c
GD.xs:7:16: gd.h: No such file or directory
GD.xs:8:21: gdfontg.h: No such file or directory
GD.xs:9:21: gdfontl.h: No such file or directory
GD.xs:10:22: gdfontmb.h: No such file or directory
GD.xs:11:21: gdfonts.h: No such file or directory
GD.xs:12:21: gdfontt.h: No such file or directory
GD.xs:340: error: syntax error before "GD__Image"
GD.xs:340: warning: data definition has no type or storage class
GD.xs:341: error: syntax error before "GD__Font"
GD.xs:341: warning: data definition has no type or storage class
GD.xs:383: error: syntax error before "gdIOCtx"
GD.xs:383: warning: no semicolon at end of struct or union
GD.xs:387: error: syntax error before '}' token
GD.xs:387: warning: data definition has no type or storage class
本帖转自(http://blog.chinaunix.net/u1/55815/showart_439768.html)
http://bbs.linuxtone.org/thread-4519-1-1.html
  




为了分析的squid的缓存命中率,记得曾经在freebsd 的ports 里见过squid-graph 工具,百度了一下,得知正是squid 的日志分析工具,以后用nagios对squid-graph 来监控。
1.下载:squid-graph




[*]wget http://down.freebsdsystem.org/monitor/squid-graph-3.2.0.tar.gz

复制代码
解压缩,安装





[*]#tar zxvf squid-graph-3.2.tar.gz
[*]#mv squid-graph /usr/local/

复制代码


2.安装perl的GD包





[*]#yum -y install perl-GD-Graph perl-GD

复制代码

3.配置nginx服务器
在相应目录下建立squid-graph文件夹





[*]#mkdir /data0/wwwroot/monitor/squid-graph

复制代码


编辑nginx的配置文档





[*]# vim /usr/local/nginx/conf/nginx.conf

复制代码

在加入




[*]location /squid-graph
[*]{
[*]alias /data0/wwwroot/monitor/squid-graph;
[*]index index.html index.htm;
[*]

[*]}

复制代码

4.安装完,分析
使用以下命令来分析




[*]#/usr/local/squid-graph/squid-graph -o=/data0/wwwroot/monitor/squid-graph < /usr/local/squid/var/logs/access_log

复制代码

还可以设置自动使用cron,每5分钟执行一次





[*]#crontab -e
[*]

[*]*/5 * * * * /usr/local/squid-graph/squid-graph -o=/data0/wwwroot/monitor/squid-graph < /usr/local/squid/var/logs/access_log

复制代码




到此结束,然后通过浏览器访问 http://ip/squid-graph
注:squid的日志要使用squid原本的格式,和emulate_httpd_log on ,有人讲这行一定要注释掉,不然程序分析不出来.记的要设置安全哦,不能让任何人都能打开哦
====================
http://blog.chinaunix.net/uid-20204594-id-1690238.html
本文介绍通过cachemgr.cgi来监控Squid代理服务器的运行情况,包括内存占用、磁盘占用、Cache命中率、Cache使用率、IP地址和域名的缓存等等详细情况:
1.设置cachemgr.conf中的代理服务器地址和端口号,如果使用默认的3128端口可不需要更改:
localhost:3333
2. 检查squid.conf中manager的设置,注意默认情况下只允许localhost可访问:
acl manager proto cache_object
acl localhost src 127.0.0.1/32
acl localnetwork src 192.168.4.0/24
http_access allow manager localhost
http_access allow manager localnetwork
http_access deny manager
3. 设置httpd
   首先将cachemgr.cgi文件复制到 /usr/local/apache/cgi-bin目录下,再添加以下设置:
   ScriptAlias /cgi-bin/ "/usr/local/apache/cgi-bin/"   
   <Location /cgi-bin/cachemgr.cgi>
   Order allow,deny
   Allow from all
   </Location>
4. 通过 http://ServerIP/cgi-bin/cachemgr.cgi 即可访问。
    如果需要更好的安全性,可以对cgi-bin/cachemgr.cgi网页设置访问设置密码,另外,登入cachemgr.cgi默认不需要密码,可以在squid.conf中通过 cachemgr_passwd 来设置访问密码。

http://xuehuile.com/blog/c73af9a05ed344da9766a76c6f93b2cc.html
  cachemgr.cgi配置过程
修改 squid.conf
  
  acl manager proto cache_object
  http_access allow manager
  
  
  squid本身提供一个cgi程序,文件名为cachemgr.cgi,squid安装完后将它复制到Apache服务器下的cgi-bin这个目录下即可使用。




#cp /usr/lib/squid/cachemgr.cgi /home/httpd/cgi-bin


  下面我们通过apache进行监控squid ,修改配置文件/etc/http/conf/http.conf,添加以下内容:




<Location /cgi-bin/cachemgr.cgi>
  AuthType Basic
  AuthName "Squidadmin"
  AuthUserFile /usr/local/squid/etc/squid.pwd
  require valid-user
  </Location>


  生成口令文件:




#cd /usr/local/squid/etc
  #htpasswd -c squid.pwd squidadmin “创建密码文件 ”
  New passwd:
  Re-type new passwd:
  Adding password for user squidadmin
  #chown apache:apche squid.pwd “将认证口令文件的属主改为apache”


  重启squid与http ,要察看Cache Manager提供的信息时,请在浏览器的地址列中键入 http://服务器的名称或IP地址/cgi-bin/cachemgr.cgi。首先看到用户认证界面,经过认证后进入登录界面,输入代理服务器地址和端口号
  
  
  http://blog.iyunv.com/huithe/article/details/6320547
  cachemgr.cgi配置过程
  

修改 squid.conf
  
  
  
  acl manager proto cache_object
  
  http_access allow manager
  
  
  
  squid本身提供一个cgi程序,文件名为cachemgr.cgi,squid安装完后将它复制到Apache服务器下的cgi-bin这个目录下即可使用。
  




#cp /usr/lib/squid/cachemgr.cgi /home/httpd/cgi-bin


  
  下面我们通过apache进行监控squid ,修改配置文件/etc/http/conf/http.conf,添加以下内容:
  




            <Location /cgi-bin/cachemgr.cgi>
      AuthType Basic
      AuthName "Squidadmin"
      AuthUserFile/usr/local/squid/etc/squid.pwd
      require valid-user
      </Location>


  
  生成口令文件:




            #cd /usr/local/squid/etc
      #htpasswd -c squid.pwd squidadmin   “创建密码文件 ”
      New passwd:
      Re-type new passwd:
      Adding password for user squidadmin
      #chown apache:apche squid.pwd   “将认证口令文件的属主改为apache”


  
   
  
  重启squid与http ,要察看Cache Manager提供的信息时,请在浏览器的地址列中键入 http://服务器的名称或IP地址/cgi-bin/cachemgr.cgi。首先看到用户认证界面,经过认证后进入登录界面,输入代理服务器地址和端口号

  ======================
  http://hi.baidu.com/cisco1674/item/6485ff0ab940dc9002ce1b94



Squid 服务中的缓存命中率


  cache命中在squid每次从它的缓存里满足HTTP请求时发生。cache命中率,是所有HTTP请求中命中的比例。Web缓存典型的cache命中率在30%到60%之间。另一个相似的度量单位叫做字节命中率,描绘了cache提供服务的数据容量(字节数)
  cache 丢失在squid不能从它的缓存里满足HTTP请求时发生。cache丢失的理由有很多种。最明显的,当squid第一次接受到对特殊资源的请求时,就是 一个cache丢失。类似的情况是,squid会清除缓存以释放空间给新对象。另外的可能是资源不可到达。原始服务器会指示cache怎样处理响应。例 如,它会提示数据不能被缓存,或在有限的时间内才被重复使用,等等。
  
cache确认保证squid不对用户返回过时数据。在重复使用缓存对象时,squid经常从原始服务器确认它。假如服务器指示squid的拷贝仍然有效,数据就发送出去。否则,squid升级它的缓存拷贝,并且转发给客户。
  ==================================================
  如何看Squid的缓存命中呢,这里有几种办法
  一是安装squid命中率监控软件squid-graph
  为了测试公司的squid,要找一个squid的命令比率查询软件,好不容易找到一个软件,叫squid-graph,晚点还要研究一下用cacti监控他的流量,
  1.下载:squid-graph http://sourceforge.net/projects/squid-graph
解压缩,安装
#tar zxvf squid-graph-3.2.tar.gz
#mv squid-graph /usr/local/
  2.安装perl的GD包
安装rpm扩展包,安装方法见我的"rpm高级管理技巧"
#yum -y install perl-GD-Graph perl-GD
  3.配置httpd服务器
在www目录下建立squid-graph文件夹
#mkdir -p /var/www/squid-graph
  编辑httpd的配置文档
# vim /etc/httpd/conf.d/squid-graph.conf
  在加入
Alias /squid-graph /var/www/squid-graph
  <Location /squid-graph>
Order deny,allow
Allow from all
Deny from all
Allow from 127.0.0.1
Allow from ::1
</Location>
  4.安装完,分析
使用以下命令来分析
#/usr/local/squid-graph/squid-graph -o=/var/www/squid-graph/ < /var/log/squid/access.log
  还可以设置自动使用cron,每5分钟执行一次
#crontab -e
  */5 * * * * /usr/local/squid-graph/squid-graph -o=/var/www/squid-graph/ < /var/log/squid/access.log
  到此结束,然后通过浏览器访问 http://ip/squid-graph/index.html
  注:squid的日志要使用squid原本的格式,和emulate_httpd_log on ,有人讲这行一定要注释掉,不然程序分析不出来.记的要设置安全哦,不能让任何人都能打开哦
  二是用系统自带的squid监控cachemgr.cgi(应将CGI放入Aapche的认证区,即输入有权限的帐户和密码才能访问)
  General Runtime Information是Cachemgr.cgi监控Squid代理服务品的核心,里面就有cache hit,如果是30%-60%的话就属于正常.
  三是最简单的了,直接在命令行模式下输入:
  # /usr/local/squid/bin/squidclient -h localhost -p 3128 mgr:info
        有时候,Squid作正向代理代理时cache命中率可能相当低,有可能是动态内容过多,而cache处得的主要是图片,JS,CSS等静态文件,这时候squid主要是作为一个Proxy使用,cache功能发挥得不明显
  
  
  =====================================
  http://zhhmj.blog.iyunv.com/1666742/1100511
  最近一直都在忙工作上的事情,没有时间来写点东西了,公司用的cache是squid,之前有过监控,但是nagios中看不到squid的命中率,于是就写了这么一个脚本来通过pnp4nagios查看squid的一些图。下面是脚本:


[*]#!/bin/bash
[*]# 这个脚本主要是检测squid的每分钟http的请求熟、cpu的使用率、可用的文件描述符、5min的请求命中率、5min的内存请求命中率和5min的硬盘请求命中率。
[*]# 并且可以通过pnp4nagios画图。
[*]
[*]PROGNAME=`basename $0`
[*]VERSION="Version 1.1"
[*]AUTHOR="zhhmj (tgariltg@gmail.com)"
[*]
[*]#DEFINES
[*]ST_OK=0
[*]ST_WR=1
[*]ST_CR=2
[*]ST_UK=3
[*]#VARS
[*]hostname="localhost"
[*]port=8001
[*]running=0
[*]warn_descriptors=100
[*]crit_descriptors=30
[*]warn_hits=70
[*]crit_hits=50
[*]
[*]print_version() {
[*]      echo "$PROGNAME $VERSION $AUTHOR"
[*]}
[*]
[*]print_help() {
[*]      echo ""
[*]      print_version
[*]      echo ""
[*]      echo "Description:"
[*]      echo "Gets percentage of hitsfor a squid reverse proxy"
[*]      echo "Options:"
[*]      echo "-h|--help"
[*]      echo "   Print help info."
[*]      echo "-H|--hostname)"
[*]      echo "   Sets the hostname, default is localhost"
[*]      echo "-P|--port)"
[*]      echo "   Sets the port, default is 8001"
[*]      echo "-wd)"
[*]      echo "   Sets the number of available file descriptors to warn at, default 100"
[*]      echo "-cd)"
[*]      echo "   Sets the number of available file descriptors to go critical at, default 30"
[*]      echo "-wh)"
[*]      echo "   Sets the percentage of hits to warn at, default 70"
[*]      echo "-ch)"
[*]      echo "   Sets the percentage of hits to go critical at, default 50"
[*]      echo ""
[*]      echo "Example:"
[*]      echo "./check_squid -H 127.0.0.1 -P 8001 -wd 100 -cd 30 -wh 70 -ch 50"
[*]      echo "WARNING - Squid is serving an average of 7.2 per minute since start with 655349 file descriptors left and 0.04 percent of CPU use and Hits as 64% of all requests"
[*]      exit $ST_UK
[*]}
[*]
[*]#获取squid的信息
[*]get_status_text() {
[*]      status_text=$(squidclient -h ${hostname} -p ${port} mgr:info 2>&1)
[*]}
[*]
[*]#确保服务器回复正常
[*]is_replying() {
[*]      case "$status_text" in
[*]                *Denied.*)
[*]                        echo "Error gettings metrics.(Access control on squid?)"
[*]                        exit $ST_CR
[*]                        ;;
[*]                *ERROR*)
[*]                        echo "Error connecting to host"
[*]                        exit $ST_CR
[*]                        ;;
[*]      esac
[*]}
[*]
[*]#下面是获取有用的信息:
[*]#Available file descriptors
[*]#CPU Usage
[*]#Average HTTP requests per minute
[*]#Hits as % of all requests by 5min
[*]#Memory hits as % of hit requests by 5min
[*]#Disk hits as % of hit requests by 5min
[*]get_statistics() {
[*]      available_descriptors=$(echo "${status_text}" | grep "Available number of file descriptors" | cut -d: -f 2 | sed -e 's/^[ \t]*//')
[*]      cpu_usage=$(echo "${status_text}" | grep "CPU Usage:" | cut -d: -f2 | cut -d% -f 1 | sed -e 's/^[ \t]*//')
[*]      avg_http_requests=$(echo "${status_text}" | grep "Average HTTP requests per minute since start" | cut -d: -f2 | cut -d% -f 1 | sed -e 's/^[ \t]*//')
[*]      all_requests_hits=$(echo "${status_text}" | grep "Hits as % of all requests" | awk '{print $8}' | awk -F\. '{print $1}')
[*]      memory_hits=$(echo "${status_text}" | grep "Memory hits as % of hit requests" | awk '{print $9}' | awk -F\. '{print $1}')
[*]      disk_hits=$(echo "${status_text}" | grep "Disk hits as % of hit requests" | awk '{print $9}' | awk -F\. '{print $1}')
[*]      #buid perfdata string
[*]      perfdata="'avail_descriptors'=$available_descriptors 'cpu_usage'=$cpu_usage 'avg_http_requests'=$avg_http_requests 'all_requests_hits'=$all_requests_hits% 'memory_hits'=$memory_hits% 'disk_hits'=$disk_hits%"
[*]}
[*]
[*]#报警对比的判断
[*]build_output() {
[*]       out="Squid is serving an average of $avg_http_requests per minute since start with $available_descriptors file descriptors left and $cpu_usage percent of CPU use and Hits as $all_requests_hits% of all requests"
[*]       if [ $available_descriptors -le $crit_descriptors ] || [ $all_requests_hits -le $crit_hits ]
[*]      then
[*]                echo "CRITICAL - ${out} | ${perfdata}"
[*]                exit $ST_CR
[*]       elif [ $available_descriptors -le $warn_descriptors ] || [ $all_requests_hits -le $warn_hits ]
[*]      then
[*]                echo "WARNING - ${out} | ${perfdata}"
[*]                exit $ST_WR
[*]      else
[*]                echo "OK - ${out} | ${perfdata}"
[*]                exit $ST_OK
[*]      fi
[*]}
[*]
[*]#主程序
[*]#获取参数
[*]while test -n "$1"; do
[*]      case "$1" in
[*]                --help|-h)
[*]                        print_help
[*]                        exit $ST_UK
[*]                        ;;
[*]                --version|-v)
[*]                        print_version
[*]                        exit $ST_UK
[*]                        ;;
[*]                --hostname|-H)
[*]                        hostname=$2
[*]                        shift
[*]                        ;;
[*]                --port|-P)
[*]                        port=$2
[*]                        shift
[*]                        ;;
[*]                -wd)
[*]                        warn_descriptors=$2
[*]                        shift
[*]                        ;;
[*]                -cd)
[*]                        crit_descriptors=$2
[*]                        shift
[*]                        ;;
[*]                -wh)
[*]                        warn_hits=$2
[*]                        shift
[*]                        ;;
[*]                -ch)
[*]                        crit_hits=$2
[*]                        shift
[*]                        ;;
[*]                *)
[*]                        echo "Unknown argument: $1"
[*]                        print_help
[*]                        exit $ST_UK
[*]                        ;;
[*]      esac
[*]      shift
[*]done
[*]
[*]#sanity
[*]if [ $warn_descriptors -lt $crit_descriptors ] || [ $warn_hits -lt $crit_hits ]
[*]then
[*]   echo "Warn descriptors must not be lower than critical and crit hits must not be lower than warn hits!"
[*]   print_help
[*]fi
[*]
[*]get_status_text
[*]is_replying
[*]get_statistics
[*]build_output
  
  本文出自 “梦想照进现实” 博客,请务必保留此出处http://zhhmj.blog.iyunv.com/1666742/1100511
  
  =====================
  http://blog.iyunv.com/kerncai/article/details/15812231
  自定义脚本监控squid的一些日常状态,脚本如下:
  脚本放置路径:/usr/local/zabbix-agent-ops/bin zabbix agent的bin下面
  cat check_squid_status.sh
########################################################################
# File Name: check_squid_status.sh
# Author: kerncai
# mail: kernkerncai@gmail.com
# Created Time: 2013年11月12日 星期二 10时30分05秒
#########################################################################
#!/bin/bash
five_ratios(){
/usr/local/squid-2.7/bin/squidclient -h localhost -p 3128 mgr:info |grep 'Request Hit Ratios:' |awk '{print$5/100}'
}
six_ratios(){
/usr/local/squid-2.7/bin/squidclient -h localhost -p 3128 mgr:info |grep 'Request Hit Ratios:' |awk '{print$7/100}'
}
objects(){
/usr/local/squid-2.7/bin/squidclient -h localhost -p 3128 mgr:info |grep 'on-disk objects' |awk '{print$1}'
}
space(){
/usr/local/squid-2.7/bin/squidclient -hlocalhost -p 3128 mgr:storedir |grep 'Filesystem Space in use:' |awk -F '/' '{print$1}'|awk '{s+=$5};END{printf "%4.2f\n",s/1024/1024}'
}
$1
  因为我需要的比较少,只是命中率、cachesize、图片数(我这边做的是图片缓存),其他的一些状态可以自行添加脚本命令

  zabbix.agentd.conf内配置如下
  UnsafeUserParameters=1
UserParameter=five.ratios,/usr/local/zabbix-agent-ops/bin/check_squid_status.sh five_ratios
UserParameter=six.ratios,/usr/local/zabbix-agent-ops/bin/check_squid_status.sh six_ratios
UserParameter=objects,/usr/local/zabbix-agent-ops/bin/check_squid_status.sh objects
UserParameter=space,/usr/local/zabbix-agent-ops/bin/check_squid_status.sh space
  相关zabbix   web的模板在github上面:https://github.com/kerncai/zabbix/tree/master/squid,squid模板和上述脚本是配套使用的 `
  包括其他的memcache、redis、apache等,脚本以及监控原理都是相同的 ,在此只是把大概的思路写一下
  个人博客:http://www.kerncai.com/
  
  ===========
  
  
  http://www.jbxue.com/article/11799.html
  本文介绍下,通过分析squid日志中的选项,来查看squid命中率的方法,与大家一起学习squid中的一些有关命中率的选项。有需要的朋友,建议好好看看这篇文章。












  通过squid的日志分析命中率:



复制代码代码示例:

  #cat access.log|gawk '{print $4}'|sort|uniq -c|sort -nr
  9568 TCP_IMS_HIT/304
6313 TCP_HIT/200
2133 TCP_MISS/200
1568 TCP_MISS/206
587 TCP_MEM_HIT/200
531 TCP_MISS/304
207 TCP_REFRESH_HIT/200
152 TCP_REFRESH_HIT/304
86 TCP_NEGATIVE_HIT/404
69 TCP_MISS/404
9 TCP_MISS/000
4 TCP_MISS/503
1 TCP_REFRESH_MISS/000
1 TCP_DENIED/400
  解释:
#cat /var/log/squid/access.log |grep TCP_MEM_HIT
  如果看到很多的TCP_MEM_HIT ,这表明该文件是从内存缓存读取的,squid已经起作用了!你再用浏览器打开该文件,应该是快如闪电了。。呵呵,大功告成了!还有其他类型的HIT,如TCP_HIT等等,这些是从磁盘读取的,我觉得加速的意义不大,只不过缓解了apache的压力而已。
  相应于HTTP请求,下列标签可能出现在access.log文件的第四个域。
  TCP_HIT
  Squid发现请求资源的貌似新鲜的拷贝,并将其立即发送到客户端。
  TCP_MISS
  Squid没有请求资源的cache拷贝。
  TCP_REFERSH_HIT
  Squid发现请求资源的貌似陈旧的拷贝,并发送确认请求到原始服务器。原始服务器返回304(未修改)响应,指示squid的拷贝仍旧是新鲜的。
  TCP_REF_FAIL_HIT
  Squid发现请求资源的貌似陈旧的拷贝,并发送确认请求到原始服务器。然而,原始服务器响应失败,或者返回的响应Squid不能理解。在此情形下,squid发送现有cache拷贝(很可能是陈旧的)到客户端。
  TCP_REFRESH_MISS
  Squid发现请求资源的貌似陈旧的拷贝,并发送确认请求到原始服务器。原始服务器响应新的内容,指示这个cache拷贝确实是陈旧的。
  TCP_CLIENT_REFRESH_MISS
  Squid发现了请求资源的拷贝,但客户端的请求包含了Cache-Control: no-cache指令。Squid转发客户端的请求到原始服务器,强迫cache确认。
  TCP_IMS_HIT
  客户端发送确认请求,Squid发现更近来的、貌似新鲜的请求资源的拷贝。Squid发送更新的内容到客户端,而不联系原始服务器。
  TCP_SWAPFAIL_MISS
  Squid发现请求资源的有效拷贝,但从磁盘装载它失败。这时squid发送请求到原始服务器,就如同这是个cache丢失一样。
  TCP_NEGATIVE_HIT
  在对原始服务器的请求导致HTTP错误时,Squid也会cache这个响应。在短时间内对这些资源的重复请求,导致了否命中。 negative_ttl指令控制这些错误被cache的时间数量。请注意这些错误只在内存cache,不会写往磁盘。下列HTTP状态码可能导致否定 cache(也遵循于其他约束): 204, 305, 400, 403, 404, 405, 414, 500, 501, 502, 503, 504。
  TCP_MEM_HIT
  Squid在内存cache里发现请求资源的有效拷贝,并将其立即发送到客户端。注意这点并非精确的呈现了所有从内存服务的响应。例如,某些cache在内存里,但要求确认的响应,会以TCP_REFRESH_HIT, TCP_REFRESH_MISS等形式记录。
  TCP_DENIED
  因为http_access或http_reply_access规则,客户端的请求被拒绝了。注意被http_access拒绝的请求在第9域的值是NONE/-,然而被http_reply_access拒绝的请求,在相应地方有一个有效值。
  TCP_OFFLINE_HIT
  当offline_mode激活时,Squid对任何cache响应返回cache命中,而不用考虑它的新鲜程度。
  TCP_REDIRECT
  重定向程序告诉Squid产生一个HTTP重定向到新的URI(见11.1节)。正常的,Squid不会记录这些重定向。假如要这样做,必须在编译squid前,手工定义LOG_TCP_REDIRECTS预处理指令。
  NONE
  无分类的结果用于特定错误,例如无效主机名。
  相应于ICP查询,下列标签可能出现在access.log文件的第四域。
  UDP_HIT
  Squid在cache里发现请求资源的貌似新鲜的拷贝。
  UDP_MISS
  Squid没有在cache里发现请求资源的貌似新鲜的拷贝。假如同一目标通过HTTP请求,就可能是个cache丢失。请对比UDP_MISS_NOFETCH。
  UDP_MISS_NOFETCH
  跟UDP_MISS类似,不同的是这里也指示了Squid不愿去处理相应的HTTP请求。假如使用了-Y命令行选项,Squid在启动并编译其内存索引时,会返回这个标签而不是UDP_MISS。
  UDP_DENIED
  因为icp_access规则,ICP查询被拒绝。假如超过95%的到某客户端的ICP响应是UDP_DENIED,并且客户端数据库激活了(见附录A),Squid在1小时内,停止发送任何ICP响应到该客户端。若这点发生,你也可在cache.log里见到一个警告。
  UDP_INVALID
  Squid接受到无效查询(例如截断的消息、无效协议版本、URI里的空格等)。Squid发送UDP_INVALID响应到客户端。
  =============
  http://www.9iyunv.com/blog/547.html

Squid缓存命中率调整惨痛教训
  
  
  
  公司的网站使用Squid 2.6STABLE2作缓存加速服务器,缓存的命中率一直不好,最好也只能达到90%,折腾了许久,终于发现影响最大的原来只是一个小参数,不过期间也学到了不少东西~
  
起初,考虑缓存命中率不高,考虑是不是给Squid的内存不够,所以就加大了内存配置,2G内存的机器给Squid 1G用,测试了两天,结果基本没有什么大的改变;于是调整refresh_pattern的参数,增大内容缓存时间,测试了两天,效果也不理想。
从kxn的文章中知道可以使用Expires模块让Apache给文件指定缓存时间,期望可以提高缓存性能,于是在服务器上:
ExpiresActive On
ExpiresDefault A2592000
结果第二天一早发现缓存命中率竟然比前一天还下降了2个百本点,有点晕了~
于是将cachemgr配置起来,详细地检查squid的运行状况,发现1G的内存缓存实际只用了不到300M,所以显然不是内存不够的问题。
为了充分利用内存,将maximum_object_size_in_memory调大到128KB,让其可以缓存更大的文件,而不是默认的8KB。顺手将ufs换成aufs,启用异步IO。
之后情况稍有改善,1个小时之后,内存的占用达到了1G,但命中率并没有明显上升。
于是又调整了内存的覆盖策略(memory_replacement_policy),听从kxn的教诲,使用lru,发现变化不大,也许是我们的访问量还不大的原因吧。
这时候缓存命中率仍然在90%以下徘徊。是不是我们的网站结构影响只能达到这么高呢?但是网站结构我没有办法改变,所以还是要从其他方面入手来解决问题。
于是使用Cache Manager 仔细的查看各个统计数据,当看到In-Memory and In-Transit Objects项目时,发现一个问题,那就是所有的网页文件都显示NOT_IN_MEMORY,这就比较奇怪了,问什么访问量最大的网页文件竟然没有被缓存到内存呢?是因为文件太大还是因为文件请求的数量太少,所以才没有被缓存呢?于是找了一个页面作测试,使用工具查看了返回的大小,只有40K多一些,与我的maximum_object_size_in_memory最大值128KB还有相当大的差距,所以不可能是这个原因。于是有使用ab程序对这个页面做了1000次请求,心想这下总会缓存了吧。可是查看缓存内容,依然没有,而且不但内存中没有,就连硬盘上也没有!
至此得出结论,一定是什么原因阻止了Squid对这些网页进行缓存。仔细的检查In-Memory and In-Transit Objects中的内容,发现IN_MEMORY 状态的对象基本都是jpg和gif图像文件,html,css等文本文件内容则都没有缓存。
这两种文件有什么不同吗?记忆中Squid也没有对这些文件类型的设定呀,refresh_pattern对所有的静态文件的设置也都是一致的,所以差异不应该来源于squid本身??也就是因为这种记忆使得我后来多费了许多的功夫~
在Apache上找到了文本文件与图像文件之间设置的差异??deflate压缩。是不是所有使用deflate压缩的内容都不会缓存呢?使用同样的 squid软件版本及配置文件,相同的Apache软件版本和配置,快速的搭建了一套测试环境。squid只有1台,重建其磁盘缓冲区,重新启动 squid,观察内存中对象的变化。果然,对于deflate压缩的内容,squid内存中没有缓存,磁盘也找不到。停止apache的deflate功能,重新请求页面,立即在squid的内存中查到了内容的缓存,这么看来是squid和deflate不太兼容了?
好吧,先将产品环境Apache的deflate停掉,过了一段时间,有两台squid的缓存命中了提高到了94%,虽然只提升了4个百分点,但是这充分验证了squid和deflate不兼容的观点。
然而,事情到此才刚刚开始!
难道Squid真的无法缓存deflate压缩的页面吗?好吧google一下,squid+deflate,在squid的邮件列表中找到了一些讨论 deflate的东西,属于比较古老的吧,说因为deflate压缩之后,会产生Content-Encoding: gzip这样的HTTP头部,属于HTTP/1.1的内容,而Squid并不完全兼容HTTP/1.1之类的,需要用Transfer-Encoding 之类的代替,可是mod_deflate和mod_gzip产生的头部就是Content-Encoding,怎么办?
为了解决这个问题,引入了另一个Apache的模块,mod_headers,用它来改写HTTP应答的头部,将Content-Encoding更改给Transfer-Encoding,可是,很自然的,页面一片乱码!说明此路不通啊!
又以Content-Encoding为关键字,快速的搜索了一下squid 2.6STABLE2的源代码,也没有发现几个地方用到这个东西,无果。
继续Google之,squid+can’t cache+Content-Encoding找到一篇文章,其中说’SQUID would never bother to cache ANYTHING that had
a “Vary:” header on it.’ 看来Squid缓存内容与Vary: 头部有关系。再次检查一下HTTP请求返回的头部内容,发现对于deflate压缩的内容,拥有”Vary: Accept-Encoding”这样一个头部,而没有经过deflate压缩的内容则没有这个头部,这次看来问题比较清楚了,因为deflate压缩添加了Vary: Accept-Encoding这个头部,导致了内容无法被squid缓存。
那么难道真的没有办法让squid缓存deflate压缩之后的内容吗?
基于上面的经验,很自然的想到使用mod_headers模块的功能将Vary头抹掉Squid不就可以缓存了吗?后来一想,这样会有其它问题: 1)如果用户浏览器不支持gzip压缩功能,那么将无法正常的浏览网页;2)其它可能很重要的Vary内容也没有了,可能会导致更大的隐患。那么这种办法不行。
还有其它的办法吗?再次的快速搜索了Squid的源文件中关于Vary的内容,在Changelog中找到了这么一条:
- Full ETag/Vary support, caching responses which varies with request details (browser, language etc).
也就是说已经完全支持带ETag/Vary头部的内容缓存了。这一条出现在Changes to squid-2.6.STABLE1,也就是说在Squid2.6STABLE1就已经完全实现了这个功能,那么2.6STABLE2应该没有问题,会不会是2.6STABLE2的BUG呢,查询了一下2.6系列的所有Changelog,没有提到此故障的信息。推断我们的故障要么由一个未知BUG导致,要么是自己配置不当。BUG的问题我无法确认,只能先从配置入手。
找来一份2.6STABLE2带的默认配置文件,搜索Vary,第一条:
# TAG: cache_vary
# Set to off to disable caching of Vary:in objects.
#
#Default:
# cache_vary on
难道我的配置文件是off不成?赶紧检查,发现还真是off,sign,原来就是这个问题折磨我,改成on,将Apache的Deflate配置启用,再观察内存对象,发现静态网页真的被缓存了!
郁闷了许久,终于可以松口气了。
过了一会儿,再查看Squid的状态,发现缓存命中率没有下降,稳定在94%左右。
也许Squid还有什么可挖掘的空间吧,改天接着折腾!
后记:Squid对Vary和Etag的支持属于2.6的一项主要改进,这一特性改进Squid对HTTP/1.1标准的支持。在将Squid升级至 2.6之前,也看过他的Changelog,但当时并不明白Vary到底有什么用途,这么一折腾,明白了不少。看来应该把HTTP/1.1的标准再好好研读一下。
  
页: [1]
查看完整版本: squid命中率的监控办法(cacti-snmpd,squid-grash,cachemgr.cgi)