Window系统bat脚本或Linux系统shell脚本处理-钉钉-邮件等报警服务

it2026-02-24  6

有时候线上服务器或者什么乱七八糟的服务器突然停止、中断了。就需要有个监控来做告警。

最近公司在window系统上了做了个.net服务用来做图片转换,老是崩溃。所以业务使用中经常发现不明所以的问题。

公司用钉钉,就利用bat脚本和钉钉的机器人通过http请求来做一个报警监控。

流程如下:

1. 访问对应请求 2. 分析响应信息 3. 如果发现包含特殊字符 4. 发送钉钉消息 5. 否则不处理 6. 定时60s执行一次。

windows系统 Bat脚本如下

color 2f title http://wordconvert.xxx.com echo http://wordconvert.xxx.com rem doc-https://www.cnblogs.com/aspirant/p/7233893.html 这个是相关参考资料 :aaa set str="0" curl -s http://www.baidu.com>check.txt set /p str=<check.txt rem check ^去除特殊符号 echo %str% ^| findstr "2001" >nul && ( echo yes > check.txt ) || ( rem execute dingTalk -H 用双引号 字符乱码需要额外处理 curl -H "Content-Type: application/json" -d "{'msgtype':'text','text': {'content':'Wow ~~~, 服务器失了个败!'}}" -s "https://oapi.dingtalk.com/robot/send?access_token=xxxx" ) timeout /T 60 /NOBREAK goto aaa

整个编写还是比较简单的。上文中的access_token值,请通过设置钉钉机器人来获取。以下是接受到了消息:

钉钉上出现了消息: :) 小机器人 Wow ~~~, 服务器失了个败?

以上文件的后缀名是 .bat适用于在windows系统上执行。

linux系统 shell脚本

复制到linux服务器上以后,首先需要修改文件后缀为.sh

然后修改操作权限如下,

[root@ottffss home]# chmod u+x monitor.sh [root@ottffss home]# ls -l total 58140 -rw-r--r-- 1 root root 12919001 Apr 30 04:10 latest-zh_CN.tar.gz -rwxr--r-- 1 root root 704 Oct 22 12:49 monitor.sh -rw------- 1 root root 22865 Oct 22 03:09 nohup.out -rw-r--r-- 1 root root 192 Jun 15 15:28 readme.txt

拥有执行权限后就可以执行了。直接执行curl命令也是正常发送消息的,但是会报很多错,就是bat文件与sh文件的语法区别了。

错误信息如下:

[root@ottffss home]# ./monitor.sh ./monitor.sh: line 1: color: command not found ./monitor.sh: line 2: title: command not found http://wordconvert.test.mistong.com... ./monitor.sh: line 5: rem: command not found ./monitor.sh: line 7: :aaa: command not found ./monitor.sh: line 12: rem: command not found ./monitor.sh: line 14: findstr: command not found ./monitor.sh: line 16: rem: command not found {"errcode":0,"errmsg":"ok"}timeout: invalid time interval ‘/T’ Try 'timeout --help' for more information. ./monitor.sh: line 23: goto: command not found

对脚本进行调整如下:

curl -s http://wx.xxx.net:8080>check.txt if [[ !$(cat check.txt) =~ "404" ]] then echo ok else # execute dingTalk -H 用双引号 字符乱码需要额外处理 curl -H "Content-Type: application/json" -d "{'msgtype':'text','text': {'content':'Wow ~~~, 服务器失了个败!'}}" -s "https://oapi.dingtalk.com/robot/send?access_token=xxxx" fi

以上代码就可以正常执行了。linux中定时执行任务有几种方式

通过 for循环 + sleep进行处理通过 cronTab -e 去编辑定时逻辑

结束

以上的脚本应该可以使用于挺多情况了。

钉钉报警企业微信其他中间应用(api需自行查询)发邮件等

通过blat发送邮件 部分脚本如下,需要安装blat工具并初始化

初始化需要使用 blat -install smtp.163.com user@163.com 3 25

bat脚本需要在 blat目录下执行才能成功

set str="0" set from=163@163.com set user=163 set pass=IGKRUWHAIPFLRWRN set to=to@361.com,to@362.com set subj=【工具出错】Server wordConvert has failed !!! set mail=check.txt set attach=*.jpg set server=smtp.163.com set debug=-debug -log blat.log -timestamp curl -s http://wordconvert.xxx.com/api/file/read>check.txt set /p str=<check.txt rem enable express echo %str% | findstr "200" >nul && ( echo yes > check.txt ) || ( blat %mail% -to %to% -s "%subj%" -u %from% -pw %pass% -charset Gb2312 >log.txt

来自 程序猿的漫漫长路

最新回复(0)