zabbix python 微信告警脚本

释放双眼,带上耳机,听听看~!

测试zabbix的微信告警耗费了大量时间,使用了开源工具(OneOaaS weixin-alert)、shell脚本工具(手动执行正常,服务器调用失败),均没有实现相关功能
以下是自己优化过的Python脚本
Python大法好!!

zabbix python 微信告警脚本zabbix python 微信告警脚本


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
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4import urllib,urllib2,json
5import sys
6reload(sys)
7sys.setdefaultencoding( "utf-8" )
8
9class WeChat(object):
10        __token_id = ''
11        # init attribute
12        def __init__(self,url):
13                self.__url = url.rstrip('/')
14                self.__corpid = 'wx80179d3a3eb675c2'
15                self.__secret = 'rf8zW7iF-VECQVwKGndrHzAkEqfcnmSXmhIGUHCKH24'
16
17        # Get TokenID
18        def authID(self):
19            params = {'corpid':self.__corpid, 'corpsecret':self.__secret}
20            data = urllib.urlencode(params)
21            content = self.getToken(data)
22            try:
23                self.__token_id = content['access_token']
24                # print content['access_token']
25            except KeyError:
26                raise KeyError
27
28        # Establish a connection
29        def getToken(self,data,url_prefix='/'):
30                url = self.__url + url_prefix + 'gettoken?'
31                try:
32                    response = urllib2.Request(url + data)
33                except KeyError:
34                    raise KeyError
35                result = urllib2.urlopen(response)
36                content = json.loads(result.read())
37                return content
38
39        # Get sendmessage url
40        def postData(self,data,url_prefix='/'):
41                url = self.__url + url_prefix + 'message/send?access_token=%s' % self.__token_id
42                request = urllib2.Request(url,data)
43                try:
44                    result = urllib2.urlopen(request)
45                except urllib2.HTTPError as e:
46                    if hasattr(e,'reason'):
47                            print 'reason',e.reason
48                    elif hasattr(e,'code'):
49                            print 'code',e.code
50                    return 0
51                else:
52                    content = json.loads(result.read())
53                    result.close()
54                return content
55
56        # send message
57        def sendMessage(self,touser,subject,message):
58
59                self.authID()
60
61                data = json.dumps({
62                    'touser':touser,
63                    'msgtype':"news",
64                    'agentid':"1",
65                    'news':{
66                        "articles":[
67                            {
68                                "title": subject,
69                                "description":message,
70                            }  
71                           ]
72                    },
73                })
74
75                response = self.postData(data)
76                print response
77
78if __name__ == '__main__':
79        a = WeChat('https://qyapi.weixin.qq.com/cgi-bin')
80        a.sendMessage(sys.argv[1],sys.argv[2],sys.argv[3])
81

weixin.py

微信发消息接口:
http://qydev.weixin.qq.com/wiki/index.php
脚本使用方法:
python weixin.py userID title content
发送给所有用户:
python weixin.py @all title content
zabbix配置微信告警
管理→报警媒介类型→创建媒体类型
zabbix python 微信告警脚本

需添加的参数:
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
配置→动作→(触发器)创建动作
zabbix python 微信告警脚本

zabbix python 微信告警脚本

zabbix python 微信告警脚本

默认操作步骤持续时间设置为60s
[FAIL]: {EVENT.TIME}: {TRIGGER.NAME}
主机: {HOST.NAME1}
状态: {TRIGGER.STATUS}
级别: {TRIGGER.SEVERITY}
name: {ITEM.NAME1}
key: {ITEM.KEY1}
最后值: {ITEM.VALUE1}
[OK]-{EVENT.RECOVERY.TIME}: {TRIGGER.NAME}
主机: {HOST.NAME1}
状态: {TRIGGER.STATUS}
级别: {TRIGGER.SEVERITY}
name: {ITEM.NAME1}
key: {ITEM.KEY1}
最后值: {ITEM.VALUE1}
动作执行的操作配置
zabbix python 微信告警脚本

zabbix python 微信告警脚本

用户配置
zabbix python 微信告警脚本

 微信告警效果图:
zabbix python 微信告警脚本
邮件告警zabbix配置
zabbix python 微信告警脚本

 

转载于:https://www.cnblogs.com/Mrhuangrui/p/7424767.html

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++迭代器

2022-1-11 12:36:11

安全资讯

微信小程序新增5项功能 优化100多个功能点

2016-12-23 17:07:03

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索