# alertmanager.yml
# 全局配置(所有接收器的默认设置)
global:
# 告警恢复后持续发送通知的时间(防止短暂恢复后再次触发)
resolve_timeout: 5m
# SMTP服务器配置(会被接收器中的email_configs覆盖)
smtp_smarthost: 'smtp.example.com:587'
smtp_from: 'alertmanager@example.com'
smtp_auth_username: 'admin@example.com'
smtp_auth_password: 'password123'
smtp_require_tls: true # 强制使用TLS加密
# Slack Webhook URL(会被接收器中的slack_configs覆盖)
slack_api_url: 'https://hooks.slack.com/services/TXXXXXX/BXXXXXX/XXXXXX'
# 告警路由树的根节点配置
route:
# 默认接收器(必须与receivers中的某个name对应)
receiver: 'default-email'
# 分组依据的标签,相同标签值的告警会被合并
group_by: ['alertname', 'cluster', 'service']
# 初次发送告警前的等待时间(等待30秒以收集同组告警)
group_wait: 30s
# 同一组告警下次发送的时间间隔(若未修复,每5分钟发送一次)
group_interval: 5m
# 重复发送未修复告警的间隔(每3小时重复提醒)
repeat_interval: 3h
# 子路由规则(按顺序匹配)
routes:
# 路由1:严重级别为critical的告警发送到pagerduty
- match:
severity: critical
receiver: 'pagerduty-team'
# 是否继续匹配后续路由(false表示匹配到此终止)
continue: false
# 路由2:服务名为web或db的告警发送到Slack
- match_re:
service: ^(web|db)$
receiver: 'slack-notify'
# 子路由:生产环境的告警单独路由到另一个Slack频道
routes:
- match:
environment: prod
receiver: 'prod-slack'
# 接收器列表(定义告警通知发送到哪里)
receivers:
# 接收器1:默认邮件通知
- name: 'default-email'
email_configs:
- to: 'admin@example.com' # 收件人
send_resolved: true # 发送恢复通知
# 自定义邮件主题
headers:
Subject: '[ALERT] {{ .CommonLabels.severity }}: {{ .CommonLabels.alertname }}'
# 接收器2:PagerDuty通知
- name: 'pagerduty-team'
pagerduty_configs:
- service_key: 'pd_api_key_123' # PagerDuty集成的API密钥
severity: 'critical' # 设置事件严重级别
# 自定义事件详情
details:
firing: '{{ .Alerts | len }}个告警触发'
summary: '{{ .CommonAnnotations.summary }}'
# 接收器3:Slack通知(非生产环境)
- name: 'slack-notify'
slack_configs:
- channel: '#alerts' # Slack频道
title: '{{ .CommonLabels.alertname }}' # 消息标题
# 消息正文(使用模板语法)
text: |-
*描述*: {{ .CommonAnnotations.description }}
*环境*: {{ .CommonLabels.environment }}
*详情链接*: <{{ .GeneratorURL }}|查看>
send_resolved: true # 发送恢复通知
# 自定义颜色(warning: 黄色, critical: 红色)
color: '{{ if eq .CommonLabels.severity "critical" }}danger{{ else }}warning{{ end }}'
# 接收器4:生产环境专用Slack频道
- name: 'prod-slack'
slack_configs:
- channel: '#prod-alerts'
title: '生产告警: {{ .CommonLabels.alertname }}'
# 抑制规则(减少冗余告警)
inhibit_rules:
# 规则1:当网络不可达时,抑制相关服务告警
- source_match:
alertname: NetworkDown # 源告警名称
severity: critical # 源告警级别
target_match:
severity: warning # 被抑制的目标告警级别
equal: ['cluster'] # 要求cluster标签值相同才抑制
# 规则2:主机宕机时抑制其上的所有服务告警
- source_match:
alertname: HostDown
target_match_re:
instance: '^.*$' # 匹配所有实例
equal: ['instance'] # 抑制相同实例的其他告警
# 自定义模板文件路径(可选)
templates:
- '/etc/alertmanager/templates/*.tmpl'