server主机不能访问外网,如何发送微信、钉钉、邮件等告警

如果我们server主机不能访问外网,但是网络环境中有能访问外网的主机(暂称为B主机),那么我们就把告警内容发到B主机(假设B为Linux平台),由B主机访问微信、钉钉接口发送告警消息
1、我们在B主机部署wgcloud-msg-release(告警消息发送辅助工具),Linux平台:wgcloud-msg-v1.1.tar.gz,windows平台:wgcloud-msg-v1.1.zip
我们下载wgcloud-msg-v1.1.tar.gz后,解压,并启动运行,默认端口是10000,也可在config/application.properties中修改
Linux启动用start.sh,停止用stop.sh,如果是windows直接运行wgcloud-msg-release.exe
2、如下,我们以企业微信应用告警为例说明
我们在告警脚本(weixin.sh)中填写企业微信的相关信息,记得给weixin.sh赋加可执行权限
#!/bin/bash
# -*- coding: utf-8 -*-
###SCRIPT_NAME:weixin.sh###
###send message from weixin for monitoring###
###leo###
content=${@:1}
content=${content//\ /}
time3=$(date "+%H:%M:%S")
content="$time3,$content"
#content=${content//\,/\\n}  #告警信息若需要换行显示,去掉此行注释
echo "告警信息 : $content"
  
  
CropID='微信企业ID'
Secret='微信企业应用Secret'
AppID=xxxxxxx       # 企业号中的应用id
UserID='xxxxxxxx'            # 部门成员id,微信接收者,多个接收者用逗号,隔开
content=${content//\"/}
ding_url="http://localhost:10000/wx"
curl -i -X POST -H "'Content-type':'application/json'" -d '{"content":"'$content'","cropID":"'$CropID'","secret":"'$Secret'","appID":'$AppID',"userID":"'$UserID'"}' $ding_url

ding_url是wgcloud-msg-release的访问地址,需要改为自己实际部署wgcloud-msg-release的IP和端口
3、修改server/config/application.yml如下配置项warnScript,注意是配置完全路径,然后重启server

#告警脚本绝对路径(若配置脚本,无论是否配置过邮件,都会执行该脚本),可以为空,参考模板:server/template/sendMsg.sh
  warnScript: /wgcloud/weixin.sh
以上完成
wgcloud-msg本身没有日志输出,如果需要调试查看输出信息,请运行./wgcloud-msg-release或wgcloud-msg-release.exe
4、如果是企业微信群机器人,只需要把weixin.sh最后2行改为
access_token即为机器人webHook地址的最后的key值

ding_url="http://localhost:10000/wxjqr"
curl -i -X POST -H "'Content-type':'application/json'" -d '{"content":"'$content'","access_token":"'$access_token'"}' $ding_url

5、如果是钉钉告警方式,只需要把weixin.sh最后2行改为
access_token即为机器人webHook地址的最后的access_token值

ding_url="http://localhost:10000/dingding"
curl -i -X POST -H "'Content-type':'application/json'" -d '{"content":"'$content'","access_token":"'$access_token'"}' $ding_url

6、如果是邮件告警方式,只需要把weixin.sh最后2行改为

ding_url="http://localhost:10000/mail"
curl -i -X POST -H "'Content-type':'application/json'" -d '{"smtpHost":"smtp.qq.com","content":"'$content'","smtpPort":"465","smtpUsername":"xxxxxxx@qq.com","smtpPassword":"xxxxxxxxx","smtpToUser":"xxxxxxx@qq.com"}' $ding_url