Zabbix實現微信警報功能

WBOY
發布: 2016-12-05 13:27:17
原創
1732 人瀏覽過

一、 申請企業微信帳號,申請地址 https://qy.weixin.qq.com/


二、 登陸企業微信帳

圖一

圖二

2、新增微信帳號

 圖一

圖二

完成上述步驟後 就完成了微信帳號的新增

三、新建應用

圖一

圖二

圖三

圖四

以上四幅圖完成後就應用創建完成

四、設定權限管理

圖一

圖二

圖三

完成以上三幅圖的操作,權限管理設定完成;到此微信設定已經完成!

五、Zabbix Server配置

圖一

圖二

圖三

完成以上三幅圖中的配置,則zabbix server的配置已經完成。

七、weixin.py程式內容

#!/usr/bin/env python
# encoding: utf-8
# Create time 2016-10-08
#Auth chenpeng
import urllib2
import json
import sys
import time
class WebChat(object):
def __init__(self,CropID,Secret):
self.CropID = CropID
self.Secret = Secret
def Get_Token(self,info):
'''
:param info: 存储执行结果和执行程序状态码code (0代表执行成功,非零表示不成功)
:return:
'''
self.info = info
gurl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s" % (self.CropID,self.Secret)
try:
#通过Get方式获取token
req = urllib2.Request(gurl)
response = urllib2.urlopen(req)
g_result = json.loads(response.read(),"UTF-8")
if g_result .has_key('access_token'):
self.info['result']= g_result ['access_token']
self.info['code'] = 0
else:
self.info['result'] = g_result
self.info['code'] = 1
except Exception,e:
self.info['code'] = 1
self.info['result'] = e
def Send_Msg(self,touser,toparty,agentid,access_token,content,info,*args,**kwargs):
'''
发送信息到微信
:param touser: 部门成员id,zabbix中定义的微信接收者,
成员ID列表(消息接收者,多个接收者用‘|'分隔,最多支持1000个)。
特殊情况:指定为@all,则向关注该企业应用的全部成员发送
:param toparty: 部门id,定义了范围,组内成员都可接收到消息,
部门ID列表,多个接收者用‘|'分隔,最多支持100个。当touser为@all时忽略本参数
:param agentid: 企业应用的id,整型。可在应用的设置页面查看
:param access_token: 根据CropID,Secret获取的访问token值
:param content: 滤出zabbix传递的第三个参数,
表示发送微信消息的内容消息内容,最长不超过2048个字节,
注意:主页型应用推送的文本消息在微信端最多只显示20个字(包含中英文)
:param info: 返回执行结果信息{'result':None,'code':None};'code':0或者非零 ;0表示成功 非零表示失败
:param args:
:param kwargs:
:return:
'''
self.touser = touser
self.toparty = toparty
self.agentid = agentid
self.conntent = content
self.access_token = access_token
self.info = info
purl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % (access_token)
data = {
"touser": "",
"toparty": "",
"totag": "", #标签ID列表,多个接收者用‘|'分隔,最多支持100个。当touser为@all时忽略本参数,非必须
"msgtype": "text", #必须
"agentid": "", #必须
"text": {
"content": "" #必须
},
"safe": "0" # 表示是否是保密消息,0表示否,1表示是,默认0
}
data['touser'] = self.touser
data['agentid'] = self.agentid
data['toparty'] = self.toparty
data['text']['content']=self.conntent
data = json.dumps(data,ensure_ascii=False)
try:
#通过PUT方式获取发送数据
req = urllib2.Request(purl, data)
response = urllib2.urlopen(req)
res = json.loads(response.read())
self.info['code'] = res['errcode']
self.info['result'] = res['errmsg']
except Exception,e:
self.info['result'] = e
self.info['code'] = 1
if __name__ == '__main__':
reload(sys)
sys.setdefaultencoding('utf-8')
def log(date, touser, content,info):
'''
发送的日志打印日志
:param date: 时间
:param touser: 发送给谁
:param content: 发送的信息内容
:param info: 发送执行的结果
:return:
'''
msg = '%s %s %s 发送结果 - %s\n' % (date, touser, content, info)
with open('msg.log', 'a') as f:
f.write(msg)
agentid = sys.argv[1]
#agentid = 1
touser = 'xxxxxxx@qq.com'
toparty = ''
content = sys.argv[2:]
content = '\n'.join(content)
#content = '测试'
CropID = 'xxxxxxxxxxxxxxxxxxx'
Secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
info={'result':None,'code':None}
date = time.strftime('%Y-%m-%d %H:%M:%S')
res=WebChat(CropID,Secret)
res.Get_Token(info)
if info['code'] == 0:
access_token = info['result']
res.Send_Msg(touser=touser, toparty=toparty, agentid=agentid, access_token=access_token,
content=content,info=info)
if info['code'] == 0:
content = eval(content)
log(date, touser, content,info)
else:
log(date, touser, content, info)
else:
log(date,touser,content,info)
登入後複製

其中代碼114、115行中的CropID 和 Secret對應的是第四步《設定權限管理》中圖三對應的CropID 和 Secret

程式碼63行中的data數據,請參考微信介面文件

網址:http://qydev.weixin.qq.com/wiki/index.php?title=%E5%8F%91%E9%80%81%E6%8E%A5%E5%8F%A3%E8% AF%B4%E6%98%8E

以上所述是小編給大家介紹的Zabbix實現微信警報功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對腳本之家網站的支持!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!