zabbix的告警机制支持通过企业微信的方式,接下来将讲解如何通过配置企业微信把zabbix告警信息发送给管理员。
一、申请企业公众号
圈起来的都是要填写的,这里可选择部门,也可以是指定人员
创建完成后,会转到应用管理界面,AgentID和secret是需要记录的,
另外还要记录企业ID,点击我的企业,最下方有企业ID.
二、在zabbix-server端编写报警脚本,这里使用的是Python脚本
[root@HC100120 alertscripts]# cat ./wechatsuccess.py
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#date: 2020-12-20
#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('/opt/zabbix','weixin.log'),
filemode = 'a')
corpid='wwxxxxxxxxxf24ce11' ##企业ID
appsecret='yYcFd8IU3gcCzcv3TLnTMAQPhy4W9_xxxxxxxx_STP8' ##自定义应用私钥
agentid=1000005
#Ȗccesstoken
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)
测试脚本运行状况
###为企业微信应用程序中的用户ID
[root@HC100120 alertscripts]# chmod +x weichat.py
[root@HC100120 alertscripts]# ./wechatsuccess.py xxxxx "test" "test alerts"
接收到消息说明ok
三、web端配置报警媒介及动作
点击管理------>报警媒介类型------>创建媒体类型------>类型选择脚本,然后填写脚本名称(和服务器上脚本名称一致),三个参数必填,分别是用户,事件,和告警内容
{ALERT.SENDTO} {ALERT.SUBJECT} {ALERT.MESSAGE}
配置告警接收用户
管理----->用户----->报警媒介(收件人就是企业微信中的用户ID)
配置动作(动作关联到某个触发器,当有触发器触发时,发送告警给特定用户)
定义操作(用于给指定用户发送信息)
恢复操作也同样是发送信息