From 73b5ff36cd943a6f526e10dfd678d74337c33485 Mon Sep 17 00:00:00 2001 From: Difrex Date: Mon, 10 Oct 2016 12:49:54 +0300 Subject: [PATCH] Matrix handler initial --- handlers/matrix/matrix.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 handlers/matrix/matrix.py diff --git a/handlers/matrix/matrix.py b/handlers/matrix/matrix.py new file mode 100644 index 0000000..1b08dcd --- /dev/null +++ b/handlers/matrix/matrix.py @@ -0,0 +1,35 @@ +from matrix_client.client import MatrixClient +import json +import sys + +# Load config +conf = json.load('/etc/sensu/handlers/notifications/matrix.json') + +# Sensu event JSON +sensu_json = sys.stdin.read() +sensu_event = json.loads(sensu_json) + +# Status codes +statuses = { + '2': '**CRITICAL**: ', + '1': '*WARNING*: ', + '0': 'OK: ' +} + +# Event information +client = sensu_event['client']['name'] +check = sensu_event['check']['name'] +output = sensu_event['check']['output'] +status = statuses[sensu_event['check']['status']] + +# Message text +text = status + client + "\n" + output + +# Initialize Matrix client +client = MatrixClient(conf['homeserver']) +token = client.login_with_password(username=conf['username'], + password=conf['password']) +# Join to Room +room = client.join_room(conf['room']) + +room.send_text(text)