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

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

最近公司在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 去编辑定时逻辑

结束

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

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

通过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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值