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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
// Registry of netdata hosts
NETDATA.alarms = {
onclick: null, // the callback to handle the click - it will be called with the alarm log entry
chart_div_offset: -50, // give that space above the chart when scrolling to it
chart_div_id_prefix: 'chart_', // the chart DIV IDs have this prefix (they should be NETDATA.name2id(chart.id))
chart_div_animation_duration: 0,// the duration of the animation while scrolling to a chart
ms_penalty: 0, // the time penalty of the next alarm
ms_between_notifications: 500, // firefox moves the alarms off-screen (above, outside the top of the screen)
// if alarms are shown faster than: one per 500ms
update_every: 10000, // the time in ms between alarm checks
notifications: false, // when true, the browser supports notifications (may not be granted though)
last_notification_id: 0, // the id of the last alarm_log we have raised an alarm for
first_notification_id: 0, // the id of the first alarm_log entry for this session
// this is used to prevent CLEAR notifications for past events
// notifications_shown: [],
server: null, // the server to connect to for fetching alarms
current: null, // the list of raised alarms - updated in the background
// a callback function to call every time the list of raised alarms is refreshed
callback: (typeof netdataAlarmsActiveCallback === 'function') ? netdataAlarmsActiveCallback : null,
// a callback function to call every time a notification is shown
// the return value is used to decide if the notification will be shown
notificationCallback: (typeof netdataAlarmsNotifCallback === 'function') ? netdataAlarmsNotifCallback : null,
recipients: null, // the list (array) of recipients to show alarms for, or null
recipientMatches: function (to_string, wanted_array) {
if (typeof wanted_array === 'undefined' || wanted_array === null || Array.isArray(wanted_array) === false) {
return true;
}
let r = ' ' + to_string.toString() + ' ';
let len = wanted_array.length;
while (len--) {
if (r.indexOf(' ' + wanted_array[len] + ' ') >= 0) {
return true;
}
}
return false;
},
activeForRecipients: function () {
let active = {};
let data = NETDATA.alarms.current;
if (typeof data === 'undefined' || data === null) {
return active;
}
for (let x in data.alarms) {
if (!data.alarms.hasOwnProperty(x)) {
continue;
}
let alarm = data.alarms[x];
if ((alarm.status === 'WARNING' || alarm.status === 'CRITICAL') && NETDATA.alarms.recipientMatches(alarm.recipient, NETDATA.alarms.recipients)) {
active[x] = alarm;
}
}
return active;
},
notify: function (entry) {
// console.log('alarm ' + entry.unique_id);
if (entry.updated) {
// console.log('alarm ' + entry.unique_id + ' has been updated by another alarm');
return;
}
let value_string = entry.value_string;
if (NETDATA.alarms.current !== null) {
// get the current value_string
let t = NETDATA.alarms.current.alarms[entry.chart + '.' + entry.name];
if (typeof t !== 'undefined' && entry.status === t.status && typeof t.value_string !== 'undefined') {
value_string = t.value_string;
}
}
let name = entry.name.replace(/_/g, ' ');
let status = entry.status.toLowerCase();
let title = name + ' = ' + value_string.toString();
let tag = entry.alarm_id;
let icon = 'images/banner-icon-144x144.png';
let interaction = false;
let data = entry;
let show = true;
// console.log('alarm ' + entry.unique_id + ' ' + entry.chart + '.' + entry.name + ' is ' + entry.status);
switch (entry.status) {
case 'REMOVED':
show = false;
break;
case 'UNDEFINED':
return;
case 'UNINITIALIZED':
return;
case 'CLEAR':
if (entry.unique_id < NETDATA.alarms.first_notification_id) {
// console.log('alarm ' + entry.unique_id + ' is not current');
return;
}
if (entry.old_status === 'UNINITIALIZED' || entry.old_status === 'UNDEFINED') {
// console.log('alarm' + entry.unique_id + ' switch to CLEAR from ' + entry.old_status);
return;
}
if (entry.no_clear_notification) {
// console.log('alarm' + entry.unique_id + ' is CLEAR but has no_clear_notification flag');
return;
}
title = name + ' back to normal (' + value_string.toString() + ')';
icon = 'images/check-mark-2-128-green.png';
interaction = false;
break;
case 'WARNING':
if (entry.old_status === 'CRITICAL') {
status = 'demoted to ' + entry.status.toLowerCase();
}
icon = 'images/alert-128-orange.png';
interaction = false;
break;
case 'CRITICAL':
if (entry.old_status === 'WARNING') {
status = 'escalated to ' + entry.status.toLowerCase();
}
icon = 'images/alert-128-red.png';
interaction = true;
break;
default:
console.log('invalid alarm status ' + entry.status);
return;
}
// filter recipients
if (show) {
show = NETDATA.alarms.recipientMatches(entry.recipient, NETDATA.alarms.recipients);
}
/*
// cleanup old notifications with the same alarm_id as this one
// it does not seem to work on any web browser - so notifications cannot be removed
let len = NETDATA.alarms.notifications_shown.length;
while (len--) {
let n = NETDATA.alarms.notifications_shown[len];
if (n.data.alarm_id === entry.alarm_id) {
console.log('removing old alarm ' + n.data.unique_id);
// close the notification
n.close.bind(n);
// remove it from the array
NETDATA.alarms.notifications_shown.splice(len, 1);
len = NETDATA.alarms.notifications_shown.length;
}
}
*/
if (show) {
if (typeof NETDATA.alarms.notificationCallback === 'function') {
show = NETDATA.alarms.notificationCallback(entry);
}
if (show) {
setTimeout(function () {
// show this notification
// console.log('new notification: ' + title);
let n = new Notification(title, {
body: entry.hostname + ' - ' + entry.chart + ' (' + entry.family + ') - ' + status + ': ' + entry.info,
tag: tag,
requireInteraction: interaction,
icon: NETDATA.serverStatic + icon,
data: data
});
n.onclick = function (event) {
event.preventDefault();
NETDATA.alarms.onclick(event.target.data);
};
// console.log(n);
// NETDATA.alarms.notifications_shown.push(n);
// console.log(entry);
}, NETDATA.alarms.ms_penalty);
NETDATA.alarms.ms_penalty += NETDATA.alarms.ms_between_notifications;
}
}
},
scrollToChart: function (chart_id) {
if (typeof chart_id === 'string') {
let offset = $('#' + NETDATA.alarms.chart_div_id_prefix + NETDATA.name2id(chart_id)).offset();
if (typeof offset !== 'undefined') {
$('html, body').animate({scrollTop: offset.top + NETDATA.alarms.chart_div_offset}, NETDATA.alarms.chart_div_animation_duration);
return true;
}
}
return false;
},
scrollToAlarm: function (alarm) {
if (typeof alarm === 'object') {
let ret = NETDATA.alarms.scrollToChart(alarm.chart);
if (ret && NETDATA.options.page_is_visible === false) {
window.focus();
}
// alert('netdata dashboard will now scroll to chart: ' + alarm.chart + '\n\nThis alarm opened to bring the browser window in front of the screen. Click on the dashboard to prevent it from appearing again.');
}
},
notifyAll: function () {
// console.log('FETCHING ALARM LOG');
NETDATA.alarms.get_log(NETDATA.alarms.last_notification_id, function (data) {
// console.log('ALARM LOG FETCHED');
if (data === null || typeof data !== 'object') {
console.log('invalid alarms log response');
return;
}
if (data.length === 0) {
console.log('received empty alarm log');
return;
}
// console.log('received alarm log of ' + data.length + ' entries, from ' + data[data.length - 1].unique_id.toString() + ' to ' + data[0].unique_id.toString());
data.sort(function (a, b) {
if (a.unique_id > b.unique_id) {
return -1;
}
if (a.unique_id < b.unique_id) {
return 1;
}
return 0;
});
NETDATA.alarms.ms_penalty = 0;
let len = data.length;
while (len--) {
if (data[len].unique_id > NETDATA.alarms.last_notification_id) {
NETDATA.alarms.notify(data[len]);
}
//else
// console.log('ignoring alarm (older) with id ' + data[len].unique_id.toString());
}
NETDATA.alarms.last_notification_id = data[0].unique_id;
if (typeof netdataAlarmsRemember === 'undefined' || netdataAlarmsRemember) {
NETDATA.localStorageSet('last_notification_id', NETDATA.alarms.last_notification_id, null);
}
// console.log('last notification id = ' + NETDATA.alarms.last_notification_id);
})
},
check_notifications: function () {
// returns true if we should fire 1+ notifications
if (NETDATA.alarms.notifications !== true) {
// console.log('web notifications are not available');
return false;
}
if (Notification.permission !== 'granted') {
// console.log('web notifications are not granted');
return false;
}
if (typeof NETDATA.alarms.current !== 'undefined' && typeof NETDATA.alarms.current.alarms === 'object') {
// console.log('can do alarms: old id = ' + NETDATA.alarms.last_notification_id + ' new id = ' + NETDATA.alarms.current.latest_alarm_log_unique_id);
if (NETDATA.alarms.current.latest_alarm_log_unique_id > NETDATA.alarms.last_notification_id) {
// console.log('new alarms detected');
return true;
}
//else console.log('no new alarms');
}
// else console.log('cannot process alarms');
return false;
},
get: function (what, callback) {
$.ajax({
url: NETDATA.alarms.server + '/api/v1/alarms?' + what.toString(),
async: true,
cache: false,
headers: {
'Cache-Control': 'no-cache, no-store',
'Pragma': 'no-cache'
},
xhrFields: {withCredentials: true} // required for the cookie
})
.done(function (data) {
data = NETDATA.xss.checkOptional('/api/v1/alarms', data /*, '.*\.(calc|calc_parsed|warn|warn_parsed|crit|crit_parsed)$' */);
if (NETDATA.alarms.first_notification_id === 0 && typeof data.latest_alarm_log_unique_id === 'number') {
NETDATA.alarms.first_notification_id = data.latest_alarm_log_unique_id;
}
if (typeof callback === 'function') {
return callback(data);
}
})
.fail(function () {
NETDATA.error(415, NETDATA.alarms.server);
if (typeof callback === 'function') {
return callback(null);
}
});
},
update_forever: function () {
if (netdataShowAlarms !== true || netdataSnapshotData !== null) {
return;
}
NETDATA.alarms.get('active', function (data) {
if (data !== null) {
NETDATA.alarms.current = data;
if (NETDATA.alarms.check_notifications()) {
NETDATA.alarms.notifyAll();
}
if (typeof NETDATA.alarms.callback === 'function') {
NETDATA.alarms.callback(data);
}
// Health monitoring is disabled on this netdata
if (data.status === false) {
return;
}
}
setTimeout(NETDATA.alarms.update_forever, NETDATA.alarms.update_every);
});
},
get_log: function (last_id, callback) {
// console.log('fetching all log after ' + last_id.toString());
$.ajax({
url: NETDATA.alarms.server + '/api/v1/alarm_log?after=' + last_id.toString(),
async: true,
cache: false,
headers: {
'Cache-Control': 'no-cache, no-store',
'Pragma': 'no-cache'
},
xhrFields: {withCredentials: true} // required for the cookie
})
.done(function (data) {
data = NETDATA.xss.checkOptional('/api/v1/alarm_log', data);
if (typeof callback === 'function') {
return callback(data);
}
})
.fail(function () {
NETDATA.error(416, NETDATA.alarms.server);
if (typeof callback === 'function') {
return callback(null);
}
});
},
init: function () {
NETDATA.alarms.server = NETDATA.fixHost(NETDATA.serverDefault);
if (typeof netdataAlarmsRemember === 'undefined' || netdataAlarmsRemember) {
NETDATA.alarms.last_notification_id =
NETDATA.localStorageGet('last_notification_id', NETDATA.alarms.last_notification_id, null);
}
if (NETDATA.alarms.onclick === null) {
NETDATA.alarms.onclick = NETDATA.alarms.scrollToAlarm;
}
if (typeof netdataAlarmsRecipients !== 'undefined' && Array.isArray(netdataAlarmsRecipients)) {
NETDATA.alarms.recipients = netdataAlarmsRecipients;
}
if (netdataShowAlarms) {
NETDATA.alarms.update_forever();
if ('Notification' in window) {
// console.log('notifications available');
NETDATA.alarms.notifications = true;
if (Notification.permission === 'default') {
Notification.requestPermission();
}
}
}
}
};
|