网站访问状态和超时时间监控报警设置

由于公司业务比较多,部署的站点也比较多。为了网站安全运行,以防故障发生时能第一时间知晓,特意编写下面监控脚本,对网站访问状态和超时时间进行监控:当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这种形式

***************当你发现自己的才华撑不起野心时,就请安静下来学习吧***************

本文转自散尽浮华博客园博客,原文链接:http://www.cnblogs.com/kevingrace/p/7658394.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值