zabbix實現微信公眾號告警
微信公眾號告警
首先在配置檔案裡修改AlertScriptsPath
[[email protected] alertscripts]# vim /usr/local/zabbix/etc/zabbix_server.conf
AlertScriptsPath=/usr/local/zabbix/alertscripts
(1)先註冊一個企業號、然後掃碼登入:
地址:https://qy.weixin.qq.com/cgi-bin/loginpage
(註冊過程就不一一展示了非常簡單)
(2)之後登入微信介面:點選我的企業我們記住一個CorpID(後面需要用到)
(3)在通訊錄裡面新增要發給他的成員
(4)我的企業,通訊裡裡面新增一個管理員使用者
(5)到許可權管理新增一個管理員:我的企業—-》許可權管理—-》新增一個管理員,就是自己:
(6)建立一個應用
(7)建立之後如下圖所示:
我們就有了:Agentid和Secret,這幾個值就夠了。
(8)點選微信外掛,掃碼二維碼關注這個企業:
(9)通過corpid和secret獲取token,傳送get請求:
[[email protected] ~]# curl "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww75c816542d965258&corpsecret=IlJSd3st_3Lz725ZOwpd31RMjPPmUvuAc3Ul9hYb50s
{“errcode":0,"errmsg":"ok","access_token":"axO29HjHI4ehg5fegDLPqaZUB5ebvAbfRxT0gP7Yl53GfuHlbj8Mzi0Q0op59GtJTyaxGBxliS6V2vUO-umWKDHbPrb8XoXdhBw89YFHPBJo6ChYuUBjqjV-67M1wBDzQBh2mVqvKgkIZpVyxPkpwZOI7DlX1RqDllRja3pi_iRwtP0UUHLBbwQm4WrVPyrPijzuCX1O8qgVvFGVzRg-EfaBEqSEXzv2FTAEhVqqYMmMsNQnh9wF_WvX4Eug3rdQgNp-qKsVkPheY7KIlQwUqr91_vxTOuTTLhryYAXUpoA","expires_in":7200}
##注:access_token和corpsecret換成你們自己的ID:##我們記住access_token:
(10)接下來我們測試一下這個使用者介面,獲取一下他有哪個使用者或者組方便下面傳送:
[[email protected] alertscripts]# curl “https://qyapi.weixin.qq.com/cgi-bin/agent/get?access_token=8vljFxihy6ks4shzo_cBs1erpGj4NdQkAi_QsYNiWxgei-w-wUoJ3oJQitJ4u_YhVANbskxXXefPsiZQf718JCcAwMZtWht0X-d5RUCSjKXPMxQzr58GJlFTwV-Gttfu2DWkpH-7IiUo2Ydcmp62mancof2raQmdlSJOFH_llvZXrsHqOeeM8sKH_VeuLyvEk8fSh5vSP10IRU1jbU-PMz20Esh4CNZzP8WyfncmDwSC5ZcTCibEPEJB145BzMFCkXll_-euuk1t8VhOrMepdpb-WfVmRljGL_U4r5XsGDY&agentid=1000002"
{"errcode":0,"errmsg":"ok","agentid":1000003,"name":"企業微信告警","square_logo_url":"http://p.qpic.cn/qqmail_pic/2400071420/f0f66ff89f483750ce9f44590fad46856a49371eb28a5834/0","description":"微信告警資訊","allow_userinfos":{"user":[{"userid":"ChenMingLe"}]},"allow_partys":{"partyid":[]},"close":0,"redirect_domain":"","report_location_flag":0,"isreportenter":0,"home_url":""}
##我們可以知道發給的使用者ID是ChenMingLe這個使用者:
(11)我們寫一個測試指令碼:
[[email protected] ~]# cd /usr/local/zabbix/alertscripts/
[[email protected] alertscripts]# ls
linkedsee.sh weixin.py zabbix_mail.py
[[email protected] alertscripts]# cat weixin.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
import sys
import urllib3
class weChat:
def __init__(self,Corpid,Secret):
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (Corpid,Secret)
res = self.url_req(url)
self.token = res["access_token"]
def url_req(self,url):
requests.packages.urllib3.disable_warnings()
req = requests.get(url, verify=False)
res = json.loads(req.text)
return res
def send_message(self,user,content):
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % self.token
data = {
"touser": user,
"msgtype": "text",
"agentid": 1000002,
"text": {
"content": content
},
"safe":"0"
}
requests.packages.urllib3.disable_warnings()
res = requests.post(url,json=data, verify=False)
if json.loads(res.content)['errmsg'] == 'ok':
return "send message sucessed"
else:
return res
if __name__ == '__main__':
user = sys.argv[1]
content = sys.argv[2]
get_token = weChat('ww75c816542d965258','IlJSd3st_3Lz725ZOwpd31RMjPPmUvuAc3Ul9hYb50s')
print get_token.send_message(user, content)
##注:weChat換成你們自己的corpid和secert:
[[email protected] alertscripts]# python weixin.py 'ChenMingLe' 'test' ##(測試指令碼)
send message sucessed
[[email protected] alertscripts]#
(12)在zabbix上新增報警媒介
(13)新增到使用者中,ChenMingle是新增到使用者名稱稱
(14)新增動作
(16)測試報警,解決報警
轉載於:https://blog.51cto.com/legehappy/1966059