Skip to content

Commit

Permalink
fix: alert mail template (#11)
Browse files Browse the repository at this point in the history
- Fix the template file name: write all custom templates into
  custom.tmpl.
- Use the template name as a prefix for the subparts definition

Refs NethServer/dev#7162
  • Loading branch information
DavidePrincipi authored Mar 5, 2025
1 parent be92fef commit e2274eb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
35 changes: 17 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,17 @@ Logs page, searching for YAML parse errors.
**This is an experimental feature, do not use in production.**
Configuration may change on the future releases.

You can change the mail template used to send alerts by creating a custom template in the `templates.d` directory.
First, create a template file, for example `myalert.tmpl`. Make sure to
define `myalert_subject` and `myalert_html` sections, as they are
used by the module to render the mail. For additional information refer to
[Alertmanager
documentation](https://prometheus.io/docs/alerting/latest/notification_examples/).

First, create a template.
Make sure to not change `custom_mail_subject` and `custom_mail_html` names, as they are used by the module to render the mail.
Execute the following commands:
```
echo '<template>' | redis-cli -x hset module/metrics1/custom_templates <template_name>
```
Example of `myalert.tmpl` contents:

Example:
```
echo '{{ define "custom_mail_subject" }}Alert on {{ range .Alerts.Firing }}{{ .Labels.instance }} {{ end }}{{ end }}
{{ define "custom_mail_html" }}
```text
{{ define "myalert_subject" }}Alert on {{ range .Alerts.Firing }}{{ .Labels.instance }} {{ end }}{{ end }}
{{ define "myalert_html" }}
<html>
<head>
<title>Alert!</title>
Expand All @@ -151,24 +149,25 @@ echo '{{ define "custom_mail_subject" }}Alert on {{ range .Alerts.Firing }}{{ .L
</p>
{{ end }}
</body></html>
{{ end }}' | redis-cli -x hset module/metrics2/custom_templates mail
{{ end }}
```

Apply the changes:
Load the template file in Redis DB:

```
runagent -m metrics1 systemctl --user restart prometheus alertmanager
redis-cli -x hset module/metrics1/custom_templates myalert <myalert.tmpl
```

Then, configure the module to use the new template:
Configure the module to use the new template:
```
api-cli run module/metrics1/configure-module --data '{"prometheus_path": "prometheus", "grafana_path": "grafana", "mail_from": "[email protected]", "mail_to": ["[email protected]"], "mail_template": "custom_mail_html"}'
api-cli run module/metrics1/configure-module --data '{"prometheus_path": "prometheus", "grafana_path": "grafana", "mail_from": "[email protected]", "mail_to": ["[email protected]"], "mail_template": "myalert"}'
```

You can test the template rendering using the following command:
```
runagent -m metrics1
podman exec -ti alertmanager amtool template render --template.glob='/etc/alertmanager/templates/*.tmpl' --template.text='{{ template "custom_mail_html" . }}'
podman exec -ti alertmanager amtool template render --template.glob='/etc/alertmanager/templates/*.tmpl' --template.text='{{ template "custom_mail_subject" . }}'
podman exec -ti alertmanager amtool template render --template.glob='/etc/alertmanager/templates/*.tmpl' --template.text='{{ template "myalert_html" . }}'
podman exec -ti alertmanager amtool template render --template.glob='/etc/alertmanager/templates/*.tmpl' --template.text='{{ template "myalert_subject" . }}'
```

## Testing
Expand Down
22 changes: 10 additions & 12 deletions imageroot/bin/reload_configuration
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,11 @@ def generate_alertmanagr_config(redis_client):
'to': config['mail_to']
}
if 'mail_template' in config and config['mail_template']:
mail_config['html'] = '{{ template "custom_mail_html" . }}'
mail_config['headers'] = { 'subject': '{{ template "custom_mail_subject" . }}' }
template_key = config['mail_template']
mail_config['html'] = '{{{{ template "{}_html" . }}}}'.format(template_key)
mail_config['headers'] = {
'subject': '{{{{ template "{}_subject" . }}}}'.format(template_key),
}
alert_config['receivers'].append({
'email_configs': [ mail_config ],
'name': 'mail-alert'
Expand Down Expand Up @@ -166,16 +169,11 @@ def generate_custom_alertmanager_rules(redis_client):
pass

def generate_custom_alertmanager_templates(redis_client):
# Search for all custom_*.yml files, delete the ones that are not in the nodes list
with os.scandir('templates.d') as it:
for entry in it:
if entry.is_file() and entry.name.startswith('custom_') and entry.name not in nodes:
os.remove(entry.path)
for key in redis_client.hgetall(f'module/{os.environ["MODULE_ID"]}/custom_templates'):
template = redis_client.hget(f'module/{os.environ["MODULE_ID"]}/custom_templates', key)
if template:
with open(f'templates.d/custom_{key}.yml', 'w') as f:
f.write(template + '\n')
# Custom template declarations are concatenated in the same file:
with open(f'templates.d/custom.tmpl', 'w') as f:
for template in redis_client.hvals(f'module/{os.environ["MODULE_ID"]}/custom_templates'):
if template:
f.write(template)

# connect to client and fetch providers for prometheus
redis_client = agent.redis_connect(use_replica=True)
Expand Down

0 comments on commit e2274eb

Please sign in to comment.