def _discover_plugins_services( *, check_plugin_name: CheckPluginName, host_name: HostName, ipaddress: Optional[HostAddress], parsed_sections_broker: ParsedSectionsBroker, on_error: OnError, ) -> Iterator[Service]: # Skip this check type if is ignored for that host if config.service_ignored(host_name, check_plugin_name, None): console.vverbose(" Skip ignored check plugin name '%s'\n" % check_plugin_name) return check_plugin = agent_based_register.get_check_plugin(check_plugin_name) if check_plugin is None: console.warning(" Missing check plugin: '%s'\n" % check_plugin_name) return host_key = HostKey( host_name, ipaddress, SourceType.MANAGEMENT if check_plugin.name.is_management_name() else SourceType.HOST, ) try: kwargs = parsed_sections_broker.get_section_kwargs( host_key, check_plugin.sections) except Exception as exc: if cmk.utils.debug.enabled() or on_error is OnError.RAISE: raise if on_error is OnError.WARN: console.warning(" Exception while parsing agent section: %s\n" % exc) return if not kwargs: return disco_params = config.get_discovery_parameters(host_name, check_plugin) if disco_params is not None: kwargs["params"] = disco_params try: plugins_services = check_plugin.discovery_function(**kwargs) yield from _enriched_discovered_services(host_name, check_plugin.name, plugins_services) except Exception as e: if on_error is OnError.RAISE: raise if on_error is OnError.WARN: console.warning( " Exception in discovery function of check plugin '%s': %s" % (check_plugin.name, e))
def _do_inv_for_realhost( host_config: config.HostConfig, ipaddress: Optional[HostAddress], *, parsed_sections_broker: ParsedSectionsBroker, run_plugin_names: Container[InventoryPluginName], ) -> InventoryTrees: tree_aggregator = _TreeAggregator() _set_cluster_property(tree_aggregator.trees.inventory, host_config) section.section_step("Executing inventory plugins") for inventory_plugin in agent_based_register.iter_all_inventory_plugins(): if inventory_plugin.name not in run_plugin_names: continue for source_type in (SourceType.HOST, SourceType.MANAGEMENT): kwargs = parsed_sections_broker.get_section_kwargs( HostKey(host_config.hostname, ipaddress, source_type), inventory_plugin.sections, ) if not kwargs: console.vverbose(" %s%s%s%s: skipped (no data)\n", tty.yellow, tty.bold, inventory_plugin.name, tty.normal) continue # Inventory functions can optionally have a second argument: parameters. # These are configured via rule sets (much like check parameters). if inventory_plugin.inventory_ruleset_name is not None: kwargs["params"] = host_config.inventory_parameters( inventory_plugin.inventory_ruleset_name) exception = tree_aggregator.aggregate_results( inventory_plugin.inventory_function(**kwargs), ) if exception: console.warning(" %s%s%s%s: failed: %s", tty.red, tty.bold, inventory_plugin.name, tty.normal, exception) else: console.verbose(" %s%s%s%s", tty.green, tty.bold, inventory_plugin.name, tty.normal) console.vverbose(": ok\n") console.verbose("\n") tree_aggregator.trees.inventory.normalize_nodes() tree_aggregator.trees.status_data.normalize_nodes() return tree_aggregator.trees
def test_get_section_kwargs(patch_register, required_sections, expected_result): node_section_content = { SectionName("one"): NODE_1, SectionName("two"): NODE_1, SectionName("three"): NODE_1 } host_key = HostKey("node1", "127.0.0.1", SourceType.HOST) parsed_sections_broker = ParsedSectionsBroker( {host_key: AgentHostSections(sections=node_section_content)}) kwargs = parsed_sections_broker.get_section_kwargs( host_key, [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_kwargs(patch_register, required_sections, expected_result): node_sections = AgentHostSections(sections={ SectionName("one"): NODE_1, SectionName("two"): NODE_1, SectionName("three"): NODE_1 }) host_key = HostKey("node1", "127.0.0.1", SourceType.HOST) parsed_sections_broker = ParsedSectionsBroker({ host_key: SectionsParser(host_sections=node_sections), }) kwargs = parsed_sections_broker.get_section_kwargs( host_key, [ParsedSectionName(n) for n in required_sections], ) assert expected_result == kwargs
def get_aggregated_result( parsed_sections_broker: ParsedSectionsBroker, host_config: config.HostConfig, ipaddress: Optional[HostAddress], service: Service, plugin: Optional[checking_classes.CheckPlugin], params_function: Callable[[], Parameters], *, value_store_manager: item_state.ValueStoreManager, ) -> AggregatedResult: """Run the check function and aggregate the subresults This function is also called during discovery. """ if plugin is None: return AggregatedResult( submit=True, data_received=True, result=CHECK_NOT_IMPLEMENTED, cache_info=None, ) check_function = (plugin.cluster_check_function if host_config.is_cluster else plugin.check_function) source_type = (SourceType.MANAGEMENT if service.check_plugin_name.is_management_name() else SourceType.HOST) config_cache = config.get_config_cache() kwargs = {} try: kwargs = parsed_sections_broker.get_section_cluster_kwargs( config_cache.get_clustered_service_node_keys( host_config.hostname, source_type, service.description, ) or [], plugin.sections, ) if host_config.is_cluster else parsed_sections_broker.get_section_kwargs( HostKey(host_config.hostname, ipaddress, source_type), plugin.sections, ) if not kwargs and not service.check_plugin_name.is_management_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 = parsed_sections_broker.get_section_cluster_kwargs( config_cache.get_clustered_service_node_keys( host_config.hostname, SourceType.MANAGEMENT, service.description, ) or [], plugin.sections, ) if host_config.is_cluster else parsed_sections_broker.get_section_kwargs( HostKey(host_config.hostname, ipaddress, SourceType.MANAGEMENT), plugin.sections, ) if not kwargs: # no data found return AggregatedResult( submit=False, data_received=False, result=RECEIVED_NO_DATA, cache_info=None, ) if service.item is not None: kwargs["item"] = service.item if plugin.check_default_parameters is not None: kwargs["params"] = params_function() with plugin_contexts.current_host(host_config.hostname), \ plugin_contexts.current_service(service), \ value_store_manager.namespace(service.id()): result = _aggregate_results(check_function(**kwargs)) except (item_state.MKCounterWrapped, checking_classes.IgnoreResultsError) as e: msg = str(e) or "No service summary available" return AggregatedResult( submit=False, data_received=True, result=(0, msg, []), cache_info=None, ) except MKTimeout: raise except Exception: if cmk.utils.debug.enabled(): raise table = check_table.get_check_table(host_config.hostname, skip_autochecks=True) result = 3, cmk.base.crash_reporting.create_check_crash_dump( host_name=host_config.hostname, service_name=service.description, plugin_name=service.check_plugin_name, plugin_kwargs=kwargs, is_manual=service.id() in table, ), [] return AggregatedResult( submit=True, data_received=True, result=result, cache_info=parsed_sections_broker.get_cache_info(plugin.sections), )