def test_filter_by_management_board_SNMP_host_with_SNMP_mgmt_board( monkeypatch, for_discovery, host_result, mgmt_board_result): ts = Scenario() ts.add_host("this_host", tags={"snmp_ds": "snmp-v1", "agent": "no-agent"}) config_cache = ts.apply(monkeypatch) h = config_cache.get_host_config("this_host") h.has_management_board = True found_check_plugins = [ c for c in _check_plugins() if c.startswith("snmp_") ] monkeypatch.setattr(config, "check_info", found_check_plugins) assert config.filter_by_management_board( "this_host", found_check_plugins, False, for_discovery=for_discovery) == set(host_result) found_check_plugins = [ c for c in _check_plugins() if c.startswith("snmp_") ] monkeypatch.setattr(config, "check_info", found_check_plugins) assert config.filter_by_management_board( "this_host", found_check_plugins, True, for_discovery=for_discovery) == set(mgmt_board_result)
def test_filter_by_management_board_unknown_check_plugins(monkeypatch, for_discovery, result): ts = Scenario() ts.add_host("this_host") ts.apply(monkeypatch) found_check_plugins = [c for c in _check_plugins()] monkeypatch.setattr(config, "check_info", []) assert config.filter_by_management_board( "this_host", found_check_plugins, False, for_discovery=for_discovery) == set(result)
def test_filter_by_management_board_TCP_host_without_mgmt_board(monkeypatch, for_discovery, result): ts = Scenario() ts.add_host("this_host") ts.apply(monkeypatch) found_check_plugins = [c for c in _check_plugins() if c.startswith("tcp_")] monkeypatch.setattr(config, "check_info", found_check_plugins) assert config.filter_by_management_board( "this_host", found_check_plugins, False, for_discovery=for_discovery) == set(result)
def test_filter_by_management_board_dual_host_without_mgmt_board(monkeypatch, for_discovery, result): ts = Scenario() ts.add_host("this_host", tags={"snmp_ds": "snmp-v1", "agent": "cmk-agent"}) ts.apply(monkeypatch) found_check_plugins = [c for c in _check_plugins()] monkeypatch.setattr(config, "check_info", found_check_plugins) assert config.filter_by_management_board( "this_host", found_check_plugins, False, for_discovery=for_discovery) == set(result)
def enforce_check_plugin_names(self, check_plugin_names): """ Returns a subset of beforehand gathered check plugin names which are supported by the data source. Example: management board checks only for management board data sources """ if check_plugin_names is not None: self._enforced_check_plugin_names = config.filter_by_management_board( self._hostname, check_plugin_names, self._for_mgmt_board) else: self._enforced_check_plugin_names = check_plugin_names
def _snmp_scan(host_config, on_error="ignore", for_inv=False, do_snmp_scan=True, for_mgmt_board=False): import cmk_base.inventory_plugins as inventory_plugins # Make hostname globally available for scan functions. # This is rarely used, but e.g. the scan for if/if64 needs # this to evaluate if_disabled_if64_checks. check_api_utils.set_hostname(host_config.hostname) snmp.initialize_single_oid_cache(host_config) console.vverbose(" SNMP scan:\n") if not config.get_config_cache().in_binary_hostlist( host_config.hostname, config.snmp_without_sys_descr): for oid, name in [(".1.3.6.1.2.1.1.1.0", "system description"), (".1.3.6.1.2.1.1.2.0", "system object")]: value = snmp.get_single_oid(host_config, oid, do_snmp_scan=do_snmp_scan) if value is None: raise MKSNMPError( "Cannot fetch %s OID %s. This might be OK for some bogus devices. " "In that case please configure the ruleset \"Hosts without system " "description OID\" to tell Check_MK not to fetch the system " "description and system object OIDs." % (name, oid)) else: # Fake OID values to prevent issues with a lot of scan functions console.vverbose( " Skipping system description OID " "(Set .1.3.6.1.2.1.1.1.0 and .1.3.6.1.2.1.1.2.0 to \"\")\n") snmp.set_single_oid_cache(host_config, ".1.3.6.1.2.1.1.1.0", "") snmp.set_single_oid_cache(host_config, ".1.3.6.1.2.1.1.2.0", "") found_check_plugin_names = [] if for_inv: items = inventory_plugins.inv_info.items() else: items = config.check_info.items() positive_found = [] default_found = [] for check_plugin_name, _unused_check in items: if config.service_ignored(host_config.hostname, check_plugin_name, None): continue else: if for_inv and not inventory_plugins.is_snmp_plugin( check_plugin_name): continue elif not for_inv and not cmk_base.check_utils.is_snmp_check( check_plugin_name): continue section_name = cmk_base.check_utils.section_name_of(check_plugin_name) # The scan function should be assigned to the section_name, because # subchecks sharing the same SNMP info of course should have # an identical scan function. But some checks do not do this # correctly if check_plugin_name in config.snmp_scan_functions: scan_function = config.snmp_scan_functions[check_plugin_name] elif section_name in config.snmp_scan_functions: scan_function = config.snmp_scan_functions[section_name] elif section_name in inventory_plugins.inv_info: scan_function = inventory_plugins.inv_info[section_name].get( "snmp_scan_function") else: scan_function = None if scan_function: try: def oid_function(oid, default_value=None, cp_name=check_plugin_name): value = snmp.get_single_oid(host_config, oid, cp_name, do_snmp_scan=do_snmp_scan) return default_value if value is None else value result = scan_function(oid_function) if result is not None and not isinstance(result, (str, bool)): if on_error == "warn": console.warning( " SNMP scan function of %s returns invalid type %s." % (check_plugin_name, type(result))) elif on_error == "raise": raise MKGeneralException("SNMP Scan aborted.") elif result: found_check_plugin_names.append(check_plugin_name) positive_found.append(check_plugin_name) except MKGeneralException: # some error messages which we explicitly want to show to the user # should be raised through this raise except Exception: if on_error == "warn": console.warning( " Exception in SNMP scan function of %s" % check_plugin_name) elif on_error == "raise": raise else: found_check_plugin_names.append(check_plugin_name) default_found.append(check_plugin_name) _output_snmp_check_plugins("SNMP scan found", positive_found) if default_found: _output_snmp_check_plugins("SNMP without scan function", default_found) filtered = config.filter_by_management_board(host_config.hostname, found_check_plugin_names, for_mgmt_board, for_discovery=True, for_inventory=for_inv) _output_snmp_check_plugins("SNMP filtered check plugin names", filtered) snmp.write_single_oid_cache(host_config) return sorted(filtered)