Prometheus學習系列(三十九)之報警模板例子

如下是警報和相應的Alertmanager配置文件設置(alertmanager.yml)的全部不一樣示例。 每一個都使用Go模板系統。git

1、自定義Slack通知

在這個例子中,咱們定製了Slack通知,以便向咱們組織的wiki發送一個URL,告知如何處理已發送的特定警報。github

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 }}'
複製代碼
2、訪問CommonAnnotations中的註釋

在這個例子中,咱們再次定製發送給Slack接收器的文本,訪問存儲在Alertmanager發送的數據的CommonAnnotations中的摘要和描述。web

警報api

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'
複製代碼

接收器bash

- name: 'team-x'
  slack_configs:
  - channel: '#alerts'
    # Alertmanager templates apply here.
    text: "<!channel> \nsummary: {{ .CommonAnnotations.summary }}\ndescription: {{ .CommonAnnotations.description }}"
複製代碼
3、範圍內全部收到的警報

最後,假設與前一個示例相同的警報,咱們定製咱們的接收器以覆蓋從Alertmanager接收的全部警報,在新線路上打印它們各自的註釋摘要和描述。app

接收器url

- name: 'default-receiver'
  slack_configs:
  - channel: '#alerts'
    title: "{{ range .Alerts }}{{ .Annotations.summary }}\n{{ end }}"
    text: "{{ range .Alerts }}{{ .Annotations.description }}\n{{ end }}"
複製代碼
4、定義可重用模板

回到咱們的第一個例子,咱們還能夠提供一個包含命名模板的文件,而後由Alertmanager加載,以免跨越多行的複雜模板。 在/alertmanager/template/myorg.tmpl下建立一個文件,並在其中建立一個名爲「slack.myorg.txt」的模板:spa

{{ define "slack.myorg.text" }}https://internal.myorg.net/wiki/alerts/{{ .GroupLabels.app }}/{{ .GroupLabels.alertname }}{{ end}}
複製代碼

配置如今加載具備「text」字段的給定名稱的模板,並提供自定義模板文件的路徑:.net

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'
複製代碼

博客文章中進一步詳細說明了此示例。code

5、連接

Prometheus官網地址:prometheus.io/ 個人Github:github.com/Alrights/pr…

相關文章
相關標籤/搜索