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

如何配置Prometheus仅在断电时触发路由器告警,忽略节点告警?

解决Prometheus路由器故障时仅触发路由器告警的方案

我的Prometheus服务器配置了两类监控目标:服务器节点信息存放在Target1.yml,路由器信息存放在Router.yml。每个站点拥有唯一的4位编号(示例为1234),目前有上万站点(总计5000个节点)。每当路由器故障或断电时,每个站点会触发5条告警,我需要实现仅触发路由器告警、忽略node1/node2/node3节点告警的效果。

现有目标配置

Target1.yml:

node1-1234.example.com
node2-1234.example.com
node3-1234.example.com
node1-4567.example.com
node2-4567.example.com
node3-4567.example.com

Router.yml:

router1-1234.example.com
router2-1234.example.com
router1-4567.example.com
router2-4567.example.com

解决方法

步骤1:给监控目标打标签

首先在Prometheus的抓取配置中,通过relabel_configs提取站点编号和节点类型标签,方便后续规则匹配:

# prometheus.yml 中的 scrape_configs 部分
scrape_configs:
  # 抓取服务器节点
  - job_name: 'server-nodes'
    file_sd_configs:
      - files: ['Target1.yml']
    relabel_configs:
      # 从主机名提取4位站点编号
      - source_labels: [__address__]
        regex: 'node[1-3]-(\d{4})\.example\.com'
        target_label: site_id
        replacement: '$1'
      # 标记节点类型为server
      - target_label: node_type
        replacement: 'server'

  # 抓取路由器节点
  - job_name: 'router-nodes'
    file_sd_configs:
      - files: ['Router.yml']
    relabel_configs:
      # 从主机名提取4位站点编号
      - source_labels: [__address__]
        regex: 'router[1-2]-(\d{4})\.example\.com'
        target_label: site_id
        replacement: '$1'
      # 标记节点类型为router
      - target_label: node_type
        replacement: 'router'

方式1:修改告警规则,直接过滤服务器告警

如果希望完全不生成服务器节点的离线告警(仅在路由器正常时才触发),可以修改告警规则:

# 告警规则文件(如alert_rules.yml)
groups:
- name: node_monitoring
  rules:
  # 仅触发路由器离线告警
  - alert: RouterDown
    expr: up == 0 and node_type == "router"
    for: 1m
    labels:
      severity: critical
    annotations:
      summary: "站点{{ $labels.site_id }}的路由器{{ $labels.instance }}离线"

  # 仅在对应站点路由器正常时,触发服务器离线告警
  - alert: ServerDown
    expr: up == 0 and node_type == "server" and on(site_id) (up{node_type="router"} == 1)
    for: 1m
    labels:
      severity: warning
    annotations:
      summary: "站点{{ $labels.site_id }}的服务器{{ $labels.instance }}离线"

这样当路由器故障时,up{node_type="router"} == 1条件不成立,服务器的离线告警不会被生成。

方式2:使用Alertmanager抑制规则

如果希望服务器告警正常生成,但路由器故障时不发送通知,可以配置Alertmanager的抑制规则:

# alertmanager.yml
route:
  group_by: ['alertname', 'site_id']
  receiver: 'your-notification-receiver' # 替换为你的通知接收器

inhibit_rules:
- source_match:
    alertname: "RouterDown"
    severity: "critical"
  target_match:
    alertname: "ServerDown"
    node_type: "server"
  equal: ['site_id']

当某站点的RouterDown告警触发时,同站点的ServerDown告警会被抑制,不会发送通知,但仍然会在Prometheus的告警页面中显示。

内容的提问来源于stack exchange,提问作者Sandosh Kumar P

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 12:54:18