def do_discovery( arg_hostnames: Set[HostName], *, selected_sections: SectionNameCollection, run_plugin_names: Container[CheckPluginName], arg_only_new: bool, only_host_labels: bool = False, ) -> None: config_cache = config.get_config_cache() use_caches = not arg_hostnames or cmk.core_helpers.cache.FileCacheFactory.maybe on_error = "raise" if cmk.utils.debug.enabled() else "warn" discovery_parameters = DiscoveryParameters( on_error=on_error, load_labels=arg_only_new, save_labels=True, only_host_labels=only_host_labels, ) host_names = _preprocess_hostnames(arg_hostnames, config_cache, only_host_labels) mode = Mode.DISCOVERY if selected_sections is NO_SELECTION else Mode.FORCE_SECTIONS # Now loop through all hosts for host_name in sorted(host_names): host_config = config_cache.get_host_config(host_name) section.section_begin(host_name) try: ipaddress = config.lookup_ip_address(host_config) parsed_sections_broker, _results = make_broker( config_cache=config_cache, host_config=host_config, ip_address=ipaddress, mode=mode, selected_sections=selected_sections, file_cache_max_age=config.discovery_max_cachefile_age() if use_caches else 0, fetcher_messages=(), force_snmp_cache_refresh=False, on_scan_error=on_error, ) _do_discovery_for( host_name, ipaddress, parsed_sections_broker, run_plugin_names, arg_only_new, discovery_parameters, ) except Exception as e: if cmk.utils.debug.enabled(): raise section.section_error("%s" % e) finally: cmk.utils.cleanup.cleanup_globals()
def check_discovery( host_name: HostName, ipaddress: Optional[HostAddress], *, # The next argument *must* remain optional for the DiscoCheckExecutor. # See Also: `cmk.base.agent_based.checking.do_check()`. fetcher_messages: Sequence[FetcherMessage] = (), ) -> ActiveCheckResult: # Note: '--cache' is set in core_cmc, nagios template or even on CL and means: # 1. use caches as default: # - Set FileCacheFactory.maybe = True (set max_cachefile_age, else 0) # - Set FileCacheFactory.use_outdated = True # 2. Then these settings are used to read cache file or not config_cache = config.get_config_cache() host_config = config_cache.get_host_config(host_name) params = host_config.discovery_check_parameters if params is None: params = host_config.default_discovery_check_parameters() rediscovery_parameters = params.get("inventory_rediscovery", {}) discovery_mode = DiscoveryMode(rediscovery_parameters.get("mode")) # In case of keepalive discovery we always have an ipaddress. When called as non keepalive # ipaddress is always None if ipaddress is None and not host_config.is_cluster: ipaddress = config.lookup_ip_address(host_config) parsed_sections_broker, source_results = make_broker( config_cache=config_cache, host_config=host_config, ip_address=ipaddress, mode=Mode.DISCOVERY, fetcher_messages=fetcher_messages, selected_sections=NO_SELECTION, file_cache_max_age=(config.discovery_max_cachefile_age() if cmk.core_helpers.cache.FileCacheFactory.maybe else 0), force_snmp_cache_refresh=False, on_scan_error=OnError.RAISE, ) host_labels = analyse_host_labels( host_config=host_config, ipaddress=ipaddress, parsed_sections_broker=parsed_sections_broker, load_labels=True, save_labels=False, on_error=OnError.RAISE, ) services = _get_host_services( host_config, ipaddress, parsed_sections_broker, on_error=OnError.RAISE, ) (status, infotexts, long_infotexts, perfdata), need_rediscovery = _aggregate_subresults( _check_service_lists( host_name=host_name, services_by_transition=services, params=params, service_filters=_ServiceFilters.from_settings(rediscovery_parameters), discovery_mode=discovery_mode, ), _check_host_labels( host_labels, int(params.get("severity_new_host_label", 1)), discovery_mode, ), _check_data_sources(source_results, mode=Mode.DISCOVERY), ) if need_rediscovery: autodiscovery_queue = _AutodiscoveryQueue() if host_config.is_cluster and host_config.nodes: for nodename in host_config.nodes: autodiscovery_queue.add(nodename) else: autodiscovery_queue.add(host_name) infotexts = list(infotexts) + ["rediscovery scheduled"] return status, infotexts, long_infotexts, perfdata
def check_discovery( host_name: HostName, ipaddress: Optional[HostAddress], *, # The next argument *must* remain optional for the DiscoCheckExecutor. # See Also: `cmk.base.agent_based.checking.do_check()`. fetcher_messages: Sequence[FetcherMessage] = (), ) -> Tuple[int, List[str], List[str], List[Tuple]]: # Note: '--cache' is set in core_cmc, nagios template or even on CL and means: # 1. use caches as default: # - Set FileCacheFactory.maybe = True (set max_cachefile_age, else 0) # - Set FileCacheFactory.use_outdated = True # 2. Then these settings are used to read cache file or not config_cache = config.get_config_cache() host_config = config_cache.get_host_config(host_name) discovery_parameters = DiscoveryParameters( on_error="raise", load_labels=True, save_labels=False, only_host_labels=False, ) params = host_config.discovery_check_parameters if params is None: params = host_config.default_discovery_check_parameters() discovery_mode = DiscoveryMode(_get_rediscovery_parameters(params).get("mode")) # In case of keepalive discovery we always have an ipaddress. When called as non keepalive # ipaddress is always None if ipaddress is None and not host_config.is_cluster: ipaddress = config.lookup_ip_address(host_config) parsed_sections_broker, source_results = make_broker( config_cache=config_cache, host_config=host_config, ip_address=ipaddress, mode=Mode.DISCOVERY, fetcher_messages=fetcher_messages, selected_sections=NO_SELECTION, file_cache_max_age=(config.discovery_max_cachefile_age() if cmk.core_helpers.cache.FileCacheFactory.maybe else 0), force_snmp_cache_refresh=False, on_scan_error=discovery_parameters.on_error, ) services, host_label_discovery_result = _get_host_services( host_config, ipaddress, parsed_sections_broker, discovery_parameters, ) status, infotexts, long_infotexts, perfdata, need_rediscovery = _aggregate_subresults( _check_service_lists(host_name, services, params, discovery_mode), _check_host_labels( host_label_discovery_result, int(params.get("severity_new_host_label", 1)), discovery_mode, ), _check_data_sources(source_results), ) if need_rediscovery: if host_config.is_cluster and host_config.nodes: for nodename in host_config.nodes: _set_rediscovery_flag(nodename) else: _set_rediscovery_flag(host_name) infotexts.append(u"rediscovery scheduled") return status, infotexts, long_infotexts, perfdata