From 290b70e4001d959df7e3ee409bf7c392119876b6 Mon Sep 17 00:00:00 2001 From: Afeef Ghannam <39904920+afeefghannam89@users.noreply.github.com> Date: Wed, 17 Aug 2022 16:00:18 +0200 Subject: [PATCH] Add object and feature for PerfdataWriter (#68) * Closes #19 * Add feature perfdata * Add documentation snippet for perfdatawriter * add tests for perfdata * added feature perfdata to readme Co-authored-by: Thilo W --- README.md | 1 + doc/features/feature-perfdata.md | 42 +++++++++++++++++++ molecule/default/converge.yml | 3 ++ .../default/tests/integration/test_icinga2.py | 13 ++++++ plugins/modules/icinga2_perfdatawriter.py | 34 +++++++++++++++ roles/icinga2/tasks/features/perfdata.yml | 12 ++++++ roles/icinga2/vars/main.yml | 1 + 7 files changed, 106 insertions(+) create mode 100644 doc/features/feature-perfdata.md create mode 100644 plugins/modules/icinga2_perfdatawriter.py create mode 100644 roles/icinga2/tasks/features/perfdata.yml diff --git a/README.md b/README.md index 7fff474b..6e674285 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ Collection to setup and manage components of the Icinga software stack. * [Feature InfluxDB2](doc/features/feature-influxdb2.md) * [Feature mainlog](doc/features/feature-mainlog.md) * [Feature notification](doc/features/feature-notification.md) + * [Feature perfdata](doc/features/feature-perfdata.md) diff --git a/doc/features/feature-perfdata.md b/doc/features/feature-perfdata.md new file mode 100644 index 00000000..077c3280 --- /dev/null +++ b/doc/features/feature-perfdata.md @@ -0,0 +1,42 @@ +## PerfdataWriter + +To enable the feature perfdata use the following block in the variable `icinga2_features`. + +**INFO** For detailed information and instructions see the Icinga 2 Docs. [Feature PerfdataWriter](https://icinga.com/docs/icinga-2/latest/doc/09-object-types/#perfdatabwriter) + +``` +icinga2_features: + - name: perfdata + host_perfdata_path: "/var/spool/icinga2/perfdata/host-perfdata" + service_perfdata_path: "/var/spool/icinga2/perfdata/service-perfdata" + host_format_template: "DATATYPE::HOSTPERFDATA" + service_format_template: "DATATYPE::SERVICEPERFDATA" + rotation_interval = 15s +``` + +### Feature variables + +* `host_perfdata_path: string` + * Path to the host performance data file. Defaults to SpoolDir + “/perfdata/host-perfdata”. + +* `service_perfdata_path: string` + * Path to the service performance data file. Defaults to SpoolDir + “/perfdata/service-perfdata”. + +* `host_temp_path: string` + * Path to the temporary host file. Defaults to SpoolDir + “/tmp/host-perfdata”. + +* `service_temp_path: string` + * Path to the temporary service file. Defaults to SpoolDir + “/tmp/service-perfdata”. + +* `host_format_template: string` + * Host Format template for the performance data file. Defaults to a template that’s suitable for use with PNP4Nagios. + +* `service_format_template: string` + * Service Format template for the performance data file. Defaults to a template that’s suitable for use with PNP4Nagios. + +* `rotation_interval: string` + * Rotation interval for the files specified in {host,service}_perfdata_path. Defaults to 30s. + +* `enable_ha: boolean` + * Enable the high availability functionality. Only valid in a cluster setup. Defaults to false. + diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index c3dfc72b..909688df 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -5,6 +5,9 @@ vars: icinga2_confd: false icinga2_features: + - name: perfdata + host_perfdata_path: "/var/spool/icinga2/perfdata/host-perfdata" + service_perfdata_path: "/var/spool/icinga2/perfdata/service-perfdata" - name: gelf host: localhost port: 12201 diff --git a/molecule/default/tests/integration/test_icinga2.py b/molecule/default/tests/integration/test_icinga2.py index 5048d1e1..e0b42a3d 100644 --- a/molecule/default/tests/integration/test_icinga2.py +++ b/molecule/default/tests/integration/test_icinga2.py @@ -168,3 +168,16 @@ def test_icinga2_feature_gelf(host): if host.system_info.distribution == 'debian': assert i2_file.user == "nagios" assert i2_file.group == "nagios" + +def test_icinga2_feature_perfdata(host): + i2_file = host.file("/etc/icinga2/features-available/perfdata.conf") + i2_link = host.file("/etc/icinga2/features-enabled/perfdata.conf") + assert i2_file.exists + assert i2_file.contains('object PerfdataWriter "perfdata" {') + assert i2_link.linked_to == "/etc/icinga2/features-available/perfdata.conf" + if host.system_info.distribution == 'centos': + assert i2_file.user == "icinga" + assert i2_file.group == "icinga" + if host.system_info.distribution == 'debian': + assert i2_file.user == "nagios" + assert i2_file.group == "nagios" diff --git a/plugins/modules/icinga2_perfdatawriter.py b/plugins/modules/icinga2_perfdatawriter.py new file mode 100644 index 00000000..52b9a98e --- /dev/null +++ b/plugins/modules/icinga2_perfdatawriter.py @@ -0,0 +1,34 @@ +#!/usr/bin/python + + +from ansible.module_utils.basic import AnsibleModule + +def main(): + module = AnsibleModule( + supports_check_mode=True, + argument_spec = dict( + state = dict(default='present', choices=['present', 'absent']), + name = dict(required=True), + order = dict(default=10, type='int'), + file = dict(default='features-available/perfdata.conf', type='str'), + host_perfdata_path = dict(type='str'), + service_perfdata_path = dict(type='str'), + host_temp_path = dict(type='str'), + service_temp_path = dict(type='str'), + host_format_template = dict(type='str'), + service_format_template = dict(type='str'), + rotation_interval = dict(type='str'), + enable_ha = dict(type='bool'), + ) + ) + + args = module.params + name = args.pop('name') + order = args.pop('order') + state = args.pop('state') + file = args.pop('file') + + module.exit_json(changed=False, args=args, name=name, order=str(order), state=state, file=file) + +if __name__ == '__main__': + main() diff --git a/roles/icinga2/tasks/features/perfdata.yml b/roles/icinga2/tasks/features/perfdata.yml new file mode 100644 index 00000000..75ad2b7a --- /dev/null +++ b/roles/icinga2/tasks/features/perfdata.yml @@ -0,0 +1,12 @@ +--- + +- name: feature perfdata PerfdataWriter object + icinga2_object: + name: perfdata + type: PerfdataWriter + file: features-available/perfdata.conf + args: "{{ icinga2_dict_features.perfdata }}" + register: result + +- set_fact: + icinga2_local_objects: "{{ icinga2_local_objects|default([]) + [result.dest] }}" diff --git a/roles/icinga2/vars/main.yml b/roles/icinga2/vars/main.yml index 0ce850d7..0d2f33d5 100644 --- a/roles/icinga2/vars/main.yml +++ b/roles/icinga2/vars/main.yml @@ -97,6 +97,7 @@ icinga2_object_types: - NotificationCommand - NotificationComponent - OpenTsdbWriter + - PerfdataWriter - ScheduledDowntime - Service - ServiceGroup