2017-08-09 22:18:28 +02:00
|
|
|
var express = require('express');
|
2018-08-14 07:03:36 +02:00
|
|
|
var router = express.Router();
|
2017-08-09 22:18:28 +02:00
|
|
|
|
2018-08-14 07:03:36 +02:00
|
|
|
router.get('/', function (req, res, next) {
|
2017-08-09 22:18:28 +02:00
|
|
|
|
2018-08-14 07:03:36 +02:00
|
|
|
var json_file = require('jsonfile');
|
2017-09-06 08:05:34 +02:00
|
|
|
var glass_config = json_file.readFileSync('config/glass_config.json');
|
|
|
|
|
2017-08-09 22:18:28 +02:00
|
|
|
const execSync = require('child_process').execSync;
|
2018-08-14 07:03:36 +02:00
|
|
|
let output = execSync('./bin/dhcpd-pools -c ' + glass_config.config_file + ' -l ' + glass_config.leases_file + ' -f j -A -s e');
|
2017-09-06 08:05:34 +02:00
|
|
|
|
|
|
|
var dhcp_data = JSON.parse(output);
|
|
|
|
|
2018-08-14 07:03:36 +02:00
|
|
|
for (var i = 0; i < dhcp_data['shared-networks'].length; i++) {
|
2017-09-06 08:05:34 +02:00
|
|
|
utilization = round(parseFloat(dhcp_data['shared-networks'][i].used / dhcp_data['shared-networks'][i].defined) * 100, 2);
|
2018-08-14 07:03:36 +02:00
|
|
|
if (isNaN(utilization))
|
2017-09-06 08:05:34 +02:00
|
|
|
utilization = 0;
|
|
|
|
|
|
|
|
dhcp_data['shared-networks'][i].utilization = utilization;
|
|
|
|
}
|
2018-08-14 07:03:36 +02:00
|
|
|
dhcp_data['shared-networks'].sort(function (a, b) {
|
2017-09-06 08:05:34 +02:00
|
|
|
return parseFloat(b.utilization) - parseFloat(a.utilization);
|
|
|
|
});
|
|
|
|
|
2018-08-14 07:03:36 +02:00
|
|
|
let shared_networks = '';
|
2017-09-06 08:05:34 +02:00
|
|
|
|
2018-08-14 07:03:36 +02:00
|
|
|
for (var i = 0; i < dhcp_data['shared-networks'].length; i++) {
|
2017-09-14 06:17:52 +02:00
|
|
|
|
|
|
|
utilization = dhcp_data['shared-networks'][i].utilization;
|
|
|
|
|
2017-09-06 08:05:34 +02:00
|
|
|
table_row = '';
|
|
|
|
table_row = table_row + '<td><b>' + dhcp_data['shared-networks'][i].location + '</b></td>';
|
2017-09-14 06:17:52 +02:00
|
|
|
table_row = table_row + '<td>' + dhcp_data['shared-networks'][i].used.toLocaleString('en') + ' (' + utilization + '%)</td>';
|
2017-10-04 23:11:11 +02:00
|
|
|
table_row = table_row + '<td class="hide_col">' + dhcp_data['shared-networks'][i].defined.toLocaleString('en') + '</td>';
|
|
|
|
table_row = table_row + '<td class="hide_col">' + dhcp_data['shared-networks'][i].free.toLocaleString('en') + '</td>';
|
2017-09-06 08:05:34 +02:00
|
|
|
|
|
|
|
utilization_color = 'green';
|
|
|
|
|
2018-08-14 07:03:36 +02:00
|
|
|
if (utilization >= 80)
|
2017-09-06 08:05:34 +02:00
|
|
|
utilization_color = 'orange';
|
2018-08-14 07:03:36 +02:00
|
|
|
if (utilization >= 90)
|
2017-09-06 08:05:34 +02:00
|
|
|
utilization_color = 'red';
|
|
|
|
|
|
|
|
table_row = table_row + '<td><div class="progress">' +
|
|
|
|
'<div class="progress-bar bg-' + utilization_color + '" role="progressbar" aria-valuenow="62" aria-valuemin="0" aria-valuemax="100" style="width: ' + utilization + '%"></div>' +
|
|
|
|
'</div></td>';
|
2017-08-09 22:18:28 +02:00
|
|
|
|
2017-09-06 08:05:34 +02:00
|
|
|
shared_networks = shared_networks + '<tr>' + table_row + '</tr>';
|
|
|
|
}
|
2017-08-09 22:18:28 +02:00
|
|
|
|
2017-09-06 08:05:34 +02:00
|
|
|
/* Display All Subnets */
|
2018-08-14 07:03:36 +02:00
|
|
|
for (var i = 0; i < dhcp_data.subnets.length; i++) {
|
2017-09-06 08:05:34 +02:00
|
|
|
utilization = round(parseFloat(dhcp_data.subnets[i].used / dhcp_data.subnets[i].defined) * 100, 2);
|
2018-08-14 07:03:36 +02:00
|
|
|
if (isNaN(utilization))
|
2017-09-06 08:05:34 +02:00
|
|
|
utilization = 0;
|
2017-08-09 22:18:28 +02:00
|
|
|
|
2017-09-06 08:05:34 +02:00
|
|
|
dhcp_data.subnets[i].utilization = utilization;
|
|
|
|
}
|
|
|
|
|
2018-08-14 07:03:36 +02:00
|
|
|
dhcp_data.subnets.sort(function (a, b) {
|
2017-09-06 08:05:34 +02:00
|
|
|
return parseFloat(b.utilization) - parseFloat(a.utilization);
|
|
|
|
});
|
|
|
|
|
|
|
|
display_subnets = '';
|
|
|
|
|
2018-08-14 07:03:36 +02:00
|
|
|
for (var i = 0; i < dhcp_data.subnets.length; i++) {
|
2017-09-14 06:17:52 +02:00
|
|
|
utilization = dhcp_data.subnets[i].utilization;
|
|
|
|
|
2017-09-06 08:05:34 +02:00
|
|
|
table_row = '';
|
|
|
|
table_row = table_row + '<td><b>' + dhcp_data.subnets[i].location + '</b></td>';
|
|
|
|
table_row = table_row + '<td>' + dhcp_data.subnets[i].range + '</td>';
|
2017-09-14 06:17:52 +02:00
|
|
|
table_row = table_row + '<td>' + dhcp_data.subnets[i].used.toLocaleString('en') + ' (' + utilization + '%)</td>';
|
2017-10-04 23:11:11 +02:00
|
|
|
table_row = table_row + '<td class="hide_col">' + dhcp_data.subnets[i].defined.toLocaleString('en') + '</td>';
|
|
|
|
table_row = table_row + '<td class="hide_col">' + dhcp_data.subnets[i].free.toLocaleString('en') + '</td>';
|
2017-09-06 08:05:34 +02:00
|
|
|
|
|
|
|
utilization_color = 'green';
|
|
|
|
|
2018-08-14 07:03:36 +02:00
|
|
|
if (utilization >= 80)
|
2017-09-06 08:05:34 +02:00
|
|
|
utilization_color = 'orange';
|
2018-08-14 07:03:36 +02:00
|
|
|
if (utilization >= 90)
|
2017-09-06 08:05:34 +02:00
|
|
|
utilization_color = 'red';
|
|
|
|
|
|
|
|
table_row = table_row + '<td><div class="progress">' +
|
|
|
|
'<div class="progress-bar bg-' + utilization_color + '" role="progressbar" aria-valuenow="62" aria-valuemin="0" aria-valuemax="100" style="width: ' + utilization + '%"></div>' +
|
|
|
|
'</div></td>';
|
|
|
|
|
|
|
|
display_subnets = display_subnets + '<tr>' + table_row + '</tr>';
|
|
|
|
}
|
|
|
|
|
|
|
|
total_leases = dhcp_data.summary.used.toLocaleString('en');
|
2017-08-09 22:18:28 +02:00
|
|
|
|
2018-08-14 07:03:36 +02:00
|
|
|
let return_data = {
|
|
|
|
"cpu_utilization": cpu_utilization,
|
|
|
|
"leases_used": total_leases,
|
|
|
|
"leases_per_second": current_leases_per_second,
|
|
|
|
"leases_per_minute": leases_per_minute,
|
|
|
|
"shared_network_table": shared_networks,
|
|
|
|
"host_name": host_name,
|
2017-09-06 08:05:34 +02:00
|
|
|
"display_subnets_table": display_subnets
|
2017-08-09 22:18:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
res.send(JSON.stringify(return_data));
|
|
|
|
});
|
|
|
|
|
2017-09-06 08:05:34 +02:00
|
|
|
module.exports = router;
|
|
|
|
|
|
|
|
function round(num, places) {
|
|
|
|
var multiplier = Math.pow(10, places);
|
|
|
|
return Math.round(num * multiplier) / multiplier;
|
|
|
|
}
|