def test_get_check_table(monkeypatch, hostname, expected_result): autochecks = { "ping-host": [ Service( CheckPluginName("smart_temp"), "bla", u'Temperature SMART bla', {}, ) ], "autocheck-overwrite": [ Service( CheckPluginName('smart_temp'), '/dev/sda', u'Temperature SMART /dev/sda', {"is_autocheck": True}, ), Service( CheckPluginName('smart_temp'), '/dev/sdb', u'Temperature SMART /dev/sdb', {"is_autocheck": True}, ), ], "ignore-not-existing-checks": [ Service( CheckPluginName("bla_blub"), "ITEM", u'Blub ITEM', {}, ), ], "node1": [ Service( CheckPluginName("smart_temp"), "auto-clustered", u"Temperature SMART auto-clustered", {}, ), Service( CheckPluginName("smart_temp"), "auto-not-clustered", u'Temperature SMART auto-not-clustered', {}, ) ], } ts = Scenario().add_host(hostname, tags={"criticality": "test"}) ts.add_host("ping-host", tags={"agent": "no-agent"}) ts.add_host("node1") ts.add_cluster("cluster1", nodes=["node1"]) ts.set_option( "static_checks", { "temperature": [ (('smart.temp', '/dev/sda', {}), [], ["no-autochecks", "autocheck-overwrite"]), (('blub.bla', 'ITEM', {}), [], ["ignore-not-existing-checks"]), (('smart.temp', 'ITEM1', {}), [], ["ignore-disabled-rules"], { "disabled": True }), (('smart.temp', 'ITEM2', {}), [], ["ignore-disabled-rules"]), (('smart.temp', '/dev/sda', { "rule": 1 }), [], ["static-check-overwrite"]), (('smart.temp', '/dev/sda', { "rule": 2 }), [], ["static-check-overwrite"]), (('smart.temp', 'static-node1', {}), [], ["node1"]), (('smart.temp', 'static-cluster', {}), [], ["cluster1"]), ] }, ) ts.set_ruleset("clustered_services", [ ([], ['node1'], [u'Temperature SMART auto-clustered$']), ]) config_cache = ts.apply(monkeypatch) monkeypatch.setattr(config_cache, "get_autochecks_of", lambda h: autochecks.get(h, [])) assert check_table.get_check_table(hostname) == expected_result
def test_get_check_table_of_static_check(monkeypatch, hostname, expected_result): static_checks = { "df_host": [ Service(CheckPluginName('df'), '/snap/core/9066', u'Filesystem /snap/core/9066', [{ 'tp_values': [('24X7', { 'inodes_levels': None })], 'tp_default_value': {} }, { 'trend_range': 24, 'show_levels': 'onmagic', 'inodes_levels': (10.0, 5.0), 'magic_normsize': 20, 'show_inodes': 'onlow', 'levels': (80.0, 90.0), 'show_reserved': False, 'levels_low': (50.0, 60.0), 'trend_perfdata': True }]), ], "df_host_1": [ Service( CheckPluginName('df'), '/snap/core/9067', u'Filesystem /snap/core/9067', { 'trend_range': 24, 'show_levels': 'onmagic', 'inodes_levels': (10.0, 5.0), 'magic_normsize': 20, 'show_inodes': 'onlow', 'levels': (80.0, 90.0), 'tp_default_value': { 'levels': (87.0, 90.0) }, 'show_reserved': False, 'tp_values': [('24X7', { 'inodes_levels': None })], 'levels_low': (50.0, 60.0), 'trend_perfdata': True }) ], "df_host_2": [ Service(CheckPluginName('df'), '/snap/core/9068', u'Filesystem /snap/core/9068', None) ], } ts = Scenario().add_host(hostname, tags={"criticality": "test"}) ts.add_host("df_host") ts.add_host("df_host_1") ts.add_host("df_host_2") ts.set_option( "static_checks", { "filesystem": [ (('df', '/snap/core/9066', [{ 'tp_values': [('24X7', { 'inodes_levels': None })], 'tp_default_value': {} }, { 'trend_range': 24, 'show_levels': 'onmagic', 'inodes_levels': (10.0, 5.0), 'magic_normsize': 20, 'show_inodes': 'onlow', 'levels': (80.0, 90.0), 'show_reserved': False, 'levels_low': (50.0, 60.0), 'trend_perfdata': True }]), [], ["df_host"]), (('df', '/snap/core/9067', [{ 'tp_values': [('24X7', { 'inodes_levels': None })], 'tp_default_value': {} }, { 'trend_range': 24, 'show_levels': 'onmagic', 'inodes_levels': (10.0, 5.0), 'magic_normsize': 20, 'show_inodes': 'onlow', 'levels': (80.0, 90.0), 'show_reserved': False, 'levels_low': (50.0, 60.0), 'trend_perfdata': True }]), [], ["df_host_1"]), (('df', '/snap/core/9068', None), [], ["df_host_2"]), ], }, ) config_cache = ts.apply(monkeypatch) monkeypatch.setattr(config_cache, "get_autochecks_of", lambda h: static_checks.get(h, [])) assert list( check_table.get_check_table(hostname).keys()) == expected_result
def test_config(monkeypatch): CheckManager().load(["df", "cpu", "chrony", "lnx_if"]) ts = Scenario().add_host("host") return ts.apply(monkeypatch)
def scenario(self, hostname, ipaddress, monkeypatch): ts = Scenario().add_host(hostname) ts.set_option("ipaddresses", {hostname: ipaddress}) ts.apply(monkeypatch) return ts
def scenario(backend, monkeypatch): # Set the `ruleset_matcher` on the config. ts = Scenario() ts.add_host(backend.hostname) ts.apply(monkeypatch)
def _cluster_scenario(monkeypatch): hostname = "test-clusterhost" ipaddress = "1.2.3.4" node1_hostname = 'test-node1' node2_hostname = 'test-node2' def fake_lookup_ip_address(host_config, *, family, for_mgmt_board=True): return ipaddress monkeypatch.setattr(ip_lookup, "lookup_ip_address", fake_lookup_ip_address) ts = Scenario() ts.add_host(node1_hostname) ts.add_host(node2_hostname) ts.add_cluster(hostname, nodes=[node1_hostname, node2_hostname]) ts.set_ruleset("inventory_df_rules", [{ 'value': { 'ignore_fs_types': ['tmpfs', 'nfs', 'smbfs', 'cifs', 'iso9660'], 'never_ignore_mountpoints': ['~.*/omd/sites/[^/]+/tmp$'] }, 'condition': { 'host_labels': { 'cmk/check_mk_server': 'yes' } } }]) ts.set_ruleset("clustered_services", [([], [node1_hostname], ['fs_'])]) host_config = ts.apply(monkeypatch).get_host_config(hostname) DiscoveredHostLabelsStore(node1_hostname).save({ 'node1_existing_label': { 'plugin_name': 'node1_plugin', 'value': 'true', } }) DiscoveredHostLabelsStore(hostname).save({ 'existing_label': { 'plugin_name': 'foo', 'value': 'bar', }, 'another_label': { 'plugin_name': 'labels', 'value': 'true', } }) broker = ParsedSectionsBroker() broker.update({ HostKey(hostname=node1_hostname, ipaddress=ipaddress, source_type=SourceType.HOST): AgentHostSections( sections={ SectionName("labels"): [ [ '{"cmk/check_mk_server":"yes"}', ], ], SectionName("df"): [ [ '/dev/sda1', 'vfat', '523248', '3668', '519580', '1%', '/boot/test-efi', ], [ 'tmpfs', 'tmpfs', '8152916', '244', '8152672', '1%', '/opt/omd/sites/test-heute/tmp', ], ], }), HostKey(hostname=node2_hostname, ipaddress=ipaddress, source_type=SourceType.HOST): AgentHostSections( sections={ SectionName("labels"): [ [ '{"node2_live_label":"true"}', ], ], SectionName("df"): [ [ '/dev/sda1', 'vfat', '523248', '3668', '519580', '1%', '/boot/test-efi', ], [ 'tmpfs', 'tmpfs', '8152916', '244', '8152672', '1%', '/opt/omd/sites/test-heute2/tmp', ], ], }), }) return ClusterScenario( host_config, ipaddress, broker, node1_hostname, node2_hostname, )
def nagios_core(self, monkeypatch): ts = Scenario().set_option("monitoring_core", "nagios") ts.apply(monkeypatch)
def cluster_config(monkeypatch): ts = Scenario().add_host("node1").add_host("host1") ts.add_cluster("cluster1", nodes=["node1"]) return ts.apply(monkeypatch)
def config_cache(self, cluster, nodes, monkeypatch): ts = Scenario().add_cluster(cluster, nodes=nodes.keys()) return ts.apply(monkeypatch)
def test_config(monkeypatch): ts = Scenario().add_host("host") return ts.apply(monkeypatch)
def fixture_core_scenario(monkeypatch): ts = Scenario().add_host("test-host") ts.set_option("ipaddresses", {"test-host": "127.0.0.1"}) return ts.apply(monkeypatch)
def test_is_ipv6_primary_host(monkeypatch, hostname, tags, result, ruleset): ts = Scenario().add_host(hostname, tags) ts.set_ruleset("primary_address_family", ruleset) config_cache = ts.apply(monkeypatch) assert config_cache.get_host_config(hostname).is_ipv6_primary == result
def test_all_offline_hosts(monkeypatch): ts = Scenario() ts.add_host("blub", tags={"criticality": "offline"}) ts.add_host("bla") ts.apply(monkeypatch) assert config.all_offline_hosts() == set()
def scenario(self, hostname, monkeypatch): ts = Scenario() ts.add_host(hostname) ts.apply(monkeypatch)
def cmc_core(self, monkeypatch): ts = Scenario().set_option("monitoring_core", "cmc") ts.apply(monkeypatch)
def config_cache(self, hostname, ipaddress, monkeypatch): ts = Scenario().add_host(hostname) return ts.apply(monkeypatch)
def _realhost_scenario(monkeypatch): hostname = "test-realhost" ipaddress = "1.2.3.4" ts = Scenario().add_host(hostname, ipaddress=ipaddress) ts.set_ruleset("inventory_df_rules", [{ 'value': { 'ignore_fs_types': ['tmpfs', 'nfs', 'smbfs', 'cifs', 'iso9660'], 'never_ignore_mountpoints': ['~.*/omd/sites/[^/]+/tmp$'] }, 'condition': { 'host_labels': { 'cmk/check_mk_server': 'yes' } } }]) ts.apply(monkeypatch) def fake_lookup_ip_address(*_a, **_kw): return ipaddress monkeypatch.setattr(ip_lookup, "lookup_ip_address", fake_lookup_ip_address) DiscoveredHostLabelsStore(hostname).save({ 'existing_label': { 'plugin_name': 'foo', 'value': 'bar', }, 'another_label': { 'plugin_name': 'labels', 'value': 'true', } }) broker = ParsedSectionsBroker({ HostKey(hostname=hostname, ipaddress=ipaddress, source_type=SourceType.HOST): SectionsParser(host_sections=AgentHostSections( sections={ SectionName("labels"): [ [ '{"cmk/check_mk_server":"yes"}', ], ], SectionName("df"): [ [ '/dev/sda1', 'vfat', '523248', '3668', '519580', '1%', '/boot/test-efi', ], [ 'tmpfs', 'tmpfs', '8152916', '244', '8152672', '1%', '/opt/omd/sites/test-heute/tmp', ], ], }), ), }) return RealHostScenario(hostname, ipaddress, broker)