def _get_sanitized_and_translated_piggybacked_hostname(self, orig_piggyback_header): piggybacked_hostname = orig_piggyback_header[4:-4] if not piggybacked_hostname: return piggybacked_hostname = config.translate_piggyback_host(self._hostname, piggybacked_hostname) if piggybacked_hostname == self._hostname or not piggybacked_hostname: return # unpiggybacked "normal" host # Protect Check_MK against unallowed host names. Normally source scripts # like agent plugins should care about cleaning their provided host names # up, but we need to be sure here to prevent bugs in Check_MK code. # a) Replace spaces by underscores return piggybacked_hostname.replace(" ", "_")
def _parse_info(self, lines): """Split agent output in chunks, splits lines by whitespaces. Returns a HostSections() object. """ sections = {} # Unparsed info for other hosts. A dictionary, indexed by the piggybacked host name. # The value is a list of lines which were received for this host. piggybacked_raw_data = {} persisted_sections = {} # handle sections with option persist(...) host = None section_content = [] section_options = {} agent_cache_info = {} separator = None encoding = None for line in lines: line = line.rstrip("\r") stripped_line = line.strip() if stripped_line[:4] == '<<<<' and stripped_line[-4:] == '>>>>': host = stripped_line[4:-4] if not host: host = None else: host = config.translate_piggyback_host( self._hostname, host) if host == self._hostname: host = None # unpiggybacked "normal" host # Protect Check_MK against unallowed host names. Normally source scripts # like agent plugins should care about cleaning their provided host names # up, but we need to be sure here to prevent bugs in Check_MK code. # a) Replace spaces by underscores if host: host = host.replace(" ", "_") elif host: # processing data for an other host piggybacked_raw_data.setdefault(host, []).append(line) # Found normal section header # section header has format <<<name:opt1(args):opt2:opt3(args)>>> elif stripped_line[:3] == '<<<' and stripped_line[-3:] == '>>>': section_header = stripped_line[3:-3] headerparts = section_header.split(":") section_name = headerparts[0] section_options = {} for o in headerparts[1:]: opt_parts = o.split("(") opt_name = opt_parts[0] if len(opt_parts) > 1: opt_args = opt_parts[1][:-1] else: opt_args = None section_options[opt_name] = opt_args section_content = sections.get(section_name, None) if section_content is None: # section appears in output for the first time section_content = [] sections[section_name] = section_content try: separator = chr(int(section_options["sep"])) except Exception: separator = None # Split of persisted section for server-side caching if "persist" in section_options: until = int(section_options["persist"]) cached_at = int(time.time()) # Estimate age of the data cache_interval = int(until - cached_at) agent_cache_info[section_name] = (cached_at, cache_interval) persisted_sections[section_name] = (cached_at, until, section_content) if "cached" in section_options: agent_cache_info[section_name] = tuple( map(int, section_options["cached"].split(","))) # The section data might have a different encoding encoding = section_options.get("encoding") elif stripped_line != '': if "nostrip" not in section_options: line = stripped_line if encoding: line = config.decode_incoming_string(line, encoding) else: line = config.decode_incoming_string(line) section_content.append(line.split(separator)) return HostSections(sections, agent_cache_info, piggybacked_raw_data, persisted_sections)