17.7.5 多渠道通知与消息格式化
多渠道通知的目标是让告警在“正确的时间、正确的人、正确的方式”触达。Alertmanager 支持 Email、Slack、Webhook、PagerDuty、DingTalk/企业微信(通过 Webhook)等渠道。设计时应区分告警等级与通知渠道:P1 走电话/短信+IM 双通道,P2 走 IM+邮件,P3 走邮件或工单;通过路由树将不同服务、环境、级别匹配到不同接收器,避免所有告警都走同一渠道造成噪音。
安装与基础验证(Alertmanager)#
# 1) 下载并安装
cd /usr/local/src
wget https://github.com/prometheus/alertmanager/releases/download/v0.27.0/alertmanager-0.27.0.linux-amd64.tar.gz
tar -xf alertmanager-0.27.0.linux-amd64.tar.gz
mv alertmanager-0.27.0.linux-amd64 /opt/alertmanager
# 2) 创建用户与目录
useradd -r -s /sbin/nologin alertmanager
mkdir -p /etc/alertmanager /var/lib/alertmanager
chown -R alertmanager:alertmanager /etc/alertmanager /var/lib/alertmanager /opt/alertmanager
# 3) 启动(前台验证)
/opt/alertmanager/alertmanager \
--config.file=/etc/alertmanager/alertmanager.yml \
--storage.path=/var/lib/alertmanager \
--web.listen-address=:9093
# 预期:浏览器访问 http://<ip>:9093 显示 Alertmanager UI
多渠道路由与消息格式化示例#
# /etc/alertmanager/alertmanager.yml
global:
smtp_smarthost: "smtp.example.com:25"
smtp_from: "alert@example.com"
smtp_require_tls: false
templates:
- "/etc/alertmanager/templates/notify.tmpl"
route:
receiver: "default-email"
group_by: ["alertname","service","env","severity"]
group_wait: 30s
group_interval: 5m
repeat_interval: 2h
routes:
- matchers:
- severity="P1"
receiver: "im-and-webhook"
- matchers:
- severity="P2"
receiver: "im-only"
- matchers:
- severity="P3"
receiver: "default-email"
receivers:
- name: "default-email"
email_configs:
- to: "oncall@example.com"
send_resolved: true
headers:
Subject: '{{ template "title" . }}'
- name: "im-only"
webhook_configs:
- url: "http://im-gateway.local/alert"
send_resolved: true
- name: "im-and-webhook"
webhook_configs:
- url: "http://im-gateway.local/alert"
send_resolved: true
- url: "http://pagerduty.local/alert"
send_resolved: true
# /etc/alertmanager/templates/notify.tmpl
{{ define "title" -}}
[{{ .CommonLabels.severity }}] {{ .CommonLabels.service }} - {{ .CommonLabels.alertname }}
{{- end }}
{{ define "summary" -}}
{{- if .CommonAnnotations.summary -}}
{{ .CommonAnnotations.summary }}
{{- else -}}
Alert fired: {{ .CommonLabels.alertname }}
{{- end -}}
{{- end }}
{{ define "body" -}}
摘要:{{ template "summary" . }}
环境:{{ .CommonLabels.env }}
实例:{{ range .Alerts }}{{ .Labels.instance }} {{ end }}
开始时间:{{ (index .Alerts 0).StartsAt }}
详情:{{ .CommonAnnotations.description }}
Runbook:{{ .CommonAnnotations.runbook_url }}
Grafana:{{ .CommonAnnotations.dashboard }}
Prometheus:{{ (index .Alerts 0).GeneratorURL }}
{{- end }}
说明:模板中的
CommonLabels、CommonAnnotations用于合并多条告警;Alerts列表用于输出实例集合。Webhook 通道建议仅发送结构化 JSON,不使用过长正文。
Webhook 消息格式化与本地验证#
# 1) 启动一个接收端查看消息体
python3 -m http.server 8000
# 2) 修改 alertmanager.yml 里 webhook url 为:
# http://<你的IP>:8000
# 3) 触发测试告警(Alertmanager自带API)
curl -XPOST -H 'Content-Type: application/json' \
-d '[
{
"labels": {
"alertname": "DiskUsageHigh",
"service": "node",
"env": "prod",
"severity": "P2",
"instance": "10.0.0.10:9100"
},
"annotations": {
"summary": "磁盘使用率超过 85%",
"description": "根分区使用率当前值为 91%",
"runbook_url": "http://wiki.local/runbook/disk",
"dashboard": "http://grafana.local/d/abc123"
}
}
]' \
http://<alertmanager_ip>:9093/api/v2/alerts
# 预期:在 http.server 输出中看到告警 JSON
命令解释与关键参数#
group_by:按哪些标签分组,决定 IM 中合并多少条告警。group_wait:新组首次发送前等待时间,避免短时间抖动。repeat_interval:相同告警重复通知间隔。send_resolved:告警恢复时发送恢复通知。
排错清单(常见问题与命令)#
# 1) 验证配置文件语法
/opt/alertmanager/amtool check-config /etc/alertmanager/alertmanager.yml
# 2) 查看运行状态与监听端口
ss -lntp | grep 9093
curl -s http://localhost:9093/-/ready
# 3) 查看当前告警是否被抑制/静默
curl -s http://localhost:9093/api/v2/silences | jq '.[] | {id, status, startsAt, endsAt, matchers}'
# 4) 查看路由是否命中(调试模式)
# 在 systemd 中加入 --log.level=debug 重新启动
练习#
1) 为 P1 告警增加短信网关 Webhook,要求同时通知 IM 与短信。
2) 修改模板,限制 IM 渠道正文最多 3 条实例,超出部分输出“...更多见链接”。
3) 使用 amtool check-config 验证配置,通过后触发测试告警,截图显示分组效果。
4) 实现生产与测试环境分流:env=prod 走 P1/P2 通道,env=staging 仅邮件。