#!/usr/bin/env python #-*- coding:utf-8 -*- import urllib import urllib2 mobile = [18688965555,] #mobile = [15013805555,] #CONTENT = u'客户' def sms(CONTENT): for i in mobile: url='http://219.33.33.33/GsmsHttp' parms = { 'username':'73333:admin', 'password':'61029000', 'from':'3399', 'to':i, 'content':CONTENT, 'presendTime':'', 'expandPrefix':'113' } querystring = urllib.urlencode(parms) print (querystring) u = urllib2.urlopen(url+'?'+querystring) print (u) import subprocess import collections import sys reload(sys) sys.setdefaultencoding('gbk') def ping2(): dic = collections.OrderedDict() #dic[u'谷歌机房'] = '8.8.8.8' dic[u'泉州移动'] = '4.2.2.2' lines = list(dic.values()) keys = list(dic.keys()) for line in lines: line = str(line) line = line.strip() ping = subprocess.Popen( ["ping", "-c", "100", "-W", "1", line], stdout = subprocess.PIPE, stderr = subprocess.PIPE ) out, error = ping.communicate() print out num= int(out.split("%")[0].split(",")[-1]) if num>30 and num<100: sms(u'{0} 网络异常,丢包率为:{1}%'.format(keys[lines.index(line)], num)) elif num==100: sms(u'{0} 网络中断'.format(keys[lines.index(line)])) print error if __name__ == '__main__': ping2()
crontab -e
*/5 6-23 * * * /root/alert/pinger13.py
wq!
crontab -l
上述短信告警,只要你的短信http接口和账号匹配的话,这是一个完整的脚本。Python是采用2.0版本。