微信报警

企业微信准备:Agentld + Secret + 企业ID号 + 部门ID

image.png

image.png

我的企业
企业ID :ww90ee3eaf907134fb
L4R46vyMPSGnCQ1jc07c95nJWIzT4Jd0zDWWQXgaZm0
image.png

image.png

image.png

image.png

image.png

image.png

python监控脚本:

有4个地方需要修改,找到他们修改成之前获取的信息

    self.__corpid = 'wwa1da242f211cdf47'   //公司的corpid
    self.__secret = 'rIrN51FEjgW6T2bdmSaEoOIB5UbaBTgddQkuC8cca1w' //应用的secret
    'toparty':2,         //部门id
    'agentid':"1000002",    //应用id
#!/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 = 'ww90ee3eaf907134fb'
                self.__secret = 'L4R46vyMPSGnCQ1jc07c95nJWIzT4Jd0zDWWQXgaZm0'
        # 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':2,
                        'msgtype':"text",
                        'agentid':"1000002",
                        '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])                                                

添加权限:

chown zabbix.zabbix /etc/zabbix/scripts/wenxin.py
chmod 777 /etc/zabbix/scripts/wenxin.py

测试脚本:

./wechat.py WangYi test test 微信名字
u'invalidparty': u'2', u'invaliduser': u'wusong', u'errcode': 0, u'errmsg': u'ok'} 成功

手机收到消息

image.png

创建报警媒介:

管理->报警媒介类型->创建媒体类型


创建新的媒介

image.png

image.png

image.png

新建动作——基本一样

新建动作

人工触发——修改触发器值

等待..... 收邮件

你可能感兴趣的:(微信报警)