Working matrix handler
This commit is contained in:
parent
73b5ff36cd
commit
90ee56cb24
57
handlers/matrix/matrix.py
Normal file → Executable file
57
handlers/matrix/matrix.py
Normal file → Executable file
|
@ -1,35 +1,56 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
from matrix_client.client import MatrixClient
|
from matrix_client.client import MatrixClient
|
||||||
import json
|
import json
|
||||||
import sys
|
import fileinput
|
||||||
|
|
||||||
# Load config
|
# 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 event JSON
|
||||||
sensu_json = sys.stdin.read()
|
sensu_json = ''
|
||||||
sensu_event = json.loads(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
|
# Status codes
|
||||||
statuses = {
|
statuses = {
|
||||||
'2': '**CRITICAL**: ',
|
2: '**CRITICAL**: ',
|
||||||
'1': '*WARNING*: ',
|
1: '*WARNING*: ',
|
||||||
'0': 'OK: '
|
0: 'OK: '
|
||||||
}
|
}
|
||||||
|
|
||||||
# Event information
|
# Event information
|
||||||
client = sensu_event['client']['name']
|
try:
|
||||||
check = sensu_event['check']['name']
|
client = sensu_event['client']['name']
|
||||||
output = sensu_event['check']['output']
|
check = sensu_event['check']['name']
|
||||||
status = statuses[sensu_event['check']['status']]
|
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
|
# Message text
|
||||||
|
# *WARNING*: Client_name\n /tmp/test does not exists
|
||||||
text = status + client + "\n" + output
|
text = status + client + "\n" + output
|
||||||
|
|
||||||
# Initialize Matrix client
|
# Check previous status and send check information
|
||||||
client = MatrixClient(conf['homeserver'])
|
if previous_status != sensu_event['check']['status']:
|
||||||
token = client.login_with_password(username=conf['username'],
|
# Initialize Matrix client
|
||||||
password=conf['password'])
|
client = MatrixClient(conf['homeserver'])
|
||||||
# Join to Room
|
token = client.login_with_password(username=conf['username'],
|
||||||
room = client.join_room(conf['room'])
|
password=conf['password'])
|
||||||
|
# Join to Room
|
||||||
|
room = client.join_room(conf['room'])
|
||||||
|
|
||||||
room.send_text(text)
|
room.send_text(text)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user