Slack alert notification in grafana and based on the query value B

Hi, I tried to build an slack alert notification in grafana and based on the query value B and set a different description in annotation. I used the { if $values.B X} but it does not achieved what I want. I have one of the alert rule below and if anyone can help to solve it?

{{ define “slack.print_alert” -}}
{{ if .Value.B 2 }}
One of the Server Offline
{{ else if .Value.B 22 }}
All the Servers in the DataCenter Offline!
{{ else }}
There are unknown alerts
{{ end }}
{{- end }}

Hi! :wave: Is it possible you forgot the eq operator?

{{ if eq .Values.B 2.0 }}

1 Like

The syntax you are using in your template for the Slack alert notification in Grafana is not correct.

{{ define "slack.print_alert" -}}
{{ if eq .Values.B 2.0 }}
One of the Servers is Offline
{{ else if eq .Values.B 22.0 }}
All the Servers in the DataCenter are Offline!
{{ else }}
There are unknown alerts
{{ end }}
{{- end }}

In the corrected template, the eq function is used to compare the value of .Values.B with the specified values (2 and 22).

:writing_hand: Update based on @georgerobinson advise.

A couple of corrections on your otherwise great answer!

  1. .Value needs to be .Values
  2. 2 and 22 need to be 2.0 and 22.0 because Go templates won’t allow comparing a float to an int

Thanks for the great help!! The alert notification work perfectly!

Thanks. It worked and now I have a better understanding on the Go Template language.