CDN可以提升用户对网站访问的体验,一般情况下,比较大的公司会在全国各个城市甚至国外部署CDN节点,这样用户可以就近选择访问的节点,可以快速访问且节省带宽。但CDN节点一多就容易出现某个节点有问题的情况,本案例的需求是监控各个CDN节点,看其是否正常。
具体要求如下:
1)提供一个所有CDN节点IP的文本 /data/cdn_ip.list ; 2)提供一个监控链接地址:http://www.lzxlinux.com/test.php ; 3)要对比节点和源站的访问结果是否一致,假设源站IP为:88.88.88.88 ; 4)当某个节点出问题时,需要发送告警邮件给admin@admin.com ; 5)每分钟执行一次脚本,告警需要做收敛,即当有故障时立即告警,若故障一直存在则30分钟再次告警; 6)假定源站一定是正常的。
邮件脚本如下:
#!/usr/bin/python#coding:utf-8import smtplibfrom email.mine.text import MIMETextimport 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 监控告警平台"+"<"+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 Falseif __name__ == "__main__" send_mail(sys.argv[1], sys.argv[2], sys.argv[3])
参考脚本如下:
#!/bin/bash#监控所有CDN节点是否正常#定义收件人邮箱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/nullthen yum install -y curlfimycurl(){ curl --connect-timeout 2 -x$1:80 $url 2>/dev/null}#定义告警函数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 "节点$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 "节点$1异常持续30分钟了" "`cat $2`" 2>/dev/null echo "0" > /tmp/$log.count #第二次告警后计时器归0 fi fi}mycurl $s_ip >/tmp/s.htmlfor ip in `cat $ipf`do mycurl $ip > /tmp/$ip.html #对比源站和节点的页面是否有差异 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 fidone