Grafana告警推送至钉钉教程

Author Avatar
邬程峰
发表:2026-04-29 23:51:32
修改:2026-04-29 23:51:32

📌 环境确认

  • Grafana v12.4.0(内置 dingtalkContact Point 插件),国内和国外的Grafana都是这个版本

  • 钉钉群 自定义机器人 权限

  • Grafana 服务器可访问 oapi.dingtalk.com:443

第一步:创建钉钉自定义机器人

  1. 钉钉群 → ⚙️ 群设置智能群助手添加机器人自定义

  2. 命名:按照用途命名

  3. 安全设置(必需三选一)

    自定义钉钉机器人接入

    • 加签(推荐):复制 SecretSECxxxxxxxx

    • 🔑 自定义关键词:添加 Grafana告警监控

    • 🌐 IP 白名单:填写 Grafana 服务器出口 IP

  4. 点击 完成完整复制 Webhook URLhttps://oapi.dingtalk.com/robot/send?access_token=xxx

第二步:Grafana 配置钉钉联系方式(Contact Point)

  1. 左侧菜单 AlertingContact points

  2. 点击 + Create contact point

  3. 填写字段(v12.4.0 界面):

    字段

    填写说明

    Name

    按照用途取一个

    Integration

    下拉选择 DingDing

    URL

    粘贴完整 URL

    Message Type

    ActionCard,这个才能正常发卡片

    Title

    留空或填 Grafana告警(若钉钉设了关键词验证,必须包含关键词)

    Message

    该部分笔者尚未完全理解,使用 gotemplate 编写卡片内容,以下是现在使用的两个模板:
    {{ range .Alerts }} {{ if eq .Status "firing" }} 国外 Kafka 积压数大于50000告警推送:消费组: {{ .Labels.consumergroup }} ,命名空间: {{ .Labels.namespace }} ,instance : {{ .Labels.instance }} {{ end }} {{ end }}

    {{ range .Alerts }} {{ if eq .Status "firing" }} CPU 使用占用超 60% 告警推送:container={{ .Labels.container }} , 命名空间={{ .Labels.namespace }}, pod={{ .Labels.pod }} {{ end }} {{ end }}

    Disable resolve message

    默认关闭(恢复时会推送Resolved通知)

  4. 点击底部

    Test
    • 钉钉收到 ✅ Test notification from Grafana → 成功

    • 报错 403 → 检查关键词/加签/白名单

    • 报错 400 → 检查 URL 是否完整或含空格

  5. 点击 Save contact point

第三步:配置通知策略(Notification Policy)

  1. AlertingNotification policies

  2. 根策略右侧点击 Add policy

  3. 配置路由规则:

    • Matchersenv = xxx(与告警规则 Labels 对应)

    • Contact point:选择上一步刚刚创建的 Contact point

    • Override general timings:勾选

    • Group wait30s

    • Group interval5m

    • Repeat interval1h

    • Mute timing:按需绑定(如维护窗口)

  4. 点击

    Save policy/Update policy

第四步:创建告警规则(Alert Rule)

  1. AlertingAlert rules+ Create alert rule

  2. Step 1: Query & Transform

    • 选择数据源(如 Prometheus

    • 编写查询,点击 Run queries 验证返回结果

    100 * (
      sum(rate(container_cpu_usage_seconds_total{image!=""}[5m])) by (pod, namespace, container)
      /
      sum(kube_pod_container_resource_limits{resource="cpu"}) by (pod, namespace, container)
    )
    ​
    sum(kafka_consumergroup_lag{consumergroup!~"anonymous.*|amazon.*|.*([0-9]{1,3}\\.){3}[0-9]{1,3}.*"}) 
      by (consumergroup, topic, namespace, instance) > 50000
  3. Step 2: Set conditions

    • 选择评估字段(如 A

    • 设置阈值:IS ABOVE 60IS ABOVE 50000

    • 点击Preview alert rule condition可以查看查询结果

  4. Step 3: Add folder and labels

    • Folder 选择之前建好的

    • Labels:添加 env = kafka 或者 env = cpu必须与策略 Matchers 一致

  5. Step 4: Evaluation

    • Evaluation group and interval:新建一个

    • Pending period5m(持续满足条件 5 分钟才触发,防网络抖动)

    • Keep firing for5m

  6. Step 5: Configure notifications

    • 确认 Contact point 是之前创建的

  7. 点击 Save

第五步:测试

验证路径

  1. 手动制造触发条件(如临时调低阈值)

  2. AlertingAlert instances 查看状态:NormalPendingFiring

  3. 钉钉群收到卡片

  4. 指标恢复后,状态变为 Resolved,钉钉推送恢复通知

评论