zabbix 通过微信(团体)企业号报警 简例
说明:1.通过在微信公众号平台注册一个团体的企业号,这种个人就可以注册;2.通过python来设置脚本;
3.然后在zabbix的web界面添加报警媒介类型,最后在action中调用报警媒介就OK了
————————————————————————————————————————
①在微信企业号中的设置:
添加成员,创建部门
http://i2.运维网.com/images/blog/201802/13/06e31a16b0795fbdd9c1f9e06426996a.png
②创建"自建应用":
重点是设置此应用谁可以看见、使用
http://i2.运维网.com/images/blog/201802/13/55c39f0acffa169962e11bae830d80a4.png
http://i2.运维网.com/images/blog/201802/13/81ace4cdf60a959514c27b5daff60d81.png
http://i2.运维网.com/images/blog/201802/13/cb9214074f13b4d491f9020df5290bf2.png
http://i2.运维网.com/images/blog/201802/13/c3cfe48287767d8288b8ee3098d0dfaa.png
③记录CorpID、secret、AgentID
http://i2.运维网.com/images/blog/201802/13/3addf403d4095565e19e9f24a581ee67.png
http://i2.运维网.com/images/blog/201802/13/25e8388edd22588c355846f8556d6d2e.png
http://i2.运维网.com/images/blog/201802/13/1734e58f4b6a2c81573bbde462ac88a7.png
④根据个人的ID、secret修改如下代码,并且将其放入zabbix /usr/lib/zabbix/alertscripts 的目录下(少数人可能会不一样);
#!/usr/bin/env python
#coding: utf-8
import time
import urllib,urllib2
import json
import sys
"""
touser否成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送
toparty否部门ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数
totag否标签ID列表,多个接收者用‘|’分隔。当touser为@all时忽略本参数
msgtype是消息类型,此时固定为:text
agentid是企业应用的id,整型。可在应用的设置页面查看
content是消息内容
safe否表示是否是保密消息,0表示否,1表示是,默认0
"""
# baseurl = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
# securl = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s' % access_token
class WeChatMSG(object):
def __init__(self,content):
self.gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
self.gettoken_content = {
'corpid' : 'wwe****cae9f961e45', #企业ID
'corpsecret' : 'fFm*****sR4DxndBHqhHgTMe42CAxO4TE' , #企业secret
}
self.main_content = {
"toparty":"2", #部门ID
"agentid":"1000002", #agentID
"msgtype": "text",
"text":{
"content":content,
}
}
def get_access_token(self,string):
token_result = json.loads(string.read())
access_token=token_result['access_token']
return access_token.encode('utf-8')
def geturl(self,url,data):
data = self.encodeurl(data)
response = urllib2.urlopen('%s?%s' % (url,data))
return response.read().decode('utf-8')
def posturl(self,url,data,isjson = True):
if isjson:
data = json.dumps(data)
response = urllib2.urlopen(url,data)
return response.read().decode('utf-8')
def encodeurl(self,dict):
data = ''
for k,v in dict.items():
data += '%s=%s%s' % (k,v,'&')
return data
if __name__ == '__main__':
if len(sys.argv) == 4:
touser,notuse,content = sys.argv
else:
print 'error segments, now exit'
sys.exit()
msgsender = WeChatMSG(content)
access_token_response = msgsender.geturl(msgsender.gettoken_url, msgsender.gettoken_content)
access_token =json.loads(access_token_response)['access_token']
sendmsg_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s' % access_token
print msgsender.posturl(sendmsg_url,msgsender.main_content)
要给py文件赋予相关权限:命令->>chmod ->>chown
⑤在zabbix的web界面添加报警媒介类型:
注意:脚本名字要正确
微信企业号注册-传送门
页:
[1]