Exemplo n.º 1
0
def _get_piggyback_processed_file_info(source_hostname, piggybacked_hostname,
                                       piggyback_file_path, time_settings):
    # type: (str, str, Path, Dict[Tuple[Optional[str], str], int]) -> Tuple[bool, Text, int]

    max_cache_age = _get_max_cache_age(source_hostname, piggybacked_hostname,
                                       time_settings)
    validity_period = _get_validity_period(source_hostname,
                                           piggybacked_hostname, time_settings)
    validity_state = _get_validity_state(source_hostname, piggybacked_hostname,
                                         time_settings)

    try:
        file_age = cmk.utils.cachefile_age(piggyback_file_path)
    except MKGeneralException:
        return False, "Piggyback file might have been deleted", 0

    if file_age > max_cache_age:
        return False, "Piggyback file too old: %s" % Age(file_age -
                                                         max_cache_age), 0

    status_file_path = _get_source_status_file_path(source_hostname)
    if not status_file_path.exists():
        reason = "Source '%s' not sending piggyback data" % source_hostname
        return _eval_file_in_validity_period(file_age, validity_period,
                                             validity_state, reason)

    if _is_piggyback_file_outdated(status_file_path, piggyback_file_path):
        reason = "Piggyback file not updated by source '%s'" % source_hostname
        return _eval_file_in_validity_period(file_age, validity_period,
                                             validity_state, reason)

    return True, "Successfully processed from source '%s'" % source_hostname, 0
Exemplo n.º 2
0
def _eval_file_in_validity_period(file_age, validity_period, validity_state,
                                  reason):
    # type: (float, Optional[int], int, str) -> Tuple[bool, Text, int]
    if validity_period is not None and file_age < validity_period:
        return (True, "%s (still valid, %s left)" %
                (reason, Age(validity_period - file_age)), validity_state)
    return False, reason, 0
Exemplo n.º 3
0
def _eval_file_in_validity_period(file_age: float,
                                  validity_period: Optional[int],
                                  validity_state: int,
                                  reason: str) -> Tuple[bool, str, int]:
    if validity_period is not None and file_age < validity_period:
        return (True, "%s (still valid, %s left)" %
                (reason, Age(validity_period - file_age)), validity_state)
    return False, reason, 0
Exemplo n.º 4
0
 def _translate_binding_simple(self, oid: pysnmp.proto.rfc1902.ObjectName,
                               value: SimpleAsn1Type) -> Tuple[str, str]:
     if oid.asTuple() == (1, 3, 6, 1, 2, 1, 1, 3, 0):
         key = 'Uptime'
     else:
         key = str(oid)
     # We could use Asn1Type.isSuperTypeOf() instead of isinstance() below.
     if isinstance(value, (pysnmp.proto.rfc1155.TimeTicks, pysnmp.proto.rfc1902.TimeTicks)):
         val = str(Age(float(value) / 100))
     else:
         val = value.prettyPrint()
     return key, val
Exemplo n.º 5
0
def _cleanup_old_source_status_files(
    piggybacked_hosts_settings: Iterable[Tuple[Path, Iterable[Path],
                                               _PiggybackTimeSettingsMap]]
) -> None:
    """Remove source status files which exceed configured maximum cache age.
    There may be several 'Piggybacked Host Files' rules where the max age is configured.
    We simply use the greatest one per source."""

    max_cache_age_by_sources: Dict[str, int] = {}
    for piggybacked_host_folder, source_hosts, time_settings in piggybacked_hosts_settings:
        for source_host in source_hosts:
            max_cache_age = _get_max_cache_age(
                HostName(source_host.name),
                HostName(piggybacked_host_folder.name),
                time_settings,
            )

            max_cache_age_of_source = max_cache_age_by_sources.get(
                source_host.name)
            if max_cache_age_of_source is None:
                max_cache_age_by_sources[source_host.name] = max_cache_age

            elif max_cache_age >= max_cache_age_of_source:
                max_cache_age_by_sources[source_host.name] = max_cache_age

    for source_state_file in _get_source_state_files():
        try:
            file_age = cmk.utils.cachefile_age(source_state_file)
        except MKGeneralException:
            continue  # File might've been deleted. That's ok.

        # No entry -> no file
        max_cache_age_of_source = max_cache_age_by_sources.get(
            source_state_file.name)
        if max_cache_age_of_source is None:
            logger.log(
                VERBOSE,
                "No piggyback data from source '%s'",
                source_state_file.name,
            )
            continue

        if file_age > max_cache_age_of_source:
            logger.log(
                VERBOSE,
                "Piggyback source status file '%s' is outdated (File too old: %s). Remove it.",
                source_state_file,
                Age(file_age - max_cache_age_of_source),
            )
            _remove_piggyback_file(source_state_file)
Exemplo n.º 6
0
def _cleanup_old_source_status_files(time_settings):
    # type: (Dict[Tuple[Optional[str], str], int]) -> None
    """Remove source status files which exceed configured maximum cache age.
    There may be several 'Piggybacked Host Files' rules where the max age is configured.
    We simply use the greatest one per source."""

    global_max_cache_age = time_settings[(None, 'max_cache_age')]  # type: int

    max_cache_age_by_sources = {}  # type: Dict[str, int]
    for piggybacked_host_folder in _get_piggybacked_host_folders():
        for source_host in _get_piggybacked_host_sources(
                piggybacked_host_folder):
            max_cache_age = _get_max_cache_age(source_host.name,
                                               piggybacked_host_folder.name,
                                               time_settings)
            max_cache_age_of_source = max_cache_age_by_sources.get(
                source_host.name)
            if max_cache_age_of_source is None:
                max_cache_age_by_sources[source_host.name] = max_cache_age

            elif max_cache_age >= max_cache_age_of_source:
                max_cache_age_by_sources[source_host.name] = max_cache_age

    base_dir = str(cmk.utils.paths.piggyback_source_dir)
    for entry in os.listdir(base_dir):
        if entry[0] == ".":
            continue

        source_file_path = os.path.join(base_dir, entry)

        try:
            file_age = cmk_base.utils.cachefile_age(source_file_path)
        except MKGeneralException:
            continue  # File might've been deleted. That's ok.

        max_cache_age = max_cache_age_by_sources.get(entry,
                                                     global_max_cache_age)
        if file_age > max_cache_age:
            console.verbose(
                "Piggyback source status file '%s' is outdated (File too old: %s). Remove it.\n"
                % (source_file_path, Age(file_age - max_cache_age)))
            _remove_piggyback_file(source_file_path)
Exemplo n.º 7
0
def _cleanup_old_source_status_files(time_settings):
    # type: (Dict[Tuple[Optional[str], str], int]) -> None
    """Remove source status files which exceed configured maximum cache age.
    There may be several 'Piggybacked Host Files' rules where the max age is configured.
    We simply use the greatest one per source."""

    global_max_cache_age = time_settings[(None, 'max_cache_age')]  # type: int

    max_cache_age_by_sources = {}  # type: Dict[str, int]
    for piggybacked_host_folder in _get_piggybacked_host_folders():
        for source_host in _get_piggybacked_host_sources(
                piggybacked_host_folder):
            max_cache_age = _get_max_cache_age(source_host.name,
                                               piggybacked_host_folder.name,
                                               time_settings)
            max_cache_age_of_source = max_cache_age_by_sources.get(
                source_host.name)
            if max_cache_age_of_source is None:
                max_cache_age_by_sources[source_host.name] = max_cache_age

            elif max_cache_age >= max_cache_age_of_source:
                max_cache_age_by_sources[source_host.name] = max_cache_age

    for source_state_file in _get_source_state_files():

        try:
            file_age = cmk.utils.cachefile_age(source_state_file)
        except MKGeneralException:
            continue  # File might've been deleted. That's ok.

        max_cache_age = max_cache_age_by_sources.get(source_state_file.name,
                                                     global_max_cache_age)
        if file_age > max_cache_age:
            logger.log(
                VERBOSE,
                "Piggyback source status file '%s' is outdated (File too old: %s). Remove it.",
                source_state_file,
                Age(file_age - max_cache_age),
            )
            _remove_piggyback_file(source_state_file)
Exemplo n.º 8
0
def _get_piggyback_processed_file_info(
    source_hostname: HostName,
    piggybacked_hostname: HostName,
    piggyback_file_path: Path,
    settings: _TimeSettingsMap,
) -> PiggybackFileInfo:

    try:
        file_age = cmk.utils.cachefile_age(piggyback_file_path)
    except MKGeneralException:
        return PiggybackFileInfo(source_hostname, piggyback_file_path, False,
                                 "Piggyback file might have been deleted", 0)

    if (outdated := file_age -
            settings.max_cache_age(source_hostname, piggybacked_hostname)) > 0:
        return PiggybackFileInfo(
            source_hostname,
            piggyback_file_path,
            False,
            "Piggyback file too old: %s" % Age(outdated),
            0,
        )
Exemplo n.º 9
0
def _get_piggyback_processed_file_info(
    source_hostname: HostName,
    piggybacked_hostname: HostName,
    piggyback_file_path: Path,
    settings: _TimeSettingsMap,
) -> PiggybackFileInfo:

    try:
        file_age = cmk.utils.cachefile_age(piggyback_file_path)
    except FileNotFoundError:
        return PiggybackFileInfo(source_hostname, piggyback_file_path, False,
                                 "Piggyback file is missing", 0)

    if (outdated := file_age -
            settings.max_cache_age(source_hostname, piggybacked_hostname)) > 0:
        return PiggybackFileInfo(
            source_hostname,
            piggyback_file_path,
            False,
            "Piggyback file too old: %s" % Age(outdated),
            0,
        )