nagios监控squid的脚本

发布时间:2020-06-20 06:26:15 作者:zhhmj
来源:网络 阅读:731

   最近一直都在忙工作上的事情,没有时间来写点东西了,公司用的cache是squid,之前有过监控,但是nagios中看不到squid的命中率,于是就写了这么一个脚本来通过pnp4nagios查看squid的一些图。下面是脚本:

  1. #!/bin/bash 
  2. # 这个脚本主要是检测squid的每分钟http的请求熟、cpu的使用率、可用的文件描述符、5min的请求命中率、5min的内存请求命中率和5min的硬盘请求命中率。 
  3. # 并且可以通过pnp4nagios画图。 
  4.  
  5. PROGNAME=`basename $0` 
  6. VERSION="Version 1.1" 
  7. AUTHOR="zhhmj (tgariltg@gmail.com)" 
  8.  
  9. #DEFINES 
  10. ST_OK=0 
  11. ST_WR=1 
  12. ST_CR=2 
  13. ST_UK=3 
  14. #VARS 
  15. hostname="localhost" 
  16. port=8001 
  17. running=0 
  18. warn_descriptors=100 
  19. crit_descriptors=30 
  20. warn_hits=70 
  21. crit_hits=50 
  22.  
  23. print_version() { 
  24.         echo "$PROGNAME $VERSION $AUTHOR" 
  25.  
  26. print_help() { 
  27.         echo "" 
  28.         print_version 
  29.         echo "" 
  30.         echo "Description:" 
  31.         echo "Gets percentage of hits  for a squid reverse proxy" 
  32.         echo "Options:" 
  33.         echo "  -h|--help" 
  34.         echo "   Print help info." 
  35.         echo "  -H|--hostname)" 
  36.         echo "   Sets the hostname, default is localhost" 
  37.         echo "  -P|--port)" 
  38.         echo "   Sets the port, default is 8001" 
  39.         echo "  -wd)" 
  40.         echo "   Sets the number of available file descriptors to warn at, default 100" 
  41.         echo "  -cd)" 
  42.         echo "   Sets the number of available file descriptors to go critical at, default 30" 
  43.         echo "  -wh)" 
  44.         echo "   Sets the percentage of hits to warn at, default 70" 
  45.         echo "  -ch)" 
  46.         echo "   Sets the percentage of hits to go critical at, default 50" 
  47.         echo "" 
  48.         echo "Example:" 
  49.         echo "  ./check_squid -H 127.0.0.1 -P 8001 -wd 100 -cd 30 -wh 70 -ch 50" 
  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" 
  51.         exit $ST_UK 
  52.  
  53. #获取squid的信息 
  54. get_status_text() { 
  55.         status_text=$(squidclient -h ${hostname} -p ${port} mgr:info 2>&1) 
  56.  
  57. #确保服务器回复正常 
  58. is_replying() { 
  59.         case "$status_text" in 
  60.                 *Denied.*) 
  61.                         echo "Error gettings metrics.(Access control on squid?)" 
  62.                         exit $ST_CR 
  63.                         ;; 
  64.                 *ERROR*) 
  65.                         echo "Error connecting to host" 
  66.                         exit $ST_CR 
  67.                         ;; 
  68.         esac 
  69.  
  70. #下面是获取有用的信息: 
  71. #Available file descriptors 
  72. #CPU Usage 
  73. #Average HTTP requests per minute 
  74. #Hits as % of all requests by 5min 
  75. #Memory hits as % of hit requests by 5min 
  76. #Disk hits as % of hit requests by 5min 
  77. get_statistics() { 
  78.         available_descriptors=$(echo "${status_text}" | grep "Available number of file descriptors" | cut -d: -f 2 | sed -e 's/^[ \t]*//'
  79.         cpu_usage=$(echo "${status_text}" | grep "CPU Usage:" | cut -d: -f2 | cut -d% -f 1 | sed -e 's/^[ \t]*//'
  80.         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]*//'
  81.         all_requests_hits=$(echo "${status_text}" | grep "Hits as % of all requests" | awk '{print $8}' | awk -F\. '{print $1}'
  82.         memory_hits=$(echo "${status_text}" | grep "Memory hits as % of hit requests" | awk '{print $9}' | awk -F\. '{print $1}'
  83.         disk_hits=$(echo "${status_text}" | grep "Disk hits as % of hit requests" | awk '{print $9}' | awk -F\. '{print $1}'
  84.         #buid perfdata string 
  85.         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%" 
  86.  
  87. #报警对比的判断 
  88. build_output() { 
  89.        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" 
  90.        if [ $available_descriptors -le $crit_descriptors ] || [ $all_requests_hits -le $crit_hits ] 
  91.         then 
  92.                 echo "CRITICAL - ${out} | ${perfdata}" 
  93.                 exit $ST_CR 
  94.        elif [ $available_descriptors -le $warn_descriptors ] || [ $all_requests_hits -le $warn_hits ] 
  95.         then 
  96.                 echo "WARNING - ${out} | ${perfdata}" 
  97.                 exit $ST_WR 
  98.         else 
  99.                 echo "OK - ${out} | ${perfdata}" 
  100.                 exit $ST_OK 
  101.         fi 
  102.  
  103. #主程序 
  104. #获取参数 
  105. while test -n "$1"; do 
  106.         case "$1" in 
  107.                 --help|-h) 
  108.                         print_help 
  109.                         exit $ST_UK 
  110.                         ;; 
  111.                 --version|-v) 
  112.                         print_version 
  113.                         exit $ST_UK 
  114.                         ;; 
  115.                 --hostname|-H) 
  116.                         hostname=$2 
  117.                         shift 
  118.                         ;; 
  119.                 --port|-P) 
  120.                         port=$2 
  121.                         shift 
  122.                         ;; 
  123.                 -wd) 
  124.                         warn_descriptors=$2 
  125.                         shift 
  126.                         ;; 
  127.                 -cd) 
  128.                         crit_descriptors=$2 
  129.                         shift 
  130.                         ;; 
  131.                 -wh) 
  132.                         warn_hits=$2 
  133.                         shift 
  134.                         ;; 
  135.                 -ch) 
  136.                         crit_hits=$2 
  137.                         shift 
  138.                         ;; 
  139.                 *) 
  140.                         echo "Unknown argument: $1" 
  141.                         print_help 
  142.                         exit $ST_UK 
  143.                         ;; 
  144.         esac 
  145.         shift 
  146. done 
  147.  
  148. #sanity 
  149. if [ $warn_descriptors -lt $crit_descriptors ] || [ $warn_hits -lt $crit_hits ] 
  150. then 
  151.    echo "Warn descriptors must not be lower than critical and crit hits must not be lower than warn hits!" 
  152.    print_help 
  153. fi 
  154.  
  155. get_status_text 
  156. is_replying 
  157. get_statistics 
  158. build_output 

 

推荐阅读:
  1. nagios中mfs如何监控脚本
  2. nagios-创建内存监控脚本及监控内存

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

check_squid nagios监控 %d

上一篇:模拟实现stl中的list

下一篇:『高级篇』docker之微服务服务docker化(18)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》