def parse( self, raw_data: result.Result[TRawData, Exception], ) -> result.Result[THostSections, Exception]: if raw_data.is_error(): return result.Error(raw_data.error) try: return result.OK(self._parse(raw_data.ok)) except Exception as exc: if cmk.utils.debug.enabled(): raise return result.Error(exc)
def run_fetcher(entry: Dict[str, Any], mode: Mode) -> FetcherMessage: """ Entrypoint to obtain data from fetcher objects. """ try: fetcher_type = FetcherType[entry["fetcher_type"]] except KeyError as exc: raise RuntimeError from exc log.logger.debug("Executing fetcher: %s", entry["fetcher_type"]) try: fetcher_params = entry["fetcher_params"] except KeyError as exc: payload = ErrorPayload(exc) return FetcherMessage( FetcherHeader( fetcher_type, PayloadType.ERROR, status=logging.CRITICAL, payload_length=len(payload), ), payload, ) try: with fetcher_type.from_json(fetcher_params) as fetcher: raw_data = fetcher.fetch(mode) except Exception as exc: raw_data = result.Error(exc) return FetcherMessage.from_raw_data(raw_data, fetcher_type)
def run_fetcher(entry: Dict[str, Any], mode: Mode) -> protocol.FetcherMessage: """ Entrypoint to obtain data from fetcher objects. """ try: fetcher_type = FetcherType[entry["fetcher_type"]] except KeyError as exc: raise RuntimeError from exc logger.debug("Executing fetcher: %s", entry["fetcher_type"]) try: fetcher_params = entry["fetcher_params"] except KeyError as exc: return protocol.FetcherMessage.error(fetcher_type, exc) try: with CPUTracker() as tracker, fetcher_type.from_json(fetcher_params) as fetcher: raw_data = fetcher.fetch(mode) except Exception as exc: raw_data = result.Error(exc) return protocol.FetcherMessage.from_raw_data( raw_data, tracker.duration, fetcher_type, )
def test_raw_data_exception(self, duration): raw_data: result.Result[AgentRawData, Exception] = result.Error(Exception("zomg!")) message = FetcherMessage.from_raw_data(raw_data, duration, FetcherType.TCP) assert isinstance(message.raw_data.error, Exception) assert str(message.raw_data.error) == "zomg!"
def test_from_raw_data_exception(self, duration): error: result.Result[AgentRawData, Exception] = result.Error(ValueError("zomg!")) message = FetcherMessage.from_raw_data(error, duration, FetcherType.TCP) assert message.header.fetcher_type is FetcherType.TCP assert message.header.payload_type is PayloadType.ERROR # Comparison of exception is "interesting" in Python so we check the type and args. assert type(message.raw_data.error) is type(error.error) assert message.raw_data.error.args == error.error.args
def fetch(self, mode: Mode) -> result.Result[TRawData, Exception]: try: with self._make_fetcher() as fetcher: return fetcher.fetch(mode) except Exception as exc: if cmk.utils.debug.enabled(): raise return result.Error(exc)
def fetch(self, mode: Mode) -> result.Result[TRawData, Exception]: """Return the data from the source, either cached or from IO.""" try: return result.OK(self._fetch(mode)) except Exception as exc: if cmk.utils.debug.enabled(): raise return result.Error(exc)
def parse( self, raw_data: result.Result[TRawData, Exception], ) -> result.Result[THostSections, Exception]: try: return raw_data.map(self._make_parser().parse) except Exception as exc: self._logger.log(VERBOSE, "ERROR: %s", exc) if cmk.utils.debug.enabled(): raise return result.Error(exc)
def parse( self, raw_data: result.Result[TRawData, Exception], *, selection: SectionNameCollection, ) -> result.Result[HostSections[TRawDataSection], Exception]: try: return raw_data.map( partial(self._make_parser().parse, selection=selection)) except Exception as exc: self._logger.log(VERBOSE, "ERROR: %s", exc) if cmk.utils.debug.enabled(): raise return result.Error(exc)
def _run_fetcher(fetcher: Fetcher, mode: Mode) -> protocol.FetcherMessage: """Entrypoint to obtain data from fetcher objects.""" logger.debug("Fetch from %s", fetcher) with CPUTracker() as tracker: try: with fetcher: raw_data = fetcher.fetch(mode) except Exception as exc: raw_data = result.Error(exc) return protocol.FetcherMessage.from_raw_data( raw_data, tracker.duration, FetcherType.from_fetcher(fetcher), )
def parse( self, raw_data: result.Result[TRawData, Exception], ) -> result.Result[THostSections, Exception]: try: host_sections = self._make_parser().parse(raw_data) if host_sections.is_error(): return host_sections host_sections.ok.add_persisted_sections( self.persisted_sections_file_path, self.use_outdated_persisted_sections, logger=self._logger, ) return host_sections except Exception as exc: self._logger.log(VERBOSE, "ERROR: %s", exc) if cmk.utils.debug.enabled(): raise return result.Error(exc)
def test_with_exception(self, source): assert source.summarize(result.Error( Exception())) == [ActiveCheckResult(3)]
def result(self) -> result.Result[AbstractRawData, Exception]: return result.Error(self._error)
def test_with_exception(self, source): assert source.summarize(result.Error(Exception())) == (3, "(?)", [])
def fetch(self, mode: Mode) -> result.Result[TRawData, Exception]: """Return the data from the source, either cached or from IO.""" try: return result.OK(self._fetch(mode)) except Exception as exc: return result.Error(exc)
def test_with_MKEmptyAgentData_exception(self, source): assert source.summarize(result.Error(MKEmptyAgentData())) == (2, "(!!)", [])
def test_with_exception(self, source, mode): assert source.summarize(result.Error(Exception()), mode=mode) == (3, "(?)")
def test_with_MKTimeout_exception(self, source): assert source.summarize(result.Error(MKTimeout())) == (2, "(!!)", [])