zabbix设置企业微信告警

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.、创建告警媒介
zabbix设置企业微信告警_第1张图片
参数
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
zabbix设置企业微信告警_第2张图片
zabbix设置企业微信告警_第3张图片
zabbix设置企业微信告警_第4张图片
zabbix设置企业微信告警_第5张图片
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)

你可能感兴趣的:(zabbix)