微信告警
微信企业号注册与使用
企业号注册:https://qy.weixin.qq.com/
企业号使用教程
登录新建的企业号,通过提前把企业成员信息添加到组织或者部门,需要填写手机号、微信号或邮箱,通过这样方式让别人扫码关注企业公众号,为了后面企业号推送消息给企业成员。
新建管理组,并加入需要通知的微信用户
创建应用
点击应用中心--新建应用
**记录此处应用ID为1000002
部门设置管理员
1、点击 设置--权限管理--普通管理组--新建
2、设置应用权限和通讯录权限
3、记录企业ID与Secert
至此,公众号配置完毕。
微信脚本调用
下载微信脚本:
[url href=http://download.cloudcared.cn/zabbix/wechat.py]wechat.py[/url]
编辑脚本,修改为自己的信息:
# -*- coding:utf-8 -*- #!/usr/bin/python3 import urllib.request import json import simplejson import re,sys,os,requests,datetime,time def gettoken(corpid, corpsecret): gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret print(gettoken_url) try: token_file = urllib.request.urlopen(gettoken_url) except urllib.request.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": "wangxiao", #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送 "msgtype": "textcard",#消息类型 "agentid": "1000002", #企业号中的应用id "textcard": { "title": "监控告警", "description": subject + '\n' + content, "url": "URL", "btntxt": "更多" }, "safe": "0" } send_data = simplejson.dumps(send_values, ensure_ascii=False).encode('utf-8') #print(send_data) send_request = urllib.request.Request(send_url,send_data) #print(send_request) response = json.loads(urllib.request.urlopen(send_request).read()) print(str(response)) if __name__ == '__main__': user = sys.argv[1] subject = sys.argv[2] content = sys.argv[3] corpid = 'wxff7' #CorpID是企业号的标识 corpsecret = 'KXmlfVf' #corpsecretSecret是应用的Secret accesstoken = gettoken(corpid, corpsecret) senddata(accesstoken, user, subject, content)
修改完成后,将其放至/usr/lib/zabbix/alertscripts,并修改相关权限
chown zabbix.zabbix wechat.py chmod +x wechat.py
测试脚本发送消息至企业号
安装simplejson
pip3 install simplejson==3.8.2 pip3 install requests
测试:
[root@zabbix alertscripts]# python3 wechat.py test-msg test hello https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wxff7d678b35937141&corpsecret=KXmlfVf8sLS3wTQ9fVXO48f8d_iJ9n7WSAX6Uwqoyv52aS8InX64zVi6zk5Fdo95 {'errcode': 0, 'errmsg': 'ok', 'invaliduser': '', 'invalidparty': '2'}
Zabbix-web前端设置
1、通过管理--示警媒介类型--创建媒体类型
脚本参数:
{ALERT.SENDTO} {ALERT.SUBJECT} {ALERT.MESSAGE}
2、点击 用户--Admin--示警媒介--添加 微信报警
注意:此处收件人填写应用ID:100002
3、点击 组态--动作--创建动作,名称为:微信报警,触发条件如下,操作添加Admin用户,并设置送达方式为微信告警
选择告警操作
告警主机:{HOST.NAME} 阈值名称:{TRIGGER.NAME} 当前状态:{TRIGGER.STATUS} {ITEM.VALUE1} 告警时间:{EVENT.DATE} {EVENT.TIME}
选择恢复操作
告警主机:{HOST.NAME} 阈值名称:{TRIGGER.NAME} 当前状态:{TRIGGER.STATUS} {ITEM.VALUE1} 告警时间:{EVENT.DATE} {EVENT.TIME} 恢复时间:{EVENT.RECOVERY.TIME}
4、完成后如下图:
测试微信报警
1、停止HTTP服务,等待告警
2、邮箱与微信均顺利收到告警,相关截图如下:
3、查看监控界面已经检测到,并相关示警媒介正在告知中:
微信: