zabbix配置微信告警功能
Zabbix支持邮件,短信告警,但是邮件告警有明显的滞后性,短信告警近来又缺少可用的免费发送短信的软件。而如果使用微信告警,一来免费,二来消息也能第一时间传达到我们的手机,是一个很不错的告警媒介选择。 近期帮朋友的zabbix配置了微信的告警,故记录下来,供大家参考。操作步骤:
一:注册微信企业号,配置相关人员和应用。
1:选择“通讯录”,添加一个部门,并新增一个成员,成员添加完之后,成员需关注你注册的企业号。
2:新建一个zabbix告警的”消息型“应用,如下:
3:配置应用的相关权限,让你应用对你的组及成员可见。记录CorpID和Secret的值。
二:zabbix配置wechat.py脚本,并测试脚本可用。
1:安装simplejson
1
2
3
4
5
# wget https://pypi.python.org/packages/f0/07/26b519e6ebb03c2a74989f7571e6ae6b82e9d7d81b8de6fcdbfc643c7b58/simplejson-3.8.2.tar.gz
# tar zxvf simplejson-3.8.2.tar.gz
# cd simplejson-3.8.2
# python setup.py build
# python setup.py install
2:进入zabbix的脚本目录(zabbix_server配置文件中指定,默认为:/usr/lib/zabbix/alertscripts/)
1
2
3
4
# cd /usr/lib/zabbix/alertscripts/
# git clone https://github.com/X-Mars/Zabbix-Alert-WeChat.git
# cp Zabbix-Alert-WeChat/wechat.py /usr/lib/zabbix/alertscripts/
# chmod +x wechat.py
3:根据注释提示修改脚本,使用之前记录的CorpID和Secret:
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
# more 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
printgettoken_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":"weixin", #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
"toparty":"2", #企业号中的部门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) #zabbix传过来的第一个参数
subject = str(sys.argv)#zabbix传过来的第二个参数
content = str(sys.argv)#zabbix传过来的第三个参数
corpid ='wx55e2936995a90a90' #CorpID是企业号的标识
corpsecret = 'CvHI0Jko3qsvv1XkEufWDWSWQj-1a5iuPpw-lcVcFze8yHFgcrBTDSFTFQ6lzPQq'#corpsecretSecret是管理组凭证密钥
accesstoken = gettoken(corpid,corpsecret)
senddata(accesstoken,user,subject,content)
4:测试脚本是否可用:
1
2
3
# ./wechat.py test test message
https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wx55e2936995a90a90&corpsecret=CvHI0Jko3qsvv1XkEufWDWSWQj-1a5iuPpw-lcVcFze8yHFgcrBTDSFTFQ6lzPQq
{u'invaliduser': u'all user invalid', u'errcode': 0, u'errmsg': u'ok'}
提示消息发送成功,查看微信消息可看到相关的测试信息:
三:zabbix web前端配置微信告警媒介与动作。
1:新增告警媒介,如下:
2:创建微信用户,如下:
配置告警媒介,选择之前创建的wechat微信告警。
添加相关权限,否则告警会不执行。
3:添加告警动作,如下:
根据个人的需要,配置相关的告警信息和恢复信息。
配置触发的操作,选择送到用户wechat
四:到此,zabbix的微信告警已经配置完成,可以模拟一条告警,看您的微信账户是否会收到改告警。这里就不做演示啦!!
不错,学习中。。。。。
不错,学习中。。。。。
不错,学习中。。。。。
不错,学习中。。。。。
不错,学习中。。。。。 正需要这个功能 不错,学习中。。。。。 不错,学习中。。。。。 不错,学习中。。。。。 不错,学习中。。。。。 不错,学习中。。。。。 学习了,正好拿来测试下 https://www.abcdocker.com/abcdocker/2573
页:
[1]