监控系统WGCLOUD,shell脚本实现微信告警

    科技2025-04-26  6

    wgcloud下载

    1.新建一个/home/weixin.sh文件,此处目录和文件名称是为测试方便,自己可以根据实际场景自定义,内容如下

    #!/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\>/} echo "告警信息 : $content" #echo "warn content is : $content" >> ./warn.log CropID='微信企业ID' Secret='微信企业应用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 # 企业号中的应用id local UserID='XXXXXXX' # 部门成员id,微信接收者 #local PartyID=$2 # 部门id,定义了范围,组内成员都可接收到消息 local Msg="告警信息:$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!"

    2.cli 测试

    sh /home/weixin.sh 测试

    3.测试完成,修改/server/config/application.yml

    #告警脚本文件,可以为空,参考:/server/template/sendMsg.sh warnScript: /home/weixin.sh

    4.好了,重启server,这样就可以在微信收到告警消息了

     

     

    Processed: 0.008, SQL: 8