From d35770c12228a73ccb4b59ce9e8446250cd1e69e Mon Sep 17 00:00:00 2001 From: Franziska Kunsmann Date: Fri, 18 Feb 2022 12:25:34 +0100 Subject: [PATCH] bundles/icinga2: rework config generation - use one file per host instead of one per bundle --- bundles/icinga2/files/icinga2/downtimes.conf | 4 +- bundles/icinga2/files/icinga2/hosts.conf | 32 --------- .../icinga2/files/icinga2/hosts_template.conf | 56 +++++++++++++++ .../files/icinga2/services_template.conf | 22 ------ bundles/icinga2/items.py | 69 +++++++------------ 5 files changed, 82 insertions(+), 101 deletions(-) create mode 100644 bundles/icinga2/files/icinga2/hosts_template.conf delete mode 100644 bundles/icinga2/files/icinga2/services_template.conf diff --git a/bundles/icinga2/files/icinga2/downtimes.conf b/bundles/icinga2/files/icinga2/downtimes.conf index 96ba242..0052816 100644 --- a/bundles/icinga2/files/icinga2/downtimes.conf +++ b/bundles/icinga2/files/icinga2/downtimes.conf @@ -1,4 +1,4 @@ -% for monitored_node in sorted(monitored_nodes): +% for monitored_node in sorted(repo.nodes): <% auto_updates_enabled = ( monitored_node.has_any_bundle(['apt', 'c3voc-addons']) @@ -6,7 +6,7 @@ monitored_node.has_bundle('pacman') and monitored_node.metadata.get('pacman/unattended-upgrades/is_enabled', False) ) - ) + ) and not monitored_node.metadata.get('icinga_options/exclude_from_monitoring', False) %>\ % if auto_updates_enabled: object ScheduledDowntime "unattended_upgrades" { diff --git a/bundles/icinga2/files/icinga2/hosts.conf b/bundles/icinga2/files/icinga2/hosts.conf index 2399637..9c0fd60 100644 --- a/bundles/icinga2/files/icinga2/hosts.conf +++ b/bundles/icinga2/files/icinga2/hosts.conf @@ -1,35 +1,3 @@ -% for monitored_node in sorted(monitored_nodes): -object Host "${monitored_node.name}" { - import "generic-host" - - address = "${monitored_node.metadata.get('icinga_options/hostname', monitored_node.hostname)}" - - # used for determining service groups - vars.bw_groups = [ "${'", "'.join(sorted({group.name for group in monitored_node.groups}))}" ] - vars.location = "${monitored_node.metadata.get('location', 'unknown')}" - vars.os = "${monitored_node.os}" - - # used for status page - vars.pretty_name = "${monitored_node.metadata.get('icinga_options/pretty_name', monitored_node.name)}" - - vars.period = "${sla_info[monitored_node.metadata.get('sla', '24x7')]}" - vars.sla = "${monitored_node.metadata.get('sla', '24x7')}" - - vars.notification.sms = ${str(monitored_node.metadata.get('icinga_options/vars.notification.sms', True)).lower()} - vars.notification.mail = true -} -% for depends_on_host in sorted(monitored_node.metadata.get('icinga_options/also_affected_by', set())): -object Dependency "${monitored_node.name}_depends_on_${depends_on_host}" { - parent_host_name = "${depends_on_host}" - child_host_name = "${monitored_node.name}" - states = [ Up ] - disable_checks = true - disable_notifications = true - ignore_soft_states = true -} -% endfor -% endfor - apply Dependency "disable-service-checks-on-host-down" to Service { disable_checks = true ignore_soft_states = true diff --git a/bundles/icinga2/files/icinga2/hosts_template.conf b/bundles/icinga2/files/icinga2/hosts_template.conf new file mode 100644 index 0000000..2be47e3 --- /dev/null +++ b/bundles/icinga2/files/icinga2/hosts_template.conf @@ -0,0 +1,56 @@ +object Host "${rnode.name}" { + import "generic-host" + + address = "${rnode.metadata.get('icinga_options/hostname', rnode.hostname)}" + + # used for determining service groups + vars.bw_groups = [ "${'", "'.join(sorted({group.name for group in rnode.groups}))}" ] + vars.location = "${rnode.metadata.get('location', 'unknown')}" + vars.os = "${rnode.os}" + + # used for status page + vars.pretty_name = "${rnode.metadata.get('icinga_options/pretty_name', rnode.name)}" + + vars.period = "${sla_info[rnode.metadata.get('sla', '24x7')]}" + vars.sla = "${rnode.metadata.get('sla', '24x7')}" + + vars.notification.sms = ${str(rnode.metadata.get('icinga_options/vars.notification.sms', True)).lower()} + vars.notification.mail = true +} + +% for depends_on_host in sorted(rnode.metadata.get('icinga_options/also_affected_by', set())): +object Dependency "${rnode.name}_depends_on_${depends_on_host}" { + parent_host_name = "${depends_on_host}" + child_host_name = "${rnode.name}" + states = [ Up ] + disable_checks = true + disable_notifications = true + ignore_soft_states = true +} +% endfor + +% for bundle, bconfig in sorted(rnode.metadata.get('icinga2_api', {}).items()): +% for service, config in sorted(bconfig.get('services', {}).items()): +object Service "${service}" { + import "generic-service" + host_name = "${rnode.name}" +% if 'check_command' not in config: + check_command = "sshmon" +% endif +% for k, v in sorted(config.items()): +% if k not in {'import', 'command_on_monitored_host'} and not k.startswith('vars.notification'): +% if isinstance(v, bool) or isinstance(v, int) or k.endswith('_interval'): + ${k} = ${str(v).lower()} +% elif isinstance(v, list): + ${k} = [${", ".join(['"' + i + '"' for i in v])}] +% else: + ${k} = "${v}" +% endif +% endif +% endfor + vars.bundle = "${bundle}" + vars.notification.sms = ${str((config.get('vars.notification.sms', False) and rnode.metadata.get('icinga_options/vars.notification.sms', True))).lower()} + vars.notification.mail = ${str(config.get('vars.notification.mail', False)).lower()} +} +% endfor +% endfor diff --git a/bundles/icinga2/files/icinga2/services_template.conf b/bundles/icinga2/files/icinga2/services_template.conf deleted file mode 100644 index 635b3a5..0000000 --- a/bundles/icinga2/files/icinga2/services_template.conf +++ /dev/null @@ -1,22 +0,0 @@ -% for node_data, icingaconf in sorted(bundle_metadata.items()): -% for service, config in sorted(icingaconf.items()): -object Service "${service}" { - import "generic-service" - host_name = "${node_data.name}" -% for k, v in sorted(config.items()): -% if k not in {'import', 'command_on_monitored_host'} and not k.startswith('vars.notification'): -% if isinstance(v, bool) or isinstance(v, int) or k in ["check_interval", "retry_interval"]: - ${k} = ${str(v).lower()} -% elif isinstance(v, list): - ${k} = [${", ".join(['"' + i + '"' for i in v])}] -% else: - ${k} = "${v}" -% endif -% endif -% endfor - vars.bundle = "${bundle_name}" - vars.notification.sms = ${str(config.get('vars.notification.sms', False)).lower()} - vars.notification.mail = ${str(config.get('vars.notification.mail', False)).lower()} -} -% endfor -% endfor diff --git a/bundles/icinga2/items.py b/bundles/icinga2/items.py index 055bec7..3ffd860 100644 --- a/bundles/icinga2/items.py +++ b/bundles/icinga2/items.py @@ -59,7 +59,7 @@ directories = { 'svc_systemd:icinga2:restart', }, }, - '/etc/icinga2/conf.d/services': { + '/etc/icinga2/conf.d/hosts': { 'owner': 'nagios', 'group': 'nagios', 'mode': '0750', @@ -169,6 +169,17 @@ files = { 'svc_systemd:icinga2:restart', }, }, + '/etc/icinga2/conf.d/hosts.conf': { + 'source': 'icinga2/hosts.conf', + 'owner': 'nagios', + 'group': 'nagios', + 'needs': { + 'pkg_apt:icinga2', + }, + 'triggers': { + 'svc_systemd:icinga2:restart', + }, + }, '/etc/icinga2/conf.d/notification_commands.conf': { 'source': 'icinga2/notification_commands.conf', 'owner': 'nagios', @@ -319,38 +330,17 @@ svc_systemd = { # The actual hosts and services management starts here -monitored_nodes = repo.nodes +bundles = set() +for rnode in repo.nodes: + if rnode.metadata.get('icinga_options/exclude_from_monitoring', False): + continue -for n in monitored_nodes[:]: - if n.metadata.get('icinga_options/exclude_from_monitoring', False): - monitored_nodes.remove(n) - -bundle_metadata = {} -for monitored_node in monitored_nodes: - node_metadata = monitored_node.metadata - - for bundle, config in sorted(node_metadata.get('icinga2_api', {}).items()): - if bundle not in bundle_metadata: - bundle_metadata[bundle] = { - 'services': {} - } - - bundle_metadata[bundle]['services'].update({ - monitored_node: config['services'] - }) - - for serv, conf in bundle_metadata[bundle]['services'][monitored_node].items(): - if 'check_command' not in conf: - # This default is also set in sshmon bundle - conf['check_command'] = 'sshmon' - -for bundle, metadata in bundle_metadata.items(): - files[f'/etc/icinga2/conf.d/services/{bundle}.conf'] = { - 'source': 'icinga2/services_template.conf', + files[f'/etc/icinga2/conf.d/hosts/{rnode.name}.conf'] = { + 'source': 'icinga2/hosts_template.conf', 'content_type': 'mako', 'context': { - 'bundle_name': bundle, - 'bundle_metadata': metadata['services'], + 'rnode': rnode, + 'sla_info': SLA_INFO, }, 'owner': 'nagios', 'group': 'nagios', @@ -360,24 +350,13 @@ for bundle, metadata in bundle_metadata.items(): }, } -files['/etc/icinga2/conf.d/hosts.conf'] = { - 'source': 'icinga2/hosts.conf', - 'content_type': 'mako', - 'context': { - 'monitored_nodes': monitored_nodes, - 'sla_info': SLA_INFO, - }, - 'owner': 'nagios', - 'group': 'nagios', - 'triggers': { - 'svc_systemd:icinga2:restart', - }, -} + bundles |= set(rnode.metadata.get('icinga2_api', {}).keys()) + files['/etc/icinga2/conf.d/groups.conf'] = { 'source': 'icinga2/groups.conf', 'content_type': 'mako', 'context': { - 'bundles': bundle_metadata.keys(), + 'bundles': bundles, }, 'owner': 'nagios', 'group': 'nagios', @@ -388,11 +367,11 @@ files['/etc/icinga2/conf.d/groups.conf'] = { 'svc_systemd:icinga2:restart', }, } + files['/etc/icinga2/conf.d/downtimes.conf'] = { 'source': 'icinga2/downtimes.conf', 'content_type': 'mako', 'context': { - 'monitored_nodes': monitored_nodes, 'days': DAYS_TO_STRING, }, 'owner': 'nagios',