2026.06.05 | youres | 23次围观
curl重定向检测为什么要接入Prometheus
单靠curl命令手动检测重定向,只能看到当前状态,无法记录历史趋势,更无法在问题刚出现时就触发告警。把curl重定向检测接入Prometheus监控体系,才能真正做到:
- 持续采集:定时自动检测,数据自动入库
- 趋势分析:重定向次数、跳转耗时、终点URL变化全部可查询
- 告警一体化:异常自动触发,钉钉/邮件/企业微信同步推送
方案一:node_exporter textfile收集curl重定向指标
利用node_exporter的textfile收集器,把curl检测结果暴露为Prometheus指标,是最轻量的接入方案。
1. 编写curl检测脚本
#!/bin/bash
# curl_redirect_metrics.sh
URL="https://www.youres.cn"
OUTPUT_FILE="/var/lib/node_exporter/curl_redirect.prom"
# 使用curl -w格式化输出,获取重定向次数和最终URL
RESULT=$(curl -s -o /dev/null -w "num_redirects=%{num_redirects}\nurl_effective=%{url_effective}\ntime_redirect=%{time_redirect}\nhttp_code=%{http_code}" -L "$URL")
NUM_REDIRECTS=$(echo "$RESULT" | grep "num_redirects" | cut -d= -f2)
URL_EFFECTIVE=$(echo "$RESULT" | grep "url_effective" | cut -d= -f2)
TIME_REDIRECT=$(echo "$RESULT" | grep "time_redirect" | cut -d= -f2)
HTTP_CODE=$(echo "$RESULT" | grep "http_code" | cut -d= -f2)
# 输出Prometheus格式指标
cat > "$OUTPUT_FILE" << EOF
# HELP curl_redirect_num_redirects Number of redirects for the URL
# TYPE curl_redirect_num_redirects gauge
curl_redirect_num_redirects{url="$URL"} $NUM_REDIRECTS
# HELP curl_redirect_time_redirect Redirect time in seconds
# TYPE curl_redirect_time_redirect gauge
curl_redirect_time_redirect{url="$URL"} $TIME_REDIRECT
# HELP curl_redirect_http_code HTTP status code after redirects
# TYPE curl_redirect_http_code gauge
curl_redirect_http_code{url="$URL"} $HTTP_CODE
EOF
2. 配置node_exporter textfile收集
确保node_exporter启动参数包含textfile收集目录:
--collector.textfile.directory=/var/lib/node_exporter/
设置定时任务,每分钟执行一次检测脚本:
* * * * * /path/to/curl_redirect_metrics.sh
方案二:编写专用导出器暴露curl重定向指标
当需要检测多个URL、需要更灵活的配置时,编写一个专用的Prometheus导出器是更好的选择。
Python版curl重定向导出器示例
from prometheus_client import Gauge, start_http_server
import pycurl
import time
import io
REDIRECT_COUNT = Gauge('curl_redirect_count', 'Number of redirects', ['url'])
REDIRECT_TIME = Gauge('curl_redirect_time_seconds', 'Redirect time', ['url'])
FINAL_URL = Gauge('curl_final_url', 'Final URL after redirects', ['url', 'final_url'])
URLS = [
'https://www.youres.cn',
'https://youres.cn/category/tech/',
]
def check_redirects():
while True:
for url in URLS:
buffer = io.BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.FOLLOWLOCATION, True)
c.setopt(c.WRITEDATA, buffer)
# 获取重定向信息
c.setopt(c.HEADERFUNCTION, lambda x: None) # 不输出header
c.perform()
redirect_count = c.getinfo(pycurl.REDIRECT_COUNT)
redirect_time = c.getinfo(pycurl.REDIRECT_TIME)
effective_url = c.getinfo(pycurl.EFFECTIVE_URL)
REDIRECT_COUNT.labels(url=url).set(redirect_count)
REDIRECT_TIME.labels(url=url).set(redirect_time)
FINAL_URL.labels(url=url, final_url=effective_url).set(1)
c.close()
time.sleep(60)
if __name__ == '__main__':
start_http_server(9101)
check_redirects()
方案三:Grafana告警一体化配置
指标接入Prometheus后,通过Grafana配置告警规则,实现重定向异常自动通知。
Prometheus告警规则示例
groups:
- name: curl_redirect_alerts
rules:
- alert: RedirectCountTooHigh
expr: curl_redirect_num_redirects{url="https://www.youres.cn"} > 3
for: 2m
labels:
severity: warning
annotations:
summary: "网站重定向次数异常增多"
description: "{{ $labels.url }} 重定向次数超过3次,当前值: {{ $value }}"
- alert: RedirectTimeTooSlow
expr: curl_redirect_time_redirect{url="https://www.youres.cn"} > 1
for: 2m
labels:
severity: warning
annotations:
summary: "网站重定向耗时过长"
description: "{{ $labels.url }} 重定向耗时超过1秒,当前值: {{ $value }}秒"
- alert: RedirectStatusCodeError
expr: curl_redirect_http_code{url="https://www.youres.cn"} != 200
for: 1m
labels:
severity: critical
annotations:
summary: "网站重定向后状态码异常"
description: "{{ $labels.url }} HTTP状态码非200,当前值: {{ $value }}"
Grafana钉钉告警通知配置
在Grafana中配置Contact point,选择DingDing类型,填入Webhook URL:
{
"msgtype": "markdown",
"markdown": {
"title": "{{ .CommonLabels.alertname }}",
"text": "## 网站重定向告警\n**URL**: {{ .CommonLabels.url }}\n**描述**: {{ .CommonAnnotations.description }}"
}
}
三个方案选型建议
| 方案 | 适用场景 | 复杂度 |
|---|---|---|
| node_exporter textfile | 已有node_exporter环境,少量URL | 低 |
| 专用导出器 | 多URL、需要灵活配置 | 中 |
| 完整告警一体化 | 生产环境、需要完整监控体系 | 高 |
内链扩展阅读
小结
把curl重定向检测接入Prometheus,核心就是三步:指标暴露、数据采集、告警通知。选型时根据现有基础设施灵活选择,别为了监控而过度设计。最轻量的textfile方案,10分钟就能跑起来;需要完整能力时,再升级到专用导出器方案。
版权声明
本文仅代表个人观点。
本文系AI辅助作者原创,未经许可,转载请保留原文链接。

发表评论