From 164ab5a269e2e516ce1216fbdca672d783f2bcac Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Sun, 15 Aug 2021 18:42:01 +0200 Subject: [PATCH] feat: add notification commands --- README.md | 29 +++++++++++++++++++++++ defaults/main.yml | 32 ++++++++++++++++++++++++++ molecule/default/converge.yml | 7 ++++++ molecule/default/tests/test_default.py | 22 +++++++++++++++--- tasks/config.yml | 13 +++++++++++ templates/commands.conf.j2 | 16 +++++++++++++ 6 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 templates/commands.conf.j2 diff --git a/README.md b/README.md index cf8a77f..dd36fd8 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,27 @@ Install and configure Icinga2 period: $service.vars.period$ ``` +- `icinga2_notification_commands` - hash with notification command configuration + +``` + mail-service-notification: + script: mail-service-notification.sh + env: + NOTIFICATIONTYPE: $notification.type$ + SERVICENAME: $service.name$ + HOSTNAME: $host.name$ + HOSTDISPLAYNAME: $host.display_name$ + HOSTADDRESS: $address$ + SERVICESTATE: $service.state$ + LONGDATETIME: $icinga.long_date_time$ + SERVICEOUTPUT: $service.output$ + NOTIFICATIONAUTHORNAME: $notification.author$ + NOTIFICATIONCOMMENT: $notification.comment$ + SERVICEDISPLAYNAME: $service.display_name$ + USEREMAIL: $user.email$ + MAILFROM: $notification_from$ +``` + - `icinga2_check_commands` - hash with check command configuration ``` @@ -186,6 +207,14 @@ Install and configure Icinga2 accept_commands: true ``` +- `icinga2_scripts` - hash with the scripts to use by icinga + +``` + log-file: | + #!/bin/bash + echo "$(date) $HOSTDISPLAYNAME" >> /tmp/notification.log +``` + ## How to use ``` diff --git a/defaults/main.yml b/defaults/main.yml index 56eedb8..483563e 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -68,6 +68,37 @@ icinga2_notifications: assign: host.vars.notification.mail groups: host.vars.notification.mail.groups users: host.vars.notification.mail.users +icinga2_notification_commands: + mail-host-notification: + script: mail-host-notification.sh + env: + NOTIFICATIONTYPE: $notification.type$ + HOSTDISPLAYNAME: $host.display_name$ + HOSTNAME: $host.name$ + HOSTADDRESS: $address$ + HOSTSTATE: $host.state$ + LONGDATETIME: $icinga.long_date_time$ + HOSTOUTPUT: $host.output$ + NOTIFICATIONAUTHORNAME: $notification.author$ + NOTIFICATIONCOMMENT: $notification.comment$ + USEREMAIL: $user.email$ + MAILFROM: $notification_from$ + mail-service-notification: + script: mail-service-notification.sh + env: + NOTIFICATIONTYPE: $notification.type$ + SERVICENAME: $service.name$ + HOSTNAME: $host.name$ + HOSTDISPLAYNAME: $host.display_name$ + HOSTADDRESS: $address$ + SERVICESTATE: $service.state$ + LONGDATETIME: $icinga.long_date_time$ + SERVICEOUTPUT: $service.output$ + NOTIFICATIONAUTHORNAME: $notification.author$ + NOTIFICATIONCOMMENT: $notification.comment$ + SERVICEDISPLAYNAME: $service.display_name$ + USEREMAIL: $user.email$ + MAILFROM: $notification_from$ icinga2_features: {} icinga2_user_groups: {} icinga2_host_groups: {} @@ -76,3 +107,4 @@ icinga2_users: {} icinga2_api_users: {} icinga2_check_commands: {} icinga2_event_commands: {} +icinga2_scripts: {} diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index e973611..5d55e65 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -21,6 +21,13 @@ assign: (host.address || host.address6) && host.vars.os == "Linux" options: event_command: ping_event + icinga2_notification_commands: + date: + script: test-notification.sh + icinga2_scripts: + test-notification.sh: | + #!/bin/bash + date >> /tmp/notification.log icinga2_hosts: localhost: template: generic-host diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py index a0d19fb..748c11f 100644 --- a/molecule/default/tests/test_default.py +++ b/molecule/default/tests/test_default.py @@ -1,4 +1,3 @@ -import os, re import testinfra.utils.ansible_runner def test_packages(host): @@ -34,6 +33,15 @@ def test_config_templates(host): assert config.contains('os = "Linux"') def test_config_commands(host): + config = host.file('/etc/icinga2/conf.d/commands.conf') + assert config.exists + assert config.is_file + assert config.user == 'root' + assert config.group == 'nagios' + assert config.mode == 0o640 + assert config.contains('NotificationCommand "date"') + +def test_config_check_commands(host): config = host.file('/etc/icinga2/conf.d/check_commands.conf') assert config.exists assert config.is_file @@ -42,7 +50,7 @@ def test_config_commands(host): assert config.mode == 0o640 assert config.contains('value = "$http_vhost$"') -def test_config_commands(host): +def test_config_database(host): config = host.file('/etc/icinga2/conf.d/ido-db.conf') assert config.exists assert config.is_file @@ -51,7 +59,7 @@ def test_config_commands(host): assert config.mode == 0o640 assert config.contains('password = "test"') -def test_config_commands(host): +def test_config_timeperiods(host): config = host.file('/etc/icinga2/conf.d/timeperiods.conf') assert config.exists assert config.is_file @@ -69,6 +77,14 @@ def test_config_notifications(host): assert config.mode == 0o640 assert config.contains('user_groups = host.vars.notification.mail.groups') +def test_script(host): + path = host.file('/etc/icinga2/scripts/test-notification.sh') + assert path.exists + assert path.is_file + assert path.user == 'root' + assert path.group == 'nagios' + assert path.mode == 0o750 + assert path.contains('date >> /tmp/notification.log') def test_service(host): service = host.service('icinga2') diff --git a/tasks/config.yml b/tasks/config.yml index 0972ff9..2bf4f12 100644 --- a/tasks/config.yml +++ b/tasks/config.yml @@ -6,6 +6,18 @@ notify: reload icinga2 tags: icinga2 +- name: copy scripts + ansible.builtin.copy: + content: '{{ item.value }}' + dest: '/etc/icinga2/scripts/{{ item.key }}' + owner: root + group: nagios + mode: 0750 + loop: '{{ icinga2_scripts|dict2items }}' + loop_control: + label: '{{ item.key }}' + tags: icinga2 + - name: copy config files ansible.builtin.template: src: '{{ item }}.conf.j2' @@ -15,6 +27,7 @@ mode: 0640 loop: - check_commands + - commands - groups - hosts - notifications diff --git a/templates/commands.conf.j2 b/templates/commands.conf.j2 new file mode 100644 index 0000000..81ad20b --- /dev/null +++ b/templates/commands.conf.j2 @@ -0,0 +1,16 @@ +# {{ ansible_managed }} +{% for name, command in icinga2_notification_commands.items() %} + +object NotificationCommand "{{ name }}" { + command = [ ConfigDir + "/scripts/{{ command.script }}" ] + +{% if command.env is defined %} + env = { +{% for env, value in command.env.items() %} + {{ env }} = {% if value is number %}{{ value }}{% else %}"{{ value }}"{% endif %} + +{% endfor %} + } +{% endif %} +} +{% endfor %}