feat: add notification commands

This commit is contained in:
Adrien Waksberg 2021-08-15 18:42:01 +02:00 committed by Adrien Waksberg
parent 123156f412
commit 164ab5a269
6 changed files with 116 additions and 3 deletions

View file

@ -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
```

View file

@ -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: {}

View file

@ -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

View file

@ -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')

View file

@ -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

View file

@ -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 %}