def test_one_nonsnmp_source(self, hostname, ipaddress, mode, config_cache, host_config, source): source = source(hostname, ipaddress, mode=mode) assert source.configurator.source_type is SourceType.HOST mhs = MultiHostSections() update_host_sections( mhs, make_nodes( config_cache, host_config, ipaddress, mode=mode, sources=[source], ), max_cachefile_age=0, selected_raw_sections=None, host_config=host_config, ) assert len(mhs) == 1 key = HostKey(hostname, ipaddress, source.configurator.source_type) assert key in mhs section = mhs[key] assert isinstance(section, AgentHostSections) assert len(section.sections) == 1 assert section.sections[SectionName("section_name_%s" % hostname)] == [["section_content"]]
def test_no_sources(self, cluster, nodes, config_cache, host_config, mode): mhs = MultiHostSections() update_host_sections( mhs, make_nodes( config_cache, host_config, None, mode=mode, sources=[], ), max_cachefile_age=0, selected_raw_sections=None, host_config=host_config, ) assert len(mhs) == len(nodes) key_clu = HostKey(cluster, None, SourceType.HOST) assert key_clu not in mhs for hostname, addr in nodes.items(): key = HostKey(hostname, addr, SourceType.HOST) assert key in mhs section = mhs[key] # yapf: disable assert (section.sections[SectionName("section_name_%s" % hostname)] == [["section_content"]]) assert not section.cache_info assert not section.piggybacked_raw_data assert not section.persisted_sections
def test_one_snmp_source(self, hostname, ipaddress, mode, config_cache, host_config): mhs = MultiHostSections() update_host_sections( mhs, make_nodes( config_cache, host_config, ipaddress, mode=mode, sources=[ SNMPConfigurator.snmp( hostname, ipaddress, mode=mode, ).make_checker(), ], ), max_cachefile_age=0, selected_raw_sections=None, host_config=host_config, ) assert len(mhs) == 1 key = HostKey(hostname, ipaddress, SourceType.HOST) assert key in mhs section = mhs[key] assert isinstance(section, SNMPHostSections) assert len(section.sections) == 1 assert section.sections[SectionName("section_name_%s" % hostname)] == [["section_content"]]
def test_no_sources(self, hostname, ipaddress, mode, config_cache, host_config): mhs = MultiHostSections() update_host_sections( mhs, make_nodes( config_cache, host_config, ipaddress, mode=mode, sources=[], ), max_cachefile_age=0, selected_raw_sections=None, host_config=host_config, ) # The length is not zero because the function always sets, # at least, a piggy back section. assert len(mhs) == 1 key = HostKey(hostname, ipaddress, SourceType.HOST) assert key in mhs section = mhs[key] assert isinstance(section, AgentHostSections) # Public attributes from ABCHostSections: assert not section.sections assert not section.cache_info assert not section.piggybacked_raw_data assert not section.persisted_sections
def test_multiple_sources_from_the_same_host( self, hostname, ipaddress, mode, config_cache, host_config, ): sources = [ ProgramConfigurator.ds( hostname, ipaddress, mode=mode, template="", ).make_checker(), TCPConfigurator( hostname, ipaddress, mode=mode, ).make_checker(), ] mhs = MultiHostSections() update_host_sections( mhs, make_nodes( config_cache, host_config, ipaddress, mode=mode, sources=sources, ), max_cachefile_age=0, selected_raw_sections=None, host_config=host_config, ) assert len(mhs) == 1 key = HostKey(hostname, ipaddress, SourceType.HOST) assert key in mhs section = mhs[key] assert isinstance(section, AgentHostSections) assert len(section.sections) == 1 # yapf: disable assert (section.sections[SectionName("section_name_%s" % hostname)] == len(sources) * [["section_content"]])
def test_get_host_sections_cluster(mode, monkeypatch, mocker): hostname = "testhost" hosts = { "host0": "10.0.0.0", "host1": "10.0.0.1", "host2": "10.0.0.2", } address = "1.2.3.4" tags = {"agent": "no-agent"} section_name = SectionName("test_section") config_cache = make_scenario(hostname, tags).apply(monkeypatch) host_config = config.HostConfig.make_host_config(hostname) def lookup_ip_address(host_config, family=None, for_mgmt_board=False): return hosts[host_config.hostname] def make_piggybacked_sections(hc): if hc.nodes == host_config.nodes: return {section_name: True} return {} def check(_, *args, **kwargs): return AgentHostSections(sections={section_name: [[str(section_name)]]}) monkeypatch.setattr( ip_lookup, "lookup_ip_address", lookup_ip_address, ) monkeypatch.setattr( _data_sources, "_make_piggybacked_sections", make_piggybacked_sections, ) monkeypatch.setattr( ABCChecker, "check", check, ) mocker.patch.object( cmk.utils.piggyback, "remove_source_status_file", autospec=True, ) mocker.patch.object( cmk.utils.piggyback, "_store_status_file_of", autospec=True, ) # Create a cluster host_config.nodes = list(hosts.keys()) mhs = MultiHostSections() update_host_sections( mhs, make_nodes( config_cache, host_config, address, mode=mode, sources=make_checkers(host_config, address, mode=mode), ), max_cachefile_age=host_config.max_cachefile_age, selected_raw_sections=None, host_config=host_config, ) assert len(mhs) == len(hosts) == 3 cmk.utils.piggyback._store_status_file_of.assert_not_called() # type: ignore[attr-defined] assert cmk.utils.piggyback.remove_source_status_file.call_count == 3 # type: ignore[attr-defined] for host, addr in hosts.items(): remove_source_status_file = cmk.utils.piggyback.remove_source_status_file remove_source_status_file.assert_any_call(host) # type: ignore[attr-defined] key = HostKey(host, addr, SourceType.HOST) assert key in mhs section = mhs[key] assert len(section.sections) == 1 assert next(iter(section.sections)) == section_name assert not section.cache_info assert not section.piggybacked_raw_data assert not section.persisted_sections
def _do_inv_for_realhost( config_cache: config.ConfigCache, host_config: config.HostConfig, sources: data_sources.Checkers, multi_host_sections: Optional[MultiHostSections], hostname: HostName, ipaddress: Optional[HostAddress], inventory_tree: StructuredDataTree, status_data_tree: StructuredDataTree, ) -> Sequence[Tuple[ABCConfigurator, ABCHostSections]]: results: List[Tuple[ABCConfigurator, ABCHostSections]] = [] for source in sources: if isinstance(source, data_sources.snmp.SNMPChecker): # TODO(ml): This modifies the SNMP fetcher config dynamically. configurator = cast(data_sources.snmp.SNMPConfigurator, source.configurator) configurator.on_snmp_scan_error = "raise" # default data_sources.FileCacheConfigurator.snmp_disabled = True configurator.use_snmpwalk_cache = False configurator.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( # TODO(ml): are # hostname == source.hostname # ipaddress == source.ipaddress # ? HostKey(hostname, ipaddress, source.configurator.source_type), SNMPHostSections(), ) # TODO(ml): This modifies the SNMP fetcher config dynamically. # Can the fetcher handle that on its own? configurator.prefetched_sections = host_sections.sections raw_data = source.configurator.default_raw_data try: with source.configurator.make_fetcher() as fetcher: raw_data = fetcher.fetch(Mode.INVENTORY) except Exception as exc: source.exception = exc hs = source.check(raw_data) results.append((source.configurator, hs)) host_sections.update(hs) if multi_host_sections is None: multi_host_sections = MultiHostSections() hs = data_sources.update_host_sections( multi_host_sections, data_sources.make_nodes( config_cache, host_config, ipaddress, data_sources.Mode.INVENTORY, sources, ), max_cachefile_age=host_config.max_cachefile_age, selected_raw_sections=None, host_config=host_config, ) results.extend(hs) section.section_step("Executing inventory plugins") console.verbose("Plugins:") for inventory_plugin in agent_based_register.iter_all_inventory_plugins(): kwargs = multi_host_sections.get_section_kwargs( HostKey(hostname, ipaddress, SourceType.HOST), inventory_plugin.sections, ) if not kwargs: continue console.verbose( " %s%s%s%s" % (tty.green, tty.bold, inventory_plugin.name, tty.normal)) # 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( str(inventory_plugin.inventory_ruleset_name) ) # TODO (mo): keep type! _aggregate_inventory_results( inventory_plugin.inventory_function(**kwargs), inventory_tree, status_data_tree, ) console.verbose("\n") return results
def do_check( hostname: HostName, ipaddress: Optional[HostAddress], only_check_plugin_names: Optional[Set[CheckPluginName]] = None, fetcher_messages: Optional[List[FetcherMessage]] = None ) -> Tuple[int, List[ServiceDetails], List[ServiceAdditionalDetails], List[str]]: cpu_tracking.start("busy") console.verbose("Checkmk version %s\n", cmk_version.__version__) config_cache = config.get_config_cache() host_config = config_cache.get_host_config(hostname) exit_spec = host_config.exit_code_spec() status: ServiceState = 0 infotexts: List[ServiceDetails] = [] long_infotexts: List[ServiceAdditionalDetails] = [] perfdata: List[str] = [] try: # In case of keepalive we always have an ipaddress (can be 0.0.0.0 or :: when # address is unknown). When called as non keepalive ipaddress may be None or # is already an address (2nd argument) if ipaddress is None and not host_config.is_cluster: ipaddress = ip_lookup.lookup_ip_address(host_config) item_state.load(hostname) # When monitoring Checkmk clusters, the cluster nodes are responsible for fetching all # information from the monitored host and cache the result for the cluster checks to be # performed on the cached information. # # This means that in case of SNMP nodes, they need to take the clustered services of the # node into account, fetch the needed sections and cache them for the cluster host. # # But later, when checking the node services, the node has to only deal with the unclustered # services. belongs_to_cluster = len(config_cache.clusters_of(hostname)) > 0 services_to_fetch = _get_services_to_fetch( host_name=hostname, belongs_to_cluster=belongs_to_cluster, config_cache=config_cache, only_check_plugins=only_check_plugin_names, ) services_to_check = _filter_clustered_services( config_cache=config_cache, host_name=hostname, belongs_to_cluster=belongs_to_cluster, services=services_to_fetch, ) # see which raw sections we may need selected_raw_sections = _get_relevant_raw_sections(services_to_fetch, host_config) sources = data_sources.make_checkers( host_config, ipaddress, mode=data_sources.Mode.CHECKING, ) mhs = MultiHostSections() result = data_sources.update_host_sections( mhs, data_sources.make_nodes( config_cache, host_config, ipaddress, data_sources.Mode.CHECKING, sources, ), selected_raw_sections=selected_raw_sections, max_cachefile_age=host_config.max_cachefile_age, host_config=host_config, fetcher_messages=fetcher_messages, ) num_success, plugins_missing_data = _do_all_checks_on_host( config_cache, host_config, ipaddress, multi_host_sections=mhs, services=services_to_check, only_check_plugins=only_check_plugin_names, ) inventory.do_inventory_actions_during_checking_for( config_cache, host_config, ipaddress, sources=sources, multi_host_sections=mhs, ) if _submit_to_core: item_state.save(hostname) for configurator, host_sections in result: # TODO(ml): This implements the hidden protocol explicitly. This step # is necessary before we get rid of it. checker = configurator.make_checker() checker.host_sections = host_sections source_state, source_output, source_perfdata = checker.get_summary_result() if source_output != "": status = max(status, source_state) infotexts.append("[%s] %s" % (configurator.id, source_output)) perfdata.extend([_convert_perf_data(p) for p in source_perfdata]) if plugins_missing_data: missing_data_status, missing_data_infotext = _check_plugins_missing_data( plugins_missing_data, exit_spec, bool(num_success), ) status = max(status, missing_data_status) infotexts.append(missing_data_infotext) cpu_tracking.end() phase_times = cpu_tracking.get_times() total_times = phase_times["TOTAL"] run_time = total_times[4] infotexts.append("execution time %.1f sec" % run_time) if config.check_mk_perfdata_with_times: perfdata += [ "execution_time=%.3f" % run_time, "user_time=%.3f" % total_times[0], "system_time=%.3f" % total_times[1], "children_user_time=%.3f" % total_times[2], "children_system_time=%.3f" % total_times[3], ] for phase, times in phase_times.items(): if phase in ["agent", "snmp", "ds"]: t = times[4] - sum(times[:4]) # real time - CPU time perfdata.append("cmk_time_%s=%.3f" % (phase, t)) else: perfdata.append("execution_time=%.3f" % run_time) return status, infotexts, long_infotexts, perfdata finally: if _checkresult_file_fd is not None: _close_checkresult_file() # "ipaddress is not None": At least when working with a cluster host it seems the ipaddress # may be None. This needs to be understood in detail and cleaned up. As the InlineSNMP # stats feature is a very rarely used debugging feature, the analyzation and fix is # postponed now. if config.record_inline_snmp_stats \ and ipaddress is not None \ and host_config.snmp_config(ipaddress).is_inline_snmp_host: inline.snmp_stats_save()