Linux用shell腳本實現(xiàn)微信告警信息推送——WGCLOUD監(jiān)控系統(tǒng)

wgcloud下載

本文是在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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容