From 9736ce3a202c200f63e45d6836417e1934d077f4 Mon Sep 17 00:00:00 2001 From: "DeltaLima (Marcus Hanisch)" Date: Wed, 6 Mar 2024 11:36:40 +0100 Subject: [PATCH] initial commit --- .gitignore | 2 + README.md | 14 ++++ info | 11 ++++ info.json | 1 + make.sh | 23 +++++++ notifications/rocketchat | 130 +++++++++++++++++++++++++++++++++++++ plugins/wato/rocketchat.py | 45 +++++++++++++ 7 files changed, 226 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 info create mode 100644 info.json create mode 100755 make.sh create mode 100755 notifications/rocketchat create mode 100644 plugins/wato/rocketchat.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2b10fc8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.mkp +build/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..6eee6fe --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# Rocket.Chat Notification Method + +Send notifications via Rocket.Chat + +Origin: https://git.la10cy.net/DeltaLima/Checkmk-rocketchat-notification + +This is a fork of https://exchange.checkmk.com/p/rocketchat-notification, adding +the ability to add a custom text for mention @someone for example within the +notification message. + +I just unpacked the MKP from checkmk exchange and hacked my stuff together. + +Some day i might read the MKP documentation to learn how to pack it the right +way. Until then, you have to use `make.sh` to create the mkp package :) diff --git a/info b/info new file mode 100644 index 0000000..ed3e183 --- /dev/null +++ b/info @@ -0,0 +1,11 @@ +{'author': 'DeltaLima (Marcus Hanisch)', + 'description': 'Send notifications via Rocket.Chat', + 'download_url': 'https://git.la10cy.net/DeltaLima/Checkmk-rocketchat-notification', + 'files': {'notifications': ['rocketchat'], + 'web': ['plugins/wato/rocketchat.py']}, + 'name': 'rocketchat-notification', + 'title': 'Rocket.Chat Notification Method', + 'version': '1.1.4', + 'version.min_required': '2.1.0p0', + 'version.packaged': '2.1.0p0', + 'version.usable_until': None} diff --git a/info.json b/info.json new file mode 100644 index 0000000..29940e6 --- /dev/null +++ b/info.json @@ -0,0 +1 @@ +{"title": "Rocket.Chat Notification Method", "name": "rocketchat-notification", "description": "Send notifications via Rocket.Chat", "version": "1.1.4", "version.packaged": "2.1.0p0", "version.min_required": "2.1.0p0", "version.usable_until": null, "author": "DeltaLima (Marcus Hanisch)", "download_url": "https://git.la10cy.net/DeltaLima/Checkmk-rocketchat-notification", "files": {"notifications": ["rocketchat"], "web": ["plugins/wato/rocketchat.py"]}} diff --git a/make.sh b/make.sh new file mode 100755 index 0000000..42b9ea6 --- /dev/null +++ b/make.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +VER="$(grep "'version':" info | cut -d \' -f4)" +echo BUILD v${VER} +echo + +mkdir -p build || exit 1 + +echo CLEAN +rm -Rf build/* + +echo +echo BUILD web.tar +tar cvf build/web.tar plugins + +echo +echo BUILD notifications.tar +tar cvf build/notifications.tar -C notifications rocketchat + +echo +echo COPY INFO +cp info* build/ +tar cvfz rocketchat-notification-${VER}.mkp -C build info info.json web.tar notifications.tar diff --git a/notifications/rocketchat b/notifications/rocketchat new file mode 100755 index 0000000..6469ff8 --- /dev/null +++ b/notifications/rocketchat @@ -0,0 +1,130 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# Rocketchat + +# SPDX-FileCopyrightText: 2013 Mathias Kettner +# SPDX-FileCopyrightText: 2021 Stefan Gehn +# SPDX-FileCopyrightText: 2023 Mark Junghanns +# SPDX-FileCopyrightText: 2024 DeltaLima (Marcus Hanisch) +# +# SPDX-License-Identifier: GPL-2.0-only + +# Rocket.Chat notification based on asciimail notification from check_mk 1.2.6p16. + +from __future__ import unicode_literals + +import sys +import requests +from cmk.notification_plugins import utils + + +tmpl_host_text = """$HOSTSTATE_EMOJI$ `$HOSTNAME$` +``` +Host: $HOSTNAME$ +Event: $EVENT_TXT$ +Output: $HOSTOUTPUT$ +``` +$PARAMETER_RC_CUSTOMTEXT$ +""" + +tmpl_service_text = """$SERVICESTATE_EMOJI$ `$HOSTNAME$/$SERVICEDESC$` +``` +Host: $HOSTNAME$ +Service: $SERVICEDESC$ +Event: $EVENT_TXT$ +Output: $SERVICEOUTPUT$ +``` +$PARAMETER_RC_CUSTOMTEXT$ +""" + + +def hoststate_as_emoji(hoststate): + if hoststate == "UP": + return "\ud83d\udd35" # large blue circle + elif hoststate == "DOWN": + return "\ud83d\udd34" # large red circle + elif hoststate == "UNREACH": + return "\u26aa\ufe0f" # medium white circle + return hoststate + + +def servicestate_as_emoji(servicestate): + if servicestate == "OK": + return "\ud83c\udd97" # squared ok + elif servicestate == "WARN": + return "\u26a0\ufe0f" # warning sign + elif servicestate == "CRIT": + return "\u2757\ufe0f" # heavy exclamation mark symbol + elif servicestate == "UNKN": + return "\u2754" # white question mark ornament + return servicestate + + +def construct_message_text(context): + context["HOSTSTATE_EMOJI"] = hoststate_as_emoji(context.get("HOSTSHORTSTATE", "")) + context["SERVICESTATE_EMOJI"] = servicestate_as_emoji( + context.get("SERVICESHORTSTATE", "") + ) + + notification_type = context["NOTIFICATIONTYPE"] + if notification_type in ["PROBLEM", "RECOVERY"]: + txt_info = "$PREVIOUS@HARDSHORTSTATE$ -> $@SHORTSTATE$" + elif notification_type.startswith("FLAP"): + if "START" in notification_type: + txt_info = "Started Flapping" + else: + txt_info = "Stopped Flapping ($@SHORTSTATE$)" + elif notification_type.startswith("DOWNTIME"): + what = notification_type[8:].title() + txt_info = "Downtime " + what + " ($@SHORTSTATE$)" + elif notification_type == "ACKNOWLEDGEMENT": + txt_info = "Acknowledged ($@SHORTSTATE$)" + elif notification_type == "CUSTOM": + txt_info = "Custom Notification ($@SHORTSTATE$)" + else: + txt_info = notification_type # Should never happen + + context["EVENT_TXT"] = utils.substitute_context( + txt_info.replace("@", context["WHAT"]), context + ) + + if context["WHAT"] == "HOST": + tmpl_text = tmpl_host_text + else: + tmpl_text = tmpl_service_text + + return utils.substitute_context(tmpl_text, context) + + +def send_rocketchat_message(webhook_url, text): + url = format(webhook_url) + json = { + "text": text, + } + + r = requests.post(url=url, json=json) + + if r.status_code != 200: + sys.stderr.write( + "Failed to send Rocket.Chat message. Status: {}, Response: {}\n".format( + r.status_code, r.text + ) + ) + return 1 # Temporary error to make Checkmk retry + + sys.stdout.write( + "Sent message to Rocket.Chat {}\n" + ) + return 0 + + +def main(): + context = utils.collect_context() + webhook_url = context["PARAMETER_WEBHOOK_URL"] + rc_customtext = context["PARAMETER_RC_CUSTOMTEXT"] + text = construct_message_text(context) + return send_rocketchat_message(webhook_url, text) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/plugins/wato/rocketchat.py b/plugins/wato/rocketchat.py new file mode 100644 index 0000000..c236b57 --- /dev/null +++ b/plugins/wato/rocketchat.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# SPDX-FileCopyrightText: 2021 Stefan Gehn +# SPDX-FileCopyrightText: 2023 Mark-Tim Junghanns +# SPDX-FileCopyrightText: 2024 DeltaLima (Marcus Hanisch) +# +# SPDX-License-Identifier: GPL-2.0-only + +from cmk.gui.valuespec import Dictionary, TextAscii +from cmk.gui.plugins.wato import notification_parameter_registry, NotificationParameter + + +@notification_parameter_registry.register +class NotificationParameterRocketchat(NotificationParameter): + @property + def ident(self): + return "rocketchat" + + @property + def spec(self): + return Dictionary( + title=_("Create notification with the following parameters"), + required_keys=["webhook_url", "rc_customtext"], + elements=[ + ( + "webhook_url", + TextAscii( + title=_("Webhook URL"), + help=_("Webhook URL des Monitoring Users"), + size=146, + allow_empty=False, + ), + ), + ( + "rc_customtext", + TextAscii( + title=_("Custom text (e.g. for mention @someone)"), + help=_("Custom text to include to the notification message, like mention @someone"), + size=146, + allow_empty=True, + ), + ), + ], + )