Skip to content

Instantly share code, notes, and snippets.

@gelldur
Forked from Himura2la/Grafana Alert Template.md
Last active October 24, 2024 10:31
Show Gist options
  • Save gelldur/94b57b2fa276fe9de180378bf6855877 to your computer and use it in GitHub Desktop.
Save gelldur/94b57b2fa276fe9de180378bf6855877 to your computer and use it in GitHub Desktop.
How to use Grafana Alerts with the Telegram
  • 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).



@gelldur
Copy link
Author

gelldur commented Apr 30, 2024

@arturucraft I'm using v9.2.20. Even so In this version I don't have option "parse mode". No such option in contact points or templates.

@arturucraft
Copy link

@arturucraft I'm using v9.2.20. Even so In this version I don't have option "parse mode". No such option in contact points or templates.

ok, thanks! i am struggling with this till now. i have opened a bug, to see why my html content is not normally rendering

@gelldur
Copy link
Author

gelldur commented May 3, 2024

@arturucraft just had a thought. Maybe your message is too long and thats why telegram not parsing it as full html? Try sending only single link message to confirm.

@arturucraft
Copy link

i think that's not the problem, because even i add another html code, it parses/renders it as expected, when i put {{ .DashboardURL }} it displaysthe variable correctly, if i put <a href="http://example.com">aaaa</a> it renders it properly, the problem is only in combination of a href and {{ .DashboardURL }} where it does not render <a href="{{ .DashboardURL }}"> πŸ“ dashboard</a>

@gelldur
Copy link
Author

gelldur commented May 4, 2024

Last think I could suggest remove emoji? Other things to test:

<a href="{{ .DashboardURL }}">{{ .DashboardURL }}<\a>

As I understand this variable is working for you only not rendering(you can't click on it).
Maybe you will find something in docs: https://grafana.com/docs/grafana/latest/alerting/manage-notifications/template-notifications/reference/

Best luck.

@arturucraft
Copy link

Last think I could suggest remove emoji? Other things to test:

<a href="{{ .DashboardURL }}">{{ .DashboardURL }}<\a>

As I understand this variable is working for you only not rendering(you can't click on it). Maybe you will find something in docs: https://grafana.com/docs/grafana/latest/alerting/manage-notifications/template-notifications/reference/

Best luck.

thanks again!

i have two other lines which do not contain emojies, but still does not work as expected:

    {{- if gt (len .GeneratorURL) 0 }} <a href="{{ .GeneratorURL }}"> source </a>  |  {{ end }}
    {{- if gt (len .PanelURL) 0 }} <a href="{{ .PanelURL }}"> panel </a> {{- end -}}

so i still believe it is smth related to combination of those two, or i am missing something, not sure what :(

@Gagi2k
Copy link

Gagi2k commented Oct 11, 2024

Hi,

i had the same problem and yesterday i was able to resolve the issue and the links are finally working.

I'm using the docker container and after i set the following env variable it started to work:

GF_SERVER_ROOT_URL=https://my-domain.com

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