联系客服:400-805-1963

nagios监控squid的脚本
更新:HHH   时间:2023-1-7


   最近一直都在忙工作上的事情,没有时间来写点东西了,公司用的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.  
  27. print_help() { 
  28.         echo "" 
  29.         print_version 
  30.         echo "" 
  31.         echo "Description:" 
  32.         echo "Gets percentage of hits  for a squid reverse proxy" 
  33.         echo "Options:" 
  34.         echo "  -h|--help" 
  35.         echo "   Print help info." 
  36.         echo "  -H|--hostname)" 
  37.         echo "   Sets the hostname, default is localhost" 
  38.         echo "  -P|--port)" 
  39.         echo "   Sets the port, default is 8001" 
  40.         echo "  -wd)" 
  41.         echo "   Sets the number of available file descriptors to warn at, default 100" 
  42.         echo "  -cd)" 
  43.         echo "   Sets the number of available file descriptors to go critical at, default 30" 
  44.         echo "  -wh)" 
  45.         echo "   Sets the percentage of hits to warn at, default 70" 
  46.         echo "  -ch)" 
  47.         echo "   Sets the percentage of hits to go critical at, default 50" 
  48.         echo "" 
  49.         echo "Example:" 
  50.         echo "  ./check_squid -H 127.0.0.1 -P 8001 -wd 100 -cd 30 -wh 70 -ch 50" 
  51.         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" 
  52.         exit $ST_UK 
  53. } 
  54.  
  55. #获取squid的信息 
  56. get_status_text() { 
  57.         status_text=$(squidclient -h ${hostname} -p ${port} mgr:info 2>&1) 
  58. } 
  59.  
  60. #确保服务器回复正常 
  61. is_replying() { 
  62.         case "$status_text" in 
  63.                 *Denied.*) 
  64.                         echo "Error gettings metrics.(Access control on squid?)" 
  65.                         exit $ST_CR 
  66.                         ;; 
  67.                 *ERROR*) 
  68.                         echo "Error connecting to host" 
  69.                         exit $ST_CR 
  70.                         ;; 
  71.         esac 
  72. } 
  73.  
  74. #下面是获取有用的信息: 
  75. #Available file descriptors 
  76. #CPU Usage 
  77. #Average HTTP requests per minute 
  78. #Hits as % of all requests by 5min 
  79. #Memory hits as % of hit requests by 5min 
  80. #Disk hits as % of hit requests by 5min 
  81. get_statistics() { 
  82.         available_descriptors=$(echo "${status_text}" | grep "Available number of file descriptors" | cut -d: -f 2 | sed -e 's/^[ \t]*//') 
  83.         cpu_usage=$(echo "${status_text}" | grep "CPU Usage:" | cut -d: -f2 | cut -d% -f 1 | sed -e 's/^[ \t]*//') 
  84.         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]*//') 
  85.         all_requests_hits=$(echo "${status_text}" | grep "Hits as % of all requests" | awk '{print $8}' | awk -F\. '{print $1}') 
  86.         memory_hits=$(echo "${status_text}" | grep "Memory hits as % of hit requests" | awk '{print $9}' | awk -F\. '{print $1}') 
  87.         disk_hits=$(echo "${status_text}" | grep "Disk hits as % of hit requests" | awk '{print $9}' | awk -F\. '{print $1}') 
  88.         #buid perfdata string 
  89.         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%" 
  90. } 
  91.  
  92. #报警对比的判断 
  93. build_output() { 
  94.        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" 
  95.        if [ $available_descriptors -le $crit_descriptors ] || [ $all_requests_hits -le $crit_hits ] 
  96.         then 
  97.                 echo "CRITICAL - ${out} | ${perfdata}" 
  98.                 exit $ST_CR 
  99.        elif [ $available_descriptors -le $warn_descriptors ] || [ $all_requests_hits -le $warn_hits ] 
  100.         then 
  101.                 echo "WARNING - ${out} | ${perfdata}" 
  102.                 exit $ST_WR 
  103.         else 
  104.                 echo "OK - ${out} | ${perfdata}" 
  105.                 exit $ST_OK 
  106.         fi 
  107. } 
  108.  
  109. #主程序 
  110. #获取参数 
  111. while test -n "$1"; do 
  112.         case "$1" in 
  113.                 --help|-h) 
  114.                         print_help 
  115.                         exit $ST_UK 
  116.                         ;; 
  117.                 --version|-v) 
  118.                         print_version 
  119.                         exit $ST_UK 
  120.                         ;; 
  121.                 --hostname|-H) 
  122.                         hostname=$2 
  123.                         shift 
  124.                         ;; 
  125.                 --port|-P) 
  126.                         port=$2 
  127.                         shift 
  128.                         ;; 
  129.                 -wd) 
  130.                         warn_descriptors=$2 
  131.                         shift 
  132.                         ;; 
  133.                 -cd) 
  134.                         crit_descriptors=$2 
  135.                         shift 
  136.                         ;; 
  137.                 -wh) 
  138.                         warn_hits=$2 
  139.                         shift 
  140.                         ;; 
  141.                 -ch) 
  142.                         crit_hits=$2 
  143.                         shift 
  144.                         ;; 
  145.                 *) 
  146.                         echo "Unknown argument: $1" 
  147.                         print_help 
  148.                         exit $ST_UK 
  149.                         ;; 
  150.         esac 
  151.         shift 
  152. done 
  153.  
  154. #sanity 
  155. if [ $warn_descriptors -lt $crit_descriptors ] || [ $warn_hits -lt $crit_hits ] 
  156. then 
  157.    echo "Warn descriptors must not be lower than critical and crit hits must not be lower than warn hits!" 
  158.    print_help 
  159. fi 
  160.  
  161. get_status_text 
  162. is_replying 
  163. get_statistics 
  164. build_output 

 

返回移动开发教程...