[Alertmanager] Cannot apply custom e-mail template

7,063 views
Skip to first unread message

dmitry.ryk...@gmail.com

unread,
Jul 28, 2017, 12:27:13 PM7/28/17
to Prometheus Users

Hi All,


I still cannot realize how I can apply my own custom html template for e-mail alert in alert-manager config. Currently, I receive an empty e-mail alert.


Can you help me with that?


My current config alert-manager config:


html: '{{ define "email.tmpl" }} {{ end }}
{{ template "email.tmpl" . }}'


I receive the following error although email.tmpl exists in that folder.


ERRO[16307] Notify for 1 alerts failed: Cancelling notify retry for "email" due to unrecoverable error: executing email html template: 

html/template::1:48: no such template "/etc/alertmanager/template/email.tmpl" source="dispatch.go:261"


Thanks in advance.

Chandra Sekar K R

unread,
Jul 31, 2017, 12:29:10 AM7/31/17
to Prometheus Users
Enter code here...


Hi,

There are three changes required for having email notifications sent using custom email template
1) Email Template file placed in some folder. In the below code snippet, I'm placing the email.tmpl template file under /etc/prometheus/alertmanager/template folder. A sample email.tmpl file can be sourced from github here.
2) I have renamed the above file as email.tmpl and modified the contents of email.tmpl with define {{ "email.html" }} & {{ end }} tags to the top & bottom section of email.tmpl file. Renaming is done just for better understanding & is not mandatory. If you chose to use it as is, make sure you refer the template file as *.html in your templates: section in alertmanager.yml file.
3) Modify the contents of alertmanager.yml file per below:

global:

  smtp_smarthost: 'your-smtp-server:25'

  smtp_from: 'fr...@emaildomain.com'


templates:

- /etc/prometheus/alertmanager/template/*.tmpl


route:

  receiver: default


receivers:

  - name: default

    email_configs:

    - to: 't...@emaildomain.com'

      require_tls: false

      html: '{{ template "email.html" . }}'


Hope the above helps.

Dmitry Rykov

unread,
Jul 31, 2017, 6:53:53 AM7/31/17
to Prometheus Users
Hi Chandra,

Thanks a lot for your help, but it still doesn't work although I performed all steps
you mentioned. Still I get an empty e-mail and when I open it via right-click I see
an empty email with email.txt file name on it.

It looks like a bug in alertmanager source code.


Thanks.

Chandra Sekar K R

unread,
Aug 1, 2017, 4:36:48 AM8/1/17
to Prometheus Users
I have seen this behaviour with AlertManager and the blank messages usually appear when the email template has errors in it. Can you please run the Alert Manager in DEBUG mode (using -log.level debug) and check. If the error is related to bad template, it would appear in the log messages.

Dmitry Rykov

unread,
Aug 1, 2017, 5:54:42 AM8/1/17
to Prometheus Users
Hi Chandra,

This is what I get from debug logging:

DEBU[12710] flushing [Average_CPU_Load_On_All_Cores[061858c][active]]  aggrGroup={}/{service=~"^(?:^.*$)$"}:{alertname="Average_CPU_Load_On_All_Cores"} source="dispatch.go:425"
DEBU[12710] Notify attempt 1 for "email.tmpl" failed: executing email html template: html/template::1:12: no such template "email.tmpl"  source="notify.go:585"
ERRO[12710] Error on notify: Cancelling notify retry for "email.tmpl" due to unrecoverable error: executing email html template: html/template::1:12: no such template "email.tmpl"  source="notify.go:283"

I changed this file name into email.html and nothing helps. I set access rights to the folder where it resides to 777 and set the access rights to
this file as 777 as well and nothing helps. Still I can see an empty e-mail.

I think this is a bug. The problem is that alert manager doesn't even find this template. If template would be incorrect then I would
see errors like you have a syntax error or bad character error as well. But I have never seen it which means the issue is in golang code. My template is correct and I'm able to open it in browser.
It doesn't even differ very much from a default html template only with background color and picture in it.

I spent a lot of time trying to get this working. But nothing helps. 


Thanks.

- Dmitry

Chandra Sekar K R

unread,
Aug 1, 2017, 7:23:10 AM8/1/17
to Prometheus Users
Can you paste/show the contents of your email.tmpl/email.html file and alertmanager.yml configuration ? The alert manager error says it's not able to find email template.

Dmitry Rykov

unread,
Aug 2, 2017, 6:36:12 AM8/2/17
to Prometheus Users
Yes, I have attached yml config file and html template. Please check whether everything is correct. 
alertmanager.yml
email.tmpl

Chandra Sekar K R

unread,
Aug 2, 2017, 8:30:40 AM8/2/17
to Prometheus Users
The templates: section in alertmanager.yml does not point to email.tmpl (- '/etc/alertmanager/template/.tmpl') file. Can you replace it with below line & give it a try ?

- /etc/alertmanager/template/email.tmpl

Dmitry Rykov

unread,
Aug 2, 2017, 9:33:11 AM8/2/17
to Prometheus Users
I tried so many times different configurations but no luck.

The parser throws an error message when you modify -template clause and trying to place the full file name email.tmpl:

INFO[183114] Loading configuration file                    file="./alertmanager.yml" source="main.go:221"
ERRO[183114] Loading configuration file failed: template: email.tmpl:1: unexpected <.> in define clause  file="./alertmanager.yml" source="main.go:224"

But when it's removed then everything is fine.

I will report a bug for the development team since I was researching the source code and found some bugs in it.


Thanks for your help anyway.

Dmitry Rykov

unread,
Aug 2, 2017, 10:47:20 AM8/2/17
to Prometheus Users
Raised an issue in the bugtracker - https://github.com/prometheus/alertmanager/issues/936

ASHISH CHOUDHARY

unread,
Oct 21, 2021, 10:07:00 AM10/21/21
to Prometheus Users
Hi All,

i'm getting same error as above as i'm trying to create custom template for email.
 Is there any solution available now?

ERRO[16307] Notify for 1 alerts failed: Cancelling notify retry for "email" due to unrecoverable error: executing email html template: 

html/template::1:48: no such template "/etc/alertmanager/template/email.tmpl" source="dispatch.go:261"


Thanks


Reply all
Reply to author
Forward
0 new messages