監(jiān)控cdn上的頁面訪問示例:
郵件腳本如下:
#!/usr/bin/python
#coding:utf-8
import smtplib
from email.mine.text import MIMEText
import sys
mail_host = 'stmp.163.com'
mail_user = 'test@163.com'
mail_pass = 'your_mail_password'
mail_postfix = '163.com'
def send_mail(to_list,subject,content):
me = "zabbix 監(jiān)控告警平臺"+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content,'plain','utf-8')
msg['Subject'] = subject
msg['From'] = me
msg['to'] = to_list
try:
s = smtplib:SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me,to_list,msg.as_string())
s.close()
return True
except Exception,e:
print str(e)
return False
if __name__ == "__main__"
send_mail(sys.argv[1], sys.argv[2], sys.argv[3])
參考腳本如下:
#!/bin/bash
#監(jiān)控所有cdn節(jié)點是否正常
#定義收件人郵箱
mail="/usr/local/sbin/mail.py"
mail_user=admin@admin.com
url="http://www.lzxlinux.com/test.php"
s_ip="88.88.88.88"
ipf="/data/cdn_ip.list"
if ! which curl &>/dev/null
then
yum install -y curl
fi
mycurl()
{
curl --connect-timeout 2 -x$1:80 $url 2>/dev/null
}
#定義告警函數(shù)
m_mail()
{
log=$1
t_s=`date +%s`
t_s2=`date -d "1 hours ago" +%s`
if [ ! -f /tmp/$log ]
then
touch /tmp/$log
chattr +a /tmp/$log
echo $t_s2 >> /tmp/$log
fi
t_s2=`tail -1 /tmp/$log |awk '{print $1}'
echo $t_s >> /tmp/$log
v=$[$t_s-$t_s2]
if [ $v -gt 1800 ]
then
python $mail $mail_user "節(jié)點$1異常" "`cat $2`" 2>/dev/null
echo "0" > /tmp/$log.count
else
if [ ! -f /tmp/$log.count ]
then
echo "0" > /tmp/$log.count
fi
nu=`cat /tmp/$log.count`
nu2=$[$nu+1]
echo $nu2 > /tmp/$log.count
if [ $nu2 -gt 30 ]
then
python $mail $mail_user "節(jié)點$1異常持續(xù)30分鐘了" "`cat $2`" 2>/dev/null
echo "0" > /tmp/$log.count #第二次告警后計時器歸0
fi
fi
}
mycurl $s_ip >/tmp/s.html
for ip in `cat $ipf`
do
mycurl $ip > /tmp/$ip.html
#對比源站和節(jié)點的頁面是否有差異
diff /tmp/s.html /tmp/$ip.html > /tmp/$ip.diff 2>/dev/null
n=`wc -l /tmp/$ip.diff |awk '{print $1}'`
if [ $n -gt 0 ]
then
m_mail $ip /tmp/$ip.diff
fi
done