var express = require('express');
var router = express.Router();
router.get('/', function (req, res, next) {
var json_file = require('jsonfile');
var glass_config = json_file.readFileSync('config/glass_config.json');
const execSync = require('child_process').execSync;
let output = execSync('./bin/dhcpd-pools -c ' + glass_config.config_file + ' -l ' + glass_config.leases_file + ' -f j -A -s e');
var dhcp_data = JSON.parse(output);
for (var i = 0; i < dhcp_data['shared-networks'].length; i++) {
utilization = round(parseFloat(dhcp_data['shared-networks'][i].used / dhcp_data['shared-networks'][i].defined) * 100, 2);
if (isNaN(utilization))
utilization = 0;
dhcp_data['shared-networks'][i].utilization = utilization;
}
dhcp_data['shared-networks'].sort(function (a, b) {
return parseFloat(b.utilization) - parseFloat(a.utilization);
});
let shared_networks = '';
for (var i = 0; i < dhcp_data['shared-networks'].length; i++) {
utilization = dhcp_data['shared-networks'][i].utilization;
table_row = '';
table_row = table_row + '
' + dhcp_data['shared-networks'][i].location + ' | ';
table_row = table_row + '' + dhcp_data['shared-networks'][i].used.toLocaleString('en') + ' (' + utilization + '%) | ';
table_row = table_row + '' + dhcp_data['shared-networks'][i].defined.toLocaleString('en') + ' | ';
table_row = table_row + '' + dhcp_data['shared-networks'][i].free.toLocaleString('en') + ' | ';
utilization_color = 'green';
if (utilization >= 80)
utilization_color = 'orange';
if (utilization >= 90)
utilization_color = 'red';
table_row = table_row + ' | ';
shared_networks = shared_networks + '' + table_row + '
';
}
/* Display All Subnets */
for (var i = 0; i < dhcp_data.subnets.length; i++) {
utilization = round(parseFloat(dhcp_data.subnets[i].used / dhcp_data.subnets[i].defined) * 100, 2);
if (isNaN(utilization))
utilization = 0;
dhcp_data.subnets[i].utilization = utilization;
}
dhcp_data.subnets.sort(function (a, b) {
return parseFloat(b.utilization) - parseFloat(a.utilization);
});
display_subnets = '';
for (var i = 0; i < dhcp_data.subnets.length; i++) {
utilization = dhcp_data.subnets[i].utilization;
table_row = '';
table_row = table_row + '' + dhcp_data.subnets[i].location + ' | ';
table_row = table_row + '' + dhcp_data.subnets[i].range + ' | ';
table_row = table_row + '' + dhcp_data.subnets[i].used.toLocaleString('en') + ' (' + utilization + '%) | ';
table_row = table_row + '' + dhcp_data.subnets[i].defined.toLocaleString('en') + ' | ';
table_row = table_row + '' + dhcp_data.subnets[i].free.toLocaleString('en') + ' | ';
utilization_color = 'green';
if (utilization >= 80)
utilization_color = 'orange';
if (utilization >= 90)
utilization_color = 'red';
table_row = table_row + ' | ';
display_subnets = display_subnets + '' + table_row + '
';
}
total_leases = dhcp_data.summary.used.toLocaleString('en');
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,
"display_subnets_table": display_subnets
};
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(return_data));
});
module.exports = router;
function round(num, places) {
var multiplier = Math.pow(10, places);
return Math.round(num * multiplier) / multiplier;
}