• zabbix3.0.4 部署之八 (zabbix3.0.4 微信报警)


    1

    [root@sv-zabbix ~]# cat /usr/local/zabbix/share/zabbix/alertscripts/weixin.py
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    import urllib,urllib2,json
    import sys
    reload(sys)
    sys.setdefaultencoding( "utf-8" )

    class WeChat(object):
            __token_id = ''
            # init attribute
            def __init__(self,url):
                    self.__url = url.rstrip('/')
                    self.__corpid = 'wx00e111c1606bb1'
                    self.__secret = 'lCrN-N17nou3L1x-eo1kejhKewBQH2XNbJ3uw9RTkSLo9m8h'

            # Get TokenID
            def authID(self):
                    params = {'corpid':self.__corpid, 'corpsecret':self.__secret}
                    data = urllib.urlencode(params)

                    content = self.getToken(data)

                    try:
                            self.__token_id = content['access_token']
                            # print content['access_token']
                    except KeyError:
                            raise KeyError

            # Establish a connection
            def getToken(self,data,url_prefix='/'):
                    url = self.__url + url_prefix + 'gettoken?'
                    try:
                            response = urllib2.Request(url + data)
                    except KeyError:
                            raise KeyError
                    result = urllib2.urlopen(response)
                    content = json.loads(result.read())
                    return content

            # Get sendmessage url
            def postData(self,data,url_prefix='/'):
                    url = self.__url + url_prefix + 'message/send?access_token=%s' % self.__token_id
                    request = urllib2.Request(url,data)
                    try:
                            result = urllib2.urlopen(request)
                    except urllib2.HTTPError as e:
                            if hasattr(e,'reason'):
                                    print 'reason',e.reason
                            elif hasattr(e,'code'):
                                    print 'code',e.code
                            return 0
                    else:
                            content = json.loads(result.read())
                            result.close()
                    return content

            # send message
            def sendMessage(self,touser,message):

                    self.authID()

                    data = json.dumps({
    #                        'touser':touser,
                            'toparty':"111",
                            'msgtype':"text",
                            'agentid':"111",
                            'text':{
                                    'content':message
                            },
                            'safe':"0"
                    },ensure_ascii=False)

                    response = self.postData(data)
                    print response


    if __name__ == '__main__':
            a = WeChat('https://qyapi.weixin.qq.com/cgi-bin')
            a.sendMessage(sys.argv[1],sys.argv[3])

  • 相关阅读:
    PAT甲级1018Public Bike Management
    PAT甲级1003Emergency
    android的AIDL
    View的滑动冲突和解决方案
    弹性滑动原理
    View的滑动原理和多种滑动方法
    Android的Activity的生命周期
    Android获取App版本号和版本名
    转:Android检查设备是否联网
    androidstudio实现增量更新步骤
  • 原文地址:https://www.cnblogs.com/sunpear/p/5816426.html
Copyright © 2020-2023  润新知