Step1.安装sendmail来发邮件
# yum -y install sendmail
# /etc/init.d/sendmail start
# chkconfig sendmail on
Step2.安装mutt邮件客户端,并设置相关信息
# yum -y install mutt
# vim /etc/Muttrc
set charset="utf-8" #设置发邮件编码
set envelope_from=yes
set rfc2047_parameters=yes #解决附件乱码问题
set realname="报警" #发件人别名
set use_from=yes #指定是否显示别名
set [email protected] #发送人地址
Step3.脚本信息
1.url文件如下
# cat url
www.baidu.com
www.sina.com
2.脚本如下
#!/bin/bash #==================================================== # Author: lizhenliang - EMail:[email protected] # Last modified: 2015-2-1 # Filename: web_mon.sh # Description: wget monitoring web is normal # blog:lizhenliang.blog.51cto.com #==================================================== while true do Mail="[email protected]" FailCount=0 Retval=0 GetUrlStatus() { for ((i=1;i<=3;i++)) #使用i++判断访问次数,如果wget两次超时则判断网站异常 do wget -T 3 --tries=1 --spider http://${1} >/dev/null 2>&1 #-T超时时间,--tries尝试1次,--spider蜘蛛 [ $? -ne 0 ] && let FailCount+=1; #访问超时时,$?不等于0,则FailCount加1 done if [ $FailCount -gt 1 ];then Retval=1 Date=`date +%F" "%H:%M` echo -e "Date : $Date\nProblem : $url is not running." | mutt -s "URL Monitor" $Mail else Retval=0 fi return $Retval #如果返回值为0,就正常退出循环,不为0则继续循环 } for url in `cat url | sed ‘/^#/d‘` do #GetUrlStatus $url && echo yes || echo no GetUrlStatus $url done sleep 2m #死循环,设置没2分钟运行一次 done
时间: 2024-10-03 22:37:27