def test_get_host_attributes(fixup_ip_lookup, monkeypatch): ts = Scenario().add_host("test-host", tags={"agent": "no-agent"}) ts.set_option("host_labels", { "test-host": { "ding": "dong", }, }) config_cache = ts.apply(monkeypatch) expected_attrs = { '_ADDRESS_4': '0.0.0.0', '_ADDRESS_6': '', '_ADDRESS_FAMILY': '4', '_FILENAME': '/wato/hosts.mk', '_TAGS': '/wato/ auto-piggyback ip-v4 ip-v4-only lan no-agent no-snmp prod site:unit', u'__TAG_address_family': u'ip-v4-only', u'__TAG_agent': u'no-agent', u'__TAG_criticality': u'prod', u'__TAG_ip-v4': u'ip-v4', u'__TAG_networking': u'lan', u'__TAG_piggyback': u'auto-piggyback', u'__TAG_site': u'unit', u'__TAG_snmp_ds': u'no-snmp', '__LABEL_ding': 'dong', '__LABELSOURCE_ding': 'explicit', 'address': '0.0.0.0', 'alias': 'test-host', } if cmk_version.is_managed_edition(): expected_attrs['_CUSTOMER'] = 'provider' attrs = core_config.get_host_attributes("test-host", config_cache) assert attrs == expected_attrs
def test_create_nagios_host_spec(hostname, result, monkeypatch): ts = Scenario().add_host("localhost") ts.add_host("host2") ts.add_cluster("cluster1") ts.add_cluster("cluster2", nodes=["node1", "node2"]) ts.add_host("node1") ts.add_host("node2") ts.add_host("switch") ts.set_option("ipaddresses", { "node1": "127.0.0.1", "node2": "127.0.0.2", }) ts.set_option("extra_host_conf", { "alias": [(u'lOCALhost', ['localhost']),], }) ts.set_option( "extra_host_conf", { "alias": [ (u'lOCALhost', ['host2']), (u'CLUSTer', ['cluster2']), ], "parents": [('switch', ['node1', 'node2']),], }) outfile = io.StringIO() cfg = core_nagios.NagiosConfig(outfile, [hostname]) config_cache = ts.apply(monkeypatch) host_attrs = core_config.get_host_attributes(hostname, config_cache) host_spec = core_nagios._create_nagios_host_spec(cfg, config_cache, hostname, host_attrs) assert host_spec == result
def _create_nagios_config_host(cfg, config_cache, hostname): # type: (NagiosConfig, ConfigCache, HostName) -> None cfg.write("\n# ----------------------------------------------------\n") cfg.write("# %s\n" % hostname) cfg.write("# ----------------------------------------------------\n") host_attrs = core_config.get_host_attributes(hostname, config_cache) if config.generate_hostconf: host_spec = _create_nagios_host_spec(cfg, config_cache, hostname, host_attrs) cfg.write(_format_nagios_object("host", host_spec)) _create_nagios_servicedefs(cfg, config_cache, hostname, host_attrs)
def test_create_nagios_host_spec(hostname_str: str, result: Dict[str, str], monkeypatch: MonkeyPatch) -> None: if cmk_version.is_managed_edition(): result = result.copy() result["_CUSTOMER"] = "provider" ts = Scenario() ts.add_host(HostName("localhost")) ts.add_host(HostName("host2")) ts.add_cluster(HostName("cluster1")) ts.add_cluster(HostName("cluster2"), nodes=["node1", "node2"]) ts.add_host(HostName("node1")) ts.add_host(HostName("node2")) ts.add_host(HostName("switch")) ts.set_option( "ipaddresses", { HostName("node1"): "127.0.0.1", HostName("node2"): "127.0.0.2", }, ) ts.set_option( "extra_host_conf", { "alias": [ ("lOCALhost", ["localhost"]), ], }, ) ts.set_option( "extra_host_conf", { "alias": [ ("lOCALhost", ["host2"]), ("CLUSTer", ["cluster2"]), ], "parents": [ ("switch", ["node1", "node2"]), ], }, ) hostname = HostName(hostname_str) outfile = io.StringIO() cfg = core_nagios.NagiosConfig(outfile, [hostname]) config_cache = ts.apply(monkeypatch) host_attrs = core_config.get_host_attributes(hostname, config_cache) host_spec = core_nagios._create_nagios_host_spec(cfg, config_cache, hostname, host_attrs) assert host_spec == result
def _translate_host_macros(self, cmd: str) -> str: attrs = core_config.get_host_attributes(self.hostname, self._config_cache) if self._host_config.is_cluster: parents_list = core_config.get_cluster_nodes_for_config(self._config_cache, self._host_config) attrs.setdefault("alias", "cluster of %s" % ", ".join(parents_list)) attrs.update( core_config.get_cluster_attributes(self._config_cache, self._host_config, parents_list)) macros = core_config.get_host_macros_from_attributes(self.hostname, attrs) return ensure_str(core_config.replace_macros(cmd, macros))
def _create_nagios_config_host(cfg, config_cache, hostname): cfg.outfile.write( "\n# ----------------------------------------------------\n") cfg.outfile.write("# %s\n" % hostname) cfg.outfile.write( "# ----------------------------------------------------\n") host_attrs = core_config.get_host_attributes(hostname, config_cache) if config.generate_hostconf: host_spec = _create_nagios_host_spec(cfg, config_cache, hostname, host_attrs) cfg.outfile.write( _format_nagios_object("host", host_spec).encode("utf-8")) _create_nagios_servicedefs(cfg, config_cache, hostname, host_attrs)
def _translate_host_macros(cmd: str, host_config: config.HostConfig) -> str: config_cache = config.get_config_cache() attrs = core_config.get_host_attributes(host_config.hostname, config_cache) if host_config.is_cluster: parents_list = core_config.get_cluster_nodes_for_config( config_cache, host_config, ) attrs.setdefault("alias", "cluster of %s" % ", ".join(parents_list)) attrs.update( core_config.get_cluster_attributes( config_cache, host_config, parents_list, )) macros = core_config.get_host_macros_from_attributes(host_config.hostname, attrs) return ensure_str(core_config.replace_macros(cmd, macros))
def test_get_host_attributes(fixup_ip_lookup, monkeypatch): ts = Scenario() ts.add_host("test-host", tags={"agent": "no-agent"}) ts.set_option( "host_labels", { "test-host": { "ding": "dong", }, }, ) config_cache = ts.apply(monkeypatch) expected_attrs = { "_ADDRESSES_4": "", "_ADDRESSES_6": "", "_ADDRESS_4": "0.0.0.0", "_ADDRESS_6": "", "_ADDRESS_FAMILY": "4", "_FILENAME": "/wato/hosts.mk", "_TAGS": "/wato/ auto-piggyback ip-v4 ip-v4-only lan no-agent no-snmp prod site:unit", "__TAG_address_family": "ip-v4-only", "__TAG_agent": "no-agent", "__TAG_criticality": "prod", "__TAG_ip-v4": "ip-v4", "__TAG_networking": "lan", "__TAG_piggyback": "auto-piggyback", "__TAG_site": "unit", "__TAG_snmp_ds": "no-snmp", "__LABEL_ding": "dong", "__LABEL_cmk/site": "NO_SITE", "__LABELSOURCE_cmk/site": "discovered", "__LABELSOURCE_ding": "explicit", "address": "0.0.0.0", "alias": "test-host", } if cmk_version.is_managed_edition(): expected_attrs["_CUSTOMER"] = "provider" attrs = core_config.get_host_attributes("test-host", config_cache) assert attrs == expected_attrs