From c64a0770e1e2ff8a01f34379c3acaa2987181c1e Mon Sep 17 00:00:00 2001 From: Donien <88634789+Donien@users.noreply.github.com> Date: Fri, 15 Nov 2024 14:03:13 +0100 Subject: [PATCH] Add object and feature for CompatLogger --- .../fragments/feature_add_compatlogger.yml | 3 + doc/role-icinga2/features.md | 1 + .../features/feature-compatlog.md | 22 +++ doc/role-icinga2/objects.md | 12 ++ plugins/modules/icinga2_compatlogger.py | 128 ++++++++++++++++++ roles/icinga2/tasks/features/compatlog.yml | 12 ++ roles/icinga2/vars/main.yml | 1 + 7 files changed, 179 insertions(+) create mode 100644 changelogs/fragments/feature_add_compatlogger.yml create mode 100644 doc/role-icinga2/features/feature-compatlog.md create mode 100644 plugins/modules/icinga2_compatlogger.py create mode 100644 roles/icinga2/tasks/features/compatlog.yml diff --git a/changelogs/fragments/feature_add_compatlogger.yml b/changelogs/fragments/feature_add_compatlogger.yml new file mode 100644 index 00000000..5afba355 --- /dev/null +++ b/changelogs/fragments/feature_add_compatlogger.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - Add object :code:`CompatLogger` and feature :code:`compatlog`. diff --git a/doc/role-icinga2/features.md b/doc/role-icinga2/features.md index 21ce5425..c3d4e063 100644 --- a/doc/role-icinga2/features.md +++ b/doc/role-icinga2/features.md @@ -11,6 +11,7 @@ Current supported features: * [Feature API](features/feature-api.md) * [Feature Command](features/feature-command.md) +* [Feature CompatLog](features/feature-compatlog.md) * [Feature ElasticSearch](features/feature-elasticsearch.md) * [Feature GelfWriter](features/feature-gelf.md) * [Feature Graphite](features/feature-graphite.md) diff --git a/doc/role-icinga2/features/feature-compatlog.md b/doc/role-icinga2/features/feature-compatlog.md new file mode 100644 index 00000000..dec8fbb6 --- /dev/null +++ b/doc/role-icinga2/features/feature-compatlog.md @@ -0,0 +1,22 @@ +## CompatLog + +To enable the feature compatlog use the following block in the variable `icinga2_features`. + +**INFO** For detailed information and instructions see the Icinga 2 Docs. [Feature CompatLog](https://icinga.com/docs/icinga-2/latest/doc/09-object-types/#compatlogger) + +``` +icinga2_features: + - name: compatlog + state: present + log_dir: "LogDir + /compat" + rotation_method: "monthly" +``` + +### Feature variables + +* `state: string` + * Enable or disable feature. Options: present, absent +* `log_dir: string` + * Set the log directory. +* `rotation_method: string` + * Set the log rotation interval. Options: hourly, daily, weekly, monthly diff --git a/doc/role-icinga2/objects.md b/doc/role-icinga2/objects.md index 180f053f..dea7b2b4 100644 --- a/doc/role-icinga2/objects.md +++ b/doc/role-icinga2/objects.md @@ -535,6 +535,18 @@ icinga2_objects: set_if: $http_ssl$ ``` +#### CompatLogger + +```yaml +icinga2_objects: +[...] + - name: mycompatlogger + type: CompatLogger + file: "local.d/complog.conf" + log_dir: "LogDir + /custom_complog" + rotation_method: "hourly" +``` + #### Dependency ``` diff --git a/plugins/modules/icinga2_compatlogger.py b/plugins/modules/icinga2_compatlogger.py new file mode 100644 index 00000000..221df898 --- /dev/null +++ b/plugins/modules/icinga2_compatlogger.py @@ -0,0 +1,128 @@ +from ansible.module_utils.basic import AnsibleModule + +DOCUMENTATION = ''' +name: icinga2_compatlogger +short_description: Creates information for CompatLogger object. +description: + - Returns information used to create a CompatLogger object. +version_added: 0.4.0 +author: + - Matthias Döhler +options: + name: + description: + - The name of the CompatLogger object. + required: true + type: str + state: + description: + - The state of the CompatLogger object. + required: false + default: present + choices: [ "present", "absent" ] + type: str + order: + description: + - Value to determine internal precedence. + required: false + default: 10 + type: int + file: + description: + - Path to the file in which the object will be defined. + required: true + default: "features-available/compatlog.conf" + type: str + log_dir: + description: + - Path to the compat log directory. + required: false + type: str + rotation_method: + description: + - Specifies when to rotate log files. + required: false + choices: [ "hourly", "daily", "weekly", "monthly" ] + type: str +''' + +EXAMPLES = ''' +icinga.icinga.icinga2_compatlogger: + name: "mycompatlogger" + log_dir: "LogDir + /compat" + rotation_method: "monthly" +''' + +RETURN = ''' +args: + description: Arguments used to create the CompatLogger object. + returned: success + type: dict + contains: + log_dir: + description: The specified log directory. + returned: success + type: str + sample: "LogDir + /compat" + rotation_method: + description: The specified rotation method. + returned: success + type: str + sample: "MONTHLY" +file: + description: Path to the file that will contain the object. + returned: success + type: str + sample: features-available/compatlog.conf +name: + description: The name of the CompatLogger object. + returned: success + type: str + sample: mycompatlogger +order: + description: The order value of this object. Used internally when combining multiple templates / objects. + returned: success + type: int + sample: 10 +state: + description: The chosen state for the object. + returned: success + type: str + sample: present +''' + +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/compatlog.conf', type='str'), + log_dir=dict(type='str'), + rotation_method=dict(type='str', choices=['hourly', 'daily', 'weekly', 'monthly']), + ) + ) + + args = module.params + name = args.pop('name') + order = args.pop('order') + state = args.pop('state') + file = args.pop('file') + + # Capslock if rotation_method is set + if args.get('rotation_method', None): + args.update({'rotation_method': args.get('rotation_method').upper()}) + + 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/compatlog.yml b/roles/icinga2/tasks/features/compatlog.yml new file mode 100644 index 00000000..c4517d3d --- /dev/null +++ b/roles/icinga2/tasks/features/compatlog.yml @@ -0,0 +1,12 @@ +--- + +- name: Feature compatlog CompatLogger object + icinga.icinga.icinga2_object: + name: compatlog + type: CompatLogger + file: features-available/compatlog.conf + args: "{{ icinga2_dict_features.compatlog }}" + 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 75b05ac2..c9019862 100644 --- a/roles/icinga2/vars/main.yml +++ b/roles/icinga2/vars/main.yml @@ -77,6 +77,7 @@ icinga2_object_types: - ApiUser - CheckCommand - CheckerComponent + - CompatLogger - Dependency - ElasticsearchWriter - Endpoint