bundles/icinga2: rework config generation - use one file per host instead of one per bundle
Some checks failed
kunsi/bundlewrap/pipeline/head There was a failure building this commit

This commit is contained in:
Franzi 2022-02-18 12:25:34 +01:00
parent 5aa9f50c79
commit d35770c122
Signed by: kunsi
GPG key ID: 12E3D2136B818350
5 changed files with 82 additions and 101 deletions

View file

@ -1,4 +1,4 @@
% for monitored_node in sorted(monitored_nodes): % for monitored_node in sorted(repo.nodes):
<% <%
auto_updates_enabled = ( auto_updates_enabled = (
monitored_node.has_any_bundle(['apt', 'c3voc-addons']) monitored_node.has_any_bundle(['apt', 'c3voc-addons'])
@ -6,7 +6,7 @@
monitored_node.has_bundle('pacman') monitored_node.has_bundle('pacman')
and monitored_node.metadata.get('pacman/unattended-upgrades/is_enabled', False) 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: % if auto_updates_enabled:
object ScheduledDowntime "unattended_upgrades" { object ScheduledDowntime "unattended_upgrades" {

View file

@ -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 { apply Dependency "disable-service-checks-on-host-down" to Service {
disable_checks = true disable_checks = true
ignore_soft_states = true ignore_soft_states = true

View file

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

View file

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

View file

@ -59,7 +59,7 @@ directories = {
'svc_systemd:icinga2:restart', 'svc_systemd:icinga2:restart',
}, },
}, },
'/etc/icinga2/conf.d/services': { '/etc/icinga2/conf.d/hosts': {
'owner': 'nagios', 'owner': 'nagios',
'group': 'nagios', 'group': 'nagios',
'mode': '0750', 'mode': '0750',
@ -169,6 +169,17 @@ files = {
'svc_systemd:icinga2:restart', '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': { '/etc/icinga2/conf.d/notification_commands.conf': {
'source': 'icinga2/notification_commands.conf', 'source': 'icinga2/notification_commands.conf',
'owner': 'nagios', 'owner': 'nagios',
@ -319,38 +330,17 @@ svc_systemd = {
# The actual hosts and services management starts here # 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[:]: files[f'/etc/icinga2/conf.d/hosts/{rnode.name}.conf'] = {
if n.metadata.get('icinga_options/exclude_from_monitoring', False): 'source': 'icinga2/hosts_template.conf',
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',
'content_type': 'mako', 'content_type': 'mako',
'context': { 'context': {
'bundle_name': bundle, 'rnode': rnode,
'bundle_metadata': metadata['services'], 'sla_info': SLA_INFO,
}, },
'owner': 'nagios', 'owner': 'nagios',
'group': 'nagios', 'group': 'nagios',
@ -360,24 +350,13 @@ for bundle, metadata in bundle_metadata.items():
}, },
} }
files['/etc/icinga2/conf.d/hosts.conf'] = { bundles |= set(rnode.metadata.get('icinga2_api', {}).keys())
'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',
},
}
files['/etc/icinga2/conf.d/groups.conf'] = { files['/etc/icinga2/conf.d/groups.conf'] = {
'source': 'icinga2/groups.conf', 'source': 'icinga2/groups.conf',
'content_type': 'mako', 'content_type': 'mako',
'context': { 'context': {
'bundles': bundle_metadata.keys(), 'bundles': bundles,
}, },
'owner': 'nagios', 'owner': 'nagios',
'group': 'nagios', 'group': 'nagios',
@ -388,11 +367,11 @@ files['/etc/icinga2/conf.d/groups.conf'] = {
'svc_systemd:icinga2:restart', 'svc_systemd:icinga2:restart',
}, },
} }
files['/etc/icinga2/conf.d/downtimes.conf'] = { files['/etc/icinga2/conf.d/downtimes.conf'] = {
'source': 'icinga2/downtimes.conf', 'source': 'icinga2/downtimes.conf',
'content_type': 'mako', 'content_type': 'mako',
'context': { 'context': {
'monitored_nodes': monitored_nodes,
'days': DAYS_TO_STRING, 'days': DAYS_TO_STRING,
}, },
'owner': 'nagios', 'owner': 'nagios',