如何配置Alertmanager根据告警级别发送至对应Slack频道?
Prometheus Alertmanager 按告警级别分发至不同Slack频道配置
核心思路是利用Alertmanager的路由匹配规则与接收者配置,根据告警的severity标签值,将告警精准路由到对应Slack频道。
以下是完整的alertmanager.yml配置示例:
global: slack_api_url: "https://hooks.slack.com/services/XXX/XXX/XXX" # 替换为你的Slack Webhook地址 route: group_by: ['alertname'] group_wait: 10s group_interval: 10s repeat_interval: 1h receiver: 'default-receiver' # 兜底接收者,防止未匹配告警丢失 routes: - match: severity: warning receiver: 'slack-warning-channel' continue: false # 匹配后不再向下路由 - match: severity: critical receiver: 'slack-critical-channel' continue: false receivers: - name: 'slack-warning-channel' slack_configs: - channel: '#warning-alerts' # 替换为你的warning频道名称 title: "⚠️ 警告告警" text: |- *告警名称:* {{ .CommonAnnotations.alertname }} *级别:* {{ .CommonLabels.severity }} *摘要:* {{ .CommonAnnotations.summary }} *详情:* {{ .CommonAnnotations.description }} - name: 'slack-critical-channel' slack_configs: - channel: '#critical-alerts' # 替换为你的critical频道名称 title: "🚨 严重告警" text: |- *告警名称:* {{ .CommonAnnotations.alertname }} *级别:* {{ .CommonLabels.severity }} *摘要:* {{ .CommonAnnotations.summary }} *详情:* {{ .CommonAnnotations.description }} *触发时间:* {{ .StartsAt.Format "2006-01-02 15:04:05" }} - name: 'default-receiver' # 可选,处理未匹配标签的告警 slack_configs: - channel: '#unclassified-alerts' title: "🔍 未分类告警" text: |- *告警名称:* {{ .CommonAnnotations.alertname }} *所有标签:* {{ range .CommonLabels }}{{ .Name }}: {{ .Value }} {{ end }}
关键配置解释
- global 段:配置Slack全局Webhook地址,所有Slack接收者默认复用该地址(也可在单个
slack_configs中单独指定)。 - route 段:
- 根路由设置了告警分组规则、等待/间隔时间、重复告警周期等基础参数。
routes列表定义了两个子路由,通过match规则精准匹配severity标签值。continue: false确保告警匹配当前路由后,不再触发后续路由,避免重复推送。
- receivers 段:
- 每个接收者对应一个Slack频道,可自定义告警消息的标题和内容模板。
- 模板中使用Alertmanager内置变量,比如
{{ .CommonLabels.severity }}获取告警级别,{{ .CommonAnnotations.summary }}获取告警摘要。
必要前置检查
- 确保你的Prometheus告警规则已为每个告警添加
severity标签,示例如下:groups: - name: system-monitor-rules rules: - alert: 高CPU使用率 expr: 100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[1m])) * 100) > 80 labels: severity: warning annotations: summary: "{{ $labels.instance }} CPU使用率过高" description: "CPU使用率已连续1分钟超过80%" - alert: 严重CPU使用率 expr: 100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[1m])) * 100) > 95 labels: severity: critical annotations: summary: "{{ $labels.instance }} CPU使用率达到临界值" description: "CPU使用率已连续1分钟超过95%" - 替换配置中的Slack Webhook地址和频道名为实际信息。
- 配置完成后重启Alertmanager生效,启动命令示例:
alertmanager --config.file=alertmanager.yml
内容的提问来源于stack exchange,提问作者Scott
相关产品推荐
相关产品推荐

