zoukankan      html  css  js  c++  java
  • Pinger短信告警Python脚本

    #!/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版本。

  • 相关阅读:
    一周随笔--15.10.06
    KVC/KVO总结
    一周随笔--15.9.28
    一周随笔--15.9.21
    CocoaPods使用中的invalid byte sequence in US-ASCII错误
    自定义TabBar
    FMDB配合sqlite使用
    常用第三方库
    LLDB编译器命令
    coreData
  • 原文地址:https://www.cnblogs.com/vincent-liang/p/6419836.html
Copyright © 2011-2022 走看看