commit b2d19bc5b8e0e4ea285099edc08cb51c01167e3d Author: Stanislav N. aka pztrn Date: Tue Jan 8 22:24:08 2019 +0500 Initial commit. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e76cf85 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# Check_mk notifications in Matrix + +This scripts gives your Check_mk installation a possibility to send notifications into [Matrix](https://matrix.org) chatroom. + +## Installation + +To install this script do the following: + +1. Copy matrix.py file contents into clipboard. +2. Execute ``omd su SITENAME``, where ``SITENAME`` is a site name for OMD. If you're using check_mk installed from source - skip this step. +3. Open ``~/local/share/check_mk/notifications/matrix.py`` for editing and paste ``matrix.py`` file contents into it. Make it executable (``chmod +x ~/local/share/check_mk/notifications/matrix.py``). Check_mk installed from source can place it's files somewhere else and admins of these installations should figure out by themselves where to put this file. + +### Dependencies + +This script has no dependencies except Python 3. It was written specifically to be very compact and understandable. + +## Configuration + +This script will send notifications as user, so you should create a separate user for it. Consult your homeserver's documentation about instructions. + +You'll need these parameters: + +* Homeserver URL - this is what you're specifying in Riot and other clients. +* Notification bot user's token. To get it log in as created user, tap on settings icon in bottom left part of Riot and scroll in the very end. +* Room ID. It's available in room settings. + +After obtaining all of them you should create new notification with parameters like this: + +![Check_mk notifications configuration](/check_mk_notifications_configuration.png) + +Where 1st parameter is a homeserver URL (with http or https), second parameter is a bot user's access token and third parameter is a room ID. \ No newline at end of file diff --git a/check_mk_notifications_configuration.png b/check_mk_notifications_configuration.png new file mode 100644 index 0000000..12717a4 Binary files /dev/null and b/check_mk_notifications_configuration.png differ diff --git a/matrix.py b/matrix.py new file mode 100644 index 0000000..832de69 --- /dev/null +++ b/matrix.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 + +import json +import os +import random +import string +import sys +import urllib.request + +MATRIXHOST = os.environ["NOTIFY_PARAMETER_1"] +MATRIXTOKEN = os.environ["NOTIFY_PARAMETER_2"] +MATRIXROOM = os.environ["NOTIFY_PARAMETER_3"] + +data = { + "TS": os.environ["NOTIFY_SHORTDATETIME"], + + # Host related info. + "HOST": os.environ["NOTIFY_HOSTNAME"], + "HOSTADDR": os.environ["NOTIFY_HOSTADDRESS"], + "HOSTSTATE": os.environ["NOTIFY_HOSTSTATE"], + "HOSTSTATEPREVIOUS": os.environ["NOTIFY_LASTHOSTSTATE"], + "HOSTSTATECOUNT": os.environ["NOTIFY_HOSTNOTIFICATIONNUMBER"], + "HOSTOUTPUT": os.environ["NOTIFY_HOSTOUTPUT"], + + # Service related info. + "SERVICE": os.environ["NOTIFY_SERVICEDESC"], + "SERVICESTATE": os.environ["NOTIFY_SERVICESTATE"], + "SERVICESTATEPREVIOUS": os.environ["NOTIFY_LASTSERVICESTATE"], + "SERVICESTATECOUNT": os.environ["NOTIFY_SERVICENOTIFICATIONNUMBER"], + "SERVICEOUTPUT": os.environ["NOTIFY_SERVICEOUTPUT"] +} + +servicemessage = '''Service {SERVICE} at {HOST} ({HOSTADDR}) | TS: {TS} | STATE: {SERVICESTATE} +
{SERVICEOUTPUT}
''' + +hostmessage = '''Host {HOST} ({HOSTADDR}) | TS: {TS} | STATE: {HOSTSTATE} +
{HOSTOUTPUT}
''' + +message = "" + +print(data) + +# Checking host status first. +if (data["HOSTSTATE"] != data["HOSTSTATEPREVIOUS"] or data["HOSTSTATECOUNT"] != "0"): + message = hostmessage.format(**data) + +# Check service state. +if (data["SERVICESTATE"] != data["SERVICESTATEPREVIOUS"] or data["SERVICESTATECOUNT"] != "0") and (data["SERVICE"] != "$SERVICEDESC$"): + message = servicemessage.format(**data) + +# Data we will send to Matrix Homeserver. +matrixDataDict = { + "msgtype": "m.text", + "body": message, + "format": "org.matrix.custom.html", + "formatted_body": message, +} +matrixData = json.dumps(matrixDataDict) +matrixData = matrixData.encode("utf-8") + +# Random transaction ID for Matrix Homeserver. +txnId = ''.join(random.SystemRandom().choice( + string.ascii_uppercase + string.digits) for _ in range(16)) + +# Authorization headers and etc. +matrixHeaders = {"Authorization": "Bearer " + MATRIXTOKEN, + "Content-Type": "application/json", "Content-Length": len(matrixData)} +# Request. +req = urllib.request.Request(url=MATRIXHOST + "/_matrix/client/r0/rooms/" + MATRIXROOM + + "/send/m.room.message/" + txnId, data=matrixData, headers=matrixHeaders, method="PUT") +# Exec. +urllib.request.urlopen(req)