如何为Prometheus AlertManager的MS Teams Webhook传递摘要/文本字段
解决AlertManager无法向MS Teams发送告警的问题
问题原因
MS Teams的入站webhook要求请求体必须包含text或summary字段,但AlertManager默认发送的webhook payload是其自身的告警格式,不符合Teams的要求,因此返回400错误。
解决方案:使用自定义通知模板适配Teams格式
1. 创建Teams通知模板文件
新建一个模板文件(比如/etc/alertmanager/teams.tmpl),内容如下:
{{ define "teams.message" }} { "@type": "MessageCard", "@context": "http://schema.org/extensions", "summary": "{{ .CommonAnnotations.summary }}", "text": "{{ .CommonAnnotations.description }}", "themeColor": "{{ if eq .Status "firing" }}FF0000{{ else }}00FF00{{ end }}", "sections": [ { "facts": [ {{ range .Alerts }} { "name": "告警名称", "value": "{{ .Labels.alertname }}" }, { "name": "实例", "value": "{{ .Labels.instance }}" }, { "name": "级别", "value": "{{ .Labels.severity }}" }, { "name": "当前值", "value": "{{ .Annotations.value }}" } {{ end }} ] } ] } {{ end }}
这个模板会把告警规则中定义的summary和description对应到Teams要求的字段,同时根据告警状态(触发/恢复)设置不同主题色,还会展示关键告警维度信息。
2. 修改AlertManager配置文件
更新alertmanager.yaml,在webhook_configs中添加body字段引用模板,同时指定模板文件路径:
route: receiver: 'teams' group_wait: 30s group_interval: 5m receivers: - name: 'teams' webhook_configs: - send_resolved: true url: '<my-msteams-webhook-url>' body: '{{ template "teams.message" . }}' templates: - '/etc/alertmanager/teams.tmpl'
3. 重启AlertManager服务
保存配置和模板后,重启AlertManager使配置生效:
systemctl restart alertmanager
关键说明
- 不要在
http_config里添加summary或params字段:AlertManager的webhook配置没有这些字段,强行添加会导致配置加载失败。 - 模板内容可按需调整:比如修改
themeColor的颜色值,或者增减sections里的信息项,适配你的业务展示需求。
内容的提问来源于stack exchange,提问作者MsA
相关产品推荐
相关产品推荐

