-
Notifications
You must be signed in to change notification settings - Fork 8
/
media_matrix.yml
247 lines (210 loc) · 7.91 KB
/
media_matrix.yml
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
zabbix_export:
version: "5.2"
date: "2021-02-24T14:57:02Z"
media_types:
- name: Matrix
type: WEBHOOK
description: |
https://github.com/jooola/zabbix-matrix-webhook#readme
parameters:
- name: matrix_url
value: <matrix_url>
- name: matrix_token
value: <matrix_token>
- name: matrix_room
value: "{ALERT.SENDTO}"
- name: alert_subject
value: "{ALERT.SUBJECT}"
- name: alert_message
value: "{ALERT.MESSAGE}"
- name: event_severity
value: "{EVENT.NSEVERITY}"
- name: event_is_problem
value: "{EVENT.VALUE}"
- name: event_is_update
value: "{EVENT.UPDATE.STATUS}"
- name: event_url
value: ""
- name: enable_colors
value: "true"
- name: enable_icons
value: "true"
- name: http_proxy
value: ""
message_templates:
- event_source: TRIGGERS
operation_mode: PROBLEM
subject: "{EVENT.NAME}"
message: |
Problem {EVENT.ID} started at {EVENT.TIME} on {EVENT.DATE}
Host: {HOST.NAME}
Severity: {EVENT.SEVERITY}
- event_source: TRIGGERS
operation_mode: RECOVERY
subject: "{EVENT.NAME} ({EVENT.DURATION})"
message: |
Problem {EVENT.ID} resolved at {EVENT.RECOVERY.TIME} on {EVENT.RECOVERY.DATE}
Host: {HOST.NAME}
Severity: {EVENT.SEVERITY}
- event_source: TRIGGERS
operation_mode: UPDATE
subject: "{EVENT.NAME} ({EVENT.AGE})"
message: |
{USER.FULLNAME} {EVENT.UPDATE.ACTION} problem at {EVENT.UPDATE.DATE} {EVENT.UPDATE.TIME}
{EVENT.UPDATE.MESSAGE}
Current problem status: {EVENT.STATUS}
Age: {EVENT.AGE}
Acknowledged: {EVENT.ACK.STATUS}
script: |
const required_input = [
"matrix_url",
"matrix_room",
"matrix_token",
"alert_subject",
"alert_message",
"event_severity",
"event_is_problem",
"event_is_update",
"enable_colors",
"enable_icons",
]
const update_color = "#000000"
const recovery_color = "#098e68"
const severity_colors = [
"#5a5a5a", // Not classified
"#2caed6", // Information
"#d6832c", // Warning
"#d6542c", // Average
"#d62c2c", // High
"#ff0000", // Disaster
]
const update_icon = String.fromCodePoint("0x1f4dd")
const recovery_icon = String.fromCodePoint("0x2705")
const severity_icons = [
String.fromCodePoint("0x2754"), // Not classified
String.fromCodePoint("0x2139"), // Information
String.fromCodePoint("0x26a0"), // Warning
String.fromCodePoint("0x274c"), // Average
String.fromCodePoint("0x1f525"), // High
String.fromCodePoint("0x1f4a5"), // Disaster
]
var Matrix = {
validate: function (params) {
required_input.forEach(function (key) {
if (key in params && params[key] != undefined) {
Matrix[key] = params[key]
} else {
throw "Missing value for key: " + key
}
})
Matrix.alert_subject = Matrix.alert_subject.replace(/\r/g, "")
Matrix.alert_message = Matrix.alert_message.replace(/\r/g, "")
Matrix.event_severity = parseInt(Matrix.event_severity)
Matrix.event_is_problem = parseInt(Matrix.event_is_problem)
Matrix.event_is_update = parseInt(Matrix.event_is_update)
if (typeof params.event_url === "string" && params.event_url.trim() !== "") {
Matrix.event_url = params.event_url
}
Matrix.enable_colors = Matrix.enable_colors.toLowerCase() == "true"
Matrix.enable_icons = Matrix.enable_icons.toLowerCase() == "true"
if (typeof params.http_proxy === "string" && params.http_proxy.trim() !== "") {
Matrix.http_proxy = params.http_proxy
}
if (Matrix.event_is_problem == 1) {
if (Matrix.event_is_update == 0) {
Matrix.kind = "problem"
Matrix.color = severity_colors[Matrix.event_severity]
Matrix.icon = severity_icons[Matrix.event_severity]
} else {
Matrix.kind = "update"
Matrix.color = update_color
Matrix.icon = update_icon
}
} else {
Matrix.kind = "recovery"
Matrix.color = recovery_color
Matrix.icon = recovery_icon
}
},
request: function (path, payload) {
var request = new HttpRequest()
request.addHeader("Content-Type: application/json")
request.addHeader("Authorization: Bearer " + Matrix.matrix_token)
var url = Matrix.matrix_url + path
Zabbix.Log(4, "[Matrix Webhook] new request to: " + url)
if (Matrix.http_proxy != undefined) {
request.setProxy(Matrix.http_proxy)
}
var blob = request.post(url, JSON.stringify(payload))
if (request.getStatus() !== 200) {
var resp = JSON.parse(blob)
if (request.getStatus() == 403 && resp.error.indexOf("not in room") !== -1) {
throw "User is not in room"
}
Zabbix.Log(4, "[Matrix Webhook] Request failed: " + resp.error)
throw "Request failed: " + request.getStatus() + " " + resp.error
}
},
joinRoom: function () {
Matrix.request("/_matrix/client/r0/rooms/" + Matrix.matrix_room + "/join", {})
},
sendMessage: function () {
var body = ""
if (Matrix.enable_icons && Matrix.icon) {
body += Matrix.icon + " "
}
body += Matrix.alert_subject + "\n"
body += Matrix.alert_message
if (Matrix.event_url != undefined) {
body += "\n" + Matrix.event_url
}
var formatted_body = ""
if (Matrix.enable_colors) {
formatted_body += '<span data-mx-color="{color}">'.replace("{color}", Matrix.color)
} else {
formatted_body += "<span>"
}
formatted_body += "<strong>"
if (Matrix.enable_icons && Matrix.icon) {
formatted_body += Matrix.icon + " "
}
if (Matrix.event_url != undefined) {
formatted_body += '<a href="{href}">'.replace("{href}", Matrix.event_url)
}
formatted_body += Matrix.alert_subject
if (Matrix.event_url != undefined) {
formatted_body += "</a>"
}
formatted_body += "</strong><br />"
formatted_body += Matrix.alert_message.replace(/\n/g, "<br />")
formatted_body += "</span>"
const payload = {
body: body,
msgtype: "m.notice",
format: "org.matrix.custom.html",
formatted_body: formatted_body,
}
Matrix.request(
"/_matrix/client/r0/rooms/" + Matrix.matrix_room + "/send/m.room.message",
payload
)
},
}
try {
var params = JSON.parse(value)
Matrix.validate(params)
try {
Matrix.sendMessage()
} catch (error) {
if (error == "User is not in room") {
Matrix.joinRoom()
Matrix.sendMessage()
} else {
throw error
}
}
return "OK"
} catch (error) {
Zabbix.Log(4, "[Matrix Webhook] Error: " + error)
throw "Sending failed: " + error
}