Exemplo n.º 1
0
def _load_prediction_information(
    *,
    tg_name: Optional[str],
    pred_dir: str,
) -> Tuple[Tuple[str, prediction.PredictionInfo], Sequence[Tuple[str, str]]]:
    # Load all prediction information, sort by time of generation
    if not os.path.exists(pred_dir):
        raise MKGeneralException(
            _("There is currently no prediction information "
              "available for this service."))

    selected_timegroup: Optional[Tuple[str, prediction.PredictionInfo]] = None
    timegroups: List[Tuple[str, prediction.PredictionInfo]] = []
    now = time.time()
    for f in os.listdir(pred_dir):
        if not f.endswith(".info"):
            continue
        tg_info = prediction.retrieve_data_for_prediction(
            pred_dir + "/" + f,
            "<unknown>"
            if selected_timegroup is None else str(selected_timegroup[1]),
        )
        if tg_info is None:
            continue

        current_tg_name = f[:-5]
        timegroups.append((current_tg_name, tg_info))
        if current_tg_name == tg_name or (
                tg_name is None and
            (tg_info["range"][0] <= now <= tg_info["range"][1])):
            selected_timegroup = (current_tg_name, tg_info)

    timegroups.sort(key=lambda x: x[1]["range"][0])

    choices: List[Tuple[str, str]] = [(name, name.title())
                                      for name, _tg_info in timegroups]

    if selected_timegroup is None:
        if not timegroups:
            raise MKGeneralException(_("Missing prediction information."))
        selected_timegroup = timegroups[0]

    return selected_timegroup, choices
Exemplo n.º 2
0
def page_graph():
    host = html.request.get_str_input_mandatory("host")
    service = html.request.get_str_input_mandatory("service")
    dsname = html.request.get_str_input_mandatory("dsname")

    html.header(_("Prediction for %s - %s - %s") % (host, service, dsname))

    # Get current value from perf_data via Livestatus
    current_value = get_current_perfdata(host, service, dsname)

    pred_dir = prediction.predictions_dir(host, service, dsname)
    if not os.path.exists(pred_dir):
        raise MKGeneralException(
            _("There is currently no prediction information "
              "available for this service."))

    # Load all prediction information, sort by time of generation
    tg_name = html.request.var("timegroup")
    timegroup = None
    timegroups = []  # type: List[prediction.PredictionInfo]
    now = time.time()
    for f in os.listdir(pred_dir):
        if not f.endswith(".info"):
            continue
        tg_info = prediction.retrieve_data_for_prediction(
            pred_dir + "/" + f,
            "<unknown>" if timegroup is None else timegroup)
        if tg_info is None:
            continue

        tg_info["name"] = f[:-5]
        timegroups.append(tg_info)
        if tg_info["name"] == tg_name or (
                tg_name is None and
            (tg_info["range"][0] <= now <= tg_info["range"][1])):
            timegroup = tg_info
            tg_name = tg_info["name"]

    timegroups.sort(key=lambda x: x["range"][0])

    choices = [(tg_info_["name"], tg_info_["name"].title())
               for tg_info_ in timegroups]

    if not timegroup:
        if not timegroups:
            raise MKGeneralException(_("Missing prediction information."))
        timegroup = timegroups[0]
        tg_name = choices[0][0]
    if tg_name is None:
        raise Exception("should not happen")

    html.begin_form("prediction")
    html.write(_("Show prediction for "))
    html.dropdown("timegroup",
                  choices,
                  deflt=tg_name,
                  onchange="document.prediction.submit();")
    html.hidden_fields()
    html.end_form()

    # Get prediction data
    path = pred_dir + "/" + timegroup["name"]
    tg_data = prediction.retrieve_data_for_prediction(path, tg_name)
    if tg_data is None:
        raise MKGeneralException(_("Missing prediction data."))

    swapped = swap_and_compute_levels(tg_data, timegroup['params'])
    vertical_range = compute_vertical_range(swapped)
    legend = [
        ("#000000", _("Reference")),
        ("#ffffff", _("OK area")),
        ("#ffff00", _("Warning area")),
        ("#ff0000", _("Critical area")),
    ]
    if current_value is not None:
        legend.append(("#0000ff", _("Current value: %.2f") % current_value))

    create_graph(timegroup["name"], graph_size, timegroup["range"],
                 vertical_range, legend)

    if "levels_upper" in timegroup['params']:
        render_dual_area(swapped["upper_warn"], swapped["upper_crit"],
                         "#fff000", 0.4)
        render_area_reverse(swapped["upper_crit"], "#ff0000", 0.1)

    if "levels_lower" in timegroup['params']:
        render_dual_area(swapped["lower_crit"], swapped["lower_warn"],
                         "#fff000", 0.4)
        render_area(swapped["lower_crit"], "#ff0000", 0.1)

    vscala_low = vertical_range[0]
    vscala_high = vertical_range[1]
    vert_scala = compute_vertical_scala(vscala_low, vscala_high)
    time_scala = [[timegroup["range"][0] + i * 3600,
                   "%02d:00" % i] for i in range(0, 25, 2)]
    render_coordinates(vert_scala, time_scala)

    if "levels_lower" in timegroup['params']:
        render_dual_area(swapped["average"], swapped["lower_warn"], "#ffffff",
                         0.5)
        render_curve(swapped["lower_warn"], "#e0e000", square=True)
        render_curve(swapped["lower_crit"], "#f0b0a0", square=True)

    if "levels_upper" in timegroup['params']:
        render_dual_area(swapped["upper_warn"], swapped["average"], "#ffffff",
                         0.5)
        render_curve(swapped["upper_warn"], "#e0e000", square=True)
        render_curve(swapped["upper_crit"], "#f0b0b0", square=True)
    render_curve(swapped["average"], "#000000")
    render_curve(swapped["average"], "#000000")  # repetition makes line bolder

    # Try to get current RRD data and render it also
    from_time, until_time = timegroup["range"]
    now = time.time()
    if from_time <= now <= until_time:
        timeseries = prediction.get_rrd_data(host, service, dsname, "MAX",
                                             from_time, until_time)
        rrd_data = timeseries.values

        render_curve(rrd_data, "#0000ff", 2)
        if current_value is not None:
            rel_time = (now - prediction.timezone_at(now)) % timegroup["slice"]
            render_point(timegroup["range"][0] + rel_time, current_value,
                         "#0000ff")

    html.footer()
Exemplo n.º 3
0
def page_graph():
    host = html.request.get_str_input_mandatory("host")
    service = html.request.get_str_input_mandatory("service")
    dsname = html.request.get_str_input_mandatory("dsname")

    breadcrumb = make_service_breadcrumb(host, service)
    html.header(
        _("Prediction for %s - %s - %s") % (host, service, dsname), breadcrumb)

    # Get current value from perf_data via Livestatus
    current_value = get_current_perfdata(host, service, dsname)

    pred_dir = prediction.predictions_dir(host, service, dsname)

    (tg_name, timegroup), choices = _load_prediction_information(
        tg_name=html.request.var("timegroup"),
        pred_dir=pred_dir,
    )

    html.begin_form("prediction")
    html.write(_("Show prediction for "))
    html.dropdown("timegroup",
                  choices,
                  deflt=tg_name,
                  onchange="document.prediction.submit();")
    html.hidden_fields()
    html.end_form()

    # Get prediction data
    path = pred_dir + "/" + tg_name
    tg_data = prediction.retrieve_data_for_prediction(path, tg_name)
    if tg_data is None:
        raise MKGeneralException(_("Missing prediction data."))

    swapped = swap_and_compute_levels(tg_data, timegroup['params'])
    vertical_range = compute_vertical_range(swapped)
    legend = [
        ("#000000", _("Reference")),
        ("#ffffff", _("OK area")),
        ("#ffff00", _("Warning area")),
        ("#ff0000", _("Critical area")),
    ]
    if current_value is not None:
        legend.append(("#0000ff", _("Current value: %.2f") % current_value))

    create_graph(tg_name, graph_size, timegroup["range"], vertical_range,
                 legend)

    if "levels_upper" in timegroup['params']:
        render_dual_area(swapped["upper_warn"], swapped["upper_crit"],
                         "#fff000", 0.4)
        render_area_reverse(swapped["upper_crit"], "#ff0000", 0.1)

    if "levels_lower" in timegroup['params']:
        render_dual_area(swapped["lower_crit"], swapped["lower_warn"],
                         "#fff000", 0.4)
        render_area(swapped["lower_crit"], "#ff0000", 0.1)

    vscala_low = vertical_range[0]
    vscala_high = vertical_range[1]
    vert_scala = compute_vertical_scala(vscala_low, vscala_high)
    time_scala = [[timegroup["range"][0] + i * 3600,
                   "%02d:00" % i] for i in range(0, 25, 2)]
    render_coordinates(vert_scala, time_scala)

    if "levels_lower" in timegroup['params']:
        render_dual_area(swapped["average"], swapped["lower_warn"], "#ffffff",
                         0.5)
        render_curve(swapped["lower_warn"], "#e0e000", square=True)
        render_curve(swapped["lower_crit"], "#f0b0a0", square=True)

    if "levels_upper" in timegroup['params']:
        render_dual_area(swapped["upper_warn"], swapped["average"], "#ffffff",
                         0.5)
        render_curve(swapped["upper_warn"], "#e0e000", square=True)
        render_curve(swapped["upper_crit"], "#f0b0b0", square=True)
    render_curve(swapped["average"], "#000000")
    render_curve(swapped["average"], "#000000")  # repetition makes line bolder

    # Try to get current RRD data and render it also
    from_time, until_time = timegroup["range"]
    now = time.time()
    if from_time <= now <= until_time:
        timeseries = prediction.get_rrd_data(host, service, dsname, "MAX",
                                             from_time, until_time)
        rrd_data = timeseries.values

        render_curve(rrd_data, "#0000ff", 2)
        if current_value is not None:
            rel_time = (now - prediction.timezone_at(now)) % timegroup["slice"]
            render_point(timegroup["range"][0] + rel_time, current_value,
                         "#0000ff")

    html.footer()