由于公司业务比较多,部署的站点也比较多。为了网站安全运行,以防故障发生时能第一时间知晓,特意编写下面监控脚本,对网站访问状态和超时时间进行监控:当code状态为5xx或者访问超时时间大于10s时进行报警。脚本脚本如下:
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
|
[root@qd-inf-logcollector01 web_monit]$
pwd
/app/script/web_monit
[root@qd-inf-logcollector01 web_monit]$ ll
total 12
-rwxr-xr-x 1 root root 870 Oct 12 21:34 http_monit.sh
//
监控脚本
-rwxr-xr-x 1 root root 857 Oct 12 21:25 sms.py
//
短信报警脚本,里面有报警联系人
-rw-r--r-- 1 root root 377 Oct 12 21:27 weblist.txt
//
监控的网站域名列表
[root@qd-inf-logcollector01 web_monit]$
cat
http_monit.sh
#!/bin/sh
weblist=
/app/script/web_monit/weblist
.txt
for
list
in
`
cat
$weblist|
grep
-E -
v
"#|^$"
`
do
httpcode=`curl -o
/dev/null
-s -w %{http_code}
"$list"
`
httptime=`curl -o
/dev/null
-s -w
"time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n"
"$list"
|
grep
time_total|
awk
-F
":"
'{print $2*1000}'
`
if
[ $httpcode = 500 ]||[ $httpcode = 502 ]||[ $httpcode = 503 ]||[ $httpcode = 504 ]
then
python
/app/script/web_monit/sms
.py $list
"$list 访问有误!状态码为$httpcode!请收到报警后尽快查看并处理!"
else
echo
"$list is checked ok!"
fi
if
[ $httptime -
ge
10000 ]
then
python
/app/script/web_monit/sms
.py $list
" $list访问超时!超时时间为$httptime毫秒!请收到报警后尽快查看并处理!"
else
echo
"$list is connect ok!"
fi
done
|
手动检查网站访问的code状态码
1
2
|
[root@qd-inf-logcollector01 web_monit]$ curl -o
/dev/null
-s -w %{http_code} http:
//www
.wang.com
200
|
手动检查网站访问的超时时间(单位:毫秒,如下网址访问的时间为0.8秒)
1
2
|
[root@qd-inf-logcollector01 web_monit]$ curl -o
/dev/null
-s -w
"time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_total: %{time_total}\n"
http:
//www
.wang.com |
grep
time_total|
awk
-F
":"
'{print $2*1000}'
800
|
网站列表和脚本执行
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
|
[root@qd-inf-logcollector01 web_monit]$
cat
weblist.txt
http:
//nop
.kevin.cn
http:
//ap
.kevin.cn
http:
//ope
.kevin.cn
http:
//opr
.kevin.cn
http:
//www
.kevin.cn
http:
//kevin
.cn
http:
//tb
.kevin.cn
http:
//www
.wang.com
https:
//www
.wang.com
http:
//doc
.kevin.cn
http:
//docs
.kevin.cn
http:
//git
.wang.com
http:
//monitor
.kevin.cn
http:
//dash
.kevin.cn
[root@qd-inf-logcollector01 web_monit]$sh http_monit.sh
http:
//nop
.kevin.cn is checked ok!
http:
//nop
.kevin.cn is connect ok!
http:
//ap
.kevin.cn is checked ok!
http:
//ap
.kevin.cn is connect ok!
http:
//ope
.kevin.cn is checked ok!
http:
//ope
.kevin.cn is connect ok!
http:
//opr
.kevin.cn is checked ok!
http:
//opr
.kevin.cn is connect ok!
http:
//www
.kevin.cn is checked ok!
http:
//www
.kevin.cn is connect ok!
http:
//kevin
.cn is checked ok!
http:
//kevin
.cn is connect ok!
http:
//tb
.kevin.cn is checked ok!
http:
//tb
.kevin.cn is connect ok!
http:
//www
.wang.com is checked ok!
http:
//www
.wang.com is connect ok!
https:
//www
.wang.com is checked ok!
https:
//www
.wang.com is connect ok!
http:
//doc
.kevin.cn is checked ok!
http:
//doc
.kevin.cn is connect ok!
http:
//docs
.kevin.cn is checked ok!
http:
//docs
.kevin.cn is connect ok!
http:
//git
.wang.com is checked ok!
http:
//git
.wang.com is connect ok!
http:
//monitor
.kevin.cn is checked ok!
http:
//monitor
.kevin.cn is connect ok!
http:
//dash
.kevin.cn is checked ok!
http:
//dash
.kevin.cn is connect ok!
|
定时监控任务(每两分钟监控一次)
1
2
|
[root@qd-inf-logcollector01 web_monit]$
crontab
-l
*
/2
* * * *
/bin/bash
-x
/app/script/web_monit/http_monit
.sh >
/dev/null
2>&1
|
简单注意下:比较运算符只有==和!=是用于字符串比较的,不可用于整数比较;整数比较只能使用-eq, -gt这种形式