自动监控url是否可用,如不可用则重启应用,并做相应的报警策略。

先上图\

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash 
#author: QingFeng
#qq: 530035210
#blog: http://my.oschina.net/pwd/blog 
#自动监控url是否可用,如不可用则重启应用,并做相应的报警策略。
#缺省的配置如下
 
logdir=/data/log/check          #日志路径
log=$logdir/log.log            #日志文件 
is_font=1                #终端是否打印日志: 1打印 0不打印 
is_log=0                 #是否记录日志: 1记录 0不记录
key="data-camp"  #进程关键字
exec_stop="/etc/init.d/data-camp  stop"  #停应用命令
exec_start="/etc/init.d/data-camp  start" #启动应用命令
 
datef(){
date "+%Y-%m-%d %H:%M:%S"
}
 
print_log(){
if [[ $is_log -eq 1  ]];then
[[ -d $logdir ]] || mkdir -p $logdir
echo "[ $(datef) ] $1" >> $log
fi
if [[ $is_font -eq 1  ]];then
echo -e "[ $(datef) ] $1"
fi
}
#定义重启
derestart(){
if [[  $1 == "" ]];then
print_log "$FUNCNAME():应用关键字不能为空"
exit
fi
if [[  $2 == "" ]];then
print_log "$FUNCNAME():启动文件不能为空"
exit
fi
if [[  $2 == "" ]];then
print_log "$FUNCNAME():启动参数口不能为空"
exit
fi
ppid=0
ppid=$(ps axu |grep "$1" |grep -v grep |grep -v "$0" |wc -l)
$2 $3 
ppid=$(ps axu |grep "$1" |grep -v grep |grep -v "$0" |wc -l)
echo $ppid > /tmp/restart.num
print_log "$FUNCNAME(): $1的进程数为:$ppid"
}
#场景一: 当网站返回码不为200,则重启应用.
check_code(){
if [[  $1 == "" ]];then
print_log "$FUNCNAME():服务器地址不能为空"
exit
fi
if [[  $2 == "" ]];then
print_log "$FUNCNAME():服务器端口不能为空"
exit
fi
print_log "$FUNCNAME():开始检测-[$1:$2]服务器的网站状态返回码."
code=$(curl -m 8 -o /dev/null -s -w  %{http_code} http://$1:$2/verdict/session/LSGJA52U7CH055974/latest/result)
if [[  $code -ne 200    ]];then
print_log "$FUNCNAME():[$1:$2]服务器的网站状态返回码不正常,开始重启应用--$code."
print_log "$FUNCNAME():执行命令: $exec_stop"
derestart  "$key"  "$exec_stop"
num2=$(cat /tmp/restart.num)
if [[ $num2 -ne 0   ]];then
print_log "$FUNCNAME():停应用失败."
fi 
print_log "$FUNCNAME():执行命令: $exec_start"
sleep 3
derestart  "$key"  "$exec_start"
num2=$(cat /tmp/restart.num)
if [[ $num2 -eq 0   ]];then
print_log "$FUNCNAME():启动应用失败."
fi
print_log "$FUNCNAME():重启应用成功."
else
print_log "$FUNCNAME():[$1:$2]服务器的网站状态返回码正常--$code."
fi 
}
#场景二: 检测网站http返回的时间
check_timeout(){
if [[  $1 == "" ]];then
print_log "$FUNCNAME():服务器地址不能为空"
exit
fi
if [[  $2 == "" ]];then
print_log "$FUNCNAME():服务器端口不能为空"
exit
fi
print_log "$FUNCNAME():开始检测-[$1:$2]服务器的网站超时时间."
httptime=`curl  -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer:%{time_starttransfer}\ntime_total: %{time_total}\n" "http://$1:$2/verdict/session/LSGJA52U7CH055974/latest/result" |grep time_total|awk -F ":" '{print $2*1000}'`
taketime=$(expr $httptime / 1000)
if [[  $httptime -gt 60000    ]];then
print_log "$FUNCNAME():[$1:$2]服务器的网站响应时间不正常,开始重启应用--$httptime ms."
print_log "$FUNCNAME():执行命令: $exec_stop"
derestart  "$key"  "$exec_stop"
num2=$(cat /tmp/restart.num)
if [[ $num2 -ne 0   ]];then
print_log "$FUNCNAME():停应用失败."
fi
print_log "$FUNCNAME():执行命令: $exec_start"
sleep 3
derestart  "$key"  "$exec_start"
num2=$(cat /tmp/restart.num)
if [[ $num2 -eq 0   ]];then
print_log "$FUNCNAME():启动应用失败."
fi
print_log "$FUNCNAME():重启应用成功."
else
print_log "$FUNCNAME():[$1:$2]服务器的网站响应时间正常--$httptime ms/$taketime s."
fi
}
check_code "localhost" "6500"
check_timeout "localhost" "6500"
坚持原创技术分享,您的支持将鼓励我继续创作!