zabbix报警微信通知

 

点击 检查问题 这样是 OK 的 

一、微信企业号申请 地址: http://work.weixin.qq.com/ 第一步注册

此处需要去微信扫一扫,扫描二维码

点击进入后台管理

二、配置企业微信

点击 我的企业----绑定企业域名----abc.com-----此时返回可以看到 CorpID(后面要用到)

点击 通讯录——>添加子部门

点击 企业应用 ——>创建应用

 

 

AgentID、Secret 很重要 接着 进入 微信企业号接口调试工具网站 http://qydev.weixin.qq.com/debug

corpID 点击 我的企业

corpsecret 点击 企业应用 ——> 点击zabbix我们创建的应用

 

 点击 检查问题 这样是 OK 的

接着 手机上需要下载 企业微信 ,下载完可以测试 网页端 发送给 手机企业微信

测试结果

准备事项: 1.部门ID

2. 应用ID

3. CorpID和Secret

三、修改Zabbix.conf

1. [root@vm1 ~]# grep alertscripts /etc/zabbix/zabbix_server.conf

2. AlertScriptsPath=/usr/lib/zabbix/alertscripts ——>查看结果这个不用修改

3. 我们设置zabbix默认脚本路径,这样在web端就可以获取到脚本

四、设置python脚本 [root@vm1 simplejson-3.8.2]# cd /usr/lib/zabbix/alertscripts/

[root@vm1 simplejson-3.8.2]# git clone https://github.com/X-Mars/Zabbix-AlertWeChat.git

如果没有 git 命令 需要安装 git-1.8.3.1-6.el7_2.1.x86_64 这个包

[root@vm1 simplejson-3.8.2]# cp Zabbix-Alert-WeChat/wechat.py /usr/lib/zabbix/ alertscripts/

[root@vm1 alertscripts]# chown zabbix. wechat.py

[root@vm1 alertscripts]# vim wechat.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 , 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":user, #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。我这里用user按部门发送
        "toparty":"2", #企业号中的部门id。
        "msgtype":"text", #消息类型。
        "agentid":"1000002", #企业号中的应用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 = 'wwb9dbf2cc1d84b5ca' #CorpID是企业号的标识
    corpsecret = 'nmqXtrSr-H7JwNl_cyjZreNZ8KYCdfHl2gDmwlWbuQ0'
    #corpsecretSecret是管理组凭证密钥
    accesstoken = gettoken(corpid,corpsecret)
    senddata(accesstoken,user,subject,content)
View Code

测试

 

如果报错:binbash^M 坏的解释器 没有那个文件或目录

[root@www alertscripts]# sed -i 's/\r$//' wechat.py

设置zabbix告警

 

 

 

posted @ 2017-09-13 21:17  dayby1  阅读(577)  评论(0)    收藏  举报