forked from aletson/zabbixweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
477 lines (435 loc) · 20.8 KB
/
index.html
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="js/ext/jquery.min.js"></script>
<script src="js/ext/jquery-ui.min.js"></script>
<link rel="stylesheet" href="css/ext/jquery-ui.min.css" type="text/css" media="all" />
<script src="js/ext/bootstrap.min.js"></script>
<link href="css/ext/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/font-awesome.min.css" type="text/css">
<script src="js/zabbix_library.js"></script>
<script src="js/jquery.circleprogress.js"></script>
<style>
.nounderline{text-decoration: none !important}
body {
font-family: "BebasNeue";
}
/* This is for the jquery-circleprogress plugin */
.circle {
width: 100px;
margin: 6px 6px 20px;
display: inline-block;
position: relative;
text-align: center;
line-height: 1.2;
}
.circle canvas {
vertical-align: top;
}
.circle strong {
position: absolute;
top: 20px;
left: 0;
width: 100%;
text-align: center;
line-height: 40px;
font-size: 26px;
}
.circle strong i {
font-style: normal;
font-size: 0.6em;
font-weight: normal;
}
.circle span {
display: block;
color: #aaa;
margin-top: 12px;
}
/* Left border for the server cells */
.col-md-3 {
margin-left: 0;
padding-left: 19px;
border-left: 1px solid #555;
}
/* CSS animation for server alerts */
.blink{
-webkit-animation: blink 700ms infinite alternate;
-moz-animation: blink 700ms infinite alternate;
-o-animation: blink 700ms infinite alternate;
animation: blink 700ms infinite alternate;
background-color: red;
}
@-webkit-keyframes blink {
from { background:rgba(255, 0, 0, 1); }
to { background:rgba(255, 0, 0, 0); }
}
@-o-keyframes blink {
from { background:rgba(255, 0, 0, 1); }
to { background:rgba(255, 0, 0, 0); }
}
@-moz-keyframes blink {
from { background:rgba(255, 0, 0, 1); }
to { background:rgba(255, 0, 0, 0); }
}
@keyframes blink {
from { background:rgba(255, 0, 0, 1); }
to { background:rgba(255, 0, 0, 0); }
};
.row{
overflow: hidden;
}
</style>
<title>Zabbix Monitoring System</title>
</head>
<body>
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<span style="float: right; font-size: 9px;">
<a data-toggle="modal" data-target="#settings-modal">settings</a>
</span>
</div>
</div>
</div>
<!-- Populated by API call -->
<div id="hosts-list" class="row"></div>
<div class="text-muted">Original work by <a href="http://www.andrewletson.com">Andrew Letson</a> with some changes from <a href="http://ebur.co">Eric Burgueño</a></div>
<!-- Settings modal -->
<div class="modal fade" id="settings-modal" tabindex="-1" role="dialog" aria-labelledby="Settings" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="Settings">Settings</h4>
</div>
<div class="modal-body">
<label for="network-dropdown"><input class="settings" type="checkbox" name="network-dropdown" id="network-dropdown" /> Show network statistics in a clickable dropdown</label><br/>
<label for="disks-dropdown"><input class="settings" type="checkbox" name="disks-dropdown" id="disks-dropdown" /> Show disk statistics in a clickable dropdown</label><br/>
<label for="show-meters"><input class="settings" type="checkbox" name="show-meters" id="show-meters" /> Display meter graphics for CPU and RAM usage (disable this for a more compact view)</label><br/>
<label for="view-list"><input type="radio" class="settings view-select" name="view-select" id="view-select-list" value="list" />List view</label>
<label for="view-grid"><input type="radio" class="settings view-select" name="view-select" id="view-select-grid" value="grid" checked />Grid view</label><br/>
<label for="view-2"><input type="radio" class="settings view-grid-cols" name="view-grid-cols" id="view-grid-cols-2" value="6">2 columns</label>
<label for="view-3"><input type="radio" class="settings view-grid-cols" name="view-grid-cols" id="view-grid-cols-3" value="4">3 columns</label>
<label for="view-4"><input type="radio" class="settings view-grid-cols" name="view-grid-cols" id="view-grid-cols-4" value="3">4 columns</label>
<label for="view-6"><input type="radio" class="settings view-grid-cols" name="view-grid-cols" id="view-grid-cols-6" value="2">6 columns</label>
<label for="view-12"><input type="radio" class="settings view-grid-cols" name="view-grid-cols" id="view-grid-cols-12" value="1">12 columns</label><br/>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Save</button>
</div>
</div>
</div>
</div>
</div>
<script>
var zabbix;
var usedHosts = [];
$(document).ready(function() {
$('#view-select-grid').click(function() {
$('.view-grid-cols').attr('disabled', false);
});
$('#view-select-list').click(function() {
$('.view-grid-cols').attr('checked', false);
$('.view-grid-cols').attr('disabled', true);
});
$('.settings').click(function() {
filterHostsAndOutput(hosts, 1); //Any settings change should trigger a redraw.
});
zabbix = new $.zabbix ('http://zabbix.org/zabbix/api_jsonrpc.php', 'guest', ''); //Change zabbix.org to your server's IP, guest to a Zabbix user and the blank to the password
if(zabbix.getApiVersion() >= '3.0.1') {
console.log(zabbix.getApiVersion());
throw new Error('Zabbix has been updated, check the new documentation then update the code and Zabbix api version');
//API changes can be problematic. The current code seems to work well with 2.2, 2.4 and 3.0 versions.
}
zabbix.authenticate(); //this gives us our API key, which gets assigned to the zabbix object
var hosts = getHostsFromZabbix(zabbix);
filterHostsAndOutput(hosts);
//TODO fix this. If the next API call starts before the last one finishes, the dashboard explodes and it's not pretty.
setInterval(function() { var hosts = getHostsFromZabbix(zabbix); filterHostsAndOutput(hosts); }, 30000);
});
function getHostsFromZabbix(zabbix) {
return zabbix.call('host.get', {
//"monitored_hosts": true, //Only display hosts that are enabled (status=enabled) in Zabbix
"selectInventory": true,
"selectItems": [
"name",
"lastvalue",
"units",
"itemid",
"key_",
"lastclock",
"value_type",
"status",
"itemid"
],//Next line is thanks to eburgueno, disabled unless you want to use it
// "groupids": "11", //Only display hosts in this groupID #. You should create a new one with all the hosts you want, and templates
"output": "extend",
"search" : {"host" : ""},
"sortfield" : "host",
"expandDescription": 1,
"expandData": 1
});
}
function Disk(Name, pctFree, used, total) {
this.Name = Name;
this.pctFree = pctFree;
this.used = used;
this.total = total;
}
function findIndexOfObjectWithAttributeInArray(array, attr, value) {
for(var i = 0; i < array.length; i += 1) {
if(array[i][attr] === value) {
return i;
}
}
return false;
}
//This process takes the hosts retrieved from the API call and creates the view on the webpage.
function filterHostsAndOutput(hosts, redraw) {
if (typeof(redraw)==='undefined') {
redraw = 0;
}
var anError = 0;
var hostsCount = 0;
if($("input:radio[name=view-grid-cols]:checked").val()) {
columnWidth = $('input:radio[name=view-grid-cols]:checked').val(); //If a value has been selected for the grid view
} else if ($('input:radio[name=view-select]:checked').val() == 'grid') {
columnWidth = 3; //If the grid view is selected but no value has been assigned for view-grid-cols
}
if(redraw == 1) {
$('#hosts-list').empty();
usedHosts = [];
}
//$('#hosts-list').empty();
var existingHosts = [];
$.each(hosts, function() {
var thisHost = $(this);
var thisHostObj = thisHost[0];
var processorCount = 1; //default to one CPU (or thread) in case system.cpu.num is not being gathered for the host
var loadTotals = 0;
var name_index = false;
thisHostObj.agentPing = '';
thisHostObj.totalMem = 0;
thisHostObj.freeMem = 0;
thisHostObj.numProcesses = 0;
thisHostObj.networkTraffic = 0;
thisHostObj.Disks = [];
$.each(thisHostObj.items, function() { //Parse the statistics to human-readable.
switch ($(this)[0].key_) {
case 'system.cpu.num': //You may have to add this to your Zabbix templates.
processorCount = $(this)[0].lastvalue;
break;
case 'system.cpu.load': //You may have to add this to your Zabbix templates (as float)
loadTotals += $(this)[0].lastvalue;
break;
case 'agent.ping':
thisHostObj.agentPing = $(this)[0].lastvalue;
break;
case 'vm.memory.size[total]': //You may have to add these as well
thisHostObj.totalMem = $(this)[0].lastvalue / 1073741824; //1024^3 => GB
break;
case 'vm.memory.size[free]':
thisHostObj.freeMem += $(this)[0].lastvalue / 1073741824; //free(avail)=free+cached+inactive on linux, free on Windows
break;
case 'vm.memory.size[cached]':
thisHostObj.freeMem += $(this)[0].lastvalue / 1073741824;
break;
case 'vm.memory.size[inactive]':
thisHostObj.freeMem += $(this)[0].lastvalue / 1073741824;
break;
case 'proc.num[]':
thisHostObj.numProcesses = $(this)[0].lastvalue;
break;
//as seen in http://stackoverflow.com/a/22099079 for case substring matching. There may be a better way to do this...
case ( ($(this)[0].key_.match(/vfs.fs.size/) )? $(this)[0].key_: undefined ):
diskname = $(this)[0].key_.substring(12,$(this)[0].key_.indexOf(",")); //12 is where the name of the disk starts
name_index = findIndexOfObjectWithAttributeInArray(thisHostObj.Disks, 'Name', diskname);
if (name_index === false) {
thisHostObj.Disks.push(new Disk(diskname));
name_index = findIndexOfObjectWithAttributeInArray(thisHostObj.Disks, 'Name', diskname);
}
switch ($(this)[0].key_) {
case ( ($(this)[0].key_.match(/,pfree/) )? $(this)[0].key_: undefined ):
thisHostObj.Disks[name_index].pctFree = 100 - $(this)[0].lastvalue;
break;
case ( ($(this)[0].key_.match(/,used/) )? $(this)[0].key_: undefined ):
thisHostObj.Disks[name_index].used = $(this)[0].lastvalue / 1073741824;
break;
case ( ($(this)[0].key_.match(/,total/) )? $(this)[0].key_: undefined ):
thisHostObj.Disks[name_index].total = $(this)[0].lastvalue / 1073741824;
break;
}
break;
case ( ($(this)[0].key_.match(/net.if.in/) || $(this)[0].key_.match(/net.if.out/) ) ? $(this)[0].key_ : undefined ): //FIXME: filter by network interface
thisHostObj.networkTraffic += $(this)[0].lastvalue / 8192; // KBps
break;
case 'net.tcp.service[http]':
//Shame we need a second API call, but using 'selectTriggers' in the host.get method doesn't return anything we can use to check the status of the trigger
//See https://www.zabbix.com/forum/showthread.php?t=24137
var webtrigger = zabbix.call('trigger.get', {
"host": thisHostObj.name,
"itemids": $(this)[0].itemid,
});
if(webtrigger[0] && webtrigger[0].status == 0 && webtrigger[0].value == 1) {
thisHostObj.error = 1;
thisHostObj.errorText = 'Web service is down (httpd or IIS)';
}
break;
}
});
if (thisHostObj.error != '' && thisHostObj.error != 1) {
thisHostObj.errorText = new String;
thisHostObj.errorText = thisHostObj.error;
}
if(thisHostObj.errorText && thisHostObj.errorText.indexOf('Interrupted system call') > -1) {
thisHostObj.errorText = 'Couldn\'t reach agent - check firewall settings';
}
if(thisHostObj.errorText && thisHostObj.errorText.indexOf('Connection refused') > -1) {
thisHostObj.errorText = 'Connection refused - check agent settings';
}
if(thisHostObj.errorText && thisHostObj.errorText.indexOf('timed out') > -1) {
thisHostObj.errorText = 'Connection timed out - SERVER MAY BE DOWN';
anError = 1;
}
if(thisHostObj.inventory.os) {
if(thisHostObj.inventory.os.indexOf('Windows Server 2008 R2') > -1) {
thisHostObj.os = 'Win Server 2008 R2';
} else if (thisHostObj.inventory.os.indexOf('Windows Server 2012') > -1) {
thisHostObj.os = 'Win Server 2012';
} else if (thisHostObj.inventory.os.indexOf('Windows Server 2003') > -1) {
thisHostObj.os = 'Win Server 2003';
} else if (thisHostObj.inventory.os.indexOf('el7') > -1) {
thisHostObj.os = 'CentOS 7';
} else if (thisHostObj.inventory.os.indexOf('el6') > -1) {
thisHostObj.os = 'CentOS 6.5';
} else if (thisHostObj.inventory.os.indexOf('Ubuntu') > -1) {//Ubuntu's uname -a does not give us the OS version
thisHostObj.os = 'Ubuntu';
} else if (thisHostObj.inventory.os.indexOf('Version 10') > -1) { //Does this conflict with any other OS?
thisHostObj.os = 'Mac OS 10.6';
} else if (thisHostObj.inventory.os.indexOf('Version 11') > -1) {
thisHostObj.os = 'Mac OS 10.7';
} else if (thisHostObj.inventory.os.indexOf('Version 12') > -1) {
thisHostObj.os = 'Mac OS 10.8';
} else if (thisHostObj.inventory.os.indexOf('Version 13') > -1) {
thisHostObj.os = 'Mac OS 10.9';
} else if (thisHostObj.inventory.os.indexOf('Version 14') > -1) {
thisHostObj.os = 'Mac OS 10.10';
} else if (thisHostObj.inventory.os.indexOf('Version 15') > -1) {
thisHostObj.os = 'Mac OS 10.11'; //Even though it doesn't exist yet....
//Feel free to roll your own OS stuff here as well! Don't feel restrained by my life choices!
} else if (thisHostObj.inventory.os.indexOf('Linux') > -1) {
thisHostObj.os = 'Linux';
} else {
//Return the unfriendly string if no patterns were matched
thisHostObj.os = thisHost[0].inventory.os;
}
} else if (thisHostObj.inventory.os_full) { //Per eburgueno, his install uses an item for Hostname to populate OS (Full Details) in Inventory
if(thisHostObj.inventory.os_full.indexOf('Linux') > -1) {
thisHostObj.os = 'Linux';
}
} else {
thisHostObj.os = 'OS Unavailable';
}
//Math
thisHostObj.usedMem = thisHostObj.totalMem - thisHostObj.freeMem;
thisHostObj.avgCpuLoad = loadTotals / processorCount;
thisHostObj.memPct = thisHostObj.usedMem / thisHostObj.totalMem;
var disks = new String;
var diskTopUsage = 0;
$.each(thisHostObj.Disks, function(i, thisDisk) {
disks += '<small>' + thisDisk.Name + ": " + thisDisk.used.toFixed(1) + 'G / ' + thisDisk.total.toFixed(1) + 'G (' + thisDisk.pctFree.toFixed(1) + '%)</small><br/>';
if (diskTopUsage < thisDisk.pctFree) {
diskTopUsage = thisDisk.pctFree;
}
});
var diskUsageClass = new String;
if (diskTopUsage && diskTopUsage > 90) {
diskUsageClass = 'panel-danger';
} else if (diskTopUsage && diskTopUsage > 75) {
diskUsageClass = 'panel-warning';
} else {
diskUsageClass = 'panel-default';
}
//TODO Check hostsCount % columnWidth to add <div class="row"></div> surrounding each row for better layout.
hostsCount++;
if ($.inArray(thisHostObj.hostid, usedHosts) !== -1 && !$("#" + thisHostObj.hostid).hasClass("blink")) {
$("#" + thisHostObj.hostid).find(".cpu-circle").circleProgress('value', thisHostObj.avgCpuLoad);
$("#" + thisHostObj.hostid).find(".mem-circle").circleProgress('value', thisHostObj.memPct);
$("#" + thisHostObj.hostid).find(".process-count").text(thisHostObj.numProcesses + " PROCS");
$("#" + thisHostObj.hostid).find(".mem-count").text('USED: ' + thisHostObj.usedMem.toFixed(1) + 'G / ' + thisHostObj.totalMem.toFixed(1) + 'G');
$("#" + thisHostObj.hostid).find(".nw-stats").html('PING: ' + thisHostObj.agentPing + 'ms<br/>TRAFFIC: ' + thisHostObj.networkTraffic.toFixed(1) + 'KBps');
$("#" + thisHostObj.hostid).find(".disk-stats-panel").html(disks);
} else if (!$("#" + thisHostObj.hostid).length) {
//Host is not in the grid, append a new entry.
usedHosts.push(thisHostObj.hostid);
if(!thisHostObj.error) {
//Build the server entry.
$('#hosts-list').append('<div class="col-md-' + columnWidth + '" id="' + thisHostObj.hostid +
'"><h4 style="text-align: center">' + thisHostObj.name + '<small> - ' + thisHostObj.os + '</small></h4><span class="circle cpu-circle" data-pct="' +
thisHostObj.avgCpuLoad + '"><strong></strong><span class="process-count">' + thisHostObj.numProcesses +
' PROCS</span></span><div class="pull-right"><span class="circle mem-circle" data-pct="' + thisHostObj.memPct +
'"><strong></strong><span class="mem-count">USED: ' + thisHostObj.usedMem.toFixed(1) + 'G / ' +
thisHostObj.totalMem.toFixed(1) + 'G' + '</span></span></div><br/><div class="row"><div class="col-md-6 nw-stats">PING: ' +
thisHostObj.agentPing + 'ms<br/>TRAFFIC: ' + thisHostObj.networkTraffic.toFixed(1) +
'KBps</div><div class="col-md-6"><span class="pull-right"><div class="panel-group" id="accordion-' + thisHostObj.hostid + '"><div class="panel ' +
diskUsageClass + ' disks-display"><div class="panel-heading"><div class="panel-title"><a data-toggle="collapse" data-parent="#accordion-' +
thisHostObj.hostid + '" href="#collapser-' + thisHostObj.hostid + '">DISKS</a></div></div><div id="collapser-' + thisHostObj.hostid + '" class="panel-collapse collapse"><div class="panel-body disk-stats-panel">' + disks +
'</div></div></div></div></span></div></div></div>');
$('#' + thisHostObj.hostid).find('.circle').each(function() {
$(this).circleProgress({
value: $(this).attr('data-pct'),
fill: {gradient: ['#1EB33C', '#F72323']},
thickness: 15
}).on('circle-animation-progress', function (event, progress, stepValue) {
$(this).find('strong').html(String((100 * stepValue).toFixed(1)) + '<i>%</i>');
});
});
} else {
$('#hosts-list').append('<div class="col-md-' + columnWidth + ' blink" id="' + thisHostObj.hostid + '"><h3 style="text-align: center; font-weight: bolder">' +
thisHostObj.name + '</h3><h4 style="font-weight: bolder">' + thisHostObj.errorText + '</h4></div>');
}
} else {
//Host exists but has an error. Add data.
$("#" + thisHostObj.hostid).html('<h4 style="text-align: center">' + thisHostObj.name + '<small> - ' + thisHostObj.os + '</small></h4><span class="circle cpu-circle" data-pct="' +
thisHostObj.avgCpuLoad + '"><strong></strong><span class="process-count">' + thisHostObj.numProcesses +
' PROCS</span></span><div class="pull-right"><span class="circle mem-circle" data-pct="' + thisHostObj.memPct +
'"><strong></strong><span class="mem-count">USED: ' + thisHostObj.usedMem.toFixed(1) + 'G / ' +
thisHostObj.totalMem.toFixed(1) + 'G' + '</span></span></div><br/><div class="row"><div class="col-md-6 nw-stats">PING: ' +
thisHostObj.agentPing + 'ms<br/>TRAFFIC: ' + thisHostObj.networkTraffic.toFixed(1) +
'KBps</div><div class="col-md-6"><span class="pull-right"><div class="panel-group" id="accordion-' + thisHostObj.hostid + '"><div class="panel ' +
diskUsageClass + ' disks-display"><div class="panel-heading"><div class="panel-title"><a data-toggle="collapse" data-parent="#accordion-' +
thisHostObj.hostid + '" href="#collapser-' + thisHostObj.hostid + '">DISKS</a></div></div><div id="collapser-' + thisHostObj.hostid + '" class="panel-collapse collapse"><div class="panel-body disk-stats-panel">' + disks +
'</div></div></div></div></span></div></div>').removeClass("blink");
}
if($.inArray(thisHostObj.hostid, existingHosts) === -1) {
existingHosts.push(thisHostObj.hostid);
}
});
//Remove hosts that Zabbix is no longer reporting.
$.each(hosts, function() {
if($.inArray($(this)[0].hostid, existingHosts) === -1) {
$('#' + $(this)[0].hostid).remove();
usedHosts.splice($.inArray($(this)[0].hostid, usedHosts), 1);
}
});
//Play the tornado siren if a server is unreachable
if(anError === 1) { //We do this outside the for loop because we only want the sound to play...once.
var times = 3;
function repeat() {
times--;
if (times === 0) {
clearInterval(loop);
}
var audio = document.createElement("audio");
audio.src = "audio/tornado2.ogg";
audio.play();
}
var loop = setInterval(repeat, 6000);
repeat();
}
}
</script>
</body>
</html>