Zabbix实现微信报警功能

Zabbix实现微信报警功能

作者:chen_vbird 字体:[增加 减小] 类型:转载 时间:2016-10-09 我要评论

这篇文章主要介绍了Zabbix实现微信报警的相关资料,本文图文并茂介绍的非常详细,需要的朋友可以参考下

一、 申请企业微信账号,申请地址 https://qy.weixin.qq.com/


二、 登陆企业微信账

图一

图二

2、添加微信账号

 图一

图二

完成以上步骤后 就完成了微信账号的添加

三、新建应用

图一

图二

图三

图四

以上四幅图完成后就应用创建完成

四、设置权限管理

图一

图二

图三

完成以上三幅图的操作,权限管理设置完成;到此微信设置已经完成!

五、Zabbix Server配置

图一

图二

图三

完成以上三幅图中的配置,则zabbix server的配置已经完成。

七、weixin.py程序内容

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/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
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
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实现微信报警功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

原文链接:http://www.cnblogs.com/cp-vbird/archive/2016/10/09/5942313.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值