shell监控脚本-监控memcache

shell监控脚本-监控memcache

注意:请先参考 shell监控脚本-准备工作,监控脚本rhel5 下测试正常,其它版本的linux 系统请自行测试
#监控memcache

  
  
  
  
  1. cat chk_memcache.sh

  2. #!/bin/bash

  3. #

  4. #script_name:chk_memcache.sh

  5. #check memcache

  6. #

  7. #last update 20130320 by dongnan

  8. #bbs# http://bbs.ywwd.net/

  9. #blog# http://dngood.blog.51cto.com

  10. #

  11. #(echo "stats";sleep 2) | telnet IP 11211

  12. #Trying IP...

  13. #Connected to IP (IP).

  14. #Escape character is '^]'.

  15. #STAT pid 26090

  16. #STAT uptime 3179206

  17. #STAT time 1363157551

  18. #STAT version 1.2.6

  19. #.................

  20. #STAT pointer_size 64

  21. #STAT threads 1

  22. #END

  23. #Connection closed by foreign host.

  24. #

  25. #printf "stats\r\n" | nc IP 11211

  26. #

  27. #nc -z bbs.ywwd.net 80 && echo $?

  28. #0

  29. #nc -z bbs.ywwd.net 81 || echo $?

  30. #1

  31. #variables

  32. nc=/usr/bin/nc

  33. usleep=/bin/usleep

  34. echo=/bin/echo

  35. sh_dir=/root/sh/

  36. crondir=${sh_dir}crontab

  37. source ${sh_dir}CONFIG

  38. hosts="$MEMCACHE_HOSTS"

  39. check_count=8

  40. fault_count=4

  41. #main

  42. #主循环遍历机器

  43. for memcache in $hosts ;do

  44. flag_file=${crondir}/log/${memcache}.memcache  

  45. log=$crondir/log/memcache_error.log

  46. #true

  47. if [ $(echo "stats" | nc -w 2 "$memcache" 11211 | wc -l) -gt 2 ];then

  48. #flag  

  49.    if [ -f $flag_file ];then

  50.        #sms  

  51.        #for mobile in $MOBILES; do  

  52.            #$echo "$memcache memache ok" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode  

  53.        #done  

  54.        #email  

  55.        for mail in $MAILS;do

  56.            echo "$memcache memcache ok" | mail -s "$memcache memcache ok" $mail

  57.        done

  58.        #delete flag  

  59.        test -e "$flag_file" && rm -f "$flag_file"

  60.    fi

  61. #false

  62. else

  63. #flag存在,跳过本次循环

  64.    test -e $flag_file && continue

  65. check_failed=0

  66.    #  

  67.    for((i=1;i<="$check_count";i++));do

  68. check_date=$(date '+ %F %T')  

  69.        #  

  70.        if [ ! $(echo "stats" | nc -w 2 "$memcache" 11211 | wc -l) -gt 2 ];then

  71.            #  

  72.            ((check_failed++))  

  73.            #error.log  

  74.            $echo "$(/bin/date +'%F %T') $memcache $check_failed fault" >> "$log"  

  75.            #  

  76.            $usleep 300000 || sleep 1 || sleep 1  

  77.        fi

  78.    done

  79.    #大(等)于fault_count发送报警  

  80.    if [ "$check_failed" -gt "$fault_count" ];then

  81.         #sms  

  82.         #for mobile in $MOBILES;do  

  83.             #$echo "$memcache memache error" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode  

  84.             #/bin/date +'%F %T' && $echo "$memcache memache error"  

  85.         #done  

  86.         #mail  

  87.         for mail in $MAILS;do

  88.             echo "$memcache memcache error" | mail -s "$memcache memcache error" $MAILS

  89.         done

  90.         #log

  91.         echo "$check_date $HOST memcache error" >> "$log"

  92.         #flag

  93.         $echo "$memcache memache error" > "$flag_file"

  94.    fi

  95. #主if结束

  96. fi

  97. #主循环结束

  98. done

#
结束
更多请:  
linux 系统运维  37275208  
vmware 虚拟化  166682360

你可能感兴趣的:(shell,脚本,memcache,监控)