以下是报警和相应的Alertmanager配置文件(alertmanager.yml)设置的所有不同示例。每个都使用Go模板系统。
在此示例中,我们自定义了Slack通知,以向组织的Wiki发送有关如何处理已发送的特定警报的URL。
global:
slack_api_url: ''
route:
receiver: 'slack-notifications'
group_by: [alertname, datacenter, app]
receivers:
- name: 'slack-notifications'
slack_configs:
- channel: '#alerts'
text: 'https://internal.myorg.net/wiki/alerts/{{ .GroupLabels.app }}/{{ .GroupLabels.alertname }}'
在此示例中,我们再次自定义发送到Slack接收器的文本,以访问存储在中CommonAnnotations的由
Alertmanager发送的summary和
description数据。
警报
groups:
- name: Instances
rules:
- alert: InstanceDown
expr: up == 0
for: 5m
labels:
severity: page
# Prometheus templates apply here in the annotation and label fields of the alert.
annotations:
description: '{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes.'
summary: 'Instance {{ $labels.instance }} down'
Receiver
- name: 'team-x'
slack_configs:
- channel: '#alerts'
# Alertmanager templates apply here.
text: " \nsummary: {{ .CommonAnnotations.summary }}\ndescription: {{ .CommonAnnotations.description }}"
最后,假设警报与前面的示例相同,我们将接收器自定义为覆盖从Alertmanager收到的所有警报,并在新行上打印它们各自的注释摘要和描述。
接收者
- name: 'default-receiver'
slack_configs:
- channel: '#alerts'
title: "{{ range .Alerts }}{{ .Annotations.summary }}\n{{ end }}"
text: "{{ range .Alerts }}{{ .Annotations.description }}\n{{ end }}"
回到我们的第一个示例,我们还可以提供一个包含命名模板的文件,然后由Alertmanager加载该模板,以避免跨越多行的复杂模板。在下面创建一个文件,/alertmanager/template/myorg.tmpl
并在其中创建一个名为“ slack.myorg.txt”的模板:
{{ define "slack.myorg.text" }}https://internal.myorg.net/wiki/alerts/{{ .GroupLabels.app }}/{{ .GroupLabels.alertname }}{{ end}}
现在,配置将使用给定名称的“文本”字段加载模板,并且我们提供了自定义模板文件的路径:
global:
slack_api_url: ''
route:
receiver: 'slack-notifications'
group_by: [alertname, datacenter, app]
receivers:
- name: 'slack-notifications'
slack_configs:
- channel: '#alerts'
text: '{{ template "slack.myorg.text" . }}'
templates:
- '/etc/alertmanager/templates/myorg.tmpl'
此博客文章中进一步详细解释了此示例。