本文是在linux系統(tǒng)用shell方式實現(xiàn)微信告警,windows系統(tǒng)用bat方式實現(xiàn)微信告警請點擊此處查看
1.首先我們要去注冊企業(yè)微信了,然后創(chuàng)建一個應(yīng)用,記得以下圖片中的標紅信息,企業(yè)ID,應(yīng)用ID,應(yīng)用Secret

image.png

image.png
2.新建一個/home/weixin.sh文件,此處目錄和文件名稱是為測試方便,自己可以根據(jù)實際場景自定義,內(nèi)容如下
#!/bin/bash
# -*- coding: utf-8 -*-
###SCRIPT_NAME:weixin.sh###
###send message from weixin for monitoring###
###leo###
content=${@:1}
content=${content//\<font color=\'red\'\>/}
content=${content//\<\/font\>\<\/br\>/}
content=${content//\ /}
echo "告警信息 : $content"
#echo "warn content is : $content" >> ./warn.log
CropID='微信企業(yè)ID'
Secret='微信企業(yè)應(yīng)用Secret'
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 int AppID=XXXXX # 企業(yè)號中的應(yīng)用id
local UserID='XXXXXXX' # 部門成員id,微信接收者,多個接收者用豎杠|隔開
#local PartyID=$2 # 部門id,定義了范圍,組內(nèi)成員都可接收到消息
local Msg="WGCLOUD告警:$1"
Msg=${Msg//\"/}
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'
}
#body $content
curl --data-ascii "$(body $content)" $PURL
printf '\n'
echo "over!"
部門成員ID查看方式如下圖,鼠標放在成員名字上即可看到賬號,即是成員ID

image.png
3.cli 測試
/home/weixin.sh 測試
4.測試完成,修改/server/config/application.yml
#告警腳本文件,可以為空,參考:/server/template/sendMsg.sh
warnScript: /home/weixin.sh
5.好了,重啟server,這樣就可以在微信收到告警消息了

image.png
6.注意windows編輯shell腳本后,上傳到linux下執(zhí)行可能出現(xiàn)\r錯誤

image.png

image.png
因為Windows系統(tǒng)中的換行符是\n\r,Linux系統(tǒng)中的換行符是\n,因此需要將\r替換為空白。
運行如下命令即可
sed -i 's/\r$//' /home/weixin.sh