Zabbix告警脚本-微信

时间:2022-02-14 08:48:48

1、weixin.sh

[root@iot-svndata02 bin]# cat weixin.sh
#!/bin/bash
###SCRIPT_NAME:weixin.sh###
###send message from weixin for zabbix monitor###
###zongx###
###V1-2017-06-12###

#CropID='wx01975981c5d1502c'
#Secret='k6hjyg4UY1Sx4YZIA6teqq5Q_cjjuDKliN3fVCUksN0'
#GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret" 
#Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F \" '{print $10}')

Gtoken=(`cat  /etc/zabbix/bin/token_new.txt`)
echo "Gtoken="$Gtoken

PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"

function body() {
        local int AppID=1000002                  #企业号中的应用id
#        local UserID=@all                         #部门成员id,zabbix中定义的微信接收者
        local UserID=$1
        local PartyID=@all                        #部门id,定义了范围,组内成员都可接收到消息
        local Msg=$(echo "$@" | cut -d" " -f3-)  #过滤出zabbix中传递的第三个参数
        printf '{\n'
        printf '\t"touser": "'"$UserID"\"",\n"
#        printf '\t"toparty": "'"$PartyID"\"",\n"
        printf '\t"msgtype": "text",\n'
        printf '\t"agentid": "'"$AppID"\"",\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

time=`date +"%Y-%m-%d"`
echo "`date`    $1      $2     $3">>/var/log/zabbix/zbx_weixin-$time.log
[root@iot-svndata02 bin]#

2、token_new.txt

[root@iot-svndata02 bin]# cat token_new.txt
v2MHGuET7Z1usPvKSE0W5xyIfNrN8NzsPYOKWOdAYXBWpYjdOcp_0T_Ub3ig1jE_bjJq15YzQLvhwnorQiu8iUGwfbkr-0HOQ8AzkFiRwK_GLE7xQURy8JUlopP0ANES4nW-BD2XyVUhSDr1lmO-BiPNJFxgHvlQDPJfrObSyvc4e8kFFdMl5463GDPZv-ZbA_QGy4zAsfs6AGW1nhb7YQ
[root@iot-svndata02 bin]#