Grafana 9.xではアラートするのにContact points、Notification policies、Alert Ruleをそれぞれ設定する必要がある。
Contact Points
E-mailやSlackなどの送信先と、送信メッセージのテンプレートを設定する。
Notification Template
- 公式の説明 Create notification templates | Grafana documentation
- サンプルがいくつか紹介されているのでこれをコピペするのが便利 Grafana Alerting: A guide to templating alert notifications
テンプレートの作成時には以下のReferenceを参照するとよい。 利用できる変数が説明されている。
例
Alert
: アラートの内容やダッシュボードURLなどを持ったデータAlert.Annotations
: summaryやdescriptionなどの情報をkey-valueで保持する
{{ define "alerts.message" -}}
{{ if .Alerts.Firing -}}
{{ len .Alerts.Firing }} firing alert(s)
{{ template "alerts.summarize" .Alerts.Firing }}
{{- end }}
{{- if .Alerts.Resolved -}}
{{ len .Alerts.Resolved }} resolved alert(s)
{{ template "alerts.summarize" .Alerts.Resolved }}
{{- end }}
{{- end }}
{{ define "alerts.summarize" -}}
{{ range . -}}
- {{ index .Annotations "summary" }}
{{ end }}
{{ end }}
Alert Ruleで、Summary and annotations を設定できる項目があるので、Summaryに以下のようなメッセージを設定しておくと、次のような通知が飛ぶようになる。
Summary
The database server {{ index $labels "instance" }} has exceeded 75% of available disk space. Disk space used is {{ index $values "B" }}%, please resize the disk within the next 24 hours
1 firing alert(s)
- The database server db1 has exceeded 75% of available disk space. Disk space used is 76%, please resize the disk size within the next 24 hours
1 resolved alert(s)
- The web server web1 has been responding to 5% of HTTP requests with 5xx errors for the last 5 minutes
Notification policies
Manage notification policies | Grafana documentation
通知をAlert ruleにつけたラベルなどでグルーピングして、通知先や間隔の設定をまとめて行える。 default policyを設定しておけば何もなければそれがつかわれる。
Alerting Rule
通知する条件やメッセージを設定する。おそらく一番触る部分