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

wgcloud下载

本文是在linux系统用shell方式实现微信告警,windows系统用bat批处理实现微信告警请点击此处

1.首先我们要去注册企业微信了,然后创建一个应用,记得以下图片中的标红信息,企业ID,应用ID,应用Secret

2.新建一个/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\>/}
content=${content//\ /}
time3=$(date "+%H:%M:%S")
content="$time3,$content"
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="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

3.cli 测试

记得先给weixin.sh添加可执行权限

chmod +x weixin.sh

然后测试下微信能否收到消息 

/home/weixin.sh 测试

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

#告警脚本文件的完整路径,注意是完整的路径,可以为空,参考:/server/template/sendMsg.sh
  warnScript: /home/weixin.sh

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

6.注意windows编辑shell脚本后,上传到linux下执行可能出现\r错误

因为Windows系统中的换行符是**\n\r**,Linux系统中的换行符是**\n**,因此需要将**\r**替换为空白。

运行如下命令即可

sed -i 's/\r$//' /home/weixin.sh

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值