Grafana告警规则引擎配置与多通道通知路由实战

Grafana告警体系架构演进

Grafana从8.0版本开始引入统一的告警引擎(Unified Alerting),替代了旧版Dashboard Alert。新版告警系统将告警规则和数据源解耦,支持多数据源查询、跨数据源表达式计算、告警静默与抑制、多通道路由等能力。对SRE团队而言,Grafana告警引擎已成为开箱即用的轻量级告警方案,在中小规模场景下可替代Alertmanager。

Grafana告警引擎的核心架构包含三个组件:Alert Rule(定义告警条件)、Contact Point(定义通知通道)、Notification Policy(定义路由规则)。三者协同工作:Alert Rule产生告警实例,Notification Policy根据标签匹配路由规则,最终通过Contact Point发送通知。

告警规则的创建与表达式语法

告警规则支持PromQL、SQL、Loki LogQL等多种查询语言。以Prometheus数据源为例,通过Terraform管理告警规则:

resource "grafana_rule_group" "node_alerts" {
  name             = "node-basic"
  folder_uid       = grafana_folder.sre.uid
  interval_seconds = 60

  rule {
    name           = "NodeHighCPU"
    condition      = "C"
    for             = "5m"
    no_data_state  = "NoData"
    exec_err_state = "Alerting"

    # A: 查询CPU使用率
    data {
      ref_id          = "A"
      relative_time_range { from = 300 }
      datasource_uid  = "prometheus"
      model = jsonencode({
        expr = "100 - (avg by(instance)(rate(node_cpu_seconds_total{mode=\"idle\"}[5m])) * 100)",
        instant = true
        type    = ""
      })
    }

    # B: 聚合处理
    data {
      ref_id = "B"
      model = jsonencode({
        type       = "reduce"
        reducer    = "last"
        expression = "A"
      })
    }

    # C: 阈值判断
    data {
      ref_id = "C"
      model = jsonencode({
        type       = "threshold"
        expression = "B"
        conditions = [{ evaluator: { params: [85], type: "gt" } }]
      })
    }

    labels = { severity = "warning", team = "infra" }
    annotations = { summary = "CPU使用率超过85%" }
  }
}

表达式的A-B-C模式是Grafana告警的核心概念:A查询数据,B聚合处理,C阈值判断。for参数控制持续时间,避免毛刺误报。no_data_state和exec_err_state分别处理无数据和查询失败场景。

Contact Point多通道通知配置

Contact Point定义告警的发送目标。Grafana支持30+通知渠道,最常用的包括企业微信和钉钉:

# 企业微信机器人
resource "grafana_contact_point" "wechat" {
  name = "wechat-infra"
  wechat {
    api_url  = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send"
    message  = <<EOT
{{ .CommonLabels.alertname }}
状态: {{ .Status }}
概要: {{ .CommonAnnotations.summary }}
详情: {{ range .Alerts }}{{ .Annotations.description }}{{ end }}
EOT
    recipient = "infra-webhook-key"
  }
}

# 钉钉机器人
resource "grafana_contact_point" "dingtalk" {
  name = "dingtalk-oncall"
  dingtalk {
    url          = "https://oapi.dingtalk.com/robot/send?access_token=xxx"
    message_type  = "markdown"
    message       = "### 告警通知\n{{ range .Alerts }}- **{{ .Labels.alertname }}**: {{ .Annotations.summary }}\n{{ end }}"
  }
}

配置时的注意事项:企业微信机器人的api_url需要是企业微信后台生成的Webhook地址;钉钉机器人的message_type建议使用markdown以获得更好的排版效果;所有通知模板都支持Go template语法,可自定义格式。

Notification Policy路由与分级策略

Notification Policy是告警路由的核心。通过标签匹配实现多级路由:

resource "grafana_notification_policy" "root" {
  group_by        = ["alertname", "cluster"]
  contact_point   = grafana_contact_point.opsgenie.name
  group_wait      = "30s"
  group_interval  = "5m"
  repeat_interval = "4h"

  # P0级告警:立即通知并电话呼入
  policy {
    matcher { label = "severity", match = "=", value = "critical" }
    contact_point   = grafana_contact_point.phone_pager.name
    group_wait      = "0s"
    group_interval  = "1m"
    repeat_interval = "1h"
  }

  # 基础设施团队路由
  policy {
    matcher { label = "team", match = "=", value = "infra" }
    contact_point = grafana_contact_point.wechat.name
  }

  # 静默期路由
  policy {
    matcher { label = "maintenance", match = "=", value = "true" }
    contact_point  = grafana_contact_point.null.name
    mute_timings   = ["always"]
  }
}

路由策略的关键参数:group_by控制告警分组,相同分组的告警合并为一条通知;group_wait是第一条告警等待合并的时间,设为0表示立即发送;group_interval是同一分组内有新告警时的最小通知间隔;repeat_interval是重复通知的间隔,防止告警刷屏。

告警静默与抑制机制

计划维护期间,可以通过静默规则临时关闭告警通知。Grafana支持两种静默方式:通过UI手动创建静默规则,或通过API自动化管理。抑制策略适用于告警关联场景,如父节点故障时抑制子节点告警,避免告警风暴。配置抑制规则需要在Alert Rule中设置标签层级关系,Notification Policy根据标签匹配实现级联抑制。

实战中的告警分级最佳实践:P0(critical)电话加即时消息,5分钟内响应;P1(warning)即时消息加邮件,30分钟内响应;P2(info)邮件通知,工作时间处理。各级别的repeat_interval应递增,避免低级别告警频繁通知干扰值班人员。

原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/grafana-gao-jing-gui-ze-yin-qing-pei-zhi-yu-duo-tong-dao/

(0)
小编小编
上一篇 13小时前
下一篇 13小时前

相关推荐