def test_discovered_service_labels_repr(): labels = DiscoveredServiceLabels() labels.add_label(ServiceLabel(u"äbc", u"123")) labels.add_label(ServiceLabel(u"ccc", u"ddd")) assert repr( labels ) == "DiscoveredServiceLabels(ServiceLabel('ccc', 'ddd'), ServiceLabel('äbc', '123'))"
def _read_raw_autochecks_uncached( self, hostname: HostName, ) -> Sequence[_AutocheckService]: """Read automatically discovered checks of one host""" path = _autochecks_path_for(hostname) try: autochecks_raw = _load_raw_autochecks( path=path, check_variables=None, ) except SyntaxError as e: logger.exception("Syntax error in file %s: %s", path, e) if cmk.utils.debug.enabled(): raise return [] except Exception as e: logger.exception("Error in file %s:\n%s", path, e) if cmk.utils.debug.enabled(): raise return [] services = [] for entry in autochecks_raw: try: item = entry["item"] except TypeError: # pre 1.6 tuple! raise MKGeneralException( "Invalid check entry '%r' of host '%s' (%s) found. This " "entry is in pre Checkmk 1.6 format and needs to be converted. This is " 'normally done by "cmk-update-config -v" during "omd update". Please ' 'execute "cmk-update-config -v" for convertig the old configuration.' % (entry, hostname, path)) try: plugin_name = CheckPluginName( maincheckify(entry["check_plugin_name"])) assert item is None or isinstance(item, str) except Exception: raise MKGeneralException( "Invalid check entry '%r' of host '%s' (%s) found. This " "entry is in pre Checkmk 2.0 format and needs to be converted. This is " 'normally done by "cmk-update-config -v" during "omd update". Please ' 'execute "cmk-update-config -v" for convertig the old configuration.' % (entry, hostname, path)) labels = DiscoveredServiceLabels() for label_id, label_value in entry["service_labels"].items(): labels.add_label(ServiceLabel(label_id, label_value)) services.append( _AutocheckService( check_plugin_name=plugin_name, item=item, discovered_parameters=entry["parameters"], service_labels=labels, )) return services
def _parse_discovered_service_label_from_dict(dict_service_labels: Dict) -> DiscoveredServiceLabels: labels = DiscoveredServiceLabels() if not isinstance(dict_service_labels, dict): return labels for key, value in dict_service_labels.items(): if key is not None: labels.add_label(ServiceLabel( ensure_str(key), ensure_str(value), )) return labels
def _parse_discovered_service_label_from_ast(ast_service_labels): # type: (ast.Dict) -> DiscoveredServiceLabels labels = DiscoveredServiceLabels() if not hasattr(ast_service_labels, "keys"): return labels for key, value in zip(ast_service_labels.keys, ast_service_labels.values): if key is not None: # mypy does not get the types of the ast objects here labels.add_label(ServiceLabel( key.s, value.s)) # type: ignore[attr-defined] return labels
def _read_raw_autochecks_of(self, hostname): # type: (HostName) -> List[Service] """Read automatically discovered checks of one host""" basedir = cmk.utils.paths.autochecks_dir filepath = basedir + '/' + hostname + '.mk' result = [] # type: List[Service] if not os.path.exists(filepath): return result check_config = config.get_check_variables() try: cmk.base.console.vverbose("Loading autochecks from %s\n", filepath) autochecks_raw = eval( open(filepath).read().decode("utf-8"), check_config, check_config) # type: List[Dict] except SyntaxError as e: cmk.base.console.verbose("Syntax error in file %s: %s\n", filepath, e, stream=sys.stderr) if cmk.utils.debug.enabled(): raise return result except Exception as e: cmk.base.console.verbose("Error in file %s:\n%s\n", filepath, e, stream=sys.stderr) if cmk.utils.debug.enabled(): raise return result for entry in autochecks_raw: if isinstance(entry, tuple): raise MKGeneralException( "Invalid check entry '%r' of host '%s' (%s) found. This " "entry is in pre Checkmk 1.6 format and needs to be converted. This is " "normally done by \"cmk-update-config -v\" during \"omd update\". Please " "execute \"cmk-update-config -v\" for convertig the old configuration." % (entry, hostname, filepath)) labels = DiscoveredServiceLabels() for label_id, label_value in entry["service_labels"].items(): labels.add_label(ServiceLabel(label_id, label_value)) # With Check_MK 1.2.7i3 items are now defined to be unicode strings. Convert # items from existing autocheck files for compatibility. TODO remove this one day item = entry["item"] if isinstance(item, str): item = convert_to_unicode(item) if not isinstance(entry["check_plugin_name"], six.string_types): raise MKGeneralException("Invalid entry '%r' in check table of host '%s': " "The check type must be a string." % (entry, hostname)) check_plugin_name = str(entry["check_plugin_name"]) try: description = config.service_description(hostname, check_plugin_name, item) except Exception: continue # ignore result.append( Service( check_plugin_name=check_plugin_name, item=item, description=description, parameters=entry["parameters"], service_labels=labels, )) return result
def _read_raw_autochecks_uncached( self, hostname: HostName, service_description: GetServiceDescription, ) -> List[Service]: """Read automatically discovered checks of one host""" path = _autochecks_path_for(hostname) try: autochecks_raw = _load_raw_autochecks( path=path, check_variables=None, ) except SyntaxError as e: console.verbose("Syntax error in file %s: %s\n", path, e, stream=sys.stderr) if cmk.utils.debug.enabled(): raise return [] except Exception as e: console.verbose("Error in file %s:\n%s\n", path, e, stream=sys.stderr) if cmk.utils.debug.enabled(): raise return [] services: List[Service] = [] for entry in autochecks_raw: try: item = entry["item"] except TypeError: # pre 1.6 tuple! raise MKGeneralException( "Invalid check entry '%r' of host '%s' (%s) found. This " "entry is in pre Checkmk 1.6 format and needs to be converted. This is " "normally done by \"cmk-update-config -v\" during \"omd update\". Please " "execute \"cmk-update-config -v\" for convertig the old configuration." % (entry, hostname, path)) try: plugin_name = CheckPluginName( maincheckify(entry["check_plugin_name"])) assert item is None or isinstance(item, str) except Exception: raise MKGeneralException( "Invalid check entry '%r' of host '%s' (%s) found. This " "entry is in pre Checkmk 1.7 format and needs to be converted. This is " "normally done by \"cmk-update-config -v\" during \"omd update\". Please " "execute \"cmk-update-config -v\" for convertig the old configuration." % (entry, hostname, path)) labels = DiscoveredServiceLabels() for label_id, label_value in entry["service_labels"].items(): labels.add_label(ServiceLabel(label_id, label_value)) try: description = service_description(hostname, plugin_name, item) except Exception: continue # ignore services.append( Service( check_plugin_name=plugin_name, item=item, description=description, parameters=entry["parameters"], service_labels=labels, )) return services
def _read_raw_autochecks_uncached( self, hostname: HostName, service_description: GetServiceDescription, ) -> List[Service]: """Read automatically discovered checks of one host""" path = _autochecks_path_for(hostname) try: autochecks_raw = _load_raw_autochecks( path=path, check_variables=None, ) except SyntaxError as e: console.verbose("Syntax error in file %s: %s\n", path, e, stream=sys.stderr) if cmk.utils.debug.enabled(): raise return [] except Exception as e: console.verbose("Error in file %s:\n%s\n", path, e, stream=sys.stderr) if cmk.utils.debug.enabled(): raise return [] services: List[Service] = [] for entry in autochecks_raw: if isinstance(entry, tuple): raise MKGeneralException( "Invalid check entry '%r' of host '%s' (%s) found. This " "entry is in pre Checkmk 1.6 format and needs to be converted. This is " "normally done by \"cmk-update-config -v\" during \"omd update\". Please " "execute \"cmk-update-config -v\" for convertig the old configuration." % (entry, hostname, path)) labels = DiscoveredServiceLabels() for label_id, label_value in entry["service_labels"].items(): labels.add_label(ServiceLabel(label_id, label_value)) # With Check_MK 1.2.7i3 items are now defined to be unicode strings. Convert # items from existing autocheck files for compatibility. TODO remove this one day item = entry["item"] if not isinstance(entry["check_plugin_name"], str): raise MKGeneralException( "Invalid entry '%r' in check table of host '%s': " "The check type must be a string." % (entry, hostname)) check_plugin_name_str = str(entry["check_plugin_name"]) # TODO (mo): centralize maincheckify: CMK-4295 check_plugin_name = CheckPluginName( maincheckify(check_plugin_name_str)) try: description = service_description(hostname, check_plugin_name, item) except Exception: continue # ignore services.append( Service( check_plugin_name=check_plugin_name_str, item=item, description=description, parameters=entry["parameters"], service_labels=labels, )) return services