钉钉报警脚本
# 1.调用格式
- 案例1
[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]#
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
- 结果
- 案例2
[root@summer logs]# curl 'https://oapi.dingtalk.com/robot/send?access_token=841826a8168ea56e39fc79e62ea4efde3d93ea312996d0e5d6afb03b032eb6d4' \
> -H 'Content-Type: application/json' \
> -d '{
> "msgtype": "markdown",
> "markdown": {
> "title":"测试杭州天气",
> "text": "#### 杭州天气 @156xxxx8827\n> 9度,西北风1级,空气良89,相对温度73%\n\n> ![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]#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- 结果
# 2.实践
模仿当前nginx进程挂机 则启动钉钉报警 执行效果:
[root@summer ~]# vim dingding.sh
[root@summer ~]# ./dingding.sh -x
{"errcode":0,"errmsg":"ok"}[root@summer ~]#
1
2
3
2
3
# 3.脚本如下
#!/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': '测试时间:$curtime\n Ip地址:$ip\n 告警信息:nginx服务出现问题正在重启,请注意\n'
},
'at': {
'isAtAll': true
}
}"
}
SendMsgToDingding2() {
curl $webhook -H 'Content-Type: application/json' -d "
{
'msgtype': 'text',
'text': {
'content': '测试时间:$curtime\n Ip地址:$ip\n 告警信息:nginx服务重启成功,请注意\n'
},
'at': {
'isAtAll': true
}
}"
}
SendMsgToDingding3() {
curl $webhook -H 'Content-Type: application/json' -d "
{
'msgtype': 'text',
'text': {
'content': '测试时间:$curtime\n Ip地址:$ip\n 告警信息:nginx服务重启失败,请注意\n'
},
'at': {
'isAtAll': true
}
}"
}
SendMsgToDingding4() {
curl $webhook -H 'Content-Type: application/json' -d "
{
'msgtype': 'text',
'text': {
'content': '测试时间:$curtime\n Ip地址:$ip\n 告警信息: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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 4.加入定时任务
[root@summer ~]# crontab -l
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=""
*/1 * * * * cd /root && ./dingding.sh >/dev/null 2>&1
1
2
3
4
5
2
3
4
5