发布时间:2023-01-12 21:00
1、企业微信管理员登录后台查看企业corpid、要发送告警的部门id,记录下来
2、创建应用
3、点击创建的应用,进去查看记录这两个值:AgentId、Secret
4、在zabbix-server端编写脚本
#!/usr/bin/env python
# coding: utf-8
import urllib
import urllib2
import json
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def gettoken(corpid, corpsecret):
gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
try:
token_file = urllib2.urlopen(gettoken_url)
except urllib2.HTTPError as e:
print e.code
print e.read().decode("utf-8")
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,
"toparty": "xxx", #部门id
"msgtype": "textcard",
"agentid": "10001xx", #应用id,Agentld
"textcard": {
"title": "%s" % (subject),
"description": "%s" % (content),
"url": "http://123.20x.87.xx:20156/zabbix/zabbix.php?action=problem.view&ddreset=1", #根据实际,填写自己的zabbix地址
"btntxt": "查看监控"
},
"safe": "0"
}
send_data = json.dumps(send_values, ensure_ascii=False)
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])
subject = str(sys.argv[2])
content = str(sys.argv[3])
corpid = 'ww8fe6xxxxxxxxx5ae' #企业id,corpid
corpsecret = '3hlRnOq_J96xxxxxxxxxxAHeY080zxxxxxxxxxxxx' #应用Secret
accesstoken = gettoken(corpid, corpsecret)
senddata(accesstoken, user, subject, content)
5、配置zabbix-web界面
点击管理------>报警媒介类型------>创建媒体类型------>类型选择脚本,然后填写脚本名称(和服务器上脚本名称一致),三个参数必填,分别是用户,事件,和告警内容
{ALERT.SENDTO} {ALERT.SUBJECT} {ALERT.MESSAGE}
6、接着就可以去配置动作,和写自己的告警项目了,通过微信告警了