安裝sendmail來(lái)發(fā)郵件
# yum -y install sendmail # /etc/init.d/sendmail start # chkconfig sendmail on
安裝mutt郵件客戶端,并設(shè)置相關(guān)信息
# yum -y install mutt # vim /etc/Muttrc set charset="utf-8" #設(shè)置發(fā)郵件編碼 set envelope_from=yes set rfc2047_parameters=yes #解決附件亂碼問(wèn)題 set realname="報(bào)警" #發(fā)件人別名 set use_from=yes #指定是否顯示別名 set from=monitor@test.com #發(fā)送人地址
腳本如下
#!/bin/bash Mail="xxx@xxx.com" FailCount=0 Retval=0 GetUrlStatus() { for ((i=1;i< =3;i++)) #使用i++判斷訪問(wèn)次數(shù),如果wget兩次超時(shí)則判斷網(wǎng)站異常 do wget -T 3 --tries=1 --spider http://${1} >/dev/null 2>&1 #-T超時(shí)時(shí)間,--tries嘗試1次,--spider蜘蛛 [ $? -ne 0 ] && let FailCount+=1; #訪問(wèn)超時(shí)時(shí),$?不等于0,則FailCount加1 done if [ $FailCount -gt 1 ];then Retval=1 Date=`date +%F" "%H:%M` echo -e "Date : $Date Problem : $url is not running." | mutt -s "URL Monitor" $Mail else Retval=0 fi return $Retval #如果返回值為0,就正常退出循環(huán),不為0則繼續(xù)循環(huán) }for url in `cat url | sed '/^#/d'`do #GetUrlStatus $url && echo yes || echo no GetUrlStatus $urldonesleep 2m #死循環(huán),設(shè)置沒(méi)2分鐘運(yùn)行一次don