网站制作学习网Linux→正文:网站监控shell脚本
字体:

网站监控shell脚本

Linux 2023/3/20 21:19:40  点击:不统计


 网站监控shell脚本
保存下面代码 为.sh 运行,可以检测网站状态


#!/bin/bash
# 定义需要监控的网站URL
url="https://www.forasp.cn"
# 定义超时时间,单位 秒
connect_out_time=3
# 定义检查超时次数,当超过后,则
check_alive_times=3
check_err_num=0
# 定义发邮件后的间隔时间后多久 再次检测,发邮件后会sleep,单位 秒
check_email_split_time=300
# 定义 正常情况下 间隔时间检测 单位 秒
check_split_time=10
# 定义非200 情况下 间隔时间检测
check_errcode_split_time=10
# 发送HTTP请求并获取状态码
while true ;do
response=$(curl --connect-timeout $connect_out_time -s -o /dev/null -w "%{http_code}" $url)
# 检查状态码,如果为200表示网站正常,否则发送邮件通知管理员
if [ $response -eq 200 ]; then
check_err_num=0
echo "站点正常, 休息 $check_alive_times 秒后重新检测"
sleep $check_alive_times
else
now_time=`date +%Y-%m-%d-%H:%M:%S`
check_err_num=$(($check_err_num + 1))
if [ $check_err_num -gt $check_alive_times ]; then
email_subject="站点 $check_alive_times 次非200"
email_body="站点 $url - $check_alive_times多次非. HTTP status 最后返回 $response. $now_time"
# 发送邮件通知管理员
echo "send emain or call other shell"
# echo "$email_body" | mail -s "$email_subject" admin@example.com
echo "出现多次失败,已经发送邮件,将于 $check_email_split_time 秒后进行重新检测"
sleep $check_email_split_time
else
echo "出现错误,间隔 $check_errcode_split_time 秒后重新检测"
sleep $check_errcode_split_time
fi
fi

done

原文章%77w%77%2Ef%6F%72%61%73%70%2E%63n

·上一篇:nginx http跳https >>    ·下一篇:nginx跨域配置 >>
推荐文章
最新文章