shell脚本监测网站并通过邮件发出通知-定位原创
利用shell脚本监测网站是否可以正常访问,如果不能正常访问,在这里通过邮件发出通知,或者手机短信发送通知,这里以邮件来测试一下具体脚本代码:(以下代码个人亲测,可用)
#/bin/bash
#用shell来批量检查网址是否正常
RETVAL=0
FAILCOUNT=0
SCRIPTS_PATH="/shell"
MAIL_GROUP="cnblackhat@qq.com 445792169@qq.com"
LOG_FILE="/tmp/web_check.log"
[ -f /etc/init.d/functions ] && . /etc/init.d/functions || exit 1
function GetUrlStatus() {
# for (i=1;i<=3;i++)
# do
# wget -T 10 --tries=1 --spider http://${1} >/dev/null 2>&1
# done
wget -T 10 --tries=3 --spider http://${1} >/dev/null 2>&1
if [ $? -ne 0 ];then
RETVAL=1
NowTime=`date +"%m-%d %H:%M:%S"`
SUBJECT="${1} is bad"
SUBJECT_CONTENT="http://${1} service is error,${NowTime}."
for MAIL_USER in $MAIL_GROUP
do
echo "send to :$MAIL_USER Title:$SUBJECT_CONTENT" > $LOG_FILE
mail -s "$SUBJECT" $MAIL_USER <$LOG_FILE
done
else
RETVAL=0
fi
return $RETVAL
}
#func end
[ ! -d "$SCRIPTS_PATH" ] && `mkdir $SCRIPTS_PATH`
cat >$SCRIPTS_PATH/domain.list<<EOF
192.168.43.28
bbs.cnblackhat.com
www.cnblackhat.com
EOF
#service check
function ServiceCheck() {
for URL in `cat $SCRIPTS_PATH/domain.list`
do
echo -n "checking $URL:"
GetUrlStatus $URL && action "successful" /bin/true || action "failure" /bin/false
done
}
ServiceCheck
测试效果图:
页:
[1]