设为首页 收藏本站
查看: 1314|回复: 0

[经验分享] nagios监控squid的脚本

[复制链接]

尚未签到

发表于 2018-12-27 08:21:24 | 显示全部楼层 |阅读模式
  最近一直都在忙工作上的事情,没有时间来写点东西了,公司用的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 hits  for 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





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-656254-1-1.html 上篇帖子: 编译安装squid3.1 下篇帖子: Varnish、Squid、Ngx_cache性能对比
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表