一、下载企业微信申请ID
https://open.work.weixin.qq.com/api/doc/10013 官方教程
二、拿到ID后配置脚本
[root@zabbix-server alertscripts]# cat /etc/zabbix/alertscripts/weixin.py
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#author: bgx
#date: 2018
#comment: zabbix接入微信报警脚本import requests
import sys
import os
import json
import logginglogging.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='wxd074861951c67ba6'
appsecret='0heVoLaEQguBAWS23_GCaBhR63Yxr8yHh7J-JUt3xEk'
agentid=1
#获取accesstoken
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=' + accesstokentouser=sys.argv[1]
subject=sys.argv[2]
#toparty='3|4|5|6'
message=sys.argv[2] + "\n\n" +sys.argv[3]params={
"touser": touser,
# "toparty": toparty,
"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)
安装python环境
yum install python2-pip -y
pip install requests
测试语句
python weixin.py t296917342 '口令' '今晚打老虎'
移动脚本
[root@zabbix-server alertscripts]# mv weixin.py /usr/lib/zabbix/alertscripts/
三、配置媒介报警
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
有问题请加博主微信进行沟通!
全部评论