zabbix配置微信报警

 如有错误,敬请谅解!

此文章仅为本人学习笔记,仅供参考,如有冒犯,请联系作者删除!! 


6.1 注册企业微信

企业微信注册地址:https://work.weixin.qq.com

设置总部门名称添加成员

 也可以成员扫码加入,点击 成员加入,过程略。

6.2 添加一个部门

6.3 添加用户

添加一个用户到上面创建的部门里面(这里采取直接将管理员添加进去):记住用户账号

6.4 创建应用

 创建完成记住AgentID和Secret:

image-20220129225918766

 记住企业ID

6.5 微信企业号接口调试工具

企业微信

 

 

6.6 配置脚本

python

在zabbix server上操作
1、安装requests组件
[root@node1 ~]# dnf install -y python-pip
[root@node1 ~]# pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple
其中-i 指定使用国内镜像源

[root@node1 ~]# dnf install -y git
[root@node1 ~]# git clone https://github.com/X-Mars/Zabbix-Alert-WeChat.git
Cloning into 'Zabbix-Alert-WeChat'...
remote: Enumerating objects: 121, done.
remote: Total 121 (delta 0), reused 0 (delta 0), pack-reused 121
Receiving objects: 100% (121/121), 26.25 KiB | 316.00 KiB/s, done.
Resolving deltas: 100% (33/33), done.
[root@node1 ~]# cd Zabbix-Alert-WeChat/
[root@node1 Zabbix-Alert-WeChat]# cp -a wechat.py /usr/lib/zabbix/alertscripts/
[root@node1 Zabbix-Alert-WeChat]# cat /usr/lib/zabbix/alertscripts/wechat.py
#!/usr/bin/python2.7
#_*_coding:utf-8 _*_
#auther:火星小刘

import requests,sys,json
import urllib3
urllib3.disable_warnings()

reload(sys)
sys.setdefaultencoding('utf-8')

def GetTokenFromServer(Corpid,Secret):
    Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
    Data = {
        "corpid":Corpid,
        "corpsecret":Secret
    }
    r = requests.get(url=Url,params=Data,verify=False)
    print(r.json())
    if r.json()['errcode'] != 0:
        return False
    else:
        Token = r.json()['access_token']
        file = open('/tmp/zabbix_wechat_config.json', 'w')
        file.write(r.text)
        file.close()
        return Token

def SendMessage(User,Agentid,Subject,Content):
    try:
        file = open('/tmp/zabbix_wechat_config.json', 'r')
        Token = json.load(file)['access_token']
        file.close()
    except:
        Token = GetTokenFromServer(Corpid, Secret)

    n = 0
    Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Token
    Data = {
        "touser": User,                                 # 企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
        #"totag": Tagid,                                # 企业号中的标签id,群发使用(推荐)
        #"toparty": Partyid,                             # 企业号中的部门id,群发时使用。
        "msgtype": "text",                              # 消息类型。
        "agentid": Agentid,                             # 企业号中的应用id。
        "text": {
            "content": Subject + '\n' + Content
        },
        "safe": "0"
    }
    r = requests.post(url=Url,data=json.dumps(Data),verify=False)
    while r.json()['errcode'] != 0 and n < 4:
        n+=1
        Token = GetTokenFromServer(Corpid, Secret)
        if Token:
            Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % Token
            r = requests.post(url=Url,data=json.dumps(Data),verify=False)
            print(r.json())

    return r.json()


if __name__ == '__main__':
    User = sys.argv[1]                                                               # zabbix传过来的第一个参数
    Subject = str(sys.argv[2])                                                       # zabbix传过来的第二个参数
    Content = str(sys.argv[3])                                                       # zabbix传过来的第三个参数

    Corpid = "wxaf"                                                                  # CorpID是企业号的标识
    Secret = "aKDdCRT76"                                                             # Secret是管理组凭证密钥
    #Tagid = "1"                                                                     # 通讯录标签ID
    Agentid = "1000001"                                                              # 应用ID
    #Partyid = "1"                                                                   # 部门ID

    Status = SendMessage(User,Agentid,Subject,Content)
    print Status

#报警脚本中文乱码:    
将Python报警的脚本加了ensure_ascii=False  解决编码问题 
r = requests.post(url=Url,data=json.dumps(Data,ensure_ascii=False),verify=False)

此脚本在CentOS7 python2 环境没问题。CentOS8 中使用python2环境可正常接收。
本文为python3,因此此处使用shell脚本。

# cat weixin.sh
#!/bin/bash
CorpID="wwd5932acb806b41850c"	# 你的企业id
Secret="BbUbI_g_4zwvgYjlRinxBu5V_xK8R1VRT-QiNLYW5O0"	#你的SecretID
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CorpID&corpsecret=$Secret"
Token=$(/usr/bin/curl -s -G $GURL |awk -F\": '{print $4}'|awk -F\" '{print $2}')
# echo $Token
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Token"

function body(){
        local int agentid=1000002		# 你的agentdid
        local UserID="@all"                 # 发送的用户ID
        local PartyID=1                  # 部门ID
        local Msg=$(echo "$@" | cut -d" " -f3-)	# 发送给所有人
        printf '{\n'
        printf '\t"touser": "'"$UserID"\"",\n"
        printf '\t"toparty": "'"$PartyID"\"",\n"
        printf '\t"msgtype": "text",\n'
        printf '\t"agentid": "'"$agentid"\"",\n"
        printf '\t"text": {\n'
        printf '\t\t"content": "'"$Msg"\""\n"
        printf '\t},\n'
        printf '\t"safe":"0"\n'
        printf '}\n'
}
/usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL
        
[root@node1 alertscripts]# chmod +x weixin.sh 
[root@node1 alertscripts]# chown zabbix.zabbix weixin.sh 
[root@node1 alertscripts]# systemctl restart zabbix-server.service 
       
命令行测试发送:
[root@node1 alertscripts]# ./weixin.sh test123
{"errcode":0,"errmsg":"ok","msgid":"mrVtVXE39it1tWVvd57npJdUhX1zmSZZyLiL4oKQeXbJt9VoCB--nykac4P9nGRzwWn0lVah5HxQ26PmXpV37g"}       

6.7 zabbix web界面中配置微信报警

1> 进入:管理 -> 报警媒介类型 -> 创建媒体类型:

脚本参数

{ALERT.SENDTO}

{ALERT.SUBJECT}

{ALERT.MESSAGE}

 配置模板

 

6.8 用户和报警媒介关联

 

 

6.9 配置动作和操作

 

6.10 测试报警


如有错误,请联系作者删除

并恳请同行朋友予以斧正,万分感谢!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春光犹上人间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值