Skip to content

Instantly share code, notes, and snippets.

@Himura2la
Last active May 16, 2024 15:46
Show Gist options
  • Save Himura2la/342603218ba2ee871f7b0e35eff9eaae to your computer and use it in GitHub Desktop.
Save Himura2la/342603218ba2ee871f7b0e35eff9eaae to your computer and use it in GitHub Desktop.
How to use Grafana Alerts with the Telegram
  • Template name: telegram.message

  • Content:

    {{ define "alert_list" }}{{ range . }}{{ .Labels.alertname }}
    {{ range .Annotations.SortedPairs }}<strong>{{ .Name }}</strong>: {{ .Value }}
    {{ end }}| {{ 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 }}
    —
    {{ end }}{{ end }}
    {{ define "telegram.message" }}
    {{ if gt (len .Alerts.Firing) 0 }}<strong>FIRING!!!</strong>
    —
    {{ template "alert_list" .Alerts.Firing }}
    {{ if gt (len .Alerts.Resolved) 0 }}
    {{ end }}{{ end }}{{ if gt (len .Alerts.Resolved) 0 }}<strong>Resolved</strong>
    —
    {{ template "alert_list" .Alerts.Resolved }}
    {{ end }}{{ 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.

Also, check out this fork by @gelldur, this templeate is much more cmprehensive.

@Himura2la
Copy link
Author

@otomay, If you are not confident with the meaning of these A/B/C stuff, there's an article you should read carefully https://grafana.com/docs/grafana/next/alerting/alerting-rules/create-grafana-managed-rule/
The meaning of A0, A1, B0, B1, etc. is unique to your alert configuration, you should clearly understand what each of them mean, because Grafana does not help with this at all. As I wrote in the original gist, I tend to give my queries meaningful names: data, metric, and condition, this spares me from guessing what does all these A/B/C mean. Your case is specific for your configuration and not related to the template itself, so I'm afraid this is not the right place to seek help, sorry.

@otomay
Copy link

otomay commented Dec 5, 2022

@Himura2la Sorry and thanks for the recommendation... the 0 and 1 flags really don't matter for what I'm doing, now I realize that... I adjusted the queries according to what you put in the beginning, and the display returns the result correctly:

image

But still, the value is not displayed when the state is normal :/ Where should I ask for help?

image

I'm using the classic condition, btw... is this the problem?

@otomay
Copy link

otomay commented Dec 5, 2022

I'm using the classic condition, btw... is this the problem?

Yes, that's it. I'm lame, sorry to bother you and thanks for your patience KEKW
image

@Himura2la
Copy link
Author

@otomay yea, Grafana is quite picky about the flow described in here https://grafana.com/docs/grafana/next/alerting/alerting-rules/create-grafana-managed-rule/
And the flow itself is far not intuitive, you never figure out these details about three expressions without docs. This is a terrible design, IMHO, I hope they fix this one day...

@pigate-eu
Copy link

How to hide "summary" and "description" texts on Telegram message? Can we make a simple alert code with only the Name of Rule and a small description such as:

High Temperature
The temperature is under 30 oC

I can't figure out what to change I'm confused!

@olekgo
Copy link

olekgo commented Jan 13, 2023

image

Like this?

Noticed you are using Alert ID annotation - what does it do? Cannot find any documentation on it

@Konano
Copy link

Konano commented Mar 15, 2023

Helps a lot!
When I was learning how to write message templates, it was much better to read this template than to read the documentation directly.

@faustinoaq
Copy link

Thanks you I managed to find the soruce code and create my own custom alerts ✨

{{/* Plantilla Telegram */}}
{{ define "alertas" -}}
{{ range . }}
{{ if len .Values -}}
Values:
{{- range $refID, $value := .Values -}}
    - {{ $refID }} = {{ $value }}
{{ end -}}
{{ end -}}
{{ if len .Labels.SortedPairs }}
Labels:
{{ range .Labels.SortedPairs -}}
    - {{ .Name }} = {{ .Value }}
{{ end -}}
{{ end -}}
{{ if len .Annotations.SortedPairs }}
Annotations:
{{ range .Annotations.SortedPairs -}}
    - {{ .Name }} = {{ .Value }}
{{ end -}}
{{ end -}}
{{ end -}} {{/* range */}}
{{ end -}} {{/* define alertas */}}

{{ define "plantilla.message" }}
{{ if gt (len .Alerts.Firing) 0 }}
🚨 FIRING!!!
{{- template "alertas" .Alerts.Firing -}}
{{ end -}}
{{ if gt (len .Alerts.Resolved) 0 }}
✅ RESOLVED!
{{- template "alertas" .Alerts.Resolved -}}
{{ end -}}
<a href="192.168.0.11:3000">Dashboard</a>
{{ end }}

image

@gelldur
Copy link

gelldur commented Mar 30, 2023

Thanks for sharing!

Checkout my fork for Grafana9 update ;)
https://gist.github.com/gelldur/94b57b2fa276fe9de180378bf6855877

image

@jdnielss
Copy link

jdnielss commented Jul 4, 2023

@gelldur can you make a tutorial to how to use telegram notification template?

@elhananjair
Copy link

Hello @Himura2la
Is it possible to include the server in value parameter of solved? The empty value will make ambuigty to identify which system have been solved.
Another question is, is it possible to change summary of solved? For now in both cases (firing and solved) it uses the same summary.

@mathematicw
Copy link

Hey guys, hello @Himura2la, thank you so much. Please help a little bit. How to really use this template? How to add here the value i want to monitor? from my expression

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment