how to add custom email template?
tried to add the default.tmpl into directory /etc/alertmanager/template/ and restarted alertmanager process, but it failed with error:
time="2016-03-02T11:00:20Z" level=error msg="Loading configuration file failed: template: redefinition of template "__text_alert_list"" file="/alertmanager.conf" source="main.go:128"
the configuration as below: templates:
- '/etc/alertmanager/template/*.tmpl'
any document or guides can help me?
The default.tmpl is compiled into the binary. You cannot overwrite anything specified in there.
You have to create a new template file with custom template names and reference those in the configuration.
@fabxc it's fine now. thanks for your help
one more question, what's the configuration parameter for the email subject? i know the parameter for body is 'html".
Subject is a regular email header. So in the header map of the configuration, you can set a custom "Subject" entry.
On Thu, Mar 3, 2016 at 2:46 AM tj13 [email protected] wrote:
@fabxc https://github.com/fabxc it's fine now. thanks for your help
one more question, what's the configuration parameter for the email subject? i know the parameter for body is 'html".
— Reply to this email directly or view it on GitHub https://github.com/prometheus/alertmanager/issues/259#issuecomment-191534820 .
It seams that I can only ADD subjects, but can not overwrite or change the default subject. Is that right? @fabxc
You can change the default subject, just provide a value for that header.
I want to change the alerting mail subject to "SYSTEM ALERT". My headers configuration as below:
headers:
'Subject': 'SYSTEM ALERT'
However, the alertmanager just put "SYSTEM ALERT" ahead of its default subject. In the email, I found that alertmanager put another subject header before its default subject header. Did I misconfigure? How to make it right?
By the way, my alertmanager is v0.1.0.
#263 should fix this.
That's fixed at head now.
I just ran across this same issue. The root of this issue seems to be twofold.
-
the docs suck
-
who cares if i redefine one of your template parameters, your default template should be defaults, I should be allowed to override them all day long. Is there some issue that prevents this?
- the docs suck
noted
- who cares if i redefine one of your template parameters, your default template should be defaults, I should be allowed to override them all day long. Is there some issue that prevents
What exactly do you mean? The one regarding the subject was a bug that was fixed. Any other instances?
I have also been very confused by the docs and I'm reading source-code now to figure out how stuff actually works. For example, for someone not used to golang templating, the fact that the template-specification in the configuration does not actually refer to a file, but instead just a section, and that sections are loaded independently of the files they are in (when using *.tmpl to import) and the fact that you get errors IF you actually add and try to edit the default.tmpl makes everything extremely confusing.
A good exercise would be to document the process of adding a custom e-mail template step-by-step as if to a five-year-old. And also working examples of the corresponding config parameters (the current sample config does not show anything templating related)
Go templating certainly has an entry barrier as well but it is working really well in the way it does – plus all questions you can Google for Go templates automatically help Alertmanager users. This aspect won't change.
But the docs from our side are still lacking but we are very open to contributions in that area. Here is a basic blog post that might help a bit: https://prometheus.io/blog/2016/03/03/custom-alertmanager-templates/
Nice. Bookmarked. I might have a stab at creating a more coherent tutorial if I find the time!
That's be great, we'd be happy to accept a guest blog post.
There really should be an plain text counterpart. HTML email is unreadable. :(
http://s.natalian.org/2016-06-27/alertmanager.txt
For documentation, multipart and thus plain text is tracked in https://github.com/prometheus/alertmanager/issues/290
This is a simple example! http://tailnode.tk/2017/03/alertmanager%E9%82%AE%E4%BB%B6%E6%A8%A1%E7%89%88/
I know this is an old issue, but the documentation is still not helpful. The only section in the docs that talks about templating refers to Slack alerts. Can we have better docs around adding custom email templates?
I agree with @SleepyBrett and @talonx, but I was able to figure out how to override the subject line in my email alerts with the information in this ticket, some searching, and a lot of trial and error. Since this thread is one of the top hit when searching for how to do custom email templates, I'll document it here. Someone from the project can feel free to put this in the official docs somewhere.
The specific issue I had was that there were too many tags cluttering up the subject line and I couldn't tell what was down by looking at the inbox on my mobile device.
The location of the my templates are in /etc/alertmanager/templates/*.tmpl. This is controlled by a variable in my /etc/alertmanager/alertmanager.yml file. Here's the relevant lines from alertmanager.yml:
templates:
- '/etc/alertmanager/templates/*.tmpl'
I took the default.tmpl file and cut out the parts that were not related to email. Thank you @fabxc for mentioning this filename. The template that I started with looked like this:
{{ define "__alertmanager" }}Alertmanager{{ end }}
{{ define "__alertmanagerURL" }}{{ .ExternalURL }}/#/alerts?receiver={{ .Receiver | urlquery }}{{ end }}
{{ define "__subject" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .GroupLabels.SortedPairs.Values | join " " }} {{ if gt (len .CommonLabels) (len .GroupLabels) }}({{ with .CommonLabels.Remove .GroupLabels.Names }}{{ .Values | join " " }}{{ end }}){{ end }}{{ end }}
{{ define "__description" }}{{ end }}
{{ define "__text_alert_list" }}{{ range . }}Labels:
{{ range .Labels.SortedPairs }} - {{ .Name }} = {{ .Value }}
{{ end }}Annotations:
{{ range .Annotations.SortedPairs }} - {{ .Name }} = {{ .Value }}
{{ end }}Source: {{ .GeneratorURL }}
{{ end }}{{ end }}
I then compared the template to some actual emails that came through. It look quite a while before I figured out where each part of the email was in the template, but I eventually lined everything up.
The following is what the template looked like when I was done.
{{ define "__alertmanager" }}Alertmanager{{ end }}
{{ define "__alertmanagerURL" }}{{ .ExternalURL }}/#/alerts?receiver={{ .Receiver | urlquery }}{{ end }}
{{ define "__subject" }}[{{ .Status | title }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ if gt (len .CommonLabels) (len .GroupLabels) }}{{ with (.CommonLabels.Remove .GroupLabels.Names).Remove (stringSlice "environment" "severity") }}{{ .Values | join " " }}{{ end }}{{ end }}{{ end }}
{{ define "__description" }}{{ end }}
{{ define "__text_alert_list" }}{{ range . }}Labels:
{{ range .Labels.SortedPairs }} - {{ .Name }} = {{ .Value }}
{{ end }}Annotations:
{{ range .Annotations.SortedPairs }} - {{ .Name }} = {{ .Value }}
{{ end }}Source: {{ .GeneratorURL }}
{{ end }}{{ end }}
The syntax was difficult to understand and as a result, I don't think I can provide a good explanation, but hopefully my explanation in combination with this example will help someone grok it enough to trudge through.
The line that starts with define "__subject" is clearly the only line I actually care to modify. This line of code is pretty long. We can see there's a .Status variable that is being printed out after the open square bracket. I don't want my alertmanager shouting at me, so I changed toUpper to title. I got these function names from the documentation.
I didn't want to see the things in the subject before the parenthesis (just "InstanceDown" in my case, which is implied by the fact that I'm getting an email from alertmanager). This was just a matter or removing some of the template.
Then we get to the interesting part that took hours of trial and error. There's a KV object called CommonLabels which populates the subject with the values of these labels. However it doesn't include all the values. It removes the GroupLabels and only shows what's left over. The keys and labels are in the body of the email, which is how I knew what additional fields I wanted to filter out.
Going back to the documentation, we can see that there's a remove function, and that function takes and array of strings as an argument. We can see that there aren't any parenthesis to denote that it's a function call, just space separated arguments (much like a command line program). We can't use square brackets nor curly braces in templates, which are what are normally used to create arrays and assign values to said arrays.
However, it turns out that there's a function called stringSlice which is available in templates. This takes multiple string arguments and returns them in an array-like object which the Remove function finds agreeable. So in the end, my code take the initial list, removes the GroupLabels as the original code did, and then removes the "environment" and "severity" labels as well.
The result is that those labels no longer show up in the subject. They do still show up in the body.
The other resource I found somewhat helpful in this effort was the Go Docs on Templating.
I did not find the alertmanager docs on templates to be helpful at all. The main reasons for that are:
- it only has a Slack example, no other transports
- the section on "templates" is tiny, and doesn't explain why the filename was chosen, why we're defining slack.myorg.text, that GroupLabels is an object or how one could figure out what is in that object
If this write-up were added to that page, along with a link to the default template, it would partially close the gap. I don't know why I was changing the defintion of __subject instead of subject or Subject. Linking to the template would at least allow readers to see examples and hack them up.
The things that would still be missing would include:
- A complete list of the objects are available? We've seen CommonLabels and GroupLabels, but what else?
- A complete list of fields and functions are available in each of these objects
- A complete list of functions that are available. (e.g. stringSlice, define)
- What are the basics of templating?
- Things in double curly braces (like
{{ define "slack.myorg.text" }}) are code that will be executed - Text not in double curly braces are literal strings
- Some code will use all content from the beginning block to an
{{ end }}block. I believedefine,if, andwithare an example of this pattern defineis a function and it is passed a string a variable; it consumes the content up to the matching{{ end }}block- The values defined are used in the alertmanager code
- See the go docs on templating for information about:
- pipelining
- if/else statements
- range
- with blocks
- Things in double curly braces (like
- A link to the alertmanager code that processes these defined values and create the email (or Slack message, or wechat message, etc.)
I hope this list of things that I feel are missing from the documentation is helpful. It's completely understandable that the documentation is likely find for people GoLang developers who work with the alertmanger code on a daily basis might, and it can be hard to know what should be added that would be helpful to readers.
I want to see the documentation get better. That's why I took the time to write all of this up: so it's clear what should be added. And hey, if this write-up never makes it into the docs, at least it'll live here as a comment for the next person who DuckDuckGoes for how to make a custom email template in alertmanager. :slightly_smiling_face: