配置企业微信报警步骤
1.有企业微信管理员账号
2.准备企业微信id、机器人id、密钥信息
3.准备python脚本
4.配置发件人、收件人
6.配置zabbix动作
企业微信私聊机器人
获取企业微信ID
登录企业微信管理后台——我的企业——企业ID

获取机器人ID
登录企业微信管理后台——应用管理——创建应用
创建完成后能看到ID 1000002

获取密钥
点击机器人下方secret,登录到企业微信中查看
准备python脚本
找到zabbix放脚本的位置
1 2 3
| cat /etc/zabbix/zabbix_server.conf | grep -i script | grep -v ^ AlertScriptsPath=/usr/lib/zabbix/alertscripts ExternalScripts=/usr/lib/zabbix/externalscripts
|
放入脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| yum -y install python-requests
cd /usr/lib/zabbix/alertscripts cat >weixin.py<<'EOF'
import requests import sys import os import json import logging logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s, %(filename)s, %(levelname)s, %(message)s', datefmt = '%a, %d %b %Y %H:%M:%S', filename = os.path.join('/tmp','weixin.log'), filemode = 'a') corpid='ww2a1f8fa4a4ce55ed' appsecret='7oHs9WWabR_NeKeQBTpwwdOUV2GKWKd0YQJzkwQiA_M' agentid='1000002'
token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret req=requests.get(token_url) accesstoken=req.json()['access_token']
msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken touser=sys.argv[1] subject=sys.argv[2]
message=sys.argv[2] + "\n\n" +sys.argv[3] params={ "touser": touser,
"msgtype": "text", "agentid": agentid, "text": { "content": message }, "safe":0 } req=requests.post(msgsend_url, data=json.dumps(params)) logging.info('sendto:' + touser + ';;subject:' + subject + ';;message:' + message) EOF chmod +x weixin.py
其中 corpid为微信企业号ID appsecret为机器人密钥 agentid为机器人的ID
脚本的三个参数都是zabbix内置变量,分别为: 报警收件人:{ALERT.SENDTO}, 报警标题:{ALERT.SUBJECT}, 报警内容:{ALERT.MESSAGE}
|
脚本测试
1
| python weixin.py 王盛 "test" "test"
|
企业微信群机器人
1.创建一个群聊,在群聊中选择创建机器人
会获得一个webhook地址
2.在zabbix-server添加报警脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| cat >/usr/lib/zabbix/alertscripts/alter.py<<'EOF'
import requests import json import sys
API_URL = "xxx"
HEADERS = { 'Content-Type': 'application/json;charset=utf-8' }
def send_text(text): texts = { "msgtype": "text", "text": { "content": text } } response = requests.post(API_URL, json=texts, headers=HEADERS) print(response.content)
if __name__ == '__main__': if len(sys.argv) != 2: print("Usage: python send_text.py <text>") sys.exit(1) text = sys.argv[1] send_text(text) EOF chmod +x alter.py
|

zabbix平台设置
配置发件人
进入zabbix-管理-媒介-新建媒介
类型选择脚本weixin.py
传参选择{ALERT.MESSAGE},并且添加消息模板

配置收件人
进入管理-用户-Admin-报警媒介
添加报警媒介为新创建的微信报警

配置动作
进入配置-动作-默认动作-操作
将新创建的媒介添加到动作中

测试
