I have configured the alert as you told me and, although I already see the labels on the query result (such as host.ip or host.hostname ) i cannot use on the notification. As discussed in this post, I have configured a new annotation called AlertValues with the following content:
{{ with $values }}
{{ range $k, $v := . }}
IP: {{$v.Labels.host.ip}}
Hostname: {{$v.Labels.host.hostname}}
Alerting value: {{ $v }}
{{ end }}
{{ end }}
How can I use that variables on the notification? If i configure the template as in the post mentioned:
{{ define "myalert" }}
[{{.Status}}] {{ .Labels.alertname }}
{{ .Annotations.AlertValues }}
{{ end }}
{{ define "mymessage" }}
{{ if gt (len .Alerts.Firing) 0 }}
{{ len .Alerts.Firing }} firing:
{{ range .Alerts.Firing }} {{ template "myalert" .}} {{ end }}
{{ end }}
{{ if gt (len .Alerts.Resolved) 0 }}
{{ len .Alerts.Resolved }} resolved:
{{ range .Alerts.Resolved }} {{ template "myalert" .}} {{ end }}
{{ end }}
{{ end }}
It shows nothing.
Viewing the logs i have the following errors:
logger=ngalert t=2023-04-11T14:50:01.98+0200 lvl=eror msg="error in expanding template" name=description value="{{$v.Labels.agent.name}} CPU is overloaded!" err="error parsing template __alert_CPU Usage Alert: template: __alert_CPU Usage Alert:1: undefined variable \"$v\""
logger=ngalert t=2023-04-11T14:50:01.98+0200 lvl=eror msg="error in expanding template" name=IP value="{{ index $values.A.Labels “host.ip\" }}" err="error parsing template __alert_CPU Usage Alert: template: __alert_CPU Usage Alert:1: unrecognized character in action: U+201C '“'"
Can you help me to continue?
Thank you