-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheck_http_curl.sh
More file actions
35 lines (33 loc) · 967 Bytes
/
check_http_curl.sh
File metadata and controls
35 lines (33 loc) · 967 Bytes
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
#!/bin/bash
#功能描述(Description):使用curl访问具体的HTTP页面,检测HTTP状态码
#连续测试3次都失败则发送邮件报警.
#curl命令选项说明:
#-m设置超时时间
#-s设置静默连接
#-o下载数据另存为
#-w返回附加信息,HTTP状态码
url=http://192.168.4.5/index.html
date=$(date +"%Y-%m-%d %H:%M:%S")
mail_to="root@localhost"
mail_subject="http_warning"
fail_times=0
for i in 1 2 3
do
status_code=$(curl -m 3 -s -o /dev/null -w %{http_code} $url)
#使用<<-重定向可以忽略tab键缩进的内容,代码可读性更好.
if [ $status_code -ne 200 ];then
let fail_times++
fi
sleep 1
done
if [ $fail_times -eq 3 ];then
mail -s $mail_subject $mail_to <<- EOF
检测时间为:$date
$url页面异常,服务器返回状态码:${status_code}.
请尽快排查异常.
EOF
else
cat >> /var/log/http_check.log <<- EOF
$date "$url 页面访问正常."
EOF
fi