如何基于Jobname为Prometheus Alertmanager配置分团队告警路由
问题根因
你之前的配置没有生效有两个核心原因:
- 你定义的宕机告警规则
alertrule.yml中,所有实例宕机的告警alertname固定为InstanceDown,不存在A_down/B_down的标签,因此路由匹配规则完全无法命中。 - Alertmanager默认路由规则为匹配到子路由后立即停止匹配,不会自动触发父节点的默认接收器,需要额外配置
continue: true才能实现同时发送给指定团队+默认组的需求。
Alertmanager路由匹配核心逻辑
- 路由为树状层级结构,告警从根路由开始逐层向下匹配子路由
- 默认匹配逻辑为首次命中符合条件的子路由后,仅触发该子路由配置的接收器,终止后续匹配
- 子路由添加
continue: true参数后,命中该路由后会继续匹配后续路由/父级路由,实现多接收器同时触发 - 所有匹配规则均基于告警携带的标签(如
alertname、job、instance、severity等)进行匹配
修正配置方案
1. 扩展Alertmanager接收器配置
修改alertmanager.yml的配置,新增TeamA、TeamB的邮件接收器及匹配规则:
global: resolve_timeout: 1m route: group_wait: 30s group_interval: 5m repeat_interval: 2h receiver: 'email-notifications' routes: # 匹配ServerA宕机告警 - match: alertname: InstanceDown job: server_a_job # 替换为ServerA实际的Job名称 receiver: TeamA continue: true # 匹配后继续触发根路由默认组发送 # 匹配ServerB宕机告警 - match: alertname: InstanceDown job: server_b_job # 替换为ServerB实际的Job名称 receiver: TeamB continue: true receivers: - name: 'email-notifications' email_configs: - to: alertgroups@example.com from: default@example.com smarthost: smtp.gmail.com:587 auth_username: default@example.com auth_identity: default@example.com auth_password: password send_resolved: true # 新增TeamA接收器 - name: 'TeamA' email_configs: - to: user1@example.com,user2@example.com,user3@example.com from: default@example.com smarthost: smtp.gmail.com:587 auth_username: default@example.com auth_identity: default@example.com auth_password: password send_resolved: true # 新增TeamB接收器 - name: 'TeamB' email_configs: - to: user1@example.com,user2@example.com from: default@example.com smarthost: smtp.gmail.com:587 auth_username: default@example.com auth_identity: default@example.com auth_password: password send_resolved: true
如果你需要通过实例IP而非Job名称匹配,将
match段中的job字段替换为instance: 你的ServerA实例IP:端口即可。
2. 配置验证
修改完成后执行以下命令校验配置语法正确性,无报错后重启Alertmanager服务即可生效:
# 校验Alertmanager配置 amtool check-config /path/to/alertmanager.yml # 重启服务(以systemd为例) systemctl restart alertmanager
内容的提问来源于stack exchange,提问作者Pratik M
相关产品推荐
相关产品推荐

