def _do_inv_for_realhost(host_config, sources, multi_host_sections, hostname, ipaddress, inventory_tree, status_data_tree): # type: (config.HostConfig, data_sources.DataSources, Optional[data_sources.MultiHostSections], HostName, Optional[HostAddress], StructuredDataTree, StructuredDataTree) -> None for source in sources.get_data_sources(): if isinstance(source, data_sources.SNMPDataSource): source.set_on_error("raise") source.set_do_snmp_scan(True) source.disable_data_source_cache() source.set_use_snmpwalk_cache(False) source.set_ignore_check_interval(True) source.set_check_plugin_name_filter( _gather_snmp_check_plugin_names_inventory) if multi_host_sections is not None: # Status data inventory already provides filled multi_host_sections object. # SNMP data source: If 'do_status_data_inv' is enabled there may be # sections for inventory plugins which were not fetched yet. source.enforce_check_plugin_names(None) host_sections = multi_host_sections.add_or_get_host_sections( hostname, ipaddress, deflt=SNMPHostSections()) source.set_fetched_check_plugin_names( set(host_sections.sections)) host_sections_from_source = source.run() host_sections.update(host_sections_from_source) if multi_host_sections is None: multi_host_sections = sources.get_host_sections() console.step("Executing inventory plugins") import cmk.base.inventory_plugins as inventory_plugins # pylint: disable=import-outside-toplevel console.verbose("Plugins:") for section_name, plugin in inventory_plugins.sorted_inventory_plugins(): section_content = multi_host_sections.get_section_content( hostname, ipaddress, section_name, for_discovery=False) if not section_content: # section not present (None or []) # Note: this also excludes existing sections without info.. continue if all([x in [[], {}, None] for x in section_content]): # Inventory plugins which get parsed info from related # check plugin may have more than one return value, eg # parse function of oracle_tablespaces returns ({}, {}) continue console.verbose(" %s%s%s%s" % (tty.green, tty.bold, section_name, tty.normal)) # Inventory functions can optionally have a second argument: parameters. # These are configured via rule sets (much like check parameters). inv_function = plugin["inv_function"] kwargs = cmk.utils.misc.make_kwargs_for( inv_function, inventory_tree=inventory_tree, status_data_tree=status_data_tree) non_kwargs = set( cmk.utils.misc.getfuncargs(inv_function)) - set(kwargs) args = [section_content] if len(non_kwargs) == 2: args += [host_config.inventory_parameters(section_name)] inv_function(*args, **kwargs) console.verbose("\n")
def _get_relevant_raw_sections(services: List[Service], host_config: config.HostConfig): # see if we can remove this function, once inventory plugins have been migrated to # the new API. In particular, we schould be able to get rid of the imports. if host_config.do_status_data_inventory: # This is called during checking, but the inventory plugins are not loaded yet import cmk.base.inventory_plugins as inventory_plugins # pylint: disable=import-outside-toplevel from cmk.base.inventory import get_inventory_context # pylint: disable=import-outside-toplevel from cmk.base.check_api import get_check_api_context # pylint: disable=import-outside-toplevel inventory_plugins.load_plugins(get_check_api_context, get_inventory_context) inventory_plugin_names: Iterable[InventoryPluginName] = (InventoryPluginName( n.split('.')[0]) for n, _plugin in inventory_plugins.sorted_inventory_plugins()) else: inventory_plugin_names = () return config.get_relevant_raw_sections( check_plugin_names=(s.check_plugin_name for s in services), inventory_plugin_names=inventory_plugin_names, )
def _do_inv_for_realhost( config_cache: config.ConfigCache, host_config: config.HostConfig, sources: data_sources.DataSources, multi_host_sections: Optional[MultiHostSections], hostname: HostName, ipaddress: Optional[HostAddress], inventory_tree: StructuredDataTree, status_data_tree: StructuredDataTree, ) -> None: for source in sources: if isinstance(source, data_sources.snmp.SNMPDataSource): source.detector.on_error = "raise" # default source.detector.do_snmp_scan = True data_sources.snmp.SNMPDataSource.disable_data_source_cache() source.set_use_snmpwalk_cache(False) source.set_ignore_check_interval(True) if multi_host_sections is not None: # Status data inventory already provides filled multi_host_sections object. # SNMP data source: If 'do_status_data_inv' is enabled there may be # sections for inventory plugins which were not fetched yet. host_sections = multi_host_sections.setdefault( HostKey(hostname, ipaddress, source.source_type), SNMPHostSections(), ) source.set_fetched_raw_section_names( set(host_sections.sections)) host_sections.update(source.run(selected_raw_sections=None)) if multi_host_sections is None: multi_host_sections = data_sources.make_host_sections( config_cache, host_config, ipaddress, sources, max_cachefile_age=host_config.max_cachefile_age, selected_raw_sections=None, ) section.section_step("Executing inventory plugins") import cmk.base.inventory_plugins as inventory_plugins # pylint: disable=import-outside-toplevel console.verbose("Plugins:") for section_name, plugin in inventory_plugins.sorted_inventory_plugins(): section_content = multi_host_sections.get_section_content( HostKey(hostname, ipaddress, SourceType.HOST), check_api_utils.HOST_PRECEDENCE, section_name, for_discovery=False, ) if not section_content: # section not present (None or []) # Note: this also excludes existing sections without info.. continue if all([x in [[], {}, None] for x in section_content]): # Inventory plugins which get parsed info from related # check plugin may have more than one return value, eg # parse function of oracle_tablespaces returns ({}, {}) continue console.verbose(" %s%s%s%s" % (tty.green, tty.bold, section_name, tty.normal)) # Inventory functions can optionally have a second argument: parameters. # These are configured via rule sets (much like check parameters). inv_function = plugin["inv_function"] kwargs = cmk.utils.misc.make_kwargs_for( inv_function, inventory_tree=inventory_tree, status_data_tree=status_data_tree) non_kwargs = set( cmk.utils.misc.getfuncargs(inv_function)) - set(kwargs) args = [section_content] if len(non_kwargs) == 2: args += [host_config.inventory_parameters(section_name)] inv_function(*args, **kwargs) console.verbose("\n")
def _do_inv_for_realhost(host_config, sources, multi_host_sections, hostname, ipaddress, inventory_tree, status_data_tree): for source in sources.get_data_sources(): if isinstance(source, data_sources.SNMPDataSource): source.set_on_error("raise") source.set_do_snmp_scan(True) source.disable_data_source_cache() source.set_use_snmpwalk_cache(False) source.set_ignore_check_interval(True) source.set_check_plugin_name_filter( _gather_snmp_check_plugin_names_inventory) if multi_host_sections is not None: # Status data inventory already provides filled multi_host_sections object. # SNMP data source: If 'do_status_data_inv' is enabled there may be # sections for inventory plugins which were not fetched yet. source.enforce_check_plugin_names(None) host_sections = multi_host_sections.add_or_get_host_sections( hostname, ipaddress) source.set_fetched_check_plugin_names( host_sections.sections.keys()) host_sections_from_source = source.run() host_sections.update(host_sections_from_source) if multi_host_sections is None: multi_host_sections = sources.get_host_sections() console.step("Executing inventory plugins") import cmk.base.inventory_plugins as inventory_plugins console.verbose("Plugins:") for section_name, plugin in inventory_plugins.sorted_inventory_plugins(): section_content = multi_host_sections.get_section_content( hostname, ipaddress, section_name, for_discovery=False) # TODO: Don't we need to take config.check_info[check_plugin_name]["handle_empty_info"]: # like it is done in checking.execute_check()? Standardize this! if not section_content: # section not present (None or []) # Note: this also excludes existing sections without info.. continue if all([x in [[], {}, None] for x in section_content]): # Inventory plugins which get parsed info from related # check plugin may have more than one return value, eg # parse function of oracle_tablespaces returns ({}, {}) continue console.verbose(" %s%s%s%s" % (tty.green, tty.bold, section_name, tty.normal)) # Inventory functions can optionally have a second argument: parameters. # These are configured via rule sets (much like check parameters). inv_function = plugin["inv_function"] inv_function_args = inspect.getargspec(inv_function).args kwargs = {} for dynamic_arg_name, dynamic_arg_value in [ ("inventory_tree", inventory_tree), ("status_data_tree", status_data_tree), ]: if dynamic_arg_name in inv_function_args: inv_function_args.remove(dynamic_arg_name) kwargs[dynamic_arg_name] = dynamic_arg_value if len(inv_function_args) == 2: params = host_config.inventory_parameters(section_name) args = [section_content, params] else: args = [section_content] inv_function(*args, **kwargs) console.verbose("\n")