zabbix 5系列之微信实时告警
1. 微信报警:
Zabbix除了可以使用邮件报警和钉钉告警之外,还可以通过微信报警,或者只要有api,你能想到的告警方式,zabbix都能实现。越灵活的告警机制越能及时有效地把告警信息推送到负责人,方便及时处理问题。
看这篇文章之前,最好看我的上篇钉钉告警,因为在这篇文章中,媒介的设置,动作的设置等介绍得比较简单,主要是功能实现,在上篇文章中,每个参数都有详细介绍。
zabbix 5系列之钉钉实时告警
1.1 微信企业号注册:
企业号注册地址:https://qy.weixin.qq.com/填写企业注册信息,个人也可以申请,不进行认证即可进行告警测试。
企业微信申请好后,可以在通讯录添加部门,以及运维人员,如果是测试,直接给管理员发也是可以的。
1.2 创建应用:
点击应用管理--点击下方创建应用--上传应用logo--输入应用名字--选择可见范围(可以是整个部门,也可以是指定人员),创建好后如下:
1.3 微信接口调试:
https://open.work.weixin.qq.com/wwopen/devtool/interface?doc_id=15074
1.4 配置微信脚本:
### 创建脚本: [root@localhost src]# cat > /usr/lib/zabbix/alertscripts/wechat <<EOF #!/usr/bin/python2.7 #_*_coding:utf-8 _*_ import requests,sys,json import urllib3 urllib3.disable_warnings() reload(sys) sys.setdefaultencoding('utf-8') def GetTokenFromServer(Corpid,Secret): Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken" Data = { "corpid":Corpid, "corpsecret":Secret } r = requests.get(url=Url,params=Data,verify=False) print(r.json()) if r.json()['errcode'] != 0: return False else: Token = r.json()['access_token'] file = open('/tmp/zabbix_wechat_config.json', 'w') file.write(r.text) file.close() return Token def SendMessage(User,Agentid,Subject,Content): try: file = open('/tmp/zabbix_wechat_config.json', 'r') Token = json.load(file)['access_token'] file.close() except: Token = GetTokenFromServer(Corpid, Secret) n = 0 Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Token Data = { "touser": User, # 指定用户告警,web中绑定用户的账号 #"totag": Tagid, # 指定企业号中的标签id,群发使用 "toparty": Partyid, # 指定企业号中的部门id,群发时使用。 "msgtype": "text", # 消息类型。 "agentid": Agentid, # 企业号中的应用id。 "text": { "content": Subject + '\n' + Content }, "safe": "0" } r = requests.post(url=Url,data=json.dumps(Data),verify=False) while r.json()['errcode'] != 0 and n < 4: n+=1 Token = GetTokenFromServer(Corpid, Secret) if Token: Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Token r = requests.post(url=Url,data=json.dumps(Data),verify=False) print(r.json()) return r.json() if __name__ == '__main__': User = sys.argv[1] Subject = str(sys.argv[2]) Content = str(sys.argv[3]) # CorpID 企业号的标识 Corpid = "ww5922d1****" # Secret 应用凭证密钥,在应用管理-点击机器人-查看secret可以得到 Secret = "PfrnUym8qOw5s64*" # 通讯录标签ID #Tagid = "1" # 应用ID Agentid = "1000008" # 部门ID Partyid = "1" Status = SendMessage(User,Agentid,Subject,Content) print Status ### 安装模块: [root@localhost src]# yum install python-pip -y [root@localhost src]# pip install requests # 测试脚本: [root@node1 alertscripts]# ./wechat lutixia test "nginx down" {u'invalidparty': u'1', u'invaliduser': u'', u'errcode': 0, u'errmsg': u'ok'}
1.5 web端创建报警媒介:
1.6 用户绑定媒介:
1.7 配置动作:
1.8 测试告警:
### 在122服务器,关闭redis服务: [root@localhost ~]# systemctl stop redis
### 在122服务器,开启redis服务: [root@localhost ~]# systemctl start redis
1.9 发送部门/组告警:
上面的告警是针对某人告警,在用户绑定媒介那里send to user这个user是通讯录中用户的账号(唯一标识,不是姓名)。如果是想对某个部门告警,那么这个user写啥就无所谓了,只需要修改脚本,把touser改为toparty,然后在脚本中指定Partyid的值即可。
#!/usr/bin/python2.7 #_*_coding:utf-8 _*_ 。。。 # "touser": User, # 把发送用户注释掉 "toparty": Partyid, # 把发送部门打开 "msgtype": "text", "agentid": Agentid, 。。。 # 通讯录标签ID #Tagid = "1" # 应用ID Agentid = "1000008" # 指定部门ID(在通讯录中可以看到) Partyid = "2" 。。。
1.10 测试告警:
### 在122服务器,关闭redis服务: [root@localhost ~]# systemctl stop redis
以上就是微信告警的配置,实验起来不难,有问题可以评论区交流。