定义函数的方式,监控网站的存活状态
#!/bin/bash
. /etc/init.d/functions
RETVAL=0
SCRIPTS_PATH="/tmp/test"
MAIL_GROUP="shengwei.tang@joy4you.com"
LOG_FILE=/tmp/test/web.log
function GetUrlStatus(){
FAILCOUNT=0
for i in `seq 3`;do
wget -T 2 --tries=1 --spider http://${1} >/dev/null 2>&1
[ $? -ne 0 ] && let FAILCOUNT+=1;
done
if [ $FAILCOUNT -gt 1 ];then
RETVAL=1
NOWTIME=$(date "+%Y-%m-%d %H:%M:%S")
echo "http://${1} service is error,${NOWTIME}" > $LOG_FILE
for MAIL_USER in $MAIL_GROUP;do
/usr/local/bin/sendEmail -f shengwei.tang@joy4you.com -t 1011464647@qq.com -s smtp.exmail.qq.com -u "${MAIL_USER}" -xu shengwei.tang@joy4you.com -xp 123456 -m $(cat ${LOG_FILE}) >& /dev/null
done
else
RETVAL=0
fi
return $RETVAL
}
[ ! -d "$SCRIPTS_PATH" ] && mkdir $SCRIPTS_PATH
[ ! -f "$SCRIPTS_PATH/domain.list" ] &&{
cat > $SCRIPTS_PATH/domain.list <<EOF
www.baidu.com
EOF
}
for URL in `cat $SCRIPTS_PATH/domain.list`;do
echo -n "checking $URL"
GetUrlStatus $URL && action " successful" /bin/true || action "failure" /bin/false
done