alertmanager告警模板🧲


已将alertmanager告警接入钉钉机器人

模板路径:/usr/local/dingtalk/templates/monitor.tmpl

{{- define "custom.title" -}}
{{- if eq .Status "firing" -}}
🚨 告警通知{{ with .CommonLabels.alertname }} · {{ . }}{{ end }}
{{- else -}}
✅ 告警恢复通知{{ with .CommonLabels.alertname }} · {{ . }}{{ end }}
{{- end -}}
{{- end -}}

{{- define "custom.content" -}}
{{- $status := .Status -}}
{{- $firingCount := len .Alerts.Firing -}}
{{- $resolvedCount := len .Alerts.Resolved -}}

# {{ if eq $status "firing" }}🚨 监控告警通知{{ else }}✅ 监控恢复通知{{ end }}

**当前状态:** {{ if eq $status "firing" }}🔴 存在触发{{ else }}🟢 已恢复{{ end }}

**触发数量:** {{ $firingCount }} 条

**恢复数量:** {{ $resolvedCount }} 条

---

{{ if gt $firingCount 0 }}
#### 🔴 正在触发的告警

{{ range .Alerts.Firing -}}
**{{ .Labels.alertname }}**
- **级别:** {{ if eq .Labels.severity "critical" }}🔴 严重{{ else if eq .Labels.severity "warning" }}🟠 警告{{ else if eq .Labels.severity "info" }}🔵 提示{{ else }}⚪ {{ .Labels.severity }}{{ end }}
- **实例:** {{ .Labels.instance }}
- **摘要:** {{ .Annotations.summary }}
- **详情:** {{ .Annotations.description }}
- **开始:** {{ .StartsAt.Local.Format "2006-01-02 15:04:05" }}

{{ end }}
{{ end }}

{{ if gt $resolvedCount 0 }}
#### 🟢 已经恢复的告警

{{ range .Alerts.Resolved -}}
**{{ .Labels.alertname }}**
- **级别:** {{ if eq .Labels.severity "critical" }}🔴 严重{{ else if eq .Labels.severity "warning" }}🟠 警告{{ else if eq .Labels.severity "info" }}🔵 提示{{ else }}⚪ {{ .Labels.severity }}{{ end }}
- **实例:** {{ .Labels.instance }}
- **摘要:** {{ .Annotations.summary }}
- **详情:** {{ .Annotations.description }}
- **开始:** {{ .StartsAt.Local.Format "2006-01-02 15:04:05" }}
- **恢复:** {{ .EndsAt.Local.Format "2006-01-02 15:04:05" }}

{{ end }}
{{ end }}

---
**监控平台:** Prometheus + Alertmanager
{{- end -}}

二、测试告警与恢复通知

1. 模拟“告警触发”(Firing)命令

这条命令会模拟发送一条严重(critical)的 CPU 告警。

curl -X POST -H "Content-Type: application/json" -d @- http://localhost:8060/dingtalk/webhook1/send <<'EOF'
{
  "version": "4",
  "status": "resolved",
  "receiver": "webhook1",
  "groupLabels": {
    "alertname": "HighMemoryUsage"
  },
  "commonLabels": {
    "alertname": "HighMemoryUsage",
    "severity": "warning"
  },
  "commonAnnotations": {},
  "externalURL": "http://localhost:9093",
  "alerts": [
    {
      "status": "resolved",
      "labels": {
        "alertname": "HighMemoryUsage",
        "severity": "warning",
        "instance": "192.168.1.100:9100"
      },
      "annotations": {
        "summary": "内存使用率过高",
        "description": "服务器 192.168.1.100 的内存使用率已恢复正常,当前值 60%。"
      },
      "startsAt": "2026-11-04T09:00:00Z",
      "endsAt": "2026-11-04T10:15:00Z",
      "generatorURL": "http://localhost:9090/graph",
      "fingerprint": "test-resolved-01"
    }
  ]
}
EOF

2. 模拟“告警恢复”(Resolved)命令

这条命令会模拟发送一条警告(warning)级别的恢复通知。

curl -X POST -H "Content-Type: application/json" -d @- http://localhost:8060/dingtalk/webhook1/send <<'EOF'
{
  "version": "4",
  "status": "resolved",
  "receiver": "webhook1",
  "groupLabels": {
    "alertname": "HighMemoryUsage"
  },
  "commonLabels": {
    "alertname": "HighMemoryUsage",
    "severity": "warning"
  },
  "commonAnnotations": {},
  "externalURL": "http://localhost:9093",
  "alerts": [
    {
      "status": "resolved",
      "labels": {
        "alertname": "HighMemoryUsage",
        "severity": "warning",
        "instance": "192.168.1.100:9100"
      },
      "annotations": {
        "summary": "内存使用率过高",
        "description": "服务器 192.168.1.100 的内存使用率已恢复正常,当前值 60%。"
      },
      "startsAt": "2026-11-04T09:00:00Z",
      "endsAt": "2026-11-04T10:15:00Z",
      "generatorURL": "http://localhost:9090/graph",
      "fingerprint": "test-resolved-01"
    }
  ]
}
EOF

三、告警通知示例:

image

四、恢复通知示例

image

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐