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

如何在Prometheus AlertManager配置中用正则匹配Node开头告警

实现以Node开头的告警统一路由到discord_infra

可以通过AlertManager的正则匹配功能实现,无需逐个配置告警名称。注意AlertManager中需使用matchers字段搭配=~运算符编写正则表达式,match仅支持精确匹配,无法满足正则需求。

正确的路由规则写法

将原配置中单独匹配NodeCPUHighUsage的路由条目,替换为以下规则:

- matchers:
    - alertname =~ "^Node.*$"
  receiver: "discord_infra"
  • ^Node.*$:正则表达式表示匹配以Node开头的所有alertname,^匹配字符串起始位置,.*匹配任意长度的任意字符,$匹配字符串结束位置,确保完全覆盖所有Node前缀的告警。

修改后的完整AlertManager配置

config:
  global:
    resolve_timeout: 5m
  inhibit_rules:
  - equal:
    - namespace
    - alertname
    source_matchers:
    - severity = critical
    target_matchers:
    - severity =~ warning|info
  - equal:
    - namespace
    - alertname
    source_matchers:
    - severity = warning
    target_matchers:
    - severity = info
  - equal:
    - namespace
    source_matchers:
    - alertname = InfoInhibitor
    target_matchers:
    - severity = info
  receivers:
  - name: "null"
  - name: discord
    discord_configs:
    - webhook_url: https://discord.com/api/webhooks/xxxzUwDy
  - name: discord_infra
    discord_configs:
    - webhook_url: https://discord.com/api/webhooks/1197183190733963356/yyyyyy
  route:
    group_by:
    - alertname
    group_interval: 5m
    group_wait: 1m
    receiver: discord
    repeat_interval: 15m
    routes:
    - matchers:
        - alertname =~ "^Node.*$"
      receiver: "discord_infra"
    - match:
        alertname: Watchdog
      receiver: "null"
    - match:
        alertname: KubeVersionMismatch
      receiver: "null"
    - match:
        alertname: KubeClientErrors
      receiver: "null"
    - group_interval: 5m
      group_wait: 1m
      match:
        severity: info
      receiver: discord
      repeat_interval: 15m
    - group_interval: 5m
      matchers: null

  templates:
  - '/etc/alertmanager/config/*.tmpl'

注意:路由匹配为从上到下顺序执行,因此需将Node前缀的匹配规则放在routes最上方,确保所有Node类告警优先匹配到discord_infra,避免被后续规则覆盖。

内容的提问来源于stack exchange,提问作者Maksim

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 19:43:16