shell監控腳本-監控web server
注意:請先參考 shell監控腳本-準備工做,監控腳本在 rhel5 下測試正常,其它版本的linux 系統請自行測試
#監控web serverhtml
cat chk_web.sh linux
#!/bin/bash web
# shell
#script_name:chk_web.sh bash
#check web server 80 port 服務器
# 運維
#last update 20130320 by dongnan curl
#bbs# http://bbs.ywwd.net/ ide
#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
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
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
#主循環結束
done
#
結束更多請: linux 系統運維 37275208 vmware 虛擬化 166682360