🚀 快速部署
# 1. 创建监控脚本
cat > /opt/website-monitor.sh << 'EOF'
#!/bin/bash
WEBSITE="https://mjdbjgs.com"
THRESHOLD=5 # 响应时间阈值(秒)
response=$(curl -o /dev/null -s -w "%{http_code} %{time_total}" $WEBSITE)
status_code=$(echo $response | cut -d' ' -f1)
response_time=$(echo $response | cut -d' ' -f2)
if [ "$status_code" != "200" ]; then
echo "⚠️ 网站宕机!状态码:$status_code"
# 发送告警
fi
if (( $(echo "$response_time > $THRESHOLD" | bc -l) )); then
echo "⚠️ 响应时间过长:${response_time}s"
fi
EOF
chmod +x /opt/website-monitor.sh
# 2. 添加定时任务(每 5 分钟检查一次)
crontab -e
*/5 * * * * /opt/website-monitor.sh >> /var/log/website-monitor.log 2>&1
📧 告警配置
# 邮件告警配置
ALERT_EMAIL="admin@example.com"
send_alert() {
echo "网站 $WEBSITE 出现异常:$1" | mail -s "🚨 网站告警" $ALERT_EMAIL
}
# 钉钉机器人告警
DINGTALK_WEBHOOK="https://oapi.dingtalk.com/robot/send?access_token=xxx"
send_dingtalk() {
curl $DINGTALK_WEBHOOK \
-H 'Content-Type: application/json' \
-d '{"msgtype":"text","text":{"content":"🚨 网站告警:'$1'"}}'
}