sensu/handlers/matrix/matrix.py

57 lines
1.3 KiB
Python
Raw Normal View History

2016-10-10 14:07:58 +03:00
#!/usr/bin/python
2016-10-10 12:49:54 +03:00
from matrix_client.client import MatrixClient
import json
2016-10-10 14:07:58 +03:00
import fileinput
2016-10-10 12:49:54 +03:00
# Load config
2016-10-10 14:07:58 +03:00
c = open('/etc/sensu/handlers/notification/matrix.json', 'r')
c_j = c.read()
conf = json.loads(c_j)
c.close()
2016-10-10 12:49:54 +03:00
# Sensu event JSON
2016-10-10 14:07:58 +03:00
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))
2016-10-10 12:49:54 +03:00
# Status codes
statuses = {
2016-10-10 14:07:58 +03:00
2: '**CRITICAL**: ',
1: '*WARNING*: ',
0: 'OK: '
2016-10-10 12:49:54 +03:00
}
# Event information
2016-10-10 14:07:58 +03:00
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]
2016-10-10 12:49:54 +03:00
# Message text
2016-10-10 14:07:58 +03:00
# *WARNING*: Client_name\n /tmp/test does not exists
2016-10-10 12:49:54 +03:00
text = status + client + "\n" + output
2016-10-10 14:07:58 +03:00
# 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'])
2016-10-10 12:49:54 +03:00
2016-10-10 14:07:58 +03:00
room.send_text(text)