You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Prometheus告警中添加Node Exporter信息区分服务器来源

解决多Docker栈Prometheus告警区分服务器来源的问题

核心问题分析

你的up == 0告警缺少服务器标识,是因为Docker容器内的实例主机名/IP重复,而up指标的标签仅来自Prometheus采集目标的配置,没有自带服务器级别的标识。下面给你几个可行的解决办法:


方案一:在Prometheus采集阶段添加服务器静态标签(推荐)

直接在Prometheus的scrape_configs里,给每台服务器的所有监控目标附加服务器专属标签(比如server或product_name),这样所有指标(包括up)都会带上这些标签,告警时直接调用即可。

示例采集配置:

scrape_configs:
  # 针对服务器1的所有Docker栈服务
  - job_name: 'docker-stack-server1'
    static_configs:
      - targets: ['server1-ip:9100', 'server1-ip:8080', 'server1-ip:9090']
        labels:
          server: 'prod-server-01'
          product_name: 'product-a'
  
  # 针对服务器2的所有Docker栈服务
  - job_name: 'docker-stack-server2'
    static_configs:
      - targets: ['server2-ip:9100', 'server2-ip:8080']
        labels:
          server: 'prod-server-02'
          product_name: 'product-b'

修改后的告警规则:

- alert: InstanceDown
  expr: up == 0
  for: 0m
  labels:
    severity: critical
  annotations:
    summary: "实例宕机: {{ $labels.instance }}"
    description: "服务器 {{ $labels.server }}(产品:{{ $labels.product_name }})上的实例 {{ $labels.instance }} 已停止运行。"

这样告警发送到Teams时,就能直接看到是哪台服务器的实例出问题了。


方案二:用PromQL关联Node Exporter指标获取服务器信息

如果没法修改采集配置,可以通过PromQL的group_left将up指标和Node Exporter的node_dmi_info(或其他带服务器标识的指标)关联,把服务器标签附加到告警结果中。

假设你的Node Exporter采集job名为node-exporter,且node_dmi_info里有product_name标签,告警规则可以这么写:

- alert: InstanceDown
  # 通过instance的IP部分关联同一服务器的up和node_dmi_info指标
  expr: up == 0 and on(instance) group_left(product_name) node_dmi_info{job="node-exporter"}
  for: 0m
  labels:
    severity: critical
    host: "{{ $labels.product_name }}"
  annotations:
    summary: "实例宕机: {{ $labels.instance }}"
    description: "来自主机 {{ $labels.product_name }} 的实例 {{ $labels.instance }} 已宕机。"

注意:这个方案需要确保up指标的instance标签(比如192.168.1.10:8080)和Node Exporter的instance标签(比如192.168.1.10:9100)的IP部分一致,否则关联会失败。


方案三:在Alertmanager模板中补充服务器信息

如果已经有实例的IP,可以在Alertmanager的Teams通知模板里,通过IP与服务器名的映射关系显示服务器标识。比如在模板中写:

{{ define "teams.instanceDown" }}
{{ range .Alerts }}
### 告警通知
- 告警级别: {{ .Labels.severity }}
- 实例: {{ .Labels.instance }}
- 服务器: {{ if eq .Labels.instance "192.168.1.10:8080" }}prod-server-01{{ else if eq .Labels.instance "192.168.1.11:8080" }}prod-server-02{{ end }}
- 描述: {{ .Annotations.description }}
{{ end }}
{{ end }}

但这种方式不够灵活,新增服务器或实例时需要手动更新模板,仅适合小规模场景。


内容的提问来源于stack exchange,提问作者jonathan-dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 11:25:00