以下是報警和相應的Alertmanager配置文件(alertmanager.yml)設置的所有不同示例。每個都使用Go模板系統。
自定義Slack通知
在此示例中,我們自定義了Slack通知,以向組織的Wiki發送有關如何處理已發送的特定警報的URL。
global:
slack_api_url: '<slack_webhook_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 }}'
訪問CommonAnnotations中的註釋
在此示例中,我們再次自定義發送到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: "<!channel> \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: '<slack_webhook_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'
此博客文章中進一步詳細解釋了此示例。