shell監控腳本-監控web server
注意:請先參考 shell監控腳本-準備工作,監控腳本在 rhel5 下測試正常,其它版本的linux 系統請自行測試
#監控web server
- cat chk_web.sh
- #!/bin/bash
- #
- #script_name:chk_web.sh
- #check web server 80 port
- #
- #last update 20130320 by dongnan
- #bbs# http://bbs.ywwd.net/
- #blog# http://dngood.blog.51cto.com
- #
- #curl -IL http://10.0.100.75/check.html 2>&1 | grep '200'
- #HTTP/1.1 200 OK
- #别忘了在你的web伺服器建立一個check.html
- #variables
- curl=/usr/bin/curl
- usleep=/bin/usleep
- echo=/bin/echo
- sh_dir=/root/sh/
- crondir=${sh_dir}crontab
- check_count=5 #故障後檢查次數
- fault_count=4 #故障次數大于(等于),則認為不可用
- options='--connect-timeout 1 -IL -A "check_www"'
- source ${sh_dir}CONFIG
- hosts="$LINUX_WEB_HOSTS"
- #main
- test -e "${crondir}/log" || mkdir -p "${crondir}/log"
- #主循環周遊機器
- for HOST in $hosts ;do
- flag_file=$crondir/log/"$HOST".web
- log=$crondir/log/web_error.log
- #curl真
- if $curl $options http://"$HOST"/check.html 2>&1 | grep '200' > /dev/null;then
- #flag真,解除報警
- if [ -f $flag_file ];then
- #sms
- #for mobile in $MOBILES; do
- #$echo "cdn_www ok" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode
- #$echo "cdn_www_index.html ok"
- #done
- #email
- for mail in $MAILS;do
- echo "$HOST 80 port ok" | mail -s "$HOST 80 port ok" $mail
- done
- #delete flag
- test -e "$flag_file" && rm -f "$flag_file"
- fi
- #curl假
- else
- #flag真,跳出循環(已報警)
- test -e $flag_file && continue
- check_failed=0
- #
- for((i=1;i<="$check_count";i++));do
- check_date=$(date '+ %F %T')
- #curl 值取反
- if ! $curl $options http://"$HOST"/check.html 2>&1 | grep '200' > /dev/null;then
- #變量加1
- ((check_failed++))
- #error.log
- $echo "$(/bin/date +'%F %T') $HOST $check_failed fault" >> "$log"
- #使用微妙或者秒
- $usleep 300000 || sleep 1
- fi
- done
- #大(等)于fault_count發送報警
- if [ "$check_failed" -ge "$fault_count" ];then
- #sms
- #for mobile in $MOBILES;do
- #$echo "www_cdn_index.html error" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode
- #/bin/date +'%F %T' && $echo "www_cdn_index.html error"
- #done
- #mail
- for mail in $MAILS;do
- echo "$HOST 80 port error" | mail -s "$HOST 80 port error" $mail
- done
- #log
- echo "$check_date $HOST web error" >> $log
- #flag
- $echo "cdn_www_index.html error" > "$flag_file"
- fi
- #主if結束
- fi