Exemplo n.º 1
0
def extend_context(context):
    if context.get('PARAMETER_2'):
        context["PARAMETER_URL_PREFIX"] = context["PARAMETER_2"]

    context["LINKEDHOSTNAME"] = utils.format_link('<a href="%s">%s</a>',
                                                  utils.host_url_from_context(context),
                                                  context["HOSTNAME"])
    context["LINKEDSERVICEDESC"] = utils.format_link('<a href="%s">%s</a>',
                                                     utils.service_url_from_context(context),
                                                     context.get("SERVICEDESC", ''))

    event_template_txt, event_template_html = event_templates(context["NOTIFICATIONTYPE"])

    context["EVENT_TXT"] = utils.substitute_context(
        event_template_txt.replace("@", context["WHAT"]), context)
    context["EVENT_HTML"] = utils.substitute_context(
        event_template_html.replace("@", context["WHAT"]), context)

    if "HOSTOUTPUT" in context:
        context["HOSTOUTPUT_HTML"] = utils.format_plugin_output(context["HOSTOUTPUT"])
    if context["WHAT"] == "SERVICE":
        context["SERVICEOUTPUT_HTML"] = utils.format_plugin_output(context["SERVICEOUTPUT"])

        long_serviceoutput = context["LONGSERVICEOUTPUT"]\
            .replace('\\n', '<br>')\
            .replace('\n', '<br>')
        context["LONGSERVICEOUTPUT_HTML"] = utils.format_plugin_output(long_serviceoutput)

    # Compute the subject of the mail
    if context['WHAT'] == 'HOST':
        tmpl = context.get('PARAMETER_HOST_SUBJECT') or TMPL_HOST_SUBJECT
        context['SUBJECT'] = utils.substitute_context(tmpl, context)
    else:
        tmpl = context.get('PARAMETER_SERVICE_SUBJECT') or TMPL_SERVICE_SUBJECT
        context['SUBJECT'] = utils.substitute_context(tmpl, context)
Exemplo n.º 2
0
def extend_context(context: dict[str, str]) -> None:
    if context.get("PARAMETER_2"):
        context["PARAMETER_URL_PREFIX"] = context["PARAMETER_2"]

    context["LINKEDHOSTNAME"] = utils.format_link(
        '<a href="%s">%s</a>', utils.host_url_from_context(context),
        context["HOSTNAME"])
    context["LINKEDSERVICEDESC"] = utils.format_link(
        '<a href="%s">%s</a>',
        utils.service_url_from_context(context),
        context.get("SERVICEDESC", ""),
    )

    event_template_txt, event_template_html = event_templates(
        context["NOTIFICATIONTYPE"])

    context["EVENT_TXT"] = utils.substitute_context(
        event_template_txt.replace("@", context["WHAT"]), context)
    context["EVENT_HTML"] = utils.substitute_context(
        event_template_html.replace("@", context["WHAT"]), context)

    if "HOSTOUTPUT" in context:
        context["HOSTOUTPUT_HTML"] = utils.format_plugin_output(
            context["HOSTOUTPUT"])
    if context["WHAT"] == "SERVICE":
        context["SERVICEOUTPUT_HTML"] = utils.format_plugin_output(
            context["SERVICEOUTPUT"])

        long_serviceoutput = (context["LONGSERVICEOUTPUT"].replace(
            "\\n", "<br>").replace("\n", "<br>"))
        context["LONGSERVICEOUTPUT_HTML"] = utils.format_plugin_output(
            long_serviceoutput)

    # Compute the subject of the mail
    if context["WHAT"] == "HOST":
        tmpl = context.get("PARAMETER_HOST_SUBJECT") or TMPL_HOST_SUBJECT
        context["SUBJECT"] = utils.substitute_context(tmpl, context)
    else:
        tmpl = context.get("PARAMETER_SERVICE_SUBJECT") or TMPL_SERVICE_SUBJECT
        context["SUBJECT"] = utils.substitute_context(tmpl, context)
Exemplo n.º 3
0
def construct_content(context):
    # A list of optional information is configurable via the parameter "elements"
    # (new configuration style)
    # Note: The value PARAMETER_ELEMENTSS is NO TYPO.
    #       Have a look at the function events.py:add_to_event_context(..)
    if "PARAMETER_ELEMENTSS" in context:
        elements = context["PARAMETER_ELEMENTSS"].split()
    else:
        elements = ["perfdata", "graph", "abstime", "address", "longoutput"]

    # If argument 2 is given (old style) or the parameter url_prefix is set (new style),
    # we know the base url to the installation and can add
    # links to hosts and services. ubercomfortable!
    if context.get('PARAMETER_2'):
        context["PARAMETER_URL_PREFIX"] = context["PARAMETER_2"]

    utils.extend_context_with_link_urls(context, '<a href="%s">%s</a>')

    # Create a notification summary in a new context variable
    # Note: This code could maybe move to cmk --notify in order to
    # make it available every in all notification scripts
    # We have the following types of notifications:

    # - Alerts                OK -> CRIT
    #   NOTIFICATIONTYPE is "PROBLEM" or "RECOVERY"

    # - Flapping              Started, Ended
    #   NOTIFICATIONTYPE is "FLAPPINGSTART" or "FLAPPINGSTOP"

    # - Downtimes             Started, Ended, Cancelled
    #   NOTIFICATIONTYPE is "DOWNTIMESTART", "DOWNTIMECANCELLED", or "DOWNTIMEEND"

    # - Acknowledgements
    #   NOTIFICATIONTYPE is "ACKNOWLEDGEMENT"

    # - Custom notifications
    #   NOTIFICATIONTYPE is "CUSTOM"

    html_info = ""
    html_state = '<span class="state$@STATE$">$@STATE$</span>'
    notification_type = context["NOTIFICATIONTYPE"]
    if notification_type in ["PROBLEM", "RECOVERY"]:
        txt_info = "$PREVIOUS@HARDSHORTSTATE$ -> $@SHORTSTATE$"
        html_info = '<span class="state$PREVIOUS@HARDSTATE$">$PREVIOUS@HARDSTATE$</span> &rarr; ' + \
                    html_state

    elif notification_type.startswith("FLAP"):
        if "START" in notification_type:
            txt_info = "Started Flapping"
        else:
            txt_info = "Stopped Flapping ($@SHORTSTATE$)"
            html_info = "Stopped Flapping (while " + html_state + ")"

    elif notification_type.startswith("DOWNTIME"):
        what = notification_type[8:].title()
        txt_info = "Downtime " + what + " ($@SHORTSTATE$)"
        html_info = "Downtime " + what + " (while " + html_state + ")"

    elif notification_type == "ACKNOWLEDGEMENT":

        txt_info = "Acknowledged ($@SHORTSTATE$)"
        html_info = "Acknowledged (while " + html_state + ")"

    elif notification_type == "CUSTOM":
        txt_info = "Custom Notification ($@SHORTSTATE$)"
        html_info = "Custom Notification (while " + html_state + ")"

    else:
        txt_info = notification_type  # Should never happen

    if not html_info:
        html_info = txt_info

    txt_info = utils.substitute_context(txt_info.replace("@", context["WHAT"]), context)
    context["EVENT_TXT"] = txt_info

    # Add HTML formated plugin output
    html_info = utils.substitute_context(html_info.replace("@", context["WHAT"]), context)
    context["EVENT_HTML"] = html_info
    if "HOSTOUTPUT" in context:
        context["HOSTOUTPUT_HTML"] = utils.format_plugin_output(context["HOSTOUTPUT"])
    if context["WHAT"] == "SERVICE":
        context["SERVICEOUTPUT_HTML"] = utils.format_plugin_output(context["SERVICEOUTPUT"])

        long_serviceoutput = context["LONGSERVICEOUTPUT"]\
            .replace('\\n', '<br>')\
            .replace('\n', '<br>')
        context["LONGSERVICEOUTPUT_HTML"] = utils.format_plugin_output(long_serviceoutput)

    attachments = []

    # Compute the subject of the mail
    if context['WHAT'] == 'HOST':
        tmpl = context.get('PARAMETER_HOST_SUBJECT') or tmpl_host_subject
        context['SUBJECT'] = utils.substitute_context(tmpl, context)
    else:
        tmpl = context.get('PARAMETER_SERVICE_SUBJECT') or tmpl_service_subject
        context['SUBJECT'] = utils.substitute_context(tmpl, context)

    # Prepare the mail contents
    content_txt, content_html = render_elements(context, elements)

    if "graph" in elements and not "ALERTHANDLEROUTPUT" in context:
        # Add PNP or Check_MK graph
        try:
            attachments, graph_code = render_performance_graphs(context)
            content_html += graph_code
        except Exception as e:
            sys.stderr.write("Failed to add graphs to mail. Continue without them. (%s)\n" % e)

    extra_html_section = ""
    if "PARAMETER_INSERT_HTML_SECTION" in context:
        extra_html_section = context['PARAMETER_INSERT_HTML_SECTION']

    content_html = utils.substitute_context(tmpl_head_html(extra_html_section), context) + \
                   content_html + \
                   utils.substitute_context(tmpl_foot_html, context)

    return content_txt, content_html, attachments