Exemple #1
0
def discover_bird_protocols(section) -> DiscoveryResult:
    if debug.enabled():
        pprint(section)
    for protocol in section.get('protocols', {}):
        yield Service(item=protocol, parameters=section)
Exemple #2
0
def check_levels(value: Union[int, float],
                 dsname: Union[None, MetricName],
                 params: Any,
                 unit: str = "",
                 factor: Union[int, float] = 1.0,
                 scale: Union[int, float] = 1.0,
                 statemarkers: bool = False,
                 human_readable_func: Optional[Callable] = None,
                 infoname: Optional[str] = None,
                 boundaries: Optional[Tuple] = None) -> ServiceCheckResult:
    """Generic function for checking a value against levels

    This also supports predictive levels.

    value:   currently measured value
    dsname:  name of the datasource in the RRD that corresponds to this value
             or None in order to skip perfdata
    params:  None or Tuple(None, None) -> no level checking.
             Tuple variants with non-None values:
             Tuple[warn_upper, crit_upper] -> upper level checking only.
             Tuple[warn_upper, crit_upper, warn_lower, crit_lower]
             -> upper and lower level checking.
             If a Dict is passed to check_levels, predictive levels are used
             automatically. The following constellations are possible:
             Dict containing "lower" as key -> lower level checking.
             Dict containing "upper" or "levels_upper_min" as key -> upper level checking.
             Dict containing "lower" and "upper"/"levels_upper_min" as key ->
             lower and upper level checking.
    unit:    unit to be displayed in the plugin output.
             Be aware: if a (builtin) human_readable_func is stated which already
             provides a unit info, then this unit is not necessary. An additional
             unit info is useful if a rate is calculated, eg.
                unit="/s",
                human_readable_func=get_bytes_human_readable,
             results in 'X B/s'.
    factor:  the levels are multiplied with this factor before applying
             them to the value. This is being used for the CPU load check
             currently. The levels here are "per CPU", so the number of
             CPUs is used as factor.
    scale:   Scale of the levels in relation to "value" and the value in the RRDs.
             For example if the levels are specified in GB and the RRD store KB, then
             the scale is 1024*1024.
    human_readable_func: Single argument function to present in a human readable fashion
                         the value. Builtin human_readable-functions already provide a unit:
                         - get_percent_human_readable
                         - get_age_human_readable
                         - get_bytes_human_readable
                         - get_filesize_human_readable
                         - get_nic_speed_human_readable
                         - get_timestamp_human_readable
                         - get_relative_date_human_readable
    infoname: Perf value name for infotext like a title.
    boundaries: Add minimum and maximum to performance data.
    """
    if unit.startswith('/'):
        unit_info: str = unit
    elif unit:
        unit_info = " %s" % unit
    else:
        unit_info = ""

    if human_readable_func is None:
        human_readable_func = lambda x: "%.2f" % (x / scale)

    def scale_value(v: Union[None, int, float]) -> Union[None, int, float]:
        if v is None:
            return None
        return v * factor * scale

    infotext = "%s%s" % (human_readable_func(value), unit_info)
    if infoname:
        infotext = "%s: %s" % (infoname, infotext)

    # {}, (), None, (None, None), (None, None, None, None) -> do not check any levels
    if not params or set(params) <= {None}:
        # always add warn/crit, because the call-site may not know it passed None,
        # and therefore expect a quadruple.
        perf = _build_perfdata(dsname, value, scale_value, (None, None), boundaries)
        return 0, infotext, perf

    # Pair of numbers -> static levels
    if isinstance(params, tuple):
        levels = tuple(scale_value(v) for v in _normalize_levels(params))
        ref_value = None

    # Dictionary -> predictive levels
    else:
        if not dsname:
            raise TypeError("Metric name is empty/None")

        try:
            ref_value, levels = _prediction.get_levels(host_name(),
                                                       service_description(),
                                                       dsname,
                                                       params,
                                                       "MAX",
                                                       levels_factor=factor * scale)
            if ref_value:
                predictive_levels_msg = "predicted reference: %s" % human_readable_func(ref_value)
            else:
                predictive_levels_msg = "no reference for prediction yet"

        except MKGeneralException as e:
            ref_value = None
            levels = (None, None, None, None)
            predictive_levels_msg = "no reference for prediction (%s)" % e

        except Exception as e:
            if _debug.enabled():
                raise
            return 3, "%s" % e, []

        if predictive_levels_msg:
            infotext += " (%s)" % predictive_levels_msg

    state, levelstext = _do_check_levels(value, levels, human_readable_func, unit_info)
    infotext += levelstext
    if statemarkers:
        infotext += state_markers[state]

    perfdata = _build_perfdata(dsname, value, scale_value, levels, boundaries, ref_value)

    return state, infotext, perfdata
Exemple #3
0
def check_levels(value,
                 dsname,
                 params,
                 unit="",
                 factor=1.0,
                 scale=1.0,
                 statemarkers=False,
                 human_readable_func=None,
                 infoname=None):
    """Generic function for checking a value against levels

    This also supports predictive levels.

    value:   currently measured value
    dsname:  name of the datasource in the RRD that corresponds to this value
             or None in order to skip perfdata
    params:  None or Tuple(None, None) -> no level checking.
             Tuple variants with non-None values:
             Tuple[warn_upper, crit_upper] -> upper level checking only.
             Tuple[warn_upper, crit_upper, warn_lower, crit_lower]
             -> upper and lower level checking.
             If a Dict is passed to check_levels, predictive levels are used
             automatically. The following constellations are possible:
             Dict containing "lower" as key -> lower level checking.
             Dict containing "upper" or "levels_upper_min" as key -> upper level checking.
             Dict containing "lower" and "upper"/"levels_upper_min" as key ->
             lower and upper level checking.
    unit:    unit to be displayed in the plugin output.
             Be aware: if a (builtin) human_readable_func is stated which already
             provides a unit info, then this unit is not necessary. An additional
             unit info is useful if a rate is calculated, eg.
                unit="/s",
                human_readable_func=get_bytes_human_readable,
             results in 'X B/s'.
    factor:  the levels are multiplied with this factor before applying
             them to the value. This is being used for the CPU load check
             currently. The levels here are "per CPU", so the number of
             CPUs is used as factor.
    scale:   Scale of the levels in relation to "value" and the value in the RRDs.
             For example if the levels are specified in GB and the RRD store KB, then
             the scale is 1024*1024.
    human_readable_func: Single argument function to present in a human readable fashion
                         the value. Builtin human_readable-functions already provide a unit:
                         - get_percent_human_readable
                         - get_age_human_readable
                         - get_bytes_human_readable
                         - get_filesize_human_readable
                         - get_nic_speed_human_readable
                         - get_timestamp_human_readable
                         - get_relative_date_human_readable
    infoname: Perf value name for infotext like a title.
    """
    unit_info = ""
    if unit.startswith('/'):
        unit_info = unit
    elif unit:
        unit_info = " %s" % unit

    if human_readable_func is None:
        human_readable_func = lambda x: "%.2f" % (x / scale)

    def scale_value(v):
        if v is None:
            return None
        return v * factor * scale

    if infoname:
        infotext = "%s: %s%s" % (infoname, human_readable_func(value),
                                 unit_info)
    else:
        infotext = "%s%s" % (human_readable_func(value), unit_info)

    # {}, (), None, (None, None), (None, None, None, None) -> do not check any levels
    if not params or set(params) <= {None}:
        if dsname:
            return 0, infotext, [(dsname, value)]
        return 0, infotext, []

    # Pair of numbers -> static levels
    elif isinstance(params, tuple):
        levels = map(scale_value, _normalize_bounds(params))
        ref_value = None

    # Dictionary -> predictive levels
    else:
        try:
            ref_value, levels = \
                      _prediction.get_levels(host_name(), service_description(),
                                dsname, params, "MAX", levels_factor=factor * scale)

            if ref_value:
                predictive_levels_msg = "predicted reference: %s" % human_readable_func(
                    ref_value)
            else:
                predictive_levels_msg = "no reference for prediction yet"

        except MKGeneralException as e:
            ref_value = None
            levels = [None, None, None, None]
            predictive_levels_msg = "no reference for prediction (%s)" % e

        except Exception as e:
            if _debug.enabled():
                raise
            return 3, "%s" % e, []

        if predictive_levels_msg:
            infotext += " (%s)" % predictive_levels_msg

    state, levelstext = _check_boundaries(value, levels, human_readable_func,
                                          unit_info)
    infotext += levelstext
    if statemarkers:
        infotext += state_markers[state]

    if dsname:
        perfdata = [(dsname, value, levels[0], levels[1])]
        if ref_value:
            perfdata.append(('predict_' + dsname, ref_value))
    else:
        perfdata = []

    return state, infotext, perfdata