From c957d105e84e0c186bd4411a58e341622fd30522 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 11 Jul 2023 15:41:57 +0200 Subject: [PATCH] Initial --- README.md | 5 ++ rocketchat-notification-1.0.0.mkp | Bin 0 -> 2329 bytes src/info | 11 +++ src/notifications/rocketchat | 124 +++++++++++++++++++++++++++++ src/web/plugins/wato/rocketchat.py | 35 ++++++++ 5 files changed, 175 insertions(+) create mode 100644 rocketchat-notification-1.0.0.mkp create mode 100644 src/info create mode 100755 src/notifications/rocketchat create mode 100644 src/web/plugins/wato/rocketchat.py diff --git a/README.md b/README.md index e69de29..57471ee 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,5 @@ +# Rocket.Chat Notification Method + +This method can be used to send notification via Rocket.Chat. +Configuration in WATO. You need to enter the URL including the token which is provided +by Rocket.Chat upon creating the respective webhook. diff --git a/rocketchat-notification-1.0.0.mkp b/rocketchat-notification-1.0.0.mkp new file mode 100644 index 0000000000000000000000000000000000000000..089c936fc96dd1fcef931886a29f548af7168f01 GIT binary patch literal 2329 zcmV+!3Fh`6iwFQZTdiaQ|Lt3CbK*D>&e!#?(0Wo8*a_w(yv$LXt+0elvXelHkjZY% zq&$uh;KA5lNe*GB^53^*jFEXscIswwb$9)I04#O4x?Ad&1dZbj{GIQN0-#zhNAG~u z_d=~y+$omJm4i~HQm&Pty;`kRc1U681rTy!F&J{^{}W&KtWb=6wpS;67Zw}mdd`IW z7J9>x<#~K>Un6^c%5CNZ!tp&xbw#~C@qFP79NUtOoZLGW>H7AV3Zpr)#GK2YdaiHv z&5*g0Z6rj%>-qf95k@cyat{Gz!syfdM10OSN7Noq#yJ)Vu|Y;o9U&h{95^oJAnMCr zYIJD%u(xE~y9L@M_NMeMq6pk@=y*IowS=Evk{iKn@AgrO_pAw(if+*%Qb9PvjffO$ zNn4>J7ZkkNkBILn6RFgBuciBl+R}i$rP_>$qUSx=p zz$DebmB6IHD1U*ZFM7%!(DFfXsD2BikhG%a?MX|&eSG{D^Pf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Vi)MzD~Y8?vpO%0qh%8K7I6b zBQ`T7G%`U|Y9AhxnOk5P#@W>kt|-@RkIGZ3#bDoL%x6-?i8ZDq{*Fb`vf11Tv5kHx zR&A7DJSCP$5vqZa1=>=Bc=SR%?{H-IGU;tF2@TtKUAPD)t7k4inQV1~xomXd7}{%f z&70P*r=89_^SXN;?=rE}XZGUsPX14qBiI4U!!+e-0PYbo$V^jOWtwrINypI#M`Y%< zX0;sz0)apv5C{YUfj}S-2m}IwKp+tRGnmr5?^N}<#8=mU7HS93g!P{wc2w2OO+jHDOQVE|M?R5%KFb2s?|(a<9~~K&7>}J7&^wi<%U#F=%OI<*s*2V zgvDn2M5@w|m78gvI8Y23C=0f9jitq`2^Ex?j1CA5ZJOGl`l7a8K{!~^LB`)E5HuZllviPruIi@py_(4~G{npce{UX$* zvKkJk)~XcN>lUJ-0ae)kMv`Q8Y?hF+^=2-O$RGW)`&;Qp)D2e1!Ofgp_?{ztCf#w( zH+Vt?cmAS>#d38!g(Y2U(n%m@hbNZH>1NUmW2`hOPR6nU1OkCTAP@)y0)apv5C{YU zfj}S-2m}IwKp+qZ1OkCTAP@)y0)apv5C{YUfj}S-2m}Iw_-6PU)nneG0C)fZ#eR}X literal 0 HcmV?d00001 diff --git a/src/info b/src/info new file mode 100644 index 0000000..62cf996 --- /dev/null +++ b/src/info @@ -0,0 +1,11 @@ +{'author': 'Mark-Tim Junghanns', + 'description': 'Send notifications via Rocket.Chat', + 'download_url': 'https://git.php-xpert.de/mtj/Checkmk-rocketchat-notification/', + 'files': {'notifications': ['rocketchat'], + 'web': ['plugins/wato/rocketchat.py']}, + 'name': 'rocketchat-notification', + 'title': 'Rocket.Chat Notification Method', + 'version': '1.0.0', + 'version.min_required': '2.2.0p3', + 'version.packaged': '2.2.0p3', + 'version.usable_until': None} diff --git a/src/notifications/rocketchat b/src/notifications/rocketchat new file mode 100755 index 0000000..b6829c7 --- /dev/null +++ b/src/notifications/rocketchat @@ -0,0 +1,124 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# Rocketchat + +# SPDX-FileCopyrightText: 2013 Mathias Kettner +# SPDX-FileCopyrightText: 2021 Stefan Gehn +# SPDX-FileCopyrightText: 2023 Mark Junghanns +# +# 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$ +```""" + +tmpl_service_text = """$SERVICESTATE_EMOJI$ `$HOSTNAME$/$SERVICEDESC$` +``` +Host: $HOSTNAME$ +Service: $SERVICEDESC$ +Event: $EVENT_TXT$ +Output: $SERVICEOUTPUT$ +```""" + + +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"] + text = construct_message_text(context) + return send_rocketchat_message(webhook_url, text) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/src/web/plugins/wato/rocketchat.py b/src/web/plugins/wato/rocketchat.py new file mode 100644 index 0000000..ef34486 --- /dev/null +++ b/src/web/plugins/wato/rocketchat.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# SPDX-FileCopyrightText: 2021 Stefan Gehn +# SPDX-FileCopyrightText: 2023 Mark-Tim Junghanns +# +# 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"], + elements=[ + ( + "webhook_url", + TextAscii( + title=_("Webhook URL"), + help=_("Webhook URL des Monitoring Users"), + size=146, + allow_empty=False, + ), + ), + ], + )