使用微信公众号-消息模版推送ZABBIX告警

发布时间 2023-10-08 09:45:56作者: 有何m不可

zabbix 布署实践【6 使用微信公众号-消息模版推送告警】

 
使用这个服务的前提是,你必须要有一个微信订阅号,或者公众号,并且是通过认证的号
因为认证过后的号才有模版消息和获取用户openid等信息的权限 ,如下,登录微信公众号的登录页后,底下有个接口权限的展示

 

 

 
 
我们可以理解为它类传于邮件发送告警的思路,
首先,你需要关注你们的微信公众号,其次zabbix后需要能调用腾讯的API接口,发送业务通知消息到指定的OpenID的人员
其次,你要有发送对象的openID,它就类似于是邮箱地址。需要将这个OpenID配置到用户的告警媒介,让其传参给我们的脚本中,
 
至于如何获取(已关注你公众号的微信用户的OpenID),可参考以下链接的PHP方法获得。
当然,如果你本身不是很懂PHP,并且你的公众号只有内部使用的话,人员使用得少,获取用户的OpenID,只需在公众号上配置一个回调的80端口域名,这个回调域名是你配置在你掌控的后台服务器上,在此服务器上安装nginx后,可以跟这个公众号进行交互,比如回复消息,或点击相关的公众号的链接按钮,通过查看微信回调过程中,携带的URL中,也是可以看出当前操作者的OpenID的,
如下截图,tail 一下回调服务器上的nginx的access log, 在openid=XXXXX  到&字符之前的这段ID就是OpenID
 

 

接着创建一个微信公众平台上的模版消息

 

有了接收告警的OpenID人员+模版消息的ID后,接来就是简单的创建脚本和动作了。
 
在zabbix 服务器端的数据库中,先创建一张表来作存放zabbix端发起微信API接口时返回的token(token请求是有次数的,并且token的失效时间较长,我们需要灵活应用)
 
mysql -u root -p
 
use zabbix;
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `wechat_token`;
CREATE TABLE `wechat_token` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `token` varchar(300) NOT NULL,
  `time_available` float(100,0) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
 
INSERT INTO `wechat_token` VALUES ('1', 'zRKH6VmKhNFLUBPwhKKeZb0KzZbfDOn8OYpl9zBEqzoQqFg-iU5Lry0mNZdKzliQYdhaq19_0qIT9LnFkw_SSJ7GrtolSJHcgCT6Kt-Pa9J8w1uG54a-fbJS2bvKoriMZNZdCGASGS', '1484297600');
 
 
 
退出数据库,
在服务器端的以下目录,创建发送微信脚本
 
 
# yum install  MySQL-python -y
 
cd /usr/lib/zabbix/alertscripts
 
[root@zabbixserver alertscripts]# vi wechat_monitor.py
#!/usr/bin/python
# coding=utf-8
 
import json
import time,sys
import MySQLdb
import requests
 
class wechat_monitor():
 
    appID = "wxe72b2acXXXXX"
    appsecret = "ed4f3975ebbfXXXXXXXXXXXXX"
    tempId = "HvPyBT6zIXj700B8hDN51DQXXXXXXXXXXXXXX"
    userOpenId = "o7oUNwLP2UXXXXXXXXXXXXXX"
 
    sendMessageUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="
    template = {'touser' : userOpenId,
                'template_id' : tempId,
                'url ' : "https://mail.qq.com",
                'topcolor ' : '#7B68EE',
                'data' : "" }
 
    tonkenURL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appID +"&secret=" + appsecret
 
    db_host = "127.0.0.1"
    db_port = 3306
    db_user = "root"
    db_password = "venic8888"
    db_name = "zabbix"
    now = time.time()
 
    def __init__(self):
 
        conn = self.__connections()
        cur=conn.cursor()
        selectSql = "select * from wechat_token"
        cur.execute(selectSql)
        data = cur.fetchone()
        if (int( self.now) - int(data[2])) < 3200:
            self.token = data[1]
        else:
            self.__getToken()
            updateSql = 'UPDATE wechat_token SET token="'+self .token+'",time_available=' +str(self.now)
            try:
                cur.execute(updateSql)
                conn.commit()
            except Exception, e:
                conn.rollback()
                print "数据库更新失败"
                print "Exception:" + e
        self.__closeDatabase(conn,cur)
 
    def __getToken(self):
#       print self.tonkenURL
        res = requests.post( self.tonkenURL)
        self.token = json.loads(res.text)["access_token"]
 
    def sendMessage(self,data):
        self.template["url" ] = ""
        self.template["data"] = data
        res = requests.post( self.sendMessageUrl + self.token, json.dumps(self.template))
        return res
 
    def __connections(self):
        try:
            conn = MySQLdb.connect(self.db_host,self.db_user,self.db_password, self.db_name,charset="utf8")
        except Exception, e:
            print "数据库连接失败"
            print "Exception:" + e
        return conn
 
    def __closeDatabase(self,conn,cur):
        try:
            cur.close()
            conn.close()
        except Exception, e:
            print "数据库关闭失败"
            print "Exception:" + e
 
if __name__ == '__main__':
    wm = wechat_monitor()
    wm.template["touser"] = sys.argv[1]
 
    data = {"first" : { "value" : sys.argv[2 ],"color" : "#173177" },
            "performance":{"value" : sys.argv[3],"color" : "#173177" },
            "time":{"value" :time.ctime(),"color" : "#173177" },
            "remark":{"value" : "更多内容请登录邮箱查看!","color" : "#173177"}}
    res = wm.sendMessage(data)
 
保存赋权。
chmod +x /usr/lib/zabbix/alertscripts/wechat_monitor.py
 
在报警媒介中,创建一个类型,启命叫WeChat

 

分析一下上面的微信脚本。
    appID = "wxe72bXXXXXXXXXX"-------这是公众号的ID(已认证过的。公众号平台上会一次性提供,需要自己留存)
    appsecret = "ed4f3975ebbf17b2XXXXXXXXXXXXX"-------这是VIFI产品公众号的密钥(已认证过的。公众号平台上会一次性提供,需要自己留存)
    tempId = "HvPyBT6zIXj700B8hXXXXXXXXXXXXXXXX"---------这是这个公众号的消息模版的ID(必须是认证过的。才有权限,公众号平台上会一次性提供,需要自己留存)
    userOpenId = "o7oUNwEl4nRKXXXXXXXX" 是我自己的OpenID用来测试提前赋值用的。
 
 
 
 
如果要抛开zabbix,先自测微信公众号的接口的话,可以通过以下脚本在自己电脑上的eclips上跑一下看看能否收到
 
#!/usr/bin/python
# coding=utf-8
 
import json
import time
 
import requests
 
appID = "wxe72bXXXXXXXXXXXXXX"
appsecret = "ed4f3975ebbf1XXXXXXXXXXXXXX"
tempId = "HvPyBT6zIXj700B8hXXXXXXXXXXXXXXXXX"
userOpenId = "o7oUNwLP2XXXXXXXXXXXXXXX"
 
sendMessageUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="
template = {'touser' : userOpenId,
            'template_id' : tempId,
            'url ' : "https://mail.qq.com",
            'topcolor ' : '#7B68EE',
            'data' : "" }
 
tonkenURL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appID +"&secret=" + appsecret
 
res = requests.post( tonkenURL)
token = json.loads(res.text)["access_token"]
 
def sendMessage(data):
    template["url" ] = ""
    template["data"] = data
    res = requests.post( sendMessageUrl + token, json.dumps(template))
    return res
 
if __name__ == '__main__':
 
    data = {"first" : { "value" : '',"color" : "#173177" },
            "performance":{"value" : '',"color" : "#173177" },
            "time":{"value" :time.ctime(),"color" : "#173177" },
            "remark":{"value" : "更多内容请登录邮箱查看!","color" : "#173177"}}
 
    res = sendMessage(data)
 
 
 接下来就是测试zabbix能否正常运行这个脚本。
首次先在zabbix服务器上自测一下
python /usr/lib/zabbix/alertscripts/wechat_monitor.py  你的OpenID  test  test
 
正常后,再定义zabbix的dashboard界面
管理员再添加一个告警媒介
 

 

关闭一台agent的机器的服务,等待其邮件和微信的告警推送。

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,负责保留追究法律责任的权利。posted @ 2017-01-16 14:53  veniceslove