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

简介:

由于公司业务比较多,部署的站点也比较多。为了网站安全运行,以防故障发生时能第一时间知晓,特意编写下面监控脚本,对网站访问状态和超时时间进行监控:当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,如需转载请自行联系原作者
相关文章
|
4月前
|
Linux API 开发者
调用GrayLog的RestAPI接口实现定时关闭和定时开启告警
调用GrayLog的RestAPI接口实现定时关闭和定时开启告警
57 0
|
5月前
|
存储 缓存 前端开发
如何实现设备组缓存的正确清除?——基于心跳请求和心跳响应的解决方案
如何实现设备组缓存的正确清除?——基于心跳请求和心跳响应的解决方案
35 0
|
10月前
|
网络协议 Shell Perl
根据web访问日志,封禁请求量异常的IP,如IP在半小时后恢复正常,则解除封禁
根据web访问日志,封禁请求量异常的IP,如IP在半小时后恢复正常,则解除封禁
69 1
|
11月前
|
存储 SQL 数据库
超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
219 0
|
12月前
|
数据采集 监控 Linux
一日一技:不用轮询,基于事件监控文件变动
一日一技:不用轮询,基于事件监控文件变动
88 0
|
运维 监控
|
Java Android开发
Andoroid 11 开机广播处理超时导致第三方应用启动延迟
Andoroid 11 开机广播处理超时导致第三方应用启动延迟
412 0
Andoroid 11 开机广播处理超时导致第三方应用启动延迟
如何设置agent上报频率监控间隔时间 - WGCLOUD
在agent/config/application.properties中设置即可
如何设置agent上报频率监控间隔时间 - WGCLOUD
|
存储 JSON 数据格式
报警系统QuickAlarm之报警规则的设定与加载
既然命名为规则,那么就需要有对应的解析器,以根据报警规则和报警类型等相关输入条件,来选择对应的报警执行器,因此本文主要包括的内容就比较清晰了 1.报警规则的定义 2.报警规则的加载 3.报警规则的解析以及报警执行器选择
218 0
报警系统QuickAlarm之报警规则的设定与加载
|
监控 数据库
zabbix告警持续频繁发送历史消息解决方法
zabbix持续频繁的发送历史的告警消息,一晚上告警内容高达1.7w条,严重影响查看真正出现问题的告警信息。 产生此问题的原因: 告警消息积压堆积,一直处于重试状态。 触发器的确存在,但是目标监控主机的状态是正常的,只要触发器触发的问题存在,那么就会一直发生告警。
983 0