Home >Backend Development >Golang >How to get the hostname in the subject of Grafana 9.0 alert emails?

How to get the hostname in the subject of Grafana 9.0 alert emails?

PHPz
PHPzforward
2024-02-13 17:30:09701browse

如何获取 Grafana 9.0 警报邮件主题中的主机名?

php Xiaobian Yuzai will introduce you how to obtain the host name in the subject of the Grafana 9.0 alert email. Grafana is a powerful open source monitoring and visualization tool that can send email notifications when problems occur by setting alert rules. However, sometimes the host name in the email subject may be the key information we need. This article will explain in detail how to obtain this information to help you better monitor and troubleshoot.

Question content

I try to use alert email template in grafana 9.0. I got the hostname of the alert host in the alert email by adding {{ $values.b0.labels.host }} to the alert rule summary. If I don't use any template it works fine.

However, I am using a template as the subject of the alert email and need to access the hostname in the template.

My template looks like this:

{{ define "xxx.email.subject" }}
{{ if .Alerts.Firing -}}[{{toUpper .Status}}:{{ len .Alerts.Firing }}] {{ .CommonLabels.alertname }}
{{ end }}
{{ if .Alerts.Resolved -}}[{{ .Status}}:{{ len .Alerts.Resolved }}]
{{ end }}
{{ end }}

I tried adding {{ .commonlabels.hostname }} and other different keywords with no success. Can someone tell me how to access the hostname directly in the template?

greeting.

Solution

The problem is that my alarm is not connected to the panel correctly. If you connect alerts with a panel, you can access the host labels using {{ .CommonLabels.host }}. Of course, you should have a setup with a defined tag.

The above is the detailed content of How to get the hostname in the subject of Grafana 9.0 alert emails?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete