天天看點

shell監控腳本-監控web server

shell監控腳本-監控web server

注意:請先參考 shell監控腳本-準備工作,監控腳本在 rhel5 下測試正常,其它版本的linux 系統請自行測試

#監控web server

  1. cat chk_web.sh
  2. #!/bin/bash
  3. #
  4. #script_name:chk_web.sh
  5. #check web server 80 port
  6. #
  7. #last update 20130320 by dongnan
  8. #bbs# http://bbs.ywwd.net/
  9. #blog# http://dngood.blog.51cto.com
  10. #
  11. #curl -IL http://10.0.100.75/check.html 2>&1 | grep '200'
  12. #HTTP/1.1 200 OK
  13. #别忘了在你的web伺服器建立一個check.html
  14. #variables
  15. curl=/usr/bin/curl
  16. usleep=/bin/usleep
  17. echo=/bin/echo
  18. sh_dir=/root/sh/
  19. crondir=${sh_dir}crontab
  20. check_count=5        #故障後檢查次數
  21. fault_count=4        #故障次數大于(等于),則認為不可用
  22. options='--connect-timeout 1 -IL -A "check_www"'
  23. source ${sh_dir}CONFIG
  24. hosts="$LINUX_WEB_HOSTS"
  25. #main
  26. test -e "${crondir}/log" || mkdir -p "${crondir}/log"
  27. #主循環周遊機器
  28. for HOST in $hosts ;do
  29. flag_file=$crondir/log/"$HOST".web
  30. log=$crondir/log/web_error.log
  31. #curl真
  32. if $curl $options http://"$HOST"/check.html 2>&1 | grep '200' > /dev/null;then
  33.    #flag真,解除報警
  34.    if [ -f $flag_file ];then
  35.        #sms
  36.        #for mobile in $MOBILES; do
  37.            #$echo "cdn_www ok" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode
  38.            #$echo "cdn_www_index.html ok"
  39.        #done
  40.        #email
  41.        for mail in $MAILS;do
  42.            echo "$HOST 80 port ok" | mail -s "$HOST 80 port ok" $mail
  43.        done
  44.        #delete flag
  45.        test -e "$flag_file" && rm -f "$flag_file"
  46.    fi
  47. #curl假
  48. else
  49.    #flag真,跳出循環(已報警)
  50.    test -e $flag_file && continue
  51. check_failed=0
  52.    #
  53.    for((i=1;i<="$check_count";i++));do
  54. check_date=$(date '+ %F %T')
  55.        #curl 值取反
  56.        if ! $curl $options http://"$HOST"/check.html 2>&1 | grep '200' > /dev/null;then
  57.            #變量加1
  58.            ((check_failed++))
  59.            #error.log
  60.            $echo "$(/bin/date +'%F %T') $HOST $check_failed fault" >> "$log"
  61.            #使用微妙或者秒
  62.            $usleep 300000 || sleep 1
  63.        fi
  64.    done
  65.    #大(等)于fault_count發送報警
  66.    if [ "$check_failed" -ge "$fault_count" ];then
  67.         #sms
  68.         #for mobile in $MOBILES;do
  69.             #$echo "www_cdn_index.html error" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode
  70.             #/bin/date +'%F %T' && $echo "www_cdn_index.html error"
  71.         #done
  72.         #mail
  73.         for mail in $MAILS;do
  74.             echo "$HOST 80 port error" | mail -s "$HOST 80 port error" $mail
  75.         done
  76.         #log
  77.         echo "$check_date $HOST web error" >> $log    
  78.         #flag
  79.         $echo "cdn_www_index.html error" > "$flag_file"
  80.    fi
  81. #主if結束
  82. fi