2026.06.05 | youres | 19次围观
curl重定向耗时过长钉钉告警配置:3个实战脚本让网站跳转问题秒级感知
网站重定向慢,用户流失快。用curl精准检测重定向耗时,配合钉钉机器人,跳转异常秒级告警到位。
为什么要监控重定向耗时?
用户访问页面,如果发生重定向,每一次跳转都会消耗时间。重定向耗时过长,直接影响用户体验和SEO排名。
用curl的-w time_redirect变量,可以精准获取重定向阶段消耗的时间,再配合钉钉机器人做告警,网站跳转异常一眼便知。
一、curl检测重定向耗时命令
基础命令
curl -o /dev/null -s -w "time_redirect: %{time_redirect}s\n" https://www.youres.cn
参数说明:
-o /dev/null:丢弃响应体-s:静默模式-w "time_redirect: %{time_redirect}s\n":输出重定向耗时
一次获取多个时间指标
curl -o /dev/null -s -w "DNS: %{time_namelookup}s\nConnect: %{time_connect}s\nRedirect: %{time_redirect}s\nTotal: %{time_total}s\n" https://www.youres.cn
输出示例:
DNS: 0.012s
Connect: 0.045s
Redirect: 0.123s
Total: 0.278s
time_redirect为0.123秒,说明重定向消耗了123毫秒。
二、Shell脚本:重定向耗时超限告警
脚本逻辑
- 用curl获取
time_redirect - 判断耗时是否超过阈值(如0.2秒)
- 超出发送钉钉告警
完整脚本
#!/bin/bash
# ========== 配置区 ==========
URL="https://www.youres.cn"
THRESHOLD=0.2 # 重定向耗时阈值(秒)
DINGTALK_WEBHOOK="https://oapi.dingtalk.com/robot/send?access_token=你的token"
# ========== 检测逻辑 ==========
redirect_time=$(curl -o /dev/null -s -w "%{time_redirect}" "$URL")
# 浮点数比较(用bc或awk)
is_timeout=$(echo "$redirect_time > $THRESHOLD" | bc)
if [ "$is_timeout" -eq 1 ]; then
# 拼接告警消息
message="## 🚨 网站重定向耗时告警\n\n- **URL**: $URL\n- **重定向耗时**: ${redirect_time}s\n- **阈值**: ${THRESHOLD}s\n- **时间**: $(date '+%Y-%m-%d %H:%M:%S')"
# 发送钉钉告警
curl -s -X POST "$DINGTALK_WEBHOOK" \
-H 'Content-Type: application/json' \
-d "{
\"msgtype\": \"markdown\",
\"markdown\": {
\"title\": \"网站重定向耗时告警\",
\"text\": \"$message\"
}
}"
echo "[$(date)] 告警已发送:重定向耗时 ${redirect_time}s"
else
echo "[$(date)] 正常:重定向耗时 ${redirect_time}s"
fi
关键点说明
- 浮点数比较:bash不支持浮点数比较,用
bc或awk - 钉钉Markdown消息:支持标题、加粗、列表格式,告警更直观
- 阈值设置建议:
- 内网服务:0.05s
- 普通网站:0.2s
- 跨境服务:0.5s
三、进阶:批量检测+多通道告警
批量检测脚本
#!/bin/bash
# 批量检测重定向耗时
URLS=(
"https://www.youres.cn"
"https://blog.youres.cn"
"https://demo.youres.cn"
)
THRESHOLD=0.2
DINGTALK_WEBHOOK="https://oapi.dingtalk.com/robot/send?access_token=你的token"
for url in "${URLS[@]}"; do
redirect_time=$(curl -o /dev/null -s -w "%{time_redirect}" "$url")
is_timeout=$(echo "$redirect_time > $THRESHOLD" | bc)
if [ "$is_timeout" -eq 1 ]; then
message="## 🚨 重定向耗时告警\n\n- **URL**: $url\n- **耗时**: ${redirect_time}s"
# 发送告警...
echo "[$(date)] $url 重定向耗时 ${redirect_time}s,告警已发送"
fi
done
多通道告警(钉钉+邮件)
# 发送钉钉告警
send_dingtalk_alert() {
curl -s -X POST "$DINGTALK_WEBHOOK" \
-H 'Content-Type: application/json' \
-d "{\"msgtype\":\"markdown\",\"markdown\":{\"title\":\"重定向告警\",\"text\":\"$1\"}}"
}
# 发送邮件告警
send_email_alert() {
echo "$1" | mail -s "网站重定向耗时告警" admin@example.com
}
# 双通道发送
[ "$is_timeout" -eq 1 ] && {
send_dingtalk_alert "$message"
send_email_alert "$message"
}
四、定时任务配置
crontab配置
# 每5分钟检测一次
*/5 * * * * /path/to/redirect_monitor.sh >> /var/log/redirect_monitor.log 2>&1
日志轮转配置
# /etc/logrotate.d/redirect_monitor
/var/log/redirect_monitor.log {
daily
rotate 7
compress
missingok
notifempty
}
五、钉钉机器人加签配置(安全增强)
为什么需要加签?
默认钉钉机器人任何人知道webhook地址都能发消息,加签后只有持有秘钥的人才能发送。
加签脚本示例
import time
import hmac
import hashlib
import base64
import requests
def dingtalk_sign(secret):
timestamp = str(round(time.time() * 1000))
secret_enc = secret.encode('utf-8')
string_to_sign = f"{timestamp}\n{secret}"
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = base64.b64encode(hmac_code).decode('utf-8')
return timestamp, sign
# 使用
timestamp, sign = dingtalk_sign("你的秘钥")
url = f"https://oapi.dingtalk.com/robot/send?access_token=xxx×tamp={timestamp}&sign={sign}"
六、常见问题排查
1. time_redirect始终为0?
原因:没有发生重定向。用curl -I -L URL检查是否有301/302响应。
2. 告警消息发送失败?
检查:
- webhook地址是否正确
- 服务器是否能访问oapi.dingtalk.com(防火墙/代理)
- 消息格式是否符合钉钉要求
3. 误报太多?
- 调大阈值
- 增加连续检测次数判断(连续3次超阈值才告警)
- 增加业务低峰期静默时间
总结
用curl的time_redirect变量检测重定向耗时,配合Shell脚本+钉钉机器人,简单几行代码就能搭建一个实用的重定向监控系统。
核心步骤:
curl -w "%{time_redirect}"获取重定向耗时- 浮点数比较判断是否超时
- 调用钉钉webhook发送告警
- crontab配置定时检测
重定向耗时监控只是网站监控的一个维度,完整方案还需要覆盖DNS解析时间、连接时间、首包时间等指标。下篇文章分享如何用curl一次性采集全链路耗时数据。
相关文章:
版权声明
本文仅代表个人观点。
本文系AI辅助作者原创,未经许可,转载请保留原文链接。

发表评论