initial commit

This commit is contained in:
DeltaLima (Marcus Hanisch) 2024-03-06 11:36:40 +01:00
commit 9736ce3a20
7 changed files with 226 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.mkp
build/

14
README.md Normal file
View File

@ -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 :)

11
info Normal file
View File

@ -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}

1
info.json Normal file
View File

@ -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"]}}

23
make.sh Executable file
View File

@ -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

130
notifications/rocketchat Executable file
View File

@ -0,0 +1,130 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Rocketchat
# SPDX-FileCopyrightText: 2013 Mathias Kettner <mk@mathias-kettner.de>
# SPDX-FileCopyrightText: 2021 Stefan Gehn <stefan+cmk@srcbox.net>
# SPDX-FileCopyrightText: 2023 Mark Junghanns <mark@markjunghanns.de>
# SPDX-FileCopyrightText: 2024 DeltaLima (Marcus Hanisch) <dontsendmespam@deltalima.org>
#
# 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())

View File

@ -0,0 +1,45 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2021 Stefan Gehn <stefan+cmk@srcbox.net>
# SPDX-FileCopyrightText: 2023 Mark-Tim Junghanns <mark@markjunghanns.de>
# SPDX-FileCopyrightText: 2024 DeltaLima (Marcus Hanisch) <dontsendmespam@deltalima.org>
#
# 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,
),
),
],
)