钉钉报警脚本

2022-09-19 14:10:02 浏览数 (1)

# 1.调用格式

  • 案例1
代码语言:javascript复制
[root@summer logs]# curl 'https://oapi.dingtalk.com/robot/send?access_token=841826a8168ea56e39fc79e62ea4efde3d93ea312996d0e5d6afb03b032eb6d4' 
>   -H 'Content-Type: application/json' 
>   -d '{"msgtype": "text", 
>     "text": {
>        "content": "我就是测试, 是不一样的烟火"
>     }
>    }'
{"errcode":0,"errmsg":"ok"}[root@summer logs]# 

  • 结果
  • 案例2
代码语言:javascript复制
[root@summer logs]# curl 'https://oapi.dingtalk.com/robot/send?access_token=841826a8168ea56e39fc79e62ea4efde3d93ea312996d0e5d6afb03b032eb6d4' 
>    -H 'Content-Type: application/json' 
>    -d '{
>      "msgtype": "markdown",
>      "markdown": {
>          "title":"测试杭州天气",
>          "text": "#### 杭州天气 @156xxxx8827n> 9度,西北风1级,空气良89,相对温度73%nn> ![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)n> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) n"
>      },
>     "at": {
>         "atMobiles": [
>             "156xxxx8827", 
>             "189xxxx8325"
>         ], 
>         "isAtAll": false
>     }
>  }'
{"errcode":0,"errmsg":"ok"}[root@summer logs]# 

  • 结果

# 2.实践

模仿当前nginx进程挂机 则启动钉钉报警 执行效果:

代码语言:javascript复制
[root@summer ~]# vim dingding.sh 
[root@summer ~]# ./dingding.sh -x
{"errcode":0,"errmsg":"ok"}[root@summer ~]#

# 3.脚本如下

代码语言:javascript复制
#!/bin/bash
ip=192.168.0.1
webhook='https://oapi.dingtalk.com/robot/send?access_token=841826a8168ea56e39fc79e62ea4efde3d93ea312996d0e5d6afb03b032eb6d4'
#检测nginx(注意修改端口)
curtime=$(date " %Y-%m-%d %H:%M:%S")
checkNginx=`lsof -i:80 |grep 'nginx' |grep -v grep |grep -v agent|sort | tail -1 | cut -f 1 -d ' '`
SendMsgToDingding1() {
    curl $webhook -H 'Content-Type: application/json' -d "
    {
        'msgtype': 'text',
        'text': {
            'content': '测试时间:$curtimen Ip地址:$ipn 告警信息:nginx服务出现问题正在重启,请注意n'
        },
        'at': {
            'isAtAll': true
        }
    }"
}

SendMsgToDingding2() {
    curl $webhook -H 'Content-Type: application/json' -d "
    {
        'msgtype': 'text',
        'text': {
            'content': '测试时间:$curtimen Ip地址:$ipn 告警信息:nginx服务重启成功,请注意n'
        },
        'at': {
            'isAtAll': true
        }
    }"
}

SendMsgToDingding3() {
    curl $webhook -H 'Content-Type: application/json' -d "
    {
        'msgtype': 'text',
        'text': {
            'content': '测试时间:$curtimen Ip地址:$ipn 告警信息:nginx服务重启失败,请注意n'
        },
        'at': {
            'isAtAll': true
        }
    }"
}

SendMsgToDingding4() {
    curl $webhook -H 'Content-Type: application/json' -d "
    {
        'msgtype': 'text',
        'text': {
            'content': '测试时间:$curtimen Ip地址:$ipn 告警信息:nginx服务正常,请注意n'
        },
        'at': {
            'isAtAll': true
        }
    }"
}

if [[ $checkNginx != 'nginx' ]]; then
    SendMsgToDingding1
    /usr/sbin/nginx 
    if [ $? -eq 0 ]; then
        SendMsgToDingding2
    else
        SendMsgToDingding3
    fi
else
    SendMsgToDingding4
# else
#     SendMsgToDingding4
fi

# 4.加入定时任务

代码语言:javascript复制
[root@summer ~]# crontab -l
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=""
*/1 * * * * cd /root && ./dingding.sh >/dev/null 2>&1

# 5.验证

0 人点赞