-
Notifications
You must be signed in to change notification settings - Fork 3
/
dhmon.js
371 lines (322 loc) · 11.3 KB
/
dhmon.js
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
var switch_status = {};
var map = null;
var ping = null;
var snmp = null;
var model = null;
var iface = null;
var switch_vlans = null;
var dhcp_status = null;
var start_fetch = null;
var alert_hosts = null;
var ifre =/(^[gxf]e-[0-9\/]+$)|(Ethernet)/;
var gere =/(^ge-[0-9\/]+$)|(GigabitEthernet)/;
var errors_to_human = {
'OK': 'Everything working as expected',
'SPEED': 'At least one link is running at non-ideal speed or link is full',
'STP': 'At least one port is blocked by Spanning Tree',
'ERRORS': 'At least one port is dropping packets due to corruption',
'WARNING': 'The switch is not replying to SNMP requests',
'CRITICAL': 'The switch has not replied to ICMP for 30 seconds or more',
'ALERT': 'The switch has at least one alert present in the monitoring system'
};
var openDialog;
var openDialogName;
// TODO(bluecmd): Yeah, event mode makes you write the best code.
// Refactor later (TM).
function checkIfaceSpeed(sw, model, ifaces) {
if (model == undefined || ifaces == undefined)
return true;
var failed = false;
for (var name in ifaces) {
var iface = ifaces[name];
/* skip access ports, sleeping computers rarely link max speed */
if (!iface.trunk)
continue;
if (ifre.exec(name) == null) {
continue;
}
if (iface.status == 'up') {
if (iface.speed == '10') {
failed = true;
} else if (iface.speed == '100') {
if (gere.exec(name) == null) {
failed = true;
}
}
}
}
return !failed;
}
function checkIfaceErrors(sw, model, ifaces) {
if (model == undefined || ifaces == undefined)
return true;
var failed = false;
var show_consumer_ifaces =
document.getElementById('hilight_consumer_issues').checked;
for (var name in ifaces) {
var iface = ifaces[name];
/* skip access ports if we don't want to show consumer ifaces */
if (!iface.trunk && !show_consumer_ifaces)
continue;
if (ifre.exec(name) == null) {
continue;
}
if (iface.status == 'up') {
if (iface.errors_in > 0 || iface.errors_out > 0) {
failed = true;
}
}
}
return !failed;
}
function checkIfaceStp(sw, model, ifaces) {
if (model == undefined || ifaces == undefined)
return true;
var failed = false;
var show_consumer_ifaces =
document.getElementById('hilight_consumer_issues').checked;
for (var name in ifaces) {
var iface = ifaces[name];
/* skip access ports if we don't want to show consumer ifaces */
if (!iface.trunk && !show_consumer_ifaces)
continue;
/* TODO(bluecmd): Maybe not set 'error' on non-ethernet
* interfaces that don't speak STP */
if (ifre.exec(name) == null) {
continue;
}
if (iface.status == 'up') {
if (iface.stp == 'error') {
failed = true;
}
}
}
return !failed;
}
function computeStatus() {
if (iface == null || model == null || snmp == null || ping == null)
return;
var now = new Date();
var latency = now - start_fetch;
console.log(
'[' + now.toLocaleString() + '] Loaded new data in ' + latency + 'ms');
switch_status = {};
for (var sw in ping) {
if (ping[sw] >= 60) {
switch_status[sw] = 'CRITICAL';
} else if (!checkIfaceStp(sw, model[sw], iface[sw])) {
switch_status[sw] = 'STP';
} else if (!checkIfaceSpeed(sw, model[sw], iface[sw])) {
switch_status[sw] = 'SPEED';
} else if (!checkIfaceErrors(sw, model[sw], iface[sw])) {
switch_status[sw] = 'ERRORS';
} else if (snmp[sw] == undefined || snmp[sw].since > 360) {
switch_status[sw] = 'WARNING';
} else if (alert_hosts[sw] != undefined) {
switch_status[sw] = 'ALERT';
} else {
switch_status[sw] = 'OK';
}
var swname = sw.split('.')[0];
if(openDialogName == swname)
updateSwitchDialog(swname, sw);
}
dhmap.updateSwitches(switch_status);
dhmenu.updateSwitches(switch_status);
}
function click(sw) {
// Close open dialog
if(openDialog){
openDialog.dialog('destroy').remove()
openDialog = undefined;
openDialogName = undefined;
}
var title = '';
var swname = sw.name.split('.')[0];
title += '<div class="status" id="switch-' + swname + '" ></div>';
title += swname.toUpperCase();
var dialog = $('<div class="switchdialog">').attr({'title': title});
dialog.append($('<span>').attr({'id': 'info-' + swname}));
dialog.append($('<div>').attr({'id': 'dhcpinfo-' + swname}));
dialog.append($('<div>').attr({'id': 'ports-' + swname}));
dialog.append($('<br/>'));
dialog.append($('<div>').attr({'id': 'portinfo-' + swname}));
dialog.dialog({width: 500, height: 390, resizable: false,
close: function() {
$(this).dialog('destroy').remove()
openDialog = undefined;
openDialogName = undefined;
}});
openDialog = dialog;
openDialogName = swname;
updateSwitchDialog(swname, sw.name);
}
function updateSwitchDialog(sw, fqdn) {
var div = $('#switch-' + sw);
if (div == undefined || !iface || iface[fqdn] == undefined)
return
div.css({'background-color': dhmap.colour[switch_status[fqdn]]});
var info = $('#info-' + sw);
info.html('<p>Status: ' + errors_to_human[switch_status[fqdn]] + '</p>');
var dhcpinfo = $('#dhcpinfo-' + sw);
dhcpinfo.html('');
var dhcptable = $('<table width="300px">');
if (switch_vlans[fqdn] != undefined) {
dhcptable.append(
'<tr><th>Network</th><th>Clients</th><th>Max</th><th>DHCP Pool Usage</th>');
for (var vlan in switch_vlans[fqdn]) {
// Grab the first network with the same VLAN
for (var network in dhcp_status) {
var ds = dhcp_status[network];
if (ds.vlan == vlan) {
dhcptable.append(
$('<tr>')
.append($('<td>').text(network))
.append($('<td>').text(ds.usage))
.append($('<td>').text(ds.max))
.append($('<td>').text(Math.ceil(ds.usage / ds.max * 100) + '%')))
}
}
}
dhcpinfo.append(dhcptable);
} else {
dhcpinfo.append('<i>No DHCP pool data available because VLAN information unavailable for switch model</i>');
}
var ports = $('#ports-' + sw);
ports.html('<hr/>');
var portsdiv = $('<div>');
var order = {};
for (var idx in iface[fqdn]) {
order[parseInt(iface[fqdn][idx].lastoid)] = idx;
}
function sortNum(a, b) {
return a - b;
}
var count = 0;
var key_order = Object.keys(order).sort(sortNum);
for (var kidx in key_order) {
var idx = order[key_order[kidx]];
var entry = iface[fqdn][idx];
var ifacename = idx;
/* Skip special interfaces */
if (ifre.exec(ifacename) == null) {
continue;
}
count++;
if (count % 24 == 1 && count > 1)
portsdiv.append('<br />');
var portdiv = $('<div class="port">').attr({
'id': 'port-' + sw + ':' + entry.lastoid});
tx_full = false;
rx_full = false;
speed_fail = false;
if (entry.status == 'up') {
portdiv.css({'background-color': dhmap.colour.OK});
tx_full = ((entry.tx_10min * 8 / 1000 / 1000) / entry.speed > 0.95);
rx_full = ((entry.rx_10min * 8 / 1000 / 1000) / entry.speed > 0.95);
speed_fail = (entry.trunk && parseInt(entry.speed) < 1000) ||
(!entry.trunk && parseInt(entry.speed) < 100);
if (parseFloat(entry.errors_in) > 0)
portdiv.css({'background-color': dhmap.colour.ERRORS});
if (parseFloat(entry.errors_out) > 0)
portdiv.css({'background-color': dhmap.colour.ERRORS});
if (tx_full || rx_full || speed_fail)
portdiv.css({'background-color': dhmap.colour.SPEED});
if (!entry.trunk && parseInt(entry.speed) < 1000 &&
gere.exec(ifacename) != null)
portdiv.css({'background-color': dhmap.colour.SPEED});
if (entry.stp == 'error')
portdiv.css({'background-color': dhmap.colour.STP});
}
if (entry.admin != 'up')
portdiv.css({'background-color': dhmap.colour.WARNING});
portdiv.hover(function(entry, ifacename, sw, tx_full, rx_full, speed_fail) {
var portinfo = $('#portinfo-' + sw);
portinfo.html('');
var table = $('<table width="90%">');
table.append(
$('<tr>').append('<td style="width: 150px">Interface:</td><td>'
+ ifacename + '</td>'));
table.append(
$('<tr>').append('<td>Status:</td><td>' + entry.status + '</td>'));
if (entry.admin != 'up') {
table.append($('<tr>').append(
'<td colspan="2"><b>Administratively Disabled</b></td>'));
}
if (entry.status == 'up') {
table.append(
$('<tr>').append('<td>Spanning Tree:</td><td>' + entry.stp + '</td>'));
sb = speed_fail ? 'font-weight: bold; animation: blinker 1s linear infinite;' : '';
table.append(
$('<tr>').append('<td>Speed:</td><td><span style="' + sb + '">' + entry.speed + ' Mbit/s</span></td>'));
tb = tx_full ? 'font-weight: bold; animation: blinker 1s linear infinite;' : '';
rb = rx_full ? 'font-weight: bold; animation: blinker 1s linear infinite;' : '';
table.append(
$('<tr>').append('<td>Traffic (10 min avg.):</td><td>' +
'TX: <span style="' + tb + '">' + Math.ceil(entry.tx_10min*8/1000/1000) + ' Mbit/s</span>, ' +
'RX: <span style="' + rb + '">' + Math.ceil(entry.rx_10min*8/1000/1000) + ' Mbit/s</span> </td>'));
table.append(
$('<tr>').append('<td>Traffic (instant):</td><td>' +
'TX: ' + Math.ceil(entry.tx*8/1000/1000) + ' Mbit/s, ' +
'RX: ' + Math.ceil(entry.rx*8/1000/1000) + ' Mbit/s </td>'));
table.append(
$('<tr>').append(
'<td>Errors:</td><td>In: ' + Math.ceil(entry.errors_in*100)/100 +
', Out: ' + Math.ceil(entry.errors_out*100)/100 + '</td>'));
}
portinfo.append(table);
$(this).css({'border-color': 'red'});
}.bind(portdiv, entry, ifacename, sw, tx_full, rx_full, speed_fail), function() {
$(this).css({'border-color': 'black'});
}.bind(portdiv));
portsdiv.append(portdiv);
}
ports.append(portsdiv);
}
$.getJSON('./data.json', function(objects) {
dhmap.init(objects, click);
dhmenu.init(objects, click);
function updateStatus() {
start_fetch = new Date();
$.when(
$.getJSON('/analytics/ping.status', function(objects) {
ping = objects;
}),
$.getJSON('/analytics/snmp.saves', function(objects) {
snmp = objects;
}),
$.getJSON('/analytics/switch.model', function(objects) {
model = objects;
}),
$.getJSON('/analytics/switch.interfaces', function(objects) {
iface = objects;
}),
$.getJSON('/analytics/dhcp.status', function(objects) {
dhcp_status = objects;
}),
$.getJSON('/analytics/switch.vlans', function(objects) {
switch_vlans = objects;
}),
$.getJSON('/analytics/alerts.hosts', function(objects) {
alert_hosts = objects;
})
).then(function() {
computeStatus();
});
}
setInterval(updateStatus, 10000);
updateStatus();
});
// Allow HTML in the dialog title
$.widget("ui.dialog", $.extend({}, $.ui.dialog.prototype, {
_title: function(title) {
if (!this.options.title ) {
title.html(" ");
} else {
title.html(this.options.title);
}
}
}));
function darkmode(mode) {
document.body.style.backgroundColor = mode ? '#111' : '#fff';
}