-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.0_MediaTypeScript
229 lines (204 loc) · 9.53 KB
/
5.0_MediaTypeScript
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
var SEVERITY_COLORS_VERT = [
'data:image/gif;base64,R0lGODdhCAABAIEAAMPDwwAAAAAAAAAAACwAAAAACAABAAAIBwABCBwoMCAAOw==', // Not classified.
'data:image/gif;base64,R0lGODdhCAABAIEAALaz/wAAAAAAAAAAACwAAAAACAABAAAIBwABCBwoMCAAOw==', // Information.
'data:image/gif;base64,R0lGODdhCAABAIEAAO7JUwAAAAAAAAAAACwAAAAACAABAAAIBwABCBwoMCAAOw==', // Warning.
'data:image/gif;base64,R0lGODdhCAABAIEAAOaDAAAAAAAAAAAAACwAAAAACAABAAAIBwABCBwoMCAAOw==', // Average.
'data:image/gif;base64,R0lGODdhCAABAIEAAOZNAAAAAAAAAAAAACwAAAAACAABAAAIBwABCBwoMCAAOw==', // High.
'data:image/gif;base64,R0lGODdhCAABAIEAAOYAAAAAAAAAAAAAACwAAAAACAABAAAIBwABCBwoMCAAOw==', // Disaster.
'data:image/gif;base64,R0lGODdhCAABAIEAAACeGgAAAAAAAAAAACwAAAAACAABAAAIBwABCBwoMCAAOw==', // Resolved.
'data:image/gif;base64,R0lGODdhCAABAIEAAMPDwwAAAAAAAAAAACwAAAAACAABAAAIBwABCBwoMCAAOw==' // Default.
];
var SEVERITY_COLORS_HOR = [
'data:image/gif;base64,R0lGODdhAQAIAIEAAMPDwwAAAAAAAAAAACwAAAAAAQAIAAAIBwABCBwoMCAAOw==', // Not classified.
'data:image/gif;base64,R0lGODdhAQAIAIEAALaz/wAAAAAAAAAAACwAAAAAAQAIAAAIBwABCBwoMCAAOw==', // Information.
'data:image/gif;base64,R0lGODdhAQAIAIEAAO7JUwAAAAAAAAAAACwAAAAAAQAIAAAIBwABCBwoMCAAOw==', // Warning.
'data:image/gif;base64,R0lGODdhAQAIAIEAAOaDAAAAAAAAAAAAACwAAAAAAQAIAAAIBwABCBwoMCAAOw==', // Average.
'data:image/gif;base64,R0lGODdhAQAIAIEAAOZNAAAAAAAAAAAAACwAAAAAAQAIAAAIBwABCBwoMCAAOw==', // High.
'data:image/gif;base64,R0lGODdhAQAIAIEAAOYAAAAAAAAAAAAAACwAAAAAAQAIAAAIBwABCBwoMCAAOw==', // Disaster.
'data:image/gif;base64,R0lGODdhAQAIAIEAAACeGgAAAAAAAAAAACwAAAAAAQAIAAAIBwABCBwoMCAAOw==', // Resolved.
'data:image/gif;base64,R0lGODdhAQAIAIEAAMPDwwAAAAAAAAAAACwAAAAAAQAIAAAIBwABCBwoMCAAOw==' // Default.
];
try {
var params = JSON.parse(value);
params.zabbix_url = params.zabbix_url.endsWith('/') ? params.zabbix_url.slice(0, -1) : params.zabbix_url;
if ([0, 1, 2, 3].indexOf(parseInt(params.event_source)) === -1) {
throw 'Incorrect "event_source" parameter given: "' + params.event_source + '".\nMust be 0-3.';
}
// Set "use_default_message" to true for non trigger-based events.
if (params.event_source !== '0') {
params.use_default_message = 'true';
}
// Check {EVENT.VALUE} for trigger-based and internal events.
if (params.event_value !== '0' && params.event_value !== '1' &&
(params.event_source === '0' || params.event_source === '3')) {
throw 'Incorrect "event_value" parameter given: "' + params.event_value + '".\nMust be 0 or 1.';
}
// Check {EVENT.UPDATE.STATUS} only for trigger-based events.
if (params.event_update_status !== '0' && params.event_update_status !== '1' && params.event_source === '0') {
throw 'Incorrect "event_update_status" parameter given: "' + params.event_update_status + '".\nMust be 0 or 1.';
}
if (params.event_value === '0') {
params.event_nseverity = '6';
}
if (!SEVERITY_COLORS_HOR[params.event_nseverity]) {
params.event_nseverity = '7';
}
var request = new CurlHttpRequest();
var body = {
type: "message",
attachments: [{
contentType: "application/vnd.microsoft.card.adaptive",
content: {
$schema: "http://adaptivecards.io/schemas/adaptive-card.json",
type: "AdaptiveCard",
version: "1.4",
"msteams": {
"width": "Full"
},
"backgroundImage": {
"url": params.bannertype === "vertical" ? SEVERITY_COLORS_VERT[params.event_nseverity] : SEVERITY_COLORS_HOR[params.event_nseverity],
"fillMode": params.bannertype === "vertical" ? "RepeatVertically" : "RepeatHorizontally"
},
body: [
{
type: "TextBlock",
text: "Zabbix",
weight: "bolder",
size: "medium"
},
{
type: "TextBlock",
text: params.alert_subject,
wrap: true,
size: "large",
weight: "bolder"
},
{
type: "TextBlock",
text: 'On ' + params.host_name + ' [' + params.host_ip + ']',
wrap: true
},
{
type: "TextBlock",
text: params.use_default_message.toLowerCase() === 'true'
? params.alert_message
: params.trigger_description,
wrap: true
}
]
}
}]
};
if (params.use_default_message.toLowerCase() !== 'true') {
// Problem message.
if (params.event_value === '1' && params.event_update_status === '0') {
body.attachments[0].content.body.push({
type: "TextBlock",
text: '**Event time:** ' + params.event_time + ' ' + params.event_date,
wrap: true
});
}
// Update message.
else if (params.event_update_status === '1') {
body.attachments[0].content.body.push({
type: "TextBlock",
text: params.event_update_user + ' ' + params.event_update_action + '.',
wrap: true
});
if (params.event_update_message) {
body.attachments[0].content.body.push({
type: "TextBlock",
text: '**Message:**\n' + params.event_update_message,
wrap: true
});
}
body.attachments[0].content.body.push({
type: "TextBlock",
text: '**Event update time:** ' + params.event_update_time + ' ' + params.event_update_date,
wrap: true
});
}
// Resolved message.
else {
body.attachments[0].content.body.push({
type: "TextBlock",
text: '**Recovery time:** ' + params.event_recovery_time + ' ' + params.event_recovery_date,
wrap: true
});
}
if (params.event_severity && params.event_severity !== '{EVENT.SEVERITY}') {
body.attachments[0].content.body.push({
type: "TextBlock",
text: '**Severity:** ' + params.event_severity,
wrap: true
});
}
if (params.event_opdata && params.event_opdata !== '{EVENT.OPDATA}') {
var opDataLines = params.event_opdata.split(/(?:\r\n|\r|\n)/);
body.attachments[0].content.body.push({
type: "TextBlock",
text: '**Operational data:**',
wrap: true
});
opDataLines.forEach(function (line) {
body.attachments[0].content.body.push({
type: "TextBlock",
text: line,
wrap: true
});
});
}
if (params.event_tags && params.event_tags !== '{EVENT.TAGS}') {
body.attachments[0].content.body.push({
type: "TextBlock",
text: '**Event tags:** ' + params.event_tags,
wrap: true
});
}
Object.keys(params).forEach(function (key) {
if (key.startsWith('fact_') && params[key] !== '') {
body.attachments[0].content.body.push({
type: "TextBlock",
text: '**' + key.substring(5) + ':** ' + params[key],
wrap: true
});
} else if (key.startsWith('openUri_') && params[key] !== '' && !params[key].startsWith('{')) {
body.attachments[0].content.body.push({
type: "ActionSet",
actions: [
{
type: "Action.OpenUrl",
title: key.substring(8),
url: params[key]
}
]
});
}
});
body.attachments[0].content.body.push({
type: "ActionSet",
actions: [
{
type: "Action.OpenUrl",
title: (params.event_source === '0' && params.event_recovery_value !== '0')
? "Acknowledge"
: "View Event History",
url: (params.event_source === '0' && params.event_recovery_value !== '0')
? params.zabbix_url + "/zabbix.php?action=popup&popup_action=acknowledge.edit&eventids%5B%5D=" + params.event_id
: params.zabbix_url + "/tr_events.php?triggerid=" + params.trigger_id + "&eventid=" + params.event_id
}
]
});
}
request.AddHeader('Content-Type: application/json');
if (typeof params.HTTPProxy === 'string' && params.HTTPProxy !== '') {
request.SetProxy(params.HTTPProxy);
}
Zabbix.Log(4, '[ MS Teams Webhook ] JSON: ' + JSON.stringify(body));
var response = request.Post(params.teams_endpoint, JSON.stringify(body));
Zabbix.Log(4, '[ MS Teams Webhook ] Response: ' + response);
return 'OK';
} catch (error) {
Zabbix.Log(3, '[ MS Teams Webhook ] ERROR: ' + error);
throw 'Sending failed: ' + error;
}