Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来越多的企业开始使用zabbix结合微信作为主要的告警方式,这样可以及时有效的把告警信息推送到接收人,方便告警的及时处理。
1、注册企业微信
注册地址: https://work.weixin.qq.com
2、配置企业微信
2.1 创建部门
2.2 添加成员
也可通过二维码邀请成员
记住成员账号(后面会用到)
3. 创建应用
填写应用信息(应用名字和可见成员)
记住应用(AgentId、Secret后面会用到)
4、配置监控脚本
准备事项:
微信企业号
企业号已经被部门成员关注
企业号有一个可以发送消息的应用,一个授权管理员,可以使用应用给成员发送消息
需要得到的信息
成员账号
组织部门ID
应用ID
CorpID和Secret
4.1 修改zabbix_agentd
[root@localhost ~]# grep alertscripts /etc/zabbix/zabbix_server.conf
# AlertScriptsPath=${datadir}/zabbix/alertscripts
AlertScriptsPath=/opt/scripts/zabbix/alertscripts
4.2 安装simplejson
wget https://pypi.python.org/packages/f0/07/26b519e6ebb03c2a74989f7571e6ae6b82e9d7d81b8de6fcdbfc643c7b58/simplejson-3.8.2.tar.gz
tar zxvf simplejson-3.8.2.tar.gz && cd simplejson-3.8.2
python setup.py build
python setup.py install
4.3 配置监控脚本
[root@localhost alertscripts]# cat /opt/scripts/zabbix/alertscripts/wechat.py
# -*- coding: utf-8 -*-
import requests
import json
class Wechat:
def __init__(self, corpid, corpsecret):
self.url = "https://qyapi.weixin.qq.com/cgi-bin"
self.corpid = corpid
self.corpsecret = corpsecret
self._token = self._get_token()
def _get_token(self):
'''
获取企业微信API接口的access_token
:return:
'''
token_url = self.url + "/gettoken?corpid=%s&corpsecret=%s" %(self.corpid, self.corpsecret)
try:
res = requests.get(token_url).json()
token = res['access_token']
return token
except Exception as e:
return str(e)
def _get_media_id(self, file_obj):
get_media_url = self.url + "/media/upload?access_token={}&type=file".format(self._token)
data = {"media": file_obj}
try:
res = requests.post(url=get_media_url, files=data)
media_id = res.json()['media_id']
return media_id
except Exception as e:
return str(e)
def send_text(self, agentid, content, touser=None, toparty=None):
send_msg_url = self.url + "/message/send?access_token=%s" % (self._token)
send_data = {
"touser": touser,
"toparty": toparty,
"msgtype": "text",
"agentid": agentid,
"text": {
"content": content
}
}
try:
res = requests.post(send_msg_url, data=json.dumps(send_data))
except Exception as e:
return str(e)
def send_image(self, agentid, file_obj, touser=None, toparty=None):
media_id = self._get_media_id(file_obj)
send_msg_url = self.url + "/message/send?access_token=%s" % (self._token)
send_data = {
"touser": touser,
"toparty": toparty,
"msgtype": "image",
"agentid": agentid,
"image": {
"media_id": media_id
}
}
try:
res = requests.post(send_msg_url, data=json.dumps(send_data))
except Exception as e:
return str(e)
4.4 报警测试
[root@localhost alertscripts]# ./wechat.py user 报警测试 error
https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww13e3038f3427b098&corpsecret=GUQCMJAHGnpotGZqmWCXC-ULnjgLH6NyO7adJUpUYik
{u'invalidparty': u'2', u'invaliduser': u'', u'errcode': 0, u'errmsg': u'ok'}
5、 zabbix web界面配置
5.1 配置报警媒介
5.2 创建报警用户
5.3 配置用户报警媒介(选择刚才创建的)
5.4 创建动作
5.5 设置报警消息
故障{TRIGGER.STATUS},服务器:{HOSTNAME1}发生: {TRIGGER.NAME}故障!
告警主机:{HOSTNAME1}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}
5.6 恢复报警
恢复{TRIGGER.STATUS}, 服务器:{HOSTNAME1}: {TRIGGER.NAME}已恢复!
告警主机:{HOSTNAME1}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}
到此,zabbix微信报警已完成,自行测试即可