object Host "${rnode.name}" {
    import "generic-host"

% if address4:
    address = "${address4}"
% endif
% if address6:
    address6 = "${address6}"
% endif

    # 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.metadata.get('hostname'))}"
    vars.show_on_statuspage = ${str(rnode.metadata.get('icinga_options/show_on_statuspage', True)).lower()}

    vars.period = "${rnode.metadata.get('icinga_options/period', '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()}
%     else:
    ${k} = "${v}"
%     endif
%    endif
%  endfor
    vars.bundle = "${bundle}"
    vars.notification.sms = ${str(config.get('vars.notification.sms', False)).lower()}
    vars.notification.mail = ${str(config.get('vars.notification.mail', False)).lower()}
}
%  endfor
% endfor
