Zabbix能够经过多种方式把告警信息发送到指定人,经常使用的有邮件,短信报警方式,可是愈来愈多的企业开始使用zabbix结合微信做为主要的告警方式,这样能够及时有效的把告警信息推送到接收人,方便告警的及时处理。python
一、注册企业微信web
注册地址: https://work.weixin.qq.comjson
二、配置企业微信api
2.1 建立部门bash
2.2 添加成员服务器
也可经过二维码邀请成员微信
记住成员帐号(后面会用到)测试
3. 建立应用ui
填写应用信息(应用名字和可见成员)url
记住应用(AgentId、Secret后面会用到)
四、配置监控脚本
---------------------
准备事项:
微信企业号
企业号已经被部门成员关注
企业号有一个能够发送消息的应用,一个受权管理员,可使用应用给成员发送消息
须要获得的信息
成员帐号
组织部门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
#!/usr/bin/python #_*_coding:utf-8 _*_ import urllib,urllib2 import json import sys import simplejson reload(sys) sys.setdefaultencoding('utf-8') def gettoken(corpid,corpsecret): gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret # print gettoken_url try: token_file = urllib2.urlopen(gettoken_url) except urllib2.HTTPError as e: print e.code print e.read().decode("utf8") 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":'damaiyunwei', #企业号中的用户账号,在zabbix用户Media中配置,若是配置不正常,将按部门发送。 "toparty":"2", #企业号中的部门id。 "msgtype":"text", #消息类型。 "agentid":"1000002", #企业号中的应用id。 "text":{ "content":subject + '\n' + content }, "safe":"0" } # send_data = json.dumps(send_values, ensure_ascii=False) send_data = simplejson.dumps(send_values, ensure_ascii=False).encode('utf-8') print(send_data) 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]) #zabbix传过来的第一个参数 subject = str(sys.argv[2]) #zabbix传过来的第二个参数 content = str(sys.argv[3]) #zabbix传过来的第三个参数 corpid = 'ww758c347e5635e284' #CorpID是企业号的标识 corpsecret = 'Sf5m6Xfu3laloq36k9mIrrpjYufGl4U61zyUApCWNcE' #corpsecretSecret是管理组凭证密钥 accesstoken = gettoken(corpid,corpsecret) senddata(accesstoken,user,subject,content)
4.4 报警测试
./wechat.py 1 报警测试 error {"text": {"content": "报警测试\nerror"}, "safe": "0", "msgtype": "text", "touser": "damaiyunwei", "agentid": "1000002", "toparty": "2"} {u'invaliduser': u'', u'errcode': 0, u'errmsg': u'ok'}
五、 zabbix web界面配置
5.1 配置报警媒介
5.2 建立报警用户
五、 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微信报警已完成,自行测试便可