linux shell脚本: 自动监控网站状态并发送提醒邮件

时间:2022-09-30 09:56:09

1.创建监控脚本:
$ vi /alidata/shell/webcheck.sh

#!/bin/sh

weblist="/alidata/shell/weblist.txt"
myemail="admin@qq.com"

for list in `cat $weblist|grep -E -v "#|^$"`
do
httpcode=`curl -o /dev/null -s -w %{http_code} "$list"`
httptime=`curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer:%{time_starttransfer}\ntime_total: %{time_total}\n" "$list"|grep time_total|awk -F ":" '{print $2*1000}'`

if [ $httpcode = 200 ]||[ $httpcode = 301 ]||[ $httpcode = 302 ]
then echo "$list is checked ok!"
else
echo "Warning: down !"
echo "$list is down!" | mutt -s "Warning: down !" $myemail
fi

if [ $httptime -ge 10000 ]
then echo "$list is timeout!" | mutt -s "Warning: timeout !" $myemail
#else echo "$list is connect ok!"
fi

done

$ chmod +x /alidata/shell/webcheck.sh

2.创建检查列表:
$ vi /alidata/shell/weblist.txt

http://www.mysite.com

3.添加计划任务:
$ crontab -e

 */ * * * /alidata/shell/webcheck.sh

注意:

1.必须先安装mutt, 用来发送邮件:
$ yum -y install mutt

2.报错:

sendmail: warning: inet_protocols: IPv6 support is disabled: Address family not supported by protocol

如何解决?
$ vi /etc/postfix/main.cf

inet_protocols = all

改为

inet_protocols = ipv4

3.报错:

postdrop: warning: unable to look up public/pickup: No such file or directory

如何解决?
$ /etc/init.d/sendmail stop
$ /etc/init.d/postfix start

;