跳转至

Prometheus 装好却没数据、Targets 全红怎么查

我踩过几次:部署完 Prometheus,进 Grafana 一片白,再进 /targets 看,一半是 down,还有一半虽然 up 但指标里翻不到我要的东西。这种「装好却没数据」的状态最耗时间,因为你可以查的点太分散。我后来形成了一张决策树,按「先定位故障段,再执行对应修复」的思路走,基本能在半小时内把根因钉死。

第一步:先判断故障在「抓取段」还是「查询段」

进 Prometheus Web UI 的 Status → Targets,看那一行 target 的状态。

状态 含义 下一步去哪查
down 抓不到任何东西 走「抓取段」:网络 / 端口 / 鉴权 / exporter
up 但查指标没有 抓到了,但不是你要的指标 走「查询段」:指标名 / relabel / PromQL
up 且指标能查到 完全正常 你查错图了,去 Grafana 大盘核对

这是分岔的起点。把这一层判断完,后续每一步都更短。

第二步:down 状态下,按报错内容分岔

点进任意一个 down 的 target,看 Last Scrape 的报错,把报错分成三类:

2.1 connection refused / i/o timeout

这是网络段。从「目标机器到 Prometheus 容器」这条链路,按顺序排查:

# 在 Prometheus 所在机器上,测目标 exporter 端口是否通
curl -v http://<target-host>:<port>/metrics
# 不通就查三样:
# 1) target 机器上 exporter 是否真的起来
ps -ef | grep -E "node_exporter|cadvisor"
# 2) 防火墙是否放行了端口
sudo iptables -L -n | grep <port>
# 3) K8s 内 Service / Pod 网络策略是否拦了
kubectl get svc -n monitoring
kubectl get netpol -A

我遇到过一次:target 在 10.0.1.15,Prometheus 在另一个 k8s 节点,中间防火墙把 9100 段全关了。把 9100-9105 段放出去,10 分钟目标就 up 了。

2.2 401 / 403 / unauthorized

这是鉴权段。最常见是 scrape_configs 里写了 basic_auth 但目标没要密码,或反过来。

# 在 Prometheus 里查这个 job 的抓取配置
curl -s http://localhost:9090/api/v1/status/flags | jq .
# 直接看 scrape 错误
curl -s http://localhost:9090/api/v1/targets | jq '.data.activeTargets[] | select(.scrapeUrl|contains("9100"))'

修复:把 basic_auth、bearer_token、tls_config 三件套按目标真实要求改齐。

2.3 TLS handshake failed

目标启用了 HTTPS 但 scrape_configs 没写 tls_config,或 CA 没配。

# 修复模板
scrape_configs:
  - job_name: node-exporter-tls
    tls_config:
      ca_file: /etc/prometheus/ca.crt
      insecure_skip_verify: false  # 生产环境不要用 true
    basic_auth:
      username: prom
      password_file: /etc/prometheus/prom-pass

第三步:up 状态下,判断「查不到指标」是哪一段

Target 显示 up 但你去 /query 输入 up{instance="10.0.1.15:9100"} 没值,或查一个常用指标名却空,按下面三条查:

3.1 指标名根本不存在

# 列出 Prometheus 里所有指标名(截前 100 个先看个大概)
curl -s http://localhost:9090/api/v1/label/__name__/values | jq -r '.data[]' | head -100
# 直接查目标 instance 上有没有值
curl -s "http://localhost:9090/api/v1/query" --data-urlencode 'query=up{instance="10.0.1.15:9100"}'

如果 __name__ 列表里根本没有你查的那个指标,说明是 exporter 没暴露该指标——去看 exporter 自己的 /metrics 端点:

# 直接打目标 /metrics,看它实际暴露了哪些指标
curl -s http://10.0.1.15:9100/metrics | grep -E "^node_" | head -20

3.2 relabel 改没了 name

这是我踩过最深的一次:某次给 relabel_configs 加了个 metric_relabel 把某些指标名做了正则过滤,写错了正则把所有 node_ 开头的指标全过没了。target 还是 up,但你一查就空。

# 查 relabel 生效后的指标名(关键看 label 有没有被改)
curl -s "http://localhost:9090/api/v1/query?query=up" | jq '.data.result'
# 看 relabel 后实际生效的 label 组合
curl -s "http://localhost:9090/api/v1/series?match[]=up" | jq '.data[0].metric'

修 metric_relabel_configs 时要写两条:regex: ^(node_.*|up|process_.*|prometheus_.*).*$ 保留白名单,避免误杀。

3.3 K8s 场景:ServiceMonitor 被删 / labelSelector 漂移

在 K8s 里 target 全 down,最常先查三条:

# 1) Pod 状态
kubectl -n monitoring get pod -l app.kubernetes.io/name=prometheus
# 2) ServiceMonitor 还在不在、labelSelector 没改错
kubectl -n monitoring get servicemonitor -o wide
kubectl -n monitoring get servicemonitor <name> -o yaml | grep -A5 selector
# 3) Prometheus 日志里看 scrape 报错
kubectl -n monitoring logs deploy/prometheus-server --tail=200 | grep -E "error|scrape"

我遇到过一次:ops 同事手动改了 ServiceMonitor 的 namespace,selector 里的 matchLabels 漂移,K8s 端 Prometheus 直接不再发 scrape 请求,target 全变 down,看 Last Scrape 是「no targets」,把 selector 改回去 30 秒就活了。

第四步:别只盯着 target,scrape 间隔和 retention 也会「让数据看起来没有」

前面几段排的是「根本没数据」。但还有一种常见误判:其实数据一直在写,只是你查的时候没看到。三种会让数据「隐形」的参数:

第一,scrape 间隔太长。如果 scrape_interval 被设成了 60s,而你 Grafana 上把时间范围拉到一年,那每个数据点之间隔了一分钟,小时间窗口看起来就是空的。进 /status/flags 或看 scrape_configs 确认实际间隔:

curl -s http://localhost:9090/api/v1/status/flags | jq '.data.scrape-interval'

第二,retention 太短,老数据被裁掉了。我见过一台节点 retention 被误配成 2h,团队以为监控挂了,其实是三天前的曲线全被清掉了。看当前实际保留窗口:

curl -s http://localhost:9090/api/v1/status/runtimeinfo | jq '{tsdb_retention: .data.tsdb.retention.time, tsdb_max: .data.tsdb.versions}'

第三,recording rule 没跑或写错了。很多团队为了加速把常用查询写成 recording rule(预计算),如果 recording rule 本身正则写错或没生效,下游 Grafana 引用的那个指标就是空的,但原始指标其实在。

# 查 recording rule 有没有产出指标(把 metric 名换成你那条 recording rule 的 __name__)
curl -s "http://localhost:9090/api/v1/query" --data-urlencode 'query=job:http_request_total'
# 看 rule 当前状态
curl -s http://localhost:9090/api/v1/status/rules | jq '.data.groups[] | {name, rules: .rules[] | {name, state, health}}'

我遇到过一次:ops 把 recording rule 的 interval 写成了跟 scrape 一样的 15s,但 group 的 evaluation_interval 设成了 5m,于是预计算指标每 5 分钟才更新一次,Grafana 上看着像「数据有断档」,其实只是刷新节奏不对。

第五步:用 promtool 把配置层错误直接钉死

前几步都是「运行时」视角,最后再用一条离线命令把配置本身验证一遍:

# 配置文件合法性
promtool check config /etc/prometheus/prometheus.yml
# rules 文件(如果有告警规则)
promtool check rules /etc/prometheus/rules/*.yml
# K8s 上跑 Prometheus 的同学,把 ConfigMap 拉下来再 check
kubectl -n monitoring get cm prometheus-configuration -o yaml | sed -n '/data:/,$p' | tail -n +2 | base64 -d > /tmp/prom.yml
promtool check config /tmp/prom.yml

promtool check config 通过,说明配置层没错;再结合前几步的运行时视角,故障段就锁定死了。

第六步:修复后怎么确认数据真的回来了

不要看 /targets 状态就完事,要确认「下游 Grafana 能查到值」:

# 1) target up 了
curl -s http://localhost:9090/api/v1/targets | jq '.data.activeTargets[] | select(.health=="up") | .scrapeUrl'
# 2) 直接查一条指标看有没有值(注意是查不到时返回 result=[])
curl -s "http://localhost:9090/api/v1/query" --data-urlencode 'query=sum(rate(node_cpu_seconds_total[5m])) by (instance)'
# 3) 进 Grafana 那个大盘,把 time range 拉近(Last 15 minutes),确认曲线不是空白

一张速查表(把上面五步压成一张卡)

现象 段 一条快速验证命令
target down 网络 curl -v http://<target>:<port>/metrics
target down 鉴权 看 Last Scrape 报错 401/403
target down TLS 看 Last Scrape 报错 TLS handshake
target up 但查不到 指标名 /api/v1/label/__name__/values 列全名
target up 但查不到 relabel /api/v1/series?match[]=up 看生效 label
K8s target 全 down ServiceMonitor kubectl get svcmon -o wide + selector
配置层错 文件 promtool check config /etc/prometheus/prometheus.yml
数据有但「隐形」 间隔/保留 /api/v1/status/flags 看 scrape-interval

把这次排障变成告警:别让"没数据"再靠人肉发现

上面六步是"出事了怎么查"。但每次都是人肉进 /targets 才发现没数据,等于监控本身没被监控。我固定把下面三类信号接成告警,让"没数据"这件事自动喊出来,而不是等大盘空白才去翻:

# 1) target down 告警(抓不到 = 监控盲区,最高优先)
groups:
  - name: scrape-health
    rules:
      - alert: TargetDown
        expr: up == 0
        for: 5m            # 持续 5 分钟才算,避开瞬时抖动
        labels:
          severity: critical
        annotations:
          summary: "target {{ $labels.instance }} 抓取中断"
          description: "{{ $labels.job }} 在 {{ $value }},Last Scrape 报错见 Prometheus /targets"
      # 2) 某 job 全部 target 都 down(比单台更严重,往往是配置/网络段坏了)
      - alert: JobDown
        expr: sum by (job) (up) == 0
        for: 10m
        labels:
          severity: critical
        annotations:
          summary: "整个 job {{ $labels.job }} 无可用 target"

      # 3) recording rule 跑失败(预计算指标会静默断档)
      - alert: RuleEvaluationFailing
        expr: prometheus_rule_evaluation_failures_total > 0
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "rule {{ $labels.rule_group }} 评估失败"

      # 4) scrape 错误率(抓取慢/超时,还没到 down 但已在劣化)
      - alert: ScrapeSlow
        expr:
          histogram_quantile(0.99, sum(rate(prometheus_scrape_duration_seconds_bucket[5m])) by (le, job)) > 10
        for: 15m
        labels:
          severity: warning
        annotations:
          summary: "job {{ $labels.job }} P99 抓取耗时超 10s"

三条经验:up == 0 要配 for: 5m,不然目标重启那十几秒会刷一堆假告警;JobDown 用 sum by (job) (up) == 0 而不是 count,因为 up 本身是 0/1,求和为零才代表这个 job 彻底没 target;RuleEvaluationFailing 很多人漏配,结果 recording rule 静默失败,下游 Grafana 一直显示旧值或空白,等你发现时已经过了很久。接上这四条,"装好却没数据"基本不会再靠人肉发现了。

加规则后别直接重启 Prometheus(会丢内存里的活跃告警状态)。用热加载:先 promtool check rules rules.yml 过一遍语法,再对 Prometheus 发 SIGHUP 或调 /api/v1/reload,然后进 /api/v1/status/rules 确认 health 是 ok、lastError 为空。K8s 上如果用 operator,改的是 Prometheus/ThanosRuler 的 ruleFiles 或 AlertmanagerConfig,operator 会自动 reload,你只需要看 Pod 日志里有没有 successfully loaded storage 之类的加载成功行。

一个我会先问自己的问题

在动手前我常先问一句:「是从来没数据,还是数据有但我没查到?」这决定了往哪条路走。如果 /targets 全绿、/query 也查得到值,那问题根本不在 Prometheus,而在 Grafana 的数据源配置或查询语句——这种情况直接跳到 Grafana 侧排,别在 Prometheus 这边浪费时间。反过来,如果 /targets 一片红,就老老实实走前面「抓取段」那几步,从网络一路查到 exporter。把「故障在哪个段」先判对,后面每一步都能砍掉一半工作量,也不会把真正坏在网络或配置的问题错怪到查询层。我还习惯在排障记录里写一行「故障段」,下次再遇到同类型问题直接翻记录,省得从头走一遍。

相关