Pinger短信告警Python脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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()

上述短信告警,只要你的短信http接口和账号匹配的话,这是一个完整的脚本。Python是采用2.0版本。

 

以上是关于Pinger短信告警Python脚本的主要内容,如果未能解决你的问题,请参考以下文章

Zabbix告警脚本-短信

zabbix发送短信告警脚本

不止短信!教你用 Python 发送告警通知到微信

通过脚本监控服务器状态(告警用邮件和短信)

Shell脚本日志关键字监控+告警

Alertmanager配置短信告警