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)
def victorops_msg(context): # type: (Dict) -> Dict """Build the message for VictorOps""" if context.get('WHAT', None) == "SERVICE": state = translate_states(context["SERVICESTATE"]) entity_id = '{SERVICEDESC}/{HOSTNAME}:{HOSTADDRESS}'.format( **context).replace(" ", "") title = "{SERVICEDESC} on {HOSTNAME}".format(**context) text = "{SERVICEOUTPUT}\n\n{service_url}".format( service_url=service_url_from_context(context), **context) else: state = translate_states(context["HOSTSTATE"]) entity_id = '{HOSTNAME}:{HOSTADDRESS}'.format(**context).replace( " ", "") title = '{HOSTNAME} is {HOSTSTATE}'.format(**context) text = "{HOSTOUTPUT}\n\n{host_url}".format( host_url=host_url_from_context(context), **context) hostname = context.get('HOSTNAME') return { "message_type": state, "entity_id": entity_id, "entity_display_name": title, "state_message": text, "host_name": hostname, "monitoring_tool": "Check_MK notification", }
def pagerduty_msg(context: Dict) -> Dict: """Build the PagerDuty incident payload""" if context.get('WHAT', None) == "SERVICE": state = context["SERVICESTATE"] incident_key = '{SERVICEDESC}/{HOSTNAME}:{HOSTADDRESS}'.format(**context).replace(" ", "") incident = "{SERVICESTATE}: {SERVICEDESC} on {HOSTNAME}".format(**context) output = context["SERVICEOUTPUT"] incident_url = service_url_from_context(context) else: state = context["HOSTSTATE"] incident_key = '{HOSTNAME}:{HOSTADDRESS}'.format(**context).replace(" ", "") incident = '{HOSTNAME} is {HOSTSTATE}'.format(**context) output = context["HOSTOUTPUT"] incident_url = host_url_from_context(context) msg_payload = { "routing_key": retrieve_from_passwordstore(context.get('PARAMETER_ROUTING_KEY')), "event_action": pagerduty_event_type(context.get('NOTIFICATIONTYPE')), "dedup_key": incident_key, "payload": { "summary": incident, "source": _notification_source_from_context(context), "severity": pagerduty_severity(state), "custom_details": { "info": output, "host": context.get('HOSTNAME'), "host_address": context.get('HOSTADDRESS'), } } } if incident_url: msg_payload.update({"client": "Check_MK", "client_url": incident_url}) return msg_payload
def slack_msg(context: Dict) -> Dict: """Build the message for slack""" if context.get("WHAT", None) == "SERVICE": color = COLORS.get(context["SERVICESTATE"]) title = "Service {NOTIFICATIONTYPE} notification".format(**context) text = "Host: {host_link} (IP: {HOSTADDRESS})\nService: {service_link}\nState: {SERVICESTATE}".format( host_link=utils.format_link("<%s|%s>", utils.host_url_from_context(context), context["HOSTNAME"]), service_link=utils.format_link( "<%s|%s>", utils.service_url_from_context(context), context["SERVICEDESC"]), **context, ) output = context["SERVICEOUTPUT"] else: color = COLORS.get(context["HOSTSTATE"]) title = "Host {NOTIFICATIONTYPE} notification".format(**context) text = "Host: {host_link} (IP: {HOSTADDRESS})\nState: {HOSTSTATE}".format( host_link=utils.format_link("<%s|%s>", utils.host_url_from_context(context), context["HOSTNAME"]), **context, ) output = context["HOSTOUTPUT"] return { "attachments": [ { "color": color, "title": title, "text": text, }, { "color": color, "title": "Additional Info", "text": output + "\nPlease take a look: " + ", ".join( map("@{}".format, context["CONTACTNAME"].split(","))), "footer": "Check_MK notification: {LONGDATETIME}".format(**context), }, ] }
def cisco_webex_teams_msg(context: Dict) -> Dict: """Build the message for Cisco Webex Teams. We use the Markdown markup language, see https://developer.webex.com/docs/api/basics. For example, we need two spaces before a newline character.""" notification_type = "%s notification" % context["NOTIFICATIONTYPE"] # notification about a service if context.get("WHAT", None) == "SERVICE": monitored_type = "Service" host_service_info = "Host: %s (IP: %s) \nService: %s" % ( utils.format_link("<%s|%s>", utils.host_url_from_context(context), context["HOSTNAME"]), context["HOSTADDRESS"], utils.format_link("<%s|%s>", utils.service_url_from_context(context), context["SERVICEDESC"]), ) state = "State: %s" % context["SERVICESTATE"] output = context["SERVICEOUTPUT"] # notification about a host else: monitored_type = "Host" host_service_info = "Host: %s (IP: %s)" % ( utils.format_link("<%s|%s>", utils.host_url_from_context(context), context["HOSTNAME"]), context["HOSTADDRESS"], ) state = "State: %s" % context["HOSTSTATE"] output = context["HOSTOUTPUT"] markdown = ("#### " + monitored_type + " " + notification_type + " \n" + host_service_info + " \n" + state + " \n#### Additional Info" + " \n" + output + " \nPlease take a look: " + ", ".join([ "@" + contact_name for contact_name in context["CONTACTNAME"].split(",") ]) + " \nCheck_MK notification: %s" % context["LONGDATETIME"]) return {"markdown": markdown}
def get_text(context): s = "" s += "$@OUTPUT$" if "PARAMETER_URL_PREFIX" in context: s += " <i>Link: </i>" s += utils.format_link('<a href="%s">%s</a>', utils.host_url_from_context(context), context["HOSTNAME"]) if context["WHAT"] != "HOST": s += utils.format_link('<a href="%s">%s</a>', utils.service_url_from_context(context), context["SERVICEDESC"]) return utils.substitute_context(s.replace("@", context["WHAT"]), context)
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)
def test_host_url_from_context(context, result): host_url = utils.host_url_from_context(context) assert host_url == result
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"] 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", '')) # 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> → ' + \ 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