├── README.md └── zabbix_weixin_kapian.py /README.md: -------------------------------------------------------------------------------- 1 | # zabbix问题确认微信卡片通知 2 | 3 | ### 环境要求 4 | 5 | #### python3 zabbix 3.4 微信企业号 6 | ##### 我刚刚学习python没多久,写的不是很好大神勿喷n(*≧▽≦*)n 7 | 个人博客 8 | https://www.aityp.com 9 | 10 | --- 11 | zabbix 从3.4版本后开始支持问题确认通知,效果如下 12 | 13 | ![image](https://typ.oss-cn-shanghai.aliyuncs.com/markdown/2017/10/zabbix_weixin_1.jpg) 14 | 15 | ## 使用方法 16 | 17 | **pull代码** 18 | ``` 19 | yum install git -y 20 | git clone https://github.com/typ431127/zabbix_weixin 21 | ``` 22 | **更改脚本参数,需要把这三个参数改成你微信企业号的** 23 | 24 | 参数 | 说明 25 | ---|--- 26 | Toparty | 企业号部门id 27 | CropID | 微信开发者CropID 28 | Secret | 微信开发者Secret 29 | 30 | 31 | ![image](https://typ.oss-cn-shanghai.aliyuncs.com/markdown/2017/10/11.png) 32 | 33 | ## 注意 34 | **开头环境选择,请设置成你自己的python解释器版本,因为我电脑上面有多个环境为了区分才写成这样** 35 | 36 | ![image](https://typ.oss-cn-shanghai.aliyuncs.com/markdown/2017/10/12.png) 37 | 38 | **把脚本放到zabbix scripts下面,执行下面命令** 39 | ``` 40 | chown zabbix:zabbix zabbix_weixin_kapian.py 41 | chmod +x zabbix_weixin_kapian.py 42 | touch /tmp/weixin_zabbix.log 43 | chown zabbix:zabbix /tmp/weixin_zabbix.log 44 | ``` 45 | 46 | **首先我们添加一个报警脚本** 47 | 48 | ![image](https://typ.oss-cn-shanghai.aliyuncs.com/markdown/2017/10/1.png) 49 | 50 | **名称随便写这里我写'微信卡片通知' 51 | 脚本名称写你放到服务器目录的名称** 52 | 53 | ![image](https://typ.oss-cn-shanghai.aliyuncs.com/markdown/2017/10/2.png) 54 | 55 | **在用户里面添加报警媒介** 56 | 57 | 58 | ![image](https://typ.oss-cn-shanghai.aliyuncs.com/markdown/2017/10/3.png) 59 | 60 | **类型:你刚才新建的脚本名称 61 | 收件人:企业微信用户名** 62 | ![image](https://typ.oss-cn-shanghai.aliyuncs.com/markdown/2017/10/4.png) 63 | **配置---动作---编辑你的报警脚本---Acknowledgement operations 64 | 默认收件人不用动 65 | 默认信息可以使用我的模板** 66 | ``` 67 | 用户:{USER.FULLNAME} 68 | 时间:{ACK.DATE} {ACK.TIME} 69 | 确认了这个问题,信息如下: 70 | {ACK.MESSAGE} 71 | 问题服务器IP:{HOSTNAME1} 72 | 问题ID:{EVENT.ID} 73 | 当前的问题是: {TRIGGER.NAME} 74 | ``` 75 | **然后添加一个操作** 76 | 77 | ![image](https://typ.oss-cn-shanghai.aliyuncs.com/markdown/2017/10/5.png) 78 | 79 | **添加操作配置如下** 80 | 81 | ![image](https://typ.oss-cn-shanghai.aliyuncs.com/markdown/2017/10/6.png) 82 | 83 | **我们停止测试服务器客户端,点击确认问题** 84 | 85 | ![image](https://typ.oss-cn-shanghai.aliyuncs.com/markdown/2017/10/8.png) 86 | 87 | **确认随便写** 88 | 89 | ![image](https://typ.oss-cn-shanghai.aliyuncs.com/markdown/2017/10/9.png) 90 | 91 | **企业号成功收到消息** 92 | 93 | ![image](https://typ.oss-cn-shanghai.aliyuncs.com/markdown/2017/10/10.png) 94 | 95 | 96 | ### LOG 97 | log目录在`/tmp/weixin_zabbix.log` 98 | 在这里可以看到每条消息的发送情况 99 | ![image](https://typ.oss-cn-shanghai.aliyuncs.com/markdown/2017/10/13.png) 100 | 101 | ### QQ:1500698928 102 | ### 个人微信 103 | ![image](https://typ.oss-cn-shanghai.aliyuncs.com/markdown/2017/10/14.jpg?x-oss-process=image/resize,h_600) 104 | 105 | ### 问题调试方法 106 | 如果你的微信收不到消息可以使用以下方法进行调试 107 | 命令行调试脚本 108 | ``` 109 | python3 zabbix_weixin_kaping.py 你的企业号用户名 1 测试 110 | ``` 111 | 查看日志输出 112 | token获取失败,请检查key等配置信息 113 | 日志里面有返回码,微信企业号错误返回码查询:http://qydev.weixin.qq.com/wiki/index.php?title=%E5%85%A8%E5%B1%80%E8%BF%94%E5%9B%9E%E7%A0%81%E8%AF%B4%E6%98%8E 114 | ![image](https://typ.oss-cn-shanghai.aliyuncs.com/markdown/2017/10/15.png) 115 | -------------------------------------------------------------------------------- /zabbix_weixin_kapian.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # zabbix notification confirmation script 4 | # Author: typ431127@gmail.com 5 | # My blog https://www.aityp.com 6 | 7 | 8 | import requests 9 | import json 10 | import time 11 | import os 12 | import sys 13 | 14 | 15 | Toparty="2" #部门id 16 | CropID='xxxxx' 17 | Secret='xxxxx' 18 | 19 | Gtoken="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+ CropID + "&corpsecret=" + Secret 20 | Purl="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" 21 | headers = {'Content-Type': 'application/json'} 22 | Time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 23 | 24 | def log(info): 25 | #注意权限,否则写不进去日志 26 | file = "/tmp/weixin_zabbix.log" 27 | if os.path.isfile(file) == False: 28 | f = open(file, 'a+') 29 | 30 | f = open(file,'a+') 31 | f.write(info) 32 | f.close() 33 | 34 | def get_token(): 35 | try: 36 | r = requests.get(Gtoken) 37 | json_data = json.loads(r.content.decode()) 38 | token = json_data["access_token"] 39 | return token 40 | 41 | except Exception as e: 42 | #print("token获取失败!",e) 43 | log(Time + ":token获取失败!" + "\n") 44 | exit(1) 45 | 46 | 47 | def msg(user,msg): 48 | token=get_token() 49 | weixin_msg = { 50 | "touser" : user, 51 | "toparty" : Toparty, 52 | "totag" : "TagID1 | TagID2", 53 | "msgtype" : "textcard", 54 | "agentid" : 1, 55 | "textcard" : { 56 | "title" : "报警确认通知", 57 | "description" : msg, 58 | "url" : "URL", 59 | "btntxt":"更多" 60 | } 61 | } 62 | push = requests.post(Purl + token,data=json.dumps(weixin_msg),headers=headers) 63 | code = json.loads(push.content.decode()) 64 | if code["errcode"] == 0: 65 | log(Time + ":消息发送成功 返回码;"+ str(code["errcode"]) + "\n") 66 | else: 67 | log(Time + ":消息发送失败 返回码:"+ str(code["errcode"]) + "\n") 68 | 69 | if __name__ == '__main__': 70 | text = sys.argv[3] 71 | user = sys.argv[1] 72 | msg(user,text) 73 | --------------------------------------------------------------------------------