本篇基于我的前两章环境续写:
1、【zabbix】docker安装zabbix、yum安装zabbix-agent
2、【zabbix】MySQL模板创建与监控
企业微信部分
一、首先得有个企业微信号

添加部门

应用管理创建应用



我的企业里面找到企业id,和上面两条一起,总共三条信息都要用到

测试以下通讯是否正常,微信企业号接口调试工具:

脚本准备部分
这里准备了两份脚本,两种发送方式,可以自选其一使用!
在宿主机/usr/lib/zabbix/alertscripts/路径下创建weixin.py
第一种脚本:这里要修改脚本里的这三条信息,也就是上文说到的三条信息
-
corpid='' #企业ID
-
appsecret='' #secret
-
agentid=1000002 #AgentID
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#comment: zabbix接入微信报警脚本
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='企业ID' #企业ID
appsecret='secret' #secret
agentid=AgentID #AgentID
#获取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=' + accesstoken
touser=sys.argv[1]
subject=sys.argv[2]
toparty='3|4|5|6'
message=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)
第二种脚本:使用微信机器人,这个需要在企业微信里面创建群,在群里创建机器人,之后复制机器人的Webhook:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#!/usr/bin/env python

本文介绍如何在Zabbix监控系统中配置企业微信告警,包括安装必要组件、编写和测试告警脚本,以及在Zabbix中设置告警媒介和动作。

3833

被折叠的 条评论
为什么被折叠?



