-
Notifications
You must be signed in to change notification settings - Fork 8
/
media_matrix.json
84 lines (84 loc) · 7.6 KB
/
media_matrix.json
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
{
"zabbix_export": {
"version": "5.0",
"date": "2021-02-24T14:57:02Z",
"media_types": [
{
"name": "Matrix",
"type": "WEBHOOK",
"description": "https://github.com/jooola/zabbix-matrix-webhook#readme\n",
"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}\nHost: {HOST.NAME}\nSeverity: {EVENT.SEVERITY}\n"
},
{
"event_source": "TRIGGERS",
"operation_mode": "RECOVERY",
"subject": "{EVENT.NAME} ({EVENT.DURATION})",
"message": "Problem {EVENT.ID} resolved at {EVENT.RECOVERY.TIME} on {EVENT.RECOVERY.DATE}\nHost: {HOST.NAME}\nSeverity: {EVENT.SEVERITY}\n"
},
{
"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}\n{EVENT.UPDATE.MESSAGE}\n\nCurrent problem status: {EVENT.STATUS}\nAge: {EVENT.AGE}\nAcknowledged: {EVENT.ACK.STATUS}\n"
}
],
"script": "const required_input = [\n \"matrix_url\",\n \"matrix_room\",\n \"matrix_token\",\n\n \"alert_subject\",\n \"alert_message\",\n\n \"event_severity\",\n \"event_is_problem\",\n \"event_is_update\",\n\n \"enable_colors\",\n \"enable_icons\",\n]\n\nconst update_color = \"#000000\"\nconst recovery_color = \"#098e68\"\nconst severity_colors = [\n \"#5a5a5a\", // Not classified\n \"#2caed6\", // Information\n \"#d6832c\", // Warning\n \"#d6542c\", // Average\n \"#d62c2c\", // High\n \"#ff0000\", // Disaster\n]\n\nconst update_icon = String.fromCodePoint(\"0x1f4dd\")\nconst recovery_icon = String.fromCodePoint(\"0x2705\")\nconst severity_icons = [\n String.fromCodePoint(\"0x2754\"), // Not classified\n String.fromCodePoint(\"0x2139\"), // Information\n String.fromCodePoint(\"0x26a0\"), // Warning\n String.fromCodePoint(\"0x274c\"), // Average\n String.fromCodePoint(\"0x1f525\"), // High\n String.fromCodePoint(\"0x1f4a5\"), // Disaster\n]\n\nvar Matrix = {\n validate: function (params) {\n required_input.forEach(function (key) {\n if (key in params && params[key] != undefined) {\n Matrix[key] = params[key]\n } else {\n throw \"Missing value for key: \" + key\n }\n })\n\n Matrix.alert_subject = Matrix.alert_subject.replace(/\\r/g, \"\")\n Matrix.alert_message = Matrix.alert_message.replace(/\\r/g, \"\")\n\n Matrix.event_severity = parseInt(Matrix.event_severity)\n Matrix.event_is_problem = parseInt(Matrix.event_is_problem)\n Matrix.event_is_update = parseInt(Matrix.event_is_update)\n\n if (typeof params.event_url === \"string\" && params.event_url.trim() !== \"\") {\n Matrix.event_url = params.event_url\n }\n\n Matrix.enable_colors = Matrix.enable_colors.toLowerCase() == \"true\"\n Matrix.enable_icons = Matrix.enable_icons.toLowerCase() == \"true\"\n\n if (typeof params.http_proxy === \"string\" && params.http_proxy.trim() !== \"\") {\n Matrix.http_proxy = params.http_proxy\n }\n\n if (Matrix.event_is_problem == 1) {\n if (Matrix.event_is_update == 0) {\n Matrix.kind = \"problem\"\n Matrix.color = severity_colors[Matrix.event_severity]\n Matrix.icon = severity_icons[Matrix.event_severity]\n } else {\n Matrix.kind = \"update\"\n Matrix.color = update_color\n Matrix.icon = update_icon\n }\n } else {\n Matrix.kind = \"recovery\"\n Matrix.color = recovery_color\n Matrix.icon = recovery_icon\n }\n },\n\n request: function (path, payload) {\n var request = new CurlHttpRequest()\n request.AddHeader(\"Content-Type: application/json\")\n request.AddHeader(\"Authorization: Bearer \" + Matrix.matrix_token)\n\n var url = Matrix.matrix_url + path\n\n Zabbix.Log(4, \"[Matrix Webhook] new request to: \" + url)\n\n if (Matrix.http_proxy != undefined) {\n request.SetProxy(Matrix.http_proxy)\n }\n\n var blob = request.Post(url, JSON.stringify(payload))\n\n if (request.Status() !== 200) {\n var resp = JSON.parse(blob)\n\n if (request.Status() == 403 && resp.error.indexOf(\"not in room\") !== -1) {\n throw \"User is not in room\"\n }\n\n Zabbix.Log(4, \"[Matrix Webhook] Request failed: \" + resp.error)\n throw \"Request failed: \" + request.Status() + \" \" + resp.error\n }\n },\n\n joinRoom: function () {\n Matrix.request(\"/_matrix/client/r0/rooms/\" + Matrix.matrix_room + \"/join\", {})\n },\n\n sendMessage: function () {\n var body = \"\"\n if (Matrix.enable_icons && Matrix.icon) {\n body += Matrix.icon + \" \"\n }\n body += Matrix.alert_subject + \"\\n\"\n body += Matrix.alert_message\n\n if (Matrix.event_url != undefined) {\n body += \"\\n\" + Matrix.event_url\n }\n\n var formatted_body = \"\"\n if (Matrix.enable_colors) {\n formatted_body += '<span data-mx-color=\"{color}\">'.replace(\"{color}\", Matrix.color)\n } else {\n formatted_body += \"<span>\"\n }\n\n formatted_body += \"<strong>\"\n if (Matrix.enable_icons && Matrix.icon) {\n formatted_body += Matrix.icon + \" \"\n }\n\n if (Matrix.event_url != undefined) {\n formatted_body += '<a href=\"{href}\">'.replace(\"{href}\", Matrix.event_url)\n }\n\n formatted_body += Matrix.alert_subject\n\n if (Matrix.event_url != undefined) {\n formatted_body += \"</a>\"\n }\n\n formatted_body += \"</strong><br />\"\n\n formatted_body += Matrix.alert_message.replace(/\\n/g, \"<br />\")\n formatted_body += \"</span>\"\n\n const payload = {\n body: body,\n msgtype: \"m.notice\",\n format: \"org.matrix.custom.html\",\n formatted_body: formatted_body,\n }\n\n Matrix.request(\n \"/_matrix/client/r0/rooms/\" + Matrix.matrix_room + \"/send/m.room.message\",\n payload\n )\n },\n}\n\ntry {\n var params = JSON.parse(value)\n\n Matrix.validate(params)\n\n try {\n Matrix.sendMessage()\n } catch (error) {\n if (error == \"User is not in room\") {\n Matrix.joinRoom()\n Matrix.sendMessage()\n } else {\n throw error\n }\n }\n\n return \"OK\"\n} catch (error) {\n Zabbix.Log(4, \"[Matrix Webhook] Error: \" + error)\n throw \"Sending failed: \" + error\n}\n"
}
]
}
}