在alertmanager.yml配置文件中添加以下代码片段:
route:
receiver: 'null'
routes:
- match_re:
severity: 'warning|critical'
receiver: 'notify'
- match_re:
severity: 'resolved'
receiver: 'notify-resolved'
receivers:
- name: 'notify'
webhook_configs:
- url: 'http://your-webhook-url.com'
send_resolved: true
- name: 'notify-resolved'
webhook_configs:
- url: 'http://your-webhook-url.com'
send_resolved: true
该代码片段定义了两个接收器(receiver):notify
和notify-resolved
。第一个接收器会在接收到severity为warning或critical的告警时发送通知,第二个接收器则会在接收到severity为resolved的告警时发送通知。这里的send_resolved
选项被设置为true
,可以保证在告警被标记为resolved时,Alertmanager也会发送相应的通知。
接着,你需要在Prometheus的配置文件中指定Alertmanager的URL和该配置文件的位置。在Prometheus的配置文件(prometheus.yml)中添加以下代码片段:
alerting:
alertmanagers:
- static_configs:
- targets: ['localhost:9093']
rule_files:
- /path/to/alert.rules
这样就可以启用Alertmanager,并且在告警创建和解除时都能够发送相应的通知了。