-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix-service-state.py
executable file
·76 lines (64 loc) · 1.91 KB
/
matrix-service-state.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from os import environ
from matrix_client.api import MatrixHttpApi
from markdown2 import Markdown
# Format Icinag2 status to color
if environ["SERVICESTATE"] == "CRITICAL":
COLOR="#ff0000"
elif environ["SERVICESTATE"] == "WARNING":
COLOR="#ffcc00"
elif environ["SERVICESTATE"] == "OK":
COLOR="#33cc33"
elif environ["SERVICESTATE"] == "UNKNOWN":
COLOR="#9933ff"
else:
COLOR="#e6e6e6"
# If notification has comment
if environ["NOTIFICATIONCOMMENT"]:
COMMENT_PLAIN = """
Comment by {NOTIFICATIONAUTHORNAME}:
{NOTIFICATIONCOMMENT}
""".format(**environ)
COMMENT_MD = """>
> *Comment by {NOTIFICATIONAUTHORNAME}:*
>
> {NOTIFICATIONCOMMENT}
>""".format(**environ)
else:
COMMENT_PLAIN = ""
COMMENT_MD = ">"
DATA = {
"COLOR": COLOR,
"COMMENT_PLAIN": COMMENT_PLAIN,
"COMMENT_MD": COMMENT_MD,
}
DATA.update(environ)
# Message without formating
MSG_PLAIN = """[{NOTIFICATIONTYPE}] Service {SERVICEDISPLAYNAME} on {HOSTDISPLAYNAME} is {SERVICESTATE}
Info: {SERVICEOUTPUT}
When: {LONGDATETIME}
{COMMENT_PLAIN}
{ICINGA_WEBURL}/monitoring/host/show?host={HOSTNAME}&service={SERVICEDESC}
""".format(**DATA)
# Message in markdown
MSG_MD = """**<font color="{COLOR}">[{NOTIFICATIONTYPE}] Service {SERVICEDISPLAYNAME} on {HOSTDISPLAYNAME} is {SERVICESTATE}</font>**
> *Info:*
>
> {SERVICEOUTPUT}
{COMMENT_MD}
> *{LONGDATETIME} - [Show in Icinga2]({ICINGA_WEBURL}/monitoring/host/show?host={HOSTNAME}&service={SERVICEDESC})*
""".format(**DATA)
# Init matrix API
matrix = MatrixHttpApi(environ['MATRIX_SERVER'], token=environ['MATRIX_TOKEN'])
# Send message in both formats to channel
response = matrix.send_message_event(
room_id=environ['MATRIX_CHANNEL'],
event_type="m.room.message",
content={
"msgtype": "m.text",
"format": "org.matrix.custom.html",
"body": MSG_PLAIN,
"formatted_body": Markdown().convert(MSG_MD),
}
)