From 7a6687469d8ba6a6dbbd8bb12fe546be226c0a88 Mon Sep 17 00:00:00 2001 From: Anton Iakimov Date: Tue, 26 Mar 2024 18:27:03 +0100 Subject: [PATCH] add general custom receivers and template --- defaults/main.yml | 11 +++++++---- files/custom.tmpl | 15 +++++++++++++++ tasks/config.yml | 10 +++++++++- templates/alertmanager.yml.j2 | 20 ++++++++++++-------- templates/docker-compose.yml.j2 | 1 + 5 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 files/custom.tmpl diff --git a/defaults/main.yml b/defaults/main.yml index d21418c..162111c 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -29,11 +29,14 @@ alertmanager_probe_interval: '5s' #alertmanager_smtp_from: ~ #alertmanager_smtp_user: ~ #alertmanager_smtp_pass: ~ -alertmanager_smtp_receivers_extra: [] +alertmanager_receivers_extra: [] # - name: 'devops' -# email_configs: -# - to: 'devops@example.org' -# send_resolved: true +# routes: +# continue: true +# receivers: +# email_configs: +# - to: 'devops@example.org' +# send_resolved: true # OAuth alertmanager_oauth_compose_file: '{{ alertmanager_service_path }}/docker-compose.oauth.yml' diff --git a/files/custom.tmpl b/files/custom.tmpl new file mode 100644 index 0000000..c1f9c13 --- /dev/null +++ b/files/custom.tmpl @@ -0,0 +1,15 @@ +{{ define "discord.custom.title" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .GroupLabels.SortedPairs.Values | join " " }}{{ end }} + +{{ define "__text_alert_description_list" }}{{ range . }}- [{{ .Annotations.description }}]({{ .GeneratorURL }}) +{{ end }}{{ end }} + +{{ define "discord.custom.message" }} +{{ if gt (len .Alerts.Firing) 0 }} +Alerts Firing: +{{ template "__text_alert_description_list" .Alerts.Firing }} +{{ end }} +{{ if gt (len .Alerts.Resolved) 0 }} +Alerts Resolved: +{{ template "__text_alert_description_list" .Alerts.Resolved }} +{{ end }} +{{ end }} diff --git a/tasks/config.yml b/tasks/config.yml index baca6d0..43ce1bd 100644 --- a/tasks/config.yml +++ b/tasks/config.yml @@ -9,7 +9,7 @@ mode: 0750 with_items: - 'data' - - 'conf' + - 'conf/templates' - name: Create config files template: @@ -22,3 +22,11 @@ with_items: - 'alertmanager.yml' - 'amtool.yml' + +- name: Create template file + copy: + src: 'custom.tmpl' + dest: '{{ alertmanager_service_path }}/conf/templates/custom.tmpl' + owner: 'dockremap' + group: 'docker' + mode: 0640 diff --git a/templates/alertmanager.yml.j2 b/templates/alertmanager.yml.j2 index a0ed643..dc41ff7 100644 --- a/templates/alertmanager.yml.j2 +++ b/templates/alertmanager.yml.j2 @@ -23,14 +23,15 @@ route: repeat_interval: '{{ alertmanager_repeat_interval | mandatory }}' routes: +{% for extra_receiver in alertmanager_receivers_extra %} +{% filter indent(width=6) %} + - {{ {'receiver': extra_receiver.name} | combine(extra_receiver.routes) | to_nice_yaml(sort_keys=false) }} +{% endfilter %} +{% endfor %} {% if alertmanager_victorops_enabled %} - receiver: 'victorops-alerts' continue: true {% endif %} -{% for extra_receiver in alertmanager_smtp_receivers_extra %} - - receiver: '{{ extra_receiver.name }}' - continue: true -{% endfor %} receivers: - name: 'admin-email' @@ -47,9 +48,12 @@ receivers: entity_display_name: '{{ alertmanager_victorops_display_name | mandatory | trim }}' state_message: '{{ alertmanager_victorops_state_message | mandatory | trim }}' {% endif %} -{% if alertmanager_smtp_receivers_extra|length > 0 %} -{% filter indent(width=2) %} -{{ alertmanager_smtp_receivers_extra | to_nice_yaml(indent=2) | trim }} +{% for extra_receiver in alertmanager_receivers_extra %} +{% filter indent(width=4) %} + - {{ {'name': extra_receiver.name} | combine(extra_receiver.receivers) | to_nice_yaml(sort_keys=false, indent=1) }} {% endfilter %} -{% endif %} +{% endfor %} + +templates: + - '/etc/alertmanager/templates/custom.tmpl' diff --git a/templates/docker-compose.yml.j2 b/templates/docker-compose.yml.j2 index fbdce41..f660dd9 100644 --- a/templates/docker-compose.yml.j2 +++ b/templates/docker-compose.yml.j2 @@ -13,6 +13,7 @@ services: volumes: - '{{ alertmanager_service_path }}/conf/alertmanager.yml:/etc/alertmanager.yml:ro' - '{{ alertmanager_service_path }}/conf/amtool.yml:/etc/amtool/config.yml:ro' + - '{{ alertmanager_service_path }}/conf/templates:/etc/alertmanager/templates:ro' - '{{ alertmanager_service_path }}/data:/data' - '/certs:/certs' command: