微信公众号告警

微信公众号告警

首先在配置文件里修改AlertScriptsPath

[root@cml alertscripts]# vim /usr/local/zabbix/etc/zabbix_server.conf

AlertScriptsPath=/usr/local/zabbix/alertscripts

 

1先注册一个企业号、然后扫码登录:

地址:https://qy.weixin.qq.com/cgi-bin/loginpage

(注册过程就不一一展示了非常简单)

2之后登录微信界面:点击我的企业我们记住一个CorpID(后面需要用到)

wKioL1m-MCbC6hZKAADT4fpleW8795.png650) this.width=650;” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/732093046312696978.png” title=”图片25.png” width=”700″ height=”565″ border=”0″ hspace=”0″ vspace=”0″ style=”width:700px;height:565px;” class=”aligncenter”> 

3)在通信录里面添加要发给他的成员

wKiom1m-MeyyW1OyAACbkAO1U6c825.png650) this.width=650;” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/8159720753958293244.png” title=”图片26.png” width=”700″ height=”183″ border=”0″ hspace=”0″ vspace=”0″ style=”width:700px;height:183px;” class=”aligncenter”> 

(4)我的企业,通讯里里面添加一个管理员用户

(5)到权限管理添加一个管理员:我的企业—-》权限管理—-》添加一个管理员,就是自己:

(6)创建一个应用

wKiom1m-MfSBiKEhAABaYTVeQ8Y440.png650) this.width=650;” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/8486669517555418131.png” title=”图片27.png” width=”700″ height=”374″ border=”0″ hspace=”0″ vspace=”0″ style=”width:700px;height:374px;” class=”aligncenter”> 

 

7创建之后如下图所示:

我们就有了:AgentidSecret,这几个值就够了。

wKioL1m-Mc6w-4PqAACs07hLSVE800.png650) this.width=650;” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/1986325771376347713.png” title=”图片28.png” width=”700″ height=”216″ border=”0″ hspace=”0″ vspace=”0″ style=”width:700px;height:216px;” class=”aligncenter”> 

8点击微信插件,扫码二维码关注这个企业:

 

wKiom1m-MgfAxJDyAAEHaUinCCU181.png650) this.width=650;” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/3026035091124036313.png” title=”图片29.png” width=”800″ height=”1077″ border=”0″ hspace=”0″ vspace=”0″ style=”width:800px;height:1077px;” class=”aligncenter”> 

9通过corpidsecret获取token,发送get请求:

[root@node1 ~]#  curl “https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww75c816542d965258&corpsecret=IlJSd3st_3Lz725ZOwpd31RMjPPmUvuAc3Ul9hYb50s”

{“errcode”:0,”errmsg”:”ok”,”access_token”:”axO29HjHI4ehg5fegDLPqaZUB5ebvAbfRxT0gP7Yl53GfuHlbj8Mzi0Q0op59GtJTyaxGBxliS6V2vUO-umWKDHbPrb8XoXdhBw89YFHPBJo6ChYuUBjqjV-67M1wBDzQBh2mVqvKgkIZpVyxPkpwZOI7DlX1RqDllRja3pi_iRwtP0UUHLBbwQm4WrVPyrPijzuCX1O8qgVvFGVzRg-EfaBEqSEXzv2FTAEhVqqYMmMsNQnh9wF_WvX4Eug3rdQgNp-qKsVkPheY7KIlQwUqr91_vxTOuTTLhryYAXUpoA”,”expires_in”:7200}

 

##:access_tokencorpsecret换成你们自己的ID:##我们记住access_token:

 

 

 

10接下来我们测试一下这个用户接口,获取一下他有哪个用户或者组方便下面发送:

 

[root@node1 alertscripts]# curl “https://qyapi.weixin.qq.com/cgi-bin/agent/get?access_token=8vljFxihy6ks4shzo_cBs1erpGj4NdQkAi_QsYNiWxgei-w-wUoJ3oJQitJ4u_YhVANbskxXXefPsiZQf718JCcAwMZtWht0X-d5RUCSjKXPMxQzr58GJlFTwV-Gttfu2DWkpH-7IiUo2Ydcmp62mancof2raQmdlSJOFH_llvZXrsHqOeeM8sKH_VeuLyvEk8fSh5vSP10IRU1jbU-PMz20Esh4CNZzP8WyfncmDwSC5ZcTCibEPEJB145BzMFCkXll_-euuk1t8VhOrMepdpb-WfVmRljGL_U4r5XsGDY&agentid=1000002″

 

 

{“errcode”:0,”errmsg”:”ok”,”agentid”:1000003,”name”:”企业微信告警“,”square_logo_url”:”http://p.qpic.cn/qqmail_pic/2400071420/f0f66ff89f483750ce9f44590fad46856a49371eb28a5834/0″,”description”:”微信告警信息“,”allow_userinfos”:{“user”:[{“userid”:”ChenMingLe“}]},”allow_partys”:{“partyid”:[]},”close”:0,”redirect_domain”:””,”report_location_flag”:0,”isreportenter”:0,”home_url”:””}

 

##我们可以知道发给的用户IDLuoHui这个用户:

 

 

11我们写一个测试脚本:

[root@cml ~]# cd /usr/local/zabbix/alertscripts/

[root@cml alertscripts]# ls

linkedsee.sh  weixin.py  zabbix_mail.py

[root@cml alertscripts]# cat weixin.py

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import requests

import json

import sys

import urllib3

class weChat:

    def __init__(self,Corpid,Secret):

        url = ‘https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s‘ % (Corpid,Secret)

        res = self.url_req(url)

        self.token = res[“access_token”]

    

    def url_req(self,url):

        requests.packages.urllib3.disable_warnings()

        req = requests.get(url, verify=False)

        res = json.loads(req.text)

        return res

 

    def send_message(self,user,content):

        url = “https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s” % self.token

        data = {

                “touser”: user,

                “msgtype”: “text”,

                “agentid”: 1000002,

                “text”: {

                        “content”: content

                        },

                “safe”:”0″

                }

        requests.packages.urllib3.disable_warnings()

        res = requests.post(url,json=data, verify=False)

        if json.loads(res.content)[‘errmsg‘] == ‘ok‘:

            return “send message sucessed”

        else:

            return res

 

 

 

if __name__ == ‘__main__‘:

    user = sys.argv[1]

    content = sys.argv[2]

    get_token = weChat(‘ww75c816542d965258‘,‘IlJSd3st_3Lz725ZOwpd31RMjPPmUvuAc3Ul9hYb50s‘)

    print get_token.send_message(user, content)

##注:weChat换成你们自己的corpidsecert:

 

[root@cml alertscripts]# python weixin.py ‘ChenMingLe‘ ‘test‘  ##(测试脚本)

send message sucessed

[root@cml alertscripts]#

wKioL1m-MevzPLAGAANN1NgM8h4405.png650) this.width=650;” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/5044492158052495380.png” title=”图片30.png” width=”700″ height=”1000″ border=”0″ hspace=”0″ vspace=”0″ style=”width:700px;height:1000px;” class=”aligncenter”> 

12)在zabbix上添加报警媒介

wKioL1m-MLPANZccAADzNIOMsBM838.png650) this.width=650;” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/8201220335350827829.png” title=”图片32.png” width=”700″ height=”470″ border=”0″ hspace=”0″ vspace=”0″ style=”width:700px;height:470px;” class=”aligncenter”> 

(13)添加到用户中,ChenMingle是添加到用户名称

wKioL1m-MYrB47APAADzNIOMsBM923.png650) this.width=650;” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/7305489270167050626.png” title=”图片32.png” width=”700″ height=”265″ border=”0″ hspace=”0″ vspace=”0″ style=”width:700px;height:265px;” class=”aligncenter”> 

(14)添加动作

wKiom1m-MavBZGLaAABl9Ye4VJ8914.png650) this.width=650;” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/2019730868873586607.png” title=”图片33.png” width=”700″ height=”479″ border=”0″ hspace=”0″ vspace=”0″ style=”width:700px;height:479px;” class=”aligncenter”>wKioL1m-MWuTuvPZAADi8Lxt70U304.png650) this.width=650;” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/7807209387363777080.png” title=”图片34.png” width=”700″ height=”515″ border=”0″ hspace=”0″ vspace=”0″ style=”width:700px;height:515px;” class=”aligncenter”>wKiom1m-MXiAcVVEAACweVxGBMU367.png650) this.width=650;” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/5200965615334035748.png” title=”图片35.png” width=”600″ height=”1071″ border=”0″ hspace=”0″ vspace=”0″ style=”width:600px;height:1071px;” class=”aligncenter”> 

(16)测试报警,解决报警

wKiom1m-MViSwSo4AALzPTdvQLg543.png650) this.width=650;” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/5182748591308600194.png” title=”图片36.png” width=”300″ height=”536″ border=”0″ hspace=”0″ vspace=”0″ style=”width:300px;height:536px;” class=”aligncenter”>wKioL1m-MRLzqGmRAALedRA13dg812.png650) this.width=650;” src=”https://images.winkp.com/imgs/winkp/_winkp/2023/08/7788758391349983830.png” title=”图片37.png” width=”300″ height=”536″ border=”0″ hspace=”0″ vspace=”0″ style=”width:300px;height:536px;” class=”aligncenter”>

本文出自 “第一个legehappy51cto博客” 博客,请务必保留此出处http://legehappy.blog.51cto.com/13251607/1966059