def test_piggyback_default_time_settings(): time_settings = [(None, "max_cache_age", piggyback_max_cachefile_age)] piggybacked_hostname = "test-host" piggyback.get_piggyback_raw_data(piggybacked_hostname, time_settings) piggyback.get_source_and_piggyback_hosts(time_settings) piggyback.has_piggyback_raw_data(piggybacked_hostname, time_settings) piggyback.cleanup_piggyback_files(time_settings)
def summarize_success( self, host_sections: HostSections[AgentRawDataSection], *, mode: Mode, ) -> Sequence[ActiveCheckResult]: """Returns useful information about the data source execution Return only summary information in case there is piggyback data""" if mode is not Mode.CHECKING: return [] sources: Final[Sequence[PiggybackRawDataInfo]] = list( itertools.chain.from_iterable( # TODO(ml): The code uses `get_piggyback_raw_data()` instead of # `HostSections.piggyback_raw_data` because this allows it to # sneakily use cached data. At minimum, we should group all cache # handling performed after the parser. get_piggyback_raw_data(origin, self.time_settings) for origin in (self.hostname, self.ipaddress))) if not sources: if self.always: return [ActiveCheckResult(1, "Missing data")] return [] return [ ActiveCheckResult(src.info.status, src.info.message) for src in sources if src.info.message ]
def test_store_piggyback_raw_data_second_source(): time_settings = [(None, "max_cache_age", piggyback_max_cachefile_age) ] # type: piggyback.PiggybackTimeSettings piggyback.store_piggyback_raw_data( "source2", {"test-host": [ b"<<<check_mk>>>", b"lulu", ]}) for raw_data_info in piggyback.get_piggyback_raw_data( "test-host", time_settings): assert raw_data_info.source_hostname in ["source1", "source2"] if raw_data_info.source_hostname == "source1": assert raw_data_info.file_path.endswith('/test-host/source1') assert raw_data_info.successfully_processed is True assert raw_data_info.reason.startswith( "Successfully processed from source 'source1'") assert raw_data_info.reason_status == 0 assert raw_data_info.raw_data == b'<<<check_mk>>>\nlala\n' else: # source2 assert raw_data_info.file_path.endswith('/test-host/source2') assert raw_data_info.successfully_processed is True assert raw_data_info.reason.startswith( "Successfully processed from source 'source2'") assert raw_data_info.reason_status == 0 assert raw_data_info.raw_data == b'<<<check_mk>>>\nlulu\n'
def summarize_success( self, host_sections: AgentHostSections, *, mode: Mode, ) -> Tuple[ServiceState, ServiceDetails]: """Returns useful information about the data source execution Return only summary information in case there is piggyback data""" if mode is not Mode.CHECKING: return 0, '' sources: Final[Sequence[PiggybackRawDataInfo]] = list( itertools.chain.from_iterable( # TODO(ml): The code uses `get_piggyback_raw_data()` instead of # `HostSections.piggyback_raw_data` because this allows it to # sneakily use cached data. At minimum, we should group all cache # handling performed after the parser. get_piggyback_raw_data(origin, self.time_settings) for origin in (self.hostname, self.ipaddress))) if not sources: if self.always: return 1, "Missing data" return 0, '' return ( max(src.reason_status for src in sources), ", ".join(src.reason for src in sources if src.reason), )
def summarize_success( self, host_sections: AgentHostSections, *, mode: Mode, ) -> ServiceCheckResult: """Returns useful information about the data source execution Return only summary information in case there is piggyback data""" if mode is not Mode.CHECKING: return 0, '', [] states = [0] infotexts = set() for origin in (self.hostname, self.ipaddress): src = None # TODO(ml): The code uses `get_piggyback_raw_data()` instead of # `HostSections.piggyback_raw_data` because this allows it to # sneakily use cached data. At minimum, we should group all cache # handling performed after the parser. for src in get_piggyback_raw_data( origin if origin else "", self.time_settings, ): states.append(src.reason_status) infotexts.add(src.reason) if self.always and not src: return 1, "Missing data", [] return max(states), ", ".join(infotexts), []
def test_get_piggyback_raw_data_successful(time_settings): for raw_data_info in piggyback.get_piggyback_raw_data("test-host", time_settings): assert raw_data_info.source_hostname == "source1" assert raw_data_info.file_path.endswith('/test-host/source1') assert raw_data_info.successfully_processed is True assert raw_data_info.reason == "Successfully processed from source 'source1'" assert raw_data_info.reason_status == 0 assert raw_data_info.raw_data == b'<<<check_mk>>>\nlala\n'
def _summarize(self) -> ServiceCheckResult: states = [0] infotexts = set() for origin in (self.hostname, self.ipaddress): for src in get_piggyback_raw_data(origin if origin else "", self._time_settings): states.append(src.reason_status) infotexts.add(src.reason) return max(states), ", ".join(infotexts), []
def test_get_piggyback_raw_data_too_old_global(): time_settings = [(None, "max_cache_age", -1)] for raw_data_info in piggyback.get_piggyback_raw_data("test-host", time_settings): assert raw_data_info.source_hostname == "source1" assert raw_data_info.file_path.endswith('/test-host/source1') assert raw_data_info.successfully_processed is False assert raw_data_info.reason.startswith("Piggyback file too old:") assert raw_data_info.reason_status == 0 assert raw_data_info.raw_data == b'<<<check_mk>>>\nlala\n'
def test_get_piggyback_raw_data_piggybacked_host_validity2(time_settings, successfully_processed, reason, reason_status): # Fake age the test-host piggyback file os.utime(str(cmk.utils.paths.piggyback_dir / "test-host" / "source1"), (time.time() - 10, time.time() - 10)) for raw_data_info in piggyback.get_piggyback_raw_data("test-host", time_settings): assert raw_data_info.source_hostname == "source1" assert raw_data_info.file_path.endswith('/test-host/source1') assert raw_data_info.successfully_processed is successfully_processed assert raw_data_info.reason == reason assert raw_data_info.reason_status == reason_status assert raw_data_info.raw_data == b'<<<check_mk>>>\nlala\n'
def test_get_piggyback_raw_data_source_validity2(time_settings, successfully_processed, reason, reason_status): source_status_file = cmk.utils.paths.piggyback_source_dir / "source1" if source_status_file.exists(): os.remove(str(source_status_file)) for raw_data_info in piggyback.get_piggyback_raw_data("test-host", time_settings): assert raw_data_info.source_hostname == "source1" assert raw_data_info.file_path.endswith('/test-host/source1') assert raw_data_info.successfully_processed is successfully_processed assert raw_data_info.reason == reason assert raw_data_info.reason_status == reason_status assert raw_data_info.raw_data == b'<<<check_mk>>>\nlala\n'
def test_get_piggyback_raw_data_not_updated(): time_settings = [(None, "max_cache_age", piggyback_max_cachefile_age)] # Fake age the test-host piggyback file os.utime(str(cmk.utils.paths.piggyback_dir / "test-host" / "source1"), (time.time() - 10, time.time() - 10)) for raw_data_info in piggyback.get_piggyback_raw_data("test-host", time_settings): assert raw_data_info.source_hostname == "source1" assert raw_data_info.file_path.endswith('/test-host/source1') assert raw_data_info.successfully_processed is False assert raw_data_info.reason == "Piggyback file not updated by source 'source1'" assert raw_data_info.reason_status == 0 assert raw_data_info.raw_data == b'<<<check_mk>>>\nlala\n'
def test_get_piggyback_raw_data_too_old_source(): time_settings: piggyback.PiggybackTimeSettings = [ (None, "max_cache_age", piggyback_max_cachefile_age), ("source1", "max_cache_age", -1), ] for raw_data_info in piggyback.get_piggyback_raw_data( HostName("test-host"), time_settings): assert raw_data_info.source_hostname == "source1" assert raw_data_info.file_path.endswith("/test-host/source1") assert raw_data_info.successfully_processed is False assert raw_data_info.reason.startswith("Piggyback file too old:") assert raw_data_info.reason_status == 0 assert raw_data_info.raw_data == b"<<<check_mk>>>\nlala\n"
def test_get_piggyback_raw_data_not_sending(): time_settings = [(None, "max_cache_age", piggyback_max_cachefile_age)] source_status_file = cmk.utils.paths.piggyback_source_dir / "source1" if source_status_file.exists(): os.remove(str(source_status_file)) for raw_data_info in piggyback.get_piggyback_raw_data("test-host", time_settings): assert raw_data_info.source_hostname == "source1" assert raw_data_info.file_path.endswith('/test-host/source1') assert raw_data_info.successfully_processed is False assert raw_data_info.reason == "Source 'source1' not sending piggyback data" assert raw_data_info.reason_status == 0 assert raw_data_info.raw_data == b'<<<check_mk>>>\nlala\n'
def test_store_piggyback_raw_data_new_host(): time_settings = [(None, "max_cache_age", piggyback_max_cachefile_age)] piggyback.store_piggyback_raw_data("source2", {"pig": [ b"<<<check_mk>>>", b"lulu", ]}) for raw_data_info in piggyback.get_piggyback_raw_data("pig", time_settings): assert raw_data_info.source_hostname == "source2" assert raw_data_info.file_path.endswith('/pig/source2') assert raw_data_info.successfully_processed is True assert raw_data_info.reason.startswith("Successfully processed from source 'source2'") assert raw_data_info.reason_status == 0 assert raw_data_info.raw_data == b'<<<check_mk>>>\nlulu\n'
def _raw_data( hostname: Optional[str], time_settings: PiggybackTimeSettings, ) -> List[PiggybackRawDataInfo]: return get_piggyback_raw_data(hostname if hostname else "", time_settings)
def _raw_data(hostname, time_settings): # type: (Optional[str], PiggybackTimeSettings) -> List[PiggybackRawDataInfo] return get_piggyback_raw_data(hostname if hostname else "", time_settings)
def _raw_data(hostname, time_settings): return get_piggyback_raw_data(hostname, time_settings)
def test_get_piggyback_raw_data_no_data(): time_settings = [(None, "max_cache_age", piggyback_max_cachefile_age)] assert piggyback.get_piggyback_raw_data("no-host", time_settings) == []
def _raw_data(hostname, time_settings): # type: (HostName, PiggybackTimeSettings) -> List[PiggybackRawDataInfo] return get_piggyback_raw_data(hostname, time_settings)