美文网首页
zabbix 设置微信报警

zabbix 设置微信报警

作者: DB哥 | 来源:发表于2019-09-29 08:37 被阅读0次

    Linux System Environment

    [root@nginx01 ~]# cat /etc/redhat-release                    #==》系统版本
    CentOS Linux release 7.5.1804 (Core)
    [root@nginx01 ~]# uname –r                                   #==》内核版本
    3.10.0-862.el7.x86_64
    [root@nginx01 ~]# uname -m                                   #==》系统架构
    x86_64
    [root@nginx01 ~]# echo $LANG                                #==》系统字符集
    en_US.UTF-8
    [root@zabbix ~]# zabbix_server -V                           #==》zabbix版本
    zabbix_server (Zabbix) 4.0.12
    

    一、注册企业微信
    注册地址: https://work.weixin.qq.com/wework_admin/loginpage_wx

    二、登录企业微信公从号添加账户

    1、应用管理 --- 创建应用

    2、上传logo填写相应的信息

    3、查看创建的启动应用
    标注:企业微信在应用管理创建的zabbix应用会自动生成的Agentld(应用代理ID)和Secret(管理组凭证密钥),weixin.py脚本中会使用到

    image.png image.png

    4、把创建zabbix应用管理进行接口调用测试
    调用测试地址: https://work.weixin.qq.com/api/devtools/devtool.php

    在企业微信查看的企业ID

    image.png

    三、企业微信添加接收警告信息的成员

    四、zabbix配置文件脚本目录路径

    [root@zabbix ~]#  grep "^AlertScriptsPath" /etc/zabbix/zabbix_server.conf
    AlertScriptsPath=/usr/lib/zabbix/alertscripts
    

    五、编写weixin.py脚本放到zabbix特定目录

    [root@zabbix ~]# vim /usr/lib/zabbix/alertscripts/weixin.py
    #!/usr/bin/python
    #_*_coding:utf-8 _*_
    
    import urllib,urllib2
    import json
    import sys
    import simplejson
    
    reload(sys)
    sys.setdefaultencoding('utf-8')
    
    def gettoken(corpid,corpsecret):
        gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
        print  gettoken_url
        try:
            token_file = urllib2.urlopen(gettoken_url)
        except urllib2.HTTPError as e:
            print e.code
            print e.read().decode("utf8")
            sys.exit()
        token_data = token_file.read().decode('utf-8')
        token_json = json.loads(token_data)
        token_json.keys()
        token = token_json['access_token']
        return token
    
    def senddata(access_token,user,subject,content):
    
        send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
        send_values = {
            "touser":"13434939203",     #==》企业微信的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
            "toparty":"1",              #==》企业微信部门ID
            "msgtype":"text",           #==》消息类型
            "agentid":"1001234",        #==》企业微信应用管理中创建zabbix应用代理ID
            "text":{
                "content":subject + '\n' + content
               },
            "safe":"0"
            }
    #    send_data = json.dumps(send_values, ensure_ascii=False)
        send_data = simplejson.dumps(send_values, ensure_ascii=False).encode('utf-8')
        send_request = urllib2.Request(send_url, send_data)
        response = json.loads(urllib2.urlopen(send_request).read())
        print str(response)
    
    if __name__ == '__main__':
        user = str(sys.argv[1])             #==》zabbix传过来的第一个参数
        subject = str(sys.argv[2])          #==》zabbix传过来的第二个参数
        content = str(sys.argv[3])          #==》zabbix传过来的第三个参数
    
        corpid =  'wwds123456dddd'              #==》企业ID
        corpsecret = 'Bdfd556o0P546764TBO543lcdf3340'  #==》Secret管理组凭证密钥
        accesstoken = gettoken(corpid,corpsecret)
        senddata(accesstoken,user,subject,content)
    
    

    六、使用命令发信息测试

    提示:yum install python-simplejson –y 使用命令安装python模块

    simplejson,否则会报错
    [root@zabbix ~]# python /usr/lib/zabbix/alertscripts/weixin.py test_message '发送测试' ‘测试信息’
    https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wwca3656191a19ed7e&corpsecret= Bdfd556o0P546764TBO543lcdf3340
    {u'invaliduser': u'13434939203', u'errcode': 0, u'errmsg': u'ok'}
    
    

    七、zabbix web添加微信报警

    1、创建发送媒休类型

    2、设置发送报警媒介类型

    3、创建接收信息用户

    4、报警媒介设置

    5、创建触发器动作

    6、设置触发动作及发送内容

    八、测试微信警告发送
    标注:主动触发相关的警告,查看微信的接收信息

    相关文章

      网友评论

          本文标题:zabbix 设置微信报警

          本文链接:https://www.haomeiwen.com/subject/rxkwyctx.html