zabbix设置企业微信告警

该博客介绍了如何在Zabbix中设置企业微信告警,包括修改zabbix_server.conf配置文件,创建并编辑告警脚本weixin.sh,设置告警媒介,并提供了测试和备用脚本。内容涉及企业微信API的使用和告警信息的传递。
摘要由CSDN通过智能技术生成

zabbix设置企业微信告警
1、找到alertscripts文件夹路径,修改zabbix_server.conf

[root@zabbixserver ~]# vim /etc/zabbix/zabbix_server.conf 
AlertScriptsPath=/usr/lib/zabbix/alertscripts

2、设置脚本

[root@zabbixserver alertscripts]# vim weixin.sh
#!/bin/bash
CropID='#################'	#企业ID
Secret='#############################'	#创建的应用SecretID
#获取access_token
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F\" '{print $10}')
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"
function body () {
     local Meg=$(echo "$@" | cut -d" " -f3-)
         echo """{
            \"touser\" : \"@all\",
            \"msgtype\" : \"text\",   
            \"agentid\" : #######,  #创建的应用AgentID这条说明要去掉
            \"text\" : {           
            \"content\" : \"$Meg\"}   
                }"""
            }
/usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL
[root@zabbixserver alertscripts]# chmod 755 weixin.sh

3.、创建告警媒介
在这里插入图片描述
参数
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
3、测试

[root@zabbixserver alertscripts]# ./weixin.sh 1111 test test
{"errcode":0,"errmsg":"ok","invaliduser":""}

也可以模拟故障看看企业微信有没有收到信息

附上另外两个脚本

#!/bin/bash
CorpID="###############"
#我的企业下面的CorpID
Secret="#############################"
#创建的应用那有Secret
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=#######
#改为AgentId 在创建的应用那里看
local UserID=$1
#发送的用户位于$1的字符串
#local PartyID=
#第一步看的通讯录中的部门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

python脚本,centos8需安装python2.7
dnf install python2 -y

[root@zabbixserver ~]# ln -s /usr/bin/python2.7 /usr/bin/python
#或者
[root@zabbixserver ~]# ln -s /usr/bin/python3 /usr/bin/python
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#date: 2021-07-28
#comment: zabbix接入企业微信报警脚本
import requests
import sys
import os
import json
import logging
corpid='##################' #企业ID
appsecret='#################################'  #secret
agentid=#######  #AgentID
#获取accesstoken
token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret
req=requests.get(token_url)
accesstoken=req.json()['access_token']
#发送消息
msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken
touser=sys.argv[1]
subject=sys.argv[2]
#toparty='3|4|5|6'
message=sys.argv[3]
params={
        "touser": touser,
#       "toparty": toparty,
        "msgtype": "text",
        "agentid": agentid,
        "text": {
                "content": message
        },
        "safe":0
}
req=requests.post(msgsend_url, data=json.dumps(params))
logging.info('sendto:' + touser + ';;subject:' + subject + ';;message:' + message)
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值