Zabbix 使用企业微信公众号发送报警短信
#使用微信号发短信一般会有条数限制,企业公众号没有这个限制,而且成员分组也方便。
安装脚本依赖:
1
|
yum
install
python-simplejson -y
|
否则会报错:
ImportError: No module named simplejson
脚本如下所示,需要修改的部分都有注释,替换掉就可以使用。
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
|
cat wechat.py
#!/usr/bin/python
#_*_coding:utf-8 _*_
import
urllib,urllib2
import
json
import
sys
import
simplejson
reload
(sys)
sys.setdefaultencoding(
'utf-8'
)
def
gettoken(corpid,corpsecret):
gettoken_url
=
'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='
+
corpid
+
'&corpsecret='
+
corpsecret
print
gettoken_url
try
:
token_file
=
urllib2.urlopen(gettoken_url)
except
urllib2.HTTPError as e:
print
e.code
print
e.read().decode(
"utf8"
)
sys.exit()
token_data
=
token_file.read().decode(
'utf-8'
)
token_json
=
json.loads(token_data)
token_json.keys()
token
=
token_json[
'access_token'
]
return
token
def
senddata(access_token,user,subject,content):
send_url
=
'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='
+
access_token
send_values
=
{
"touser"
:
"@all"
,
#企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
"toparty"
:
"1"
,
#企业号中的部门id。
"msgtype"
:
"text"
,
#消息类型。
"agentid"
:
"1"
,
#企业号中的应用id。
"text"
:{
"content"
:subject
+
'\n'
+
content
},
"safe"
:
"0"
}
# send_data = json.dumps(send_values, ensure_ascii=False)
send_data
=
simplejson.dumps(send_values, ensure_ascii
=
False
).encode(
'utf-8'
)
send_request
=
urllib2.Request(send_url, send_data)
response
=
json.loads(urllib2.urlopen(send_request).read())
print
str
(response)
if
__name__
=
=
'__main__'
:
user
=
str
(sys.argv[
1
])
#zabbix传过来的第一个参数
subject
=
str
(sys.argv[
2
])
#zabbix传过来的第二个参数
content
=
str
(sys.argv[
3
])
#zabbix传过来的第三个参数
corpid
=
'wx97sdfzccsd'
#CorpID是企业号的标识
corpsecret
=
'7FbbmUuMFbD6TDkYvAi8jWCnAg3KVE8bVMJIJX8QHN9mz_fwdsdsffsdada'
#corpsecretSecret是管理组凭证密钥
accesstoken
=
gettoken(corpid,corpsecret)
senddata(accesstoken,user,subject,content)
|
#报警媒介:添加三个参数,配置微信报警脚本。
1
2
3
|
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
|
#报警动作:添加的内容如下:
名称:wechat
默认接收人:上海二区VPC机房:{TRIGGER.STATUS}
默认信息: 主机:{HOST.NAME}
地址:{HOST.IP}
故障发生时间:{EVENT.DATE} {EVENT.TIME}
事件:{TRIGGER.NAME}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
恢复主题:上海二区VPC机房:{TRIGGER.STATUS}: {TRIGGER.NAME}
恢复信息:
主机:{HOST.NAME}
地址:{HOST.IP}
故障发生时间:{EVENT.DATE} {EVENT.TIME}
事件:{TRIGGER.NAME}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
#条件:这块几乎不用做什么修改,保持默认的就好。
#操作:这块主要配置报警持续时间和发送第几部操作,发送给谁。
#用户报警媒介添加:
#指定报警媒介:
收到报警短信的结果如下图:
本文转自青衫解衣 51CTO博客,原文链接:http://blog.51cto.com/215687833/1890265