- Template name:
telegram.message
- Content:
{{- /* Telegram message to use: {{ template "telegram.message2" . }} */ -}}
{{ define "__alerts_list" -}}
{{ range . }}
{{if ne (index .Labels "alertname") "" -}}
{{ if eq .Status "firing" }}π΄{{ else }}π’{{ end }}
{{- if ne (index .Labels "severity") "" -}}
<u><b>P{{ index .Labels "severity" }}</b></u> {{ end -}}
<b>{{ index .Labels "alertname" }}</b> π {{ .StartsAt.Format "15:04:05 ποΈ 2006-01-02" }}{{ end -}}
{{ if len .Annotations }}
<i>Annotations:</i>
{{ range .Annotations.SortedPairs -}}
- {{ .Name }}: {{ .Value }}
{{ end -}}
{{ end }}
{{ if len .Labels -}}
<i>Labels:</i>
{{ range .Labels.SortedPairs -}}
- {{ .Name }}: {{ .Value }}
{{ end -}}
{{ end }}
<i>Value:</i> <pre>{{ .ValueString }}</pre>
{{- if gt (len .GeneratorURL) 0 }}<a href="{{ .GeneratorURL }}">source</a> | {{ end }}
{{- if gt (len .SilenceURL) 0 }}<a href="{{ .SilenceURL }}">π silence</a> | {{ end }}
{{- if gt (len .DashboardURL) 0 }}π <a href="{{ .DashboardURL }}">dashboard</a> | {{ end }}
{{- if gt (len .PanelURL) 0 }}<a href="{{ .PanelURL }}">panel</a> {{- end -}}
<pre>--------</pre>
{{- end -}} {{- /* range */ -}}
{{- end -}} {{- /* define __alerts_list */ -}}
{{ define "__telegram.title" -}}
{{ if ne (index .CommonLabels "severity") "" }} <u><b>P{{ index .CommonLabels "severity" }}</b></u> {{ end -}}
{{ if ne (index .CommonLabels "alertname") "" -}}
[{{ index .CommonLabels "alertname" }}]
{{- end -}}
{{- end -}}{{- /* define __telegram */ -}}
{{ define "telegram.message" }}
{{ if gt (len .Alerts.Firing) 0 }}
π¨ <b>ALARM</b> (#{{ .Alerts.Firing | len }})
{{- template "__alerts_list" .Alerts.Firing }}{{ end -}}
{{ if gt (len .Alerts.Resolved) 0 }}
β
<b>RESOLVED</b>{{ template "__telegram.title" . }} (#{{ .Alerts.Resolved | len }})
{{- template "__alerts_list" .Alerts.Resolved }}{{ end }}
<a href="{{ .ExternalURL }}">π² Grafana</a>
{{- end -}}
- Contact point | Optional Telegram settings | Message: `{{ template "telegram.message" . }}`
- Add verbose annotations to your alerts. Example: `The <code>{{ $labels.host }}</code> host is low on free space! Used: {{ $values.display.Value }}%` (where `display` is one of the queries).
- I usually add the following queries: `data` (the datasource query), `metric` (the reduce expression), `condition`, and `display`, which is `round($metric)`. [Multi-dimensional rules documentation](https://grafana.com/docs/grafana/next/alerting/alerting-rules/create-grafana-managed-rule).
thanks for quick turnaround. i guess the issue is in here:
because when i put just {{ .GeneratorURL }} in the template the URL is properly printed, but when it is in the if and in a href it is not showing up at all, leading to print just "source" in the alert. is it properly working on your side? I am on grafana 10. i guess i need to play around to see how i will overcome this.
thanks again!