mirror of https://github.com/grafana/grafana
prometheushacktoberfestmetricsmonitoringalertinggrafanagoinfluxdbmysqlpostgresanalyticsdata-visualizationdashboardbusiness-intelligenceelasticsearch
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
297 lines
9.3 KiB
297 lines
9.3 KiB
|
3 years ago
|
---
|
||
|
2 years ago
|
aliases:
|
||
|
|
- ../../manage-notifications/template-notifications/create-notification-templates/ # /docs/grafana/latest/alerting/manage-notifications/template-notifications/create-notification-templates/
|
||
|
|
canonical: https://grafana.com/docs/grafana/latest/alerting/configure-notifications/template-notifications/create-notification-templates/
|
||
|
2 years ago
|
description: Create notification templates to sent to your contact points
|
||
|
3 years ago
|
keywords:
|
||
|
|
- grafana
|
||
|
|
- alerting
|
||
|
|
- notifications
|
||
|
|
- templates
|
||
|
|
- create templates
|
||
|
|
- edit templates
|
||
|
|
- delete templates
|
||
|
2 years ago
|
labels:
|
||
|
|
products:
|
||
|
|
- cloud
|
||
|
|
- enterprise
|
||
|
|
- oss
|
||
|
3 years ago
|
title: Create notification templates
|
||
|
3 years ago
|
weight: 200
|
||
|
3 years ago
|
---
|
||
|
|
|
||
|
3 years ago
|
# Create notification templates
|
||
|
3 years ago
|
|
||
|
3 years ago
|
Create reusable notification templates to send to your contact points.
|
||
|
3 years ago
|
|
||
|
3 years ago
|
You can add one or more templates to your notification template.
|
||
|
3 years ago
|
|
||
|
3 years ago
|
Your notification template name must be unique. You cannot have two templates with the same name in the same notification template or in different notification templates. Avoid defining templates with the same name as default templates, such as: `__subject`, `__text_values_list`, `__text_alert_list`, `default.title` and `default.message`.
|
||
|
3 years ago
|
|
||
|
2 years ago
|
To create a notification template, complete the following steps.
|
||
|
3 years ago
|
|
||
|
2 years ago
|
1. Click **Alerts & IRM** -> **Contact points**.
|
||
|
|
1. Click the **Notification Templates** tab and then **+ Add notification template**.
|
||
|
3 years ago
|
|
||
|
2 years ago
|
1. Enter a name for the notification template.
|
||
|
3 years ago
|
|
||
|
2 years ago
|
1. Write the content of the template in the content field.
|
||
|
3 years ago
|
|
||
|
2 years ago
|
1. Save your changes.
|
||
|
3 years ago
|
|
||
|
|
`{{ define "email.subject" }}` and `{{ end }}` is automatically added to the start and end of the content:
|
||
|
|
|
||
|
3 years ago
|
To create a notification template that contains more than one template:
|
||
|
3 years ago
|
|
||
|
2 years ago
|
1. Click **+ Add notification template**.
|
||
|
3 years ago
|
|
||
|
3 years ago
|
2. Enter a name for the notification template.
|
||
|
3 years ago
|
|
||
|
|
3. Write each template in the Content field, including `{{ define "name-of-template" }}` and `{{ end }}` at the start and end of each template.
|
||
|
|
|
||
|
2 years ago
|
4. Save your changes.
|
||
|
3 years ago
|
|
||
|
|
## Preview notification templates
|
||
|
|
|
||
|
|
Preview how your notification templates will look before using them in your contact points, helping you understand the result of the template you are creating as well as enabling you to fix any errors before saving it.
|
||
|
|
|
||
|
|
**Note:** This feature is only for Grafana Alertmanager.
|
||
|
|
|
||
|
|
To preview your notification templates:
|
||
|
|
|
||
|
2 years ago
|
1. Navigate to **Alerts&IRM** -> **Alerting** -> **Contact points** -> **Notification Templates**.
|
||
|
|
1. Click **+ Add notification template** or edit an existing template.
|
||
|
3 years ago
|
1. Add or update your template content.
|
||
|
|
|
||
|
2 years ago
|
Default data is provided and you can add or edit alert data to it as well as alert instances. You can add alert data directly in the Payload data window itself or click **Select alert instances** or **Add custom alerts**.
|
||
|
3 years ago
|
|
||
|
|
1. [Optional] To add alert data from existing alert instances:
|
||
|
|
|
||
|
2 years ago
|
a. Click **Select alert instances**.
|
||
|
3 years ago
|
|
||
|
|
b. Hover over the alert instances to view more information on each alert instance.
|
||
|
|
|
||
|
|
c. Click **Confirm** to add the alert instance(s) to the payload.
|
||
|
|
|
||
|
2 years ago
|
1. [Optional] To add alert data using the Alert data editor, click **Add custom data:**
|
||
|
3 years ago
|
|
||
|
|
a. Add annotations, custom labels and/or set a dashboard or a panel.
|
||
|
|
|
||
|
|
b. Toggle Firing/resolved depending on whether you want to add firing or resolved alerts to your notification.
|
||
|
|
|
||
|
2 years ago
|
c. Click **Add alert data**.
|
||
|
3 years ago
|
|
||
|
|
d. Click **Refresh preview** to see what your template content will look like and the corresponding payload data.
|
||
|
|
|
||
|
|
If there are any errors in your template, they are displayed in the Preview and you can correct them before saving.
|
||
|
|
|
||
|
2 years ago
|
1. Save your changes.
|
||
|
3 years ago
|
|
||
|
|
## Template the subject of an email
|
||
|
|
|
||
|
|
Template the subject of an email to contain the number of firing and resolved alerts:
|
||
|
|
|
||
|
|
```
|
||
|
|
1 firing alert(s), 0 resolved alerts(s)
|
||
|
|
```
|
||
|
|
|
||
|
|
1. Create a template called `email.subject` with the following content:
|
||
|
|
|
||
|
3 years ago
|
```
|
||
|
|
{{ define "email.subject" }}
|
||
|
|
{{ len .Alerts.Firing }} firing alert(s), {{ len .Alerts.Resolved }} resolved alert(s)
|
||
|
|
{{ end }}
|
||
|
|
```
|
||
|
3 years ago
|
|
||
|
|
2. Execute the template from the subject field in your contact point integration:
|
||
|
|
|
||
|
3 years ago
|
```
|
||
|
|
{{ template "email.subject" . }}
|
||
|
|
```
|
||
|
3 years ago
|
|
||
|
|
## Template the message of an email
|
||
|
|
|
||
|
|
Template the message of an email to contain a summary of all firing and resolved alerts:
|
||
|
|
|
||
|
|
```
|
||
|
|
There are 2 firing alert(s), and 1 resolved alert(s)
|
||
|
|
|
||
|
|
Firing alerts:
|
||
|
|
|
||
|
|
- alertname=Test 1 grafana_folder=GrafanaCloud has value(s) B=1
|
||
|
|
- alertname=Test 2 grafana_folder=GrafanaCloud has value(s) B=2
|
||
|
|
|
||
|
|
Resolved alerts:
|
||
|
|
|
||
|
|
- alertname=Test 3 grafana_folder=GrafanaCloud has value(s) B=0
|
||
|
|
```
|
||
|
|
|
||
|
3 years ago
|
1. Create a notification template called `email` with two templates in the content: `email.message_alert` and `email.message`.
|
||
|
3 years ago
|
|
||
|
|
The `email.message_alert` template is used to print the labels and values for each firing and resolved alert while the `email.message` template contains the structure of the email.
|
||
|
|
|
||
|
3 years ago
|
```
|
||
|
|
{{- define "email.message_alert" -}}
|
||
|
|
{{- range .Labels.SortedPairs }}{{ .Name }}={{ .Value }} {{ end }} has value(s)
|
||
|
|
{{- range $k, $v := .Values }} {{ $k }}={{ $v }}{{ end }}
|
||
|
|
{{- end -}}
|
||
|
3 years ago
|
|
||
|
3 years ago
|
{{ define "email.message" }}
|
||
|
|
There are {{ len .Alerts.Firing }} firing alert(s), and {{ len .Alerts.Resolved }} resolved alert(s)
|
||
|
3 years ago
|
|
||
|
3 years ago
|
{{ if .Alerts.Firing -}}
|
||
|
|
Firing alerts:
|
||
|
|
{{- range .Alerts.Firing }}
|
||
|
|
- {{ template "email.message_alert" . }}
|
||
|
|
{{- end }}
|
||
|
|
{{- end }}
|
||
|
3 years ago
|
|
||
|
3 years ago
|
{{ if .Alerts.Resolved -}}
|
||
|
|
Resolved alerts:
|
||
|
|
{{- range .Alerts.Resolved }}
|
||
|
|
- {{ template "email.message_alert" . }}
|
||
|
|
{{- end }}
|
||
|
|
{{- end }}
|
||
|
3 years ago
|
|
||
|
3 years ago
|
{{ end }}
|
||
|
|
```
|
||
|
3 years ago
|
|
||
|
|
2. Execute the template from the message field in your contact point integration:
|
||
|
|
|
||
|
3 years ago
|
```
|
||
|
|
{{ template "email.message" . }}
|
||
|
|
```
|
||
|
3 years ago
|
|
||
|
|
## Template the title of a Slack message
|
||
|
|
|
||
|
|
Template the title of a Slack message to contain the number of firing and resolved alerts:
|
||
|
|
|
||
|
|
```
|
||
|
|
1 firing alert(s), 0 resolved alerts(s)
|
||
|
|
```
|
||
|
|
|
||
|
|
1. Create a template called `slack.title` with the following content:
|
||
|
|
|
||
|
3 years ago
|
```
|
||
|
|
{{ define "slack.title" }}
|
||
|
|
{{ len .Alerts.Firing }} firing alert(s), {{ len .Alerts.Resolved }} resolved alert(s)
|
||
|
|
{{ end }}
|
||
|
|
```
|
||
|
3 years ago
|
|
||
|
|
2. Execute the template from the title field in your contact point integration:
|
||
|
|
|
||
|
3 years ago
|
```
|
||
|
|
{{ template "slack.title" . }}
|
||
|
|
```
|
||
|
3 years ago
|
|
||
|
|
## Template the content of a Slack message
|
||
|
|
|
||
|
3 years ago
|
Template the content of a Slack message to contain a description of all firing and resolved alerts, including their labels, annotations, Silence URL and Dashboard URL.
|
||
|
|
|
||
|
|
**Note:**
|
||
|
|
|
||
|
|
This template is for Grafana-managed alerts only.
|
||
|
|
To use the template for Grafana Mimir/Loki-managed alerts, delete the references to DashboardURL and SilenceURL.
|
||
|
|
For more information, see the [Prometheus documentation on notifications](https://prometheus.io/docs/alerting/latest/notifications/).
|
||
|
3 years ago
|
|
||
|
|
```
|
||
|
|
1 firing alert(s):
|
||
|
|
|
||
|
|
[firing] Test1
|
||
|
|
Labels:
|
||
|
|
- alertname: Test1
|
||
|
|
- grafana_folder: GrafanaCloud
|
||
|
|
Annotations:
|
||
|
|
- description: This is a test alert
|
||
|
|
Silence: https://example.com/alerting/silence/new?alertmanager=grafana&matcher=alertname%3DTest1&matcher=grafana_folder%3DGrafanaCloud
|
||
|
|
Go to dashboard: https://example.com/d/dlhdLqF4z?orgId=1
|
||
|
|
|
||
|
|
1 resolved alert(s):
|
||
|
|
|
||
|
|
[firing] Test2
|
||
|
|
Labels:
|
||
|
|
- alertname: Test2
|
||
|
|
- grafana_folder: GrafanaCloud
|
||
|
|
Annotations:
|
||
|
|
- description: This is another test alert
|
||
|
|
Silence: https://example.com/alerting/silence/new?alertmanager=grafana&matcher=alertname%3DTest2&matcher=grafana_folder%3DGrafanaCloud
|
||
|
|
Go to dashboard: https://example.com/d/dlhdLqF4z?orgId=1
|
||
|
|
```
|
||
|
|
|
||
|
|
1. Create a template called `slack` with two templates in the content: `slack.print_alert` and `slack.message`.
|
||
|
|
|
||
|
|
The `slack.print_alert` template is used to print the labels, annotations, SilenceURL and DashboardURL while the `slack.message` template contains the structure of the notification.
|
||
|
|
|
||
|
3 years ago
|
```
|
||
|
|
{{ define "slack.print_alert" -}}
|
||
|
|
[{{.Status}}] {{ .Labels.alertname }}
|
||
|
|
Labels:
|
||
|
|
{{ range .Labels.SortedPairs -}}
|
||
|
|
- {{ .Name }}: {{ .Value }}
|
||
|
|
{{ end -}}
|
||
|
|
{{ if .Annotations -}}
|
||
|
|
Annotations:
|
||
|
|
{{ range .Annotations.SortedPairs -}}
|
||
|
|
- {{ .Name }}: {{ .Value }}
|
||
|
|
{{ end -}}
|
||
|
|
{{ end -}}
|
||
|
|
{{ if .SilenceURL -}}
|
||
|
|
Silence: {{ .SilenceURL }}
|
||
|
|
{{ end -}}
|
||
|
|
{{ if .DashboardURL -}}
|
||
|
|
Go to dashboard: {{ .DashboardURL }}
|
||
|
|
{{- end }}
|
||
|
|
{{- end }}
|
||
|
|
|
||
|
|
{{ define "slack.message" -}}
|
||
|
|
{{ if .Alerts.Firing -}}
|
||
|
|
{{ len .Alerts.Firing }} firing alert(s):
|
||
|
|
{{ range .Alerts.Firing }}
|
||
|
|
{{ template "slack.print_alert" . }}
|
||
|
|
{{ end -}}
|
||
|
|
{{ end }}
|
||
|
|
{{ if .Alerts.Resolved -}}
|
||
|
|
{{ len .Alerts.Resolved }} resolved alert(s):
|
||
|
|
{{ range .Alerts.Resolved }}
|
||
|
|
{{ template "slack.print_alert" .}}
|
||
|
|
{{ end -}}
|
||
|
|
{{ end }}
|
||
|
|
{{- end }}
|
||
|
|
```
|
||
|
3 years ago
|
|
||
|
|
2. Execute the template from the text body field in your contact point integration:
|
||
|
|
|
||
|
3 years ago
|
```
|
||
|
|
{{ template "slack.message" . }}
|
||
|
|
```
|
||
|
3 years ago
|
|
||
|
|
## Template both email and Slack with shared templates
|
||
|
|
|
||
|
3 years ago
|
Instead of creating separate notification templates for email and Slack, you can share the same template.
|
||
|
3 years ago
|
|
||
|
|
For example, if you want to send an email with this subject and Slack message with this title:
|
||
|
|
|
||
|
|
```
|
||
|
|
1 firing alert(s), 0 resolved alerts(s)
|
||
|
|
```
|
||
|
|
|
||
|
|
1. Create a template called `common.subject_title` with the following content:
|
||
|
|
|
||
|
3 years ago
|
```
|
||
|
|
{{ define "common.subject_title" }}
|
||
|
|
{{ len .Alerts.Firing }} firing alert(s), {{ len .Alerts.Resolved }} resolved alert(s)
|
||
|
|
{{ end }}
|
||
|
|
```
|
||
|
3 years ago
|
|
||
|
|
2. For email, execute the template from the subject field in your email contact point integration:
|
||
|
|
|
||
|
3 years ago
|
```
|
||
|
|
{{ template "common.subject_title" . }}
|
||
|
|
```
|
||
|
3 years ago
|
|
||
|
|
3. For Slack, execute the template from the title field in your Slack contact point integration:
|
||
|
|
|
||
|
3 years ago
|
```
|
||
|
|
{{ template "common.subject_title" . }}
|
||
|
|
```
|