Beispiel #1
0
def _ip_to_hostname(config_cache, ip):
    # type: (config.ConfigCache, Optional[HostAddress]) -> Optional[HostName]
    if not _config_cache.exists("ip_to_hostname"):
        cache = _config_cache.get_dict("ip_to_hostname")

        for host in config_cache.all_active_realhosts():
            try:
                cache[ip_lookup.lookup_ipv4_address(host)] = host
            except Exception:
                pass
    else:
        cache = _config_cache.get_dict("ip_to_hostname")

    return cache.get(ip)
Beispiel #2
0
def update_timeperiods_cache():
    # { "last_update": 1498820128, "timeperiods": [{"24x7": True}] }
    # The value is store within the config cache since we need a fresh start on reload
    tp_cache = config_cache.get_dict("timeperiods_cache")

    if not tp_cache:
        response = livestatus.LocalConnection().query(
            "GET timeperiods\nColumns: name in")
        for tp_name, tp_active in response:
            tp_cache[tp_name] = bool(tp_active)
Beispiel #3
0
def timeperiod_active(timeperiod):
    # type: (TimeperiodName) -> Optional[bool]
    """Returns
    True : active
    False: inactive
    None : unknown timeperiod

    Raises an exception if e.g. a timeout or connection error appears.
    This way errors can be handled upstream."""
    update_timeperiods_cache()
    return _config_cache.get_dict("timeperiods_cache").get(timeperiod)
Beispiel #4
0
def cached_dns_lookup(hostname, family):
    # type: (HostName, int) -> Optional[str]
    cache = _config_cache.get_dict("cached_dns_lookup")
    cache_id = hostname, family

    # Address has already been resolved in prior call to this function?
    try:
        return cache[cache_id]
    except KeyError:
        pass

    ip_lookup_cache = _get_ip_lookup_cache()

    cached_ip = ip_lookup_cache.get(cache_id)
    if cached_ip and config.use_dns_cache:
        cache[cache_id] = cached_ip
        return cached_ip

    host_config = config.get_config_cache().get_host_config(hostname)

    if host_config.is_no_ip_host:
        cache[cache_id] = None
        return None

    # Now do the actual DNS lookup
    try:
        ipa = socket.getaddrinfo(
            hostname, None, family == 4 and socket.AF_INET
            or socket.AF_INET6)[0][4][0]

        # Update our cached address if that has changed or was missing
        if ipa != cached_ip:
            console.verbose("Updating IPv%d DNS cache for %s: %s\n" %
                            (family, hostname, ipa))
            ip_lookup_cache.update_cache(cache_id, ipa)

        cache[cache_id] = ipa  # Update in-memory-cache
        return ipa

    except (MKTerminate, MKTimeout):
        # We should be more specific with the exception handler below, then we
        # could drop this special handling here
        raise

    except Exception as e:
        # DNS failed. Use cached IP address if present, even if caching
        # is disabled.
        if cached_ip:
            cache[cache_id] = cached_ip
            return cached_ip
        cache[cache_id] = None
        raise MKIPAddressLookupError(
            "Failed to lookup IPv%d address of %s via DNS: %s" %
            (family, hostname, e))
Beispiel #5
0
def check_timeperiod(timeperiod):
    # Let exceptions happen, they will be handled upstream.
    try:
        update_timeperiods_cache()
    except MKTimeout:
        raise

    except Exception:
        if cmk.utils.debug.enabled():
            raise

        # If the query is not successful better skip this check then fail
        return True

    # Note: This also returns True when the timeperiod is unknown
    #       The following function timeperiod_active handles this differently
    return config_cache.get_dict("timeperiods_cache").get(timeperiod, True)
Beispiel #6
0
def check_timeperiod(timeperiod):
    # type: (TimeperiodName) -> bool
    """Check if a timeperiod is currently active. We have no other way than
    doing a Livestatus query. This is not really nice, but if you have a better
    idea, please tell me..."""
    # Let exceptions happen, they will be handled upstream.
    try:
        update_timeperiods_cache()
    except MKTimeout:
        raise

    except Exception:
        if cmk.utils.debug.enabled():
            raise

        # If the query is not successful better skip this check then fail
        return True

    # Note: This also returns True when the timeperiod is unknown
    #       The following function timeperiod_active handles this differently
    return _config_cache.get_dict("timeperiods_cache").get(timeperiod, True)
Beispiel #7
0
def cleanup_timeperiod_caches():
    # type: () -> None
    _config_cache.get_dict("timeperiods_cache").clear()
Beispiel #8
0
def cleanup_timeperiod_caches():
    config_cache.get_dict("timeperiods_cache").clear()
Beispiel #9
0
def timeperiod_active(timeperiod):
    update_timeperiods_cache()
    return config_cache.get_dict("timeperiods_cache").get(timeperiod)