diff --git a/handlers/matrix/matrix.py b/handlers/matrix/matrix.py old mode 100644 new mode 100755 index 1b08dcd..b865be4 --- a/handlers/matrix/matrix.py +++ b/handlers/matrix/matrix.py @@ -1,35 +1,56 @@ +#!/usr/bin/python + from matrix_client.client import MatrixClient import json -import sys +import fileinput # Load config -conf = json.load('/etc/sensu/handlers/notifications/matrix.json') +c = open('/etc/sensu/handlers/notification/matrix.json', 'r') +c_j = c.read() +conf = json.loads(c_j) +c.close() # Sensu event JSON -sensu_json = sys.stdin.read() -sensu_event = json.loads(sensu_json) +sensu_json = '' +for line in fileinput.input(): + sensu_json = sensu_json + line + +sensu_event = None +try: + sensu_event = json.loads(sensu_json) +except Exception as e: + print(str(e)) # Status codes statuses = { - '2': '**CRITICAL**: ', - '1': '*WARNING*: ', - '0': 'OK: ' + 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']] +try: + client = sensu_event['client']['name'] + check = sensu_event['check']['name'] + output = sensu_event['check']['output'] + status = statuses[sensu_event['check']['status']] + history = sensu_event['check']['history'] +except Exception as e: + print(str(e)) + +previous_status = history[len(history)-1] # Message text +# *WARNING*: Client_name\n /tmp/test does not exists 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']) +# Check previous status and send check information +if previous_status != sensu_event['check']['status']: + # 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) + room.send_text(text)