def test_get_section_cluster_kwargs(patch_register, required_sections, expected_result): node1_section_content = { SectionName("one"): NODE_1, SectionName("two"): NODE_1, SectionName("three"): NODE_1 } node2_section_content = { SectionName("two"): NODE_2, SectionName("three"): NODE_2, } multi_host_sections = MultiHostSections() multi_host_sections.setdefault( HostKey("node1", "127.0.0.1", SourceType.HOST), AgentHostSections(sections=node1_section_content), ) multi_host_sections.setdefault( HostKey("node2", "127.0.0.1", SourceType.HOST), AgentHostSections(sections=node2_section_content), ) kwargs = multi_host_sections.get_section_cluster_kwargs( [ HostKey("node1", "127.0.0.1", SourceType.HOST), HostKey("node2", "127.0.0.1", SourceType.HOST), ], [ParsedSectionName(n) for n in required_sections], ) assert expected_result == kwargs,\ "Section content: Expected '%s' but got '%s'" % (expected_result, kwargs)
def test_get_section_cluster_kwargs(monkeypatch, required_sections, expected_result): _set_up(monkeypatch, "cluster", ["node2", "node1"], {"node1": "cluster", "node2": "cluster"}) node1_section_content = { SectionName("one"): NODE_1, # TODO (mo): CMK-4232 # SectionName("two"): NODE_1, SectionName("three"): NODE_1 } node2_section_content = { SectionName("two"): NODE_2, SectionName("three"): NODE_2, } multi_host_sections = MultiHostSections() multi_host_sections.setdefault( HostKey("node1", "127.0.0.1", SourceType.HOST), AgentHostSections(sections=node1_section_content), ) multi_host_sections.setdefault( HostKey("node2", "127.0.0.1", SourceType.HOST), AgentHostSections(sections=node2_section_content), ) kwargs = multi_host_sections.get_section_cluster_kwargs( HostKey("cluster", None, SourceType.HOST), [ParsedSectionName(n) for n in required_sections], "_service_description", ) assert expected_result == kwargs,\ "Section content: Expected '%s' but got '%s'" % (expected_result, kwargs)
def test_get_section_cluster_kwargs(monkeypatch, required_sections, expected_result): _set_up(monkeypatch, "cluster", ["node2", "node1"], { "node1": "cluster", "node2": "cluster" }) node1_section_content = { "one": NODE_1, # TODO (mo): CMK-4232 # "two": NODE_1, "three": NODE_1 } node2_section_content = {"two": NODE_2, "three": NODE_2} multi_host_sections = MultiHostSections() multi_host_sections.setdefault_host_sections( ("node1", "127.0.0.1"), AgentHostSections(sections=node1_section_content), ) multi_host_sections.setdefault_host_sections( ("node2", "127.0.0.1"), AgentHostSections(sections=node2_section_content), ) kwargs = multi_host_sections.get_section_cluster_kwargs( "cluster", [PluginName(n) for n in required_sections], "_service_description") assert expected_result == kwargs,\ "Section content: Expected '%s' but got '%s'" % (expected_result, kwargs)
def get_aggregated_result( multi_host_sections: MultiHostSections, host_config: config.HostConfig, ipaddress: Optional[HostAddress], service: Service, plugin: Optional[checking_types.CheckPlugin], params_function: Callable[[], checking_types.Parameters], ) -> Tuple[bool, bool, ServiceCheckResult]: if plugin is None: return False, True, CHECK_NOT_IMPLEMENTED check_function = (plugin.cluster_check_function if host_config.is_cluster else plugin.check_function) source_type = (SourceType.MANAGEMENT if service.check_plugin_name.startswith('mgmt_') else SourceType.HOST) kwargs = {} try: kwargs = multi_host_sections.get_section_cluster_kwargs( HostKey(host_config.hostname, None, source_type), plugin.sections, service.description, ) if host_config.is_cluster else multi_host_sections.get_section_kwargs( HostKey(host_config.hostname, ipaddress, source_type), plugin.sections, ) if not kwargs: return False, False, RECEIVED_NO_DATA if service.item is not None: kwargs["item"] = service.item if plugin.check_ruleset_name: kwargs["params"] = params_function() with value_store.context(plugin.name, service.item): result = _aggregate_results(check_function(**kwargs)) except (item_state.MKCounterWrapped, checking_types.IgnoreResultsError) as e: msg = str(e) or "No service summary available" return False, True, (0, msg, []) except MKTimeout: raise except Exception: if cmk.utils.debug.enabled(): raise result = 3, cmk.base.crash_reporting.create_check_crash_dump( host_config.hostname, service.check_plugin_name, kwargs, is_manual_check(host_config.hostname, service.check_plugin_name, service.item), service.description, ), [] return True, True, result
def get_aggregated_result( multi_host_sections: MultiHostSections, host_config: config.HostConfig, ipaddress: Optional[HostAddress], service: Service, plugin: Optional[CheckPlugin], params_function: Callable[[], checking_classes.Parameters], ) -> Tuple[bool, bool, ServiceCheckResult]: """Run the check function and aggregate the subresults This function is also called during discovery. Returns a triple: bool: should the result be submitted to the core bool: did we receive data for the plugin ServiceCheckResult: The aggregated result as returned by the plugin, or a fallback """ if plugin is None: return False, True, CHECK_NOT_IMPLEMENTED check_function = (plugin.cluster_check_function if host_config.is_cluster else plugin.check_function) source_type = (SourceType.MANAGEMENT if is_management_name(service.check_plugin_name) else SourceType.HOST) kwargs = {} try: kwargs = multi_host_sections.get_section_cluster_kwargs( HostKey(host_config.hostname, None, source_type), plugin.sections, service.description, ) if host_config.is_cluster else multi_host_sections.get_section_kwargs( HostKey(host_config.hostname, ipaddress, source_type), plugin.sections, ) if not kwargs and not is_management_name(service.check_plugin_name): # in 1.6 some plugins where discovered for management boards, but with # the regular host plugins name. In this case retry with the source type # forced to MANAGEMENT: kwargs = multi_host_sections.get_section_cluster_kwargs( HostKey(host_config.hostname, None, SourceType.MANAGEMENT), plugin.sections, service.description, ) if host_config.is_cluster else multi_host_sections.get_section_kwargs( HostKey(host_config.hostname, ipaddress, SourceType.MANAGEMENT), plugin.sections, ) if not kwargs: # no data found return False, False, RECEIVED_NO_DATA if service.item is not None: kwargs["item"] = service.item if plugin.check_ruleset_name: kwargs["params"] = params_function() with value_store.context(plugin.name, service.item): result = _aggregate_results(check_function(**kwargs)) except (item_state.MKCounterWrapped, checking_classes.IgnoreResultsError) as e: msg = str(e) or "No service summary available" return False, True, (0, msg, []) except MKTimeout: raise except Exception: if cmk.utils.debug.enabled(): raise result = 3, cmk.base.crash_reporting.create_check_crash_dump( host_config.hostname, service.check_plugin_name, kwargs, is_manual_check(host_config.hostname, service.id()), service.description, ), [] return True, True, result