Fix indents app.js

This commit is contained in:
Akkadius 2017-10-05 13:05:06 -05:00
parent 765df5f74f
commit 0a2e180a13
1 changed files with 281 additions and 283 deletions

112
app.js
View File

@ -17,11 +17,11 @@ var glass_config = json_file.readFileSync('config/glass_config.json');
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
if(glass_config.ip_ranges_to_allow != "") {
if (glass_config.ip_ranges_to_allow != "") {
var ip_filter = require('express-ipfilter').IpFilter;
var ips = glass_config.ip_ranges_to_allow;
app.use(ip_filter(ips, {mode: 'allow'}));
@ -58,14 +58,14 @@ app.use('/api/get_mac_oui_list/', require('./api/get_mac_oui_list'));
app.set('view engine', 'html');
// catch 404 and forward to error handler
app.use(function(req, res, next) {
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
@ -119,7 +119,7 @@ if (fs.existsSync(oui_database_file)) {
lines[l] = lines[l].trim();
var oui_line_data = lines[l].split(":::");
if(typeof oui_line_data[1] !== "undefined")
if (typeof oui_line_data[1] !== "undefined")
oui_data[oui_line_data[0].trim()] = oui_line_data[1].trim();
}
console.log("[Glass Server] OUI Database Loaded");
@ -135,7 +135,7 @@ dhcp_lease_data = {};
lease_read_buffer = "";
fs = require('fs');
fs.readFile(glass_config.leases_file, 'utf8', function (err,data) {
fs.readFile(glass_config.leases_file, 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
@ -155,23 +155,23 @@ tail = new tail_module(
options
);
tail.on("line", function(data) {
tail.on("line", function (data) {
unix_time = Math.floor(new Date() / 1000);
/* Buffering lines until we get full lease data */
lease_read_buffer = lease_read_buffer + data + "\n";
/* End of lease - cut off and parse the buffer */
if (/}/i.test(data)){
if (/}/i.test(data)) {
lease_parser.parse(lease_read_buffer);
lease_read_buffer = "";
}
/* Count leases per second */
if(/lease/.test(data)) {
if (/lease/.test(data)) {
leases_per_second++;
}
if(current_time != unix_time) {
if (current_time != unix_time) {
current_time = unix_time;
current_leases_per_second = leases_per_second;
leases_last_update_time = unix_time;
@ -189,10 +189,10 @@ var glass_config = json_file.readFileSync('config/glass_config.json');
var options = {};
options.interval = 1000;
var dashboard_timer = setInterval(function(){
var dashboard_timer = setInterval(function () {
// console.log("Checking timers...");
unix_time = Math.floor(new Date() / 1000);
if((unix_time - 5) > leases_last_update_time){
if ((unix_time - 5) > leases_last_update_time) {
current_leases_per_second = 0;
}
@ -206,7 +206,7 @@ var dashboard_timer = setInterval(function(){
var leases_per_minute_data = [];
var leases_per_minute_counter = 0;
leases_per_minute_counter_timer = setInterval(function(){
leases_per_minute_counter_timer = setInterval(function () {
// console.log("leases per minute counter %i", leases_per_minute_counter);
leases_per_minute_data[leases_per_minute_counter] = current_leases_per_second;
@ -214,8 +214,8 @@ leases_per_minute_counter_timer = setInterval(function(){
/* Count how many actual data sets we walked that have values */
leases_per_minute = 0;
for (i = 0; i < 59; i++){
if(leases_per_minute_data[i] > 0) {
for (i = 0; i < 59; i++) {
if (leases_per_minute_data[i] > 0) {
leases_per_minute += leases_per_minute_data[i];
// console.log("iteration " + i + " val: " + leases_per_minute_data[i] + " lpm: " + leases_per_minute);
}
@ -229,7 +229,7 @@ leases_per_minute_counter_timer = setInterval(function(){
/* Websockets statistics subscription broadcast */
if(ws_event_subscribers('dhcp_statistics')) {
if (ws_event_subscribers('dhcp_statistics')) {
return_data = {
"cpu_utilization": cpu_utilization,
"leases_per_second": current_leases_per_second,
@ -243,14 +243,14 @@ leases_per_minute_counter_timer = setInterval(function(){
/**
* Poll: CPU Utilization
*/
cpu_utilization_poll = setInterval(function(){
cpu_utilization_poll = setInterval(function () {
cpu_utilization = parseFloat(execSync("top -bn 1 | awk 'NR>7{s+=$9} END {print s/4}'").toString())
}, (15 * 1000));
/**
* Clean Expired Leases
*/
lease_clean_timer = setInterval(function(){
lease_clean_timer = setInterval(function () {
lease_parser.clean();
}, (60 * 1000));
@ -268,7 +268,7 @@ function get_socket_clients_connected_count() {
*/
fs.watch('config/glass_config.json', function (event, filename) {
if (filename) {
setTimeout(function(){
setTimeout(function () {
glass_config = json_file.readFileSync('config/glass_config.json');
console.log("[Glass Server] Config Loaded");
}, 1000);
@ -282,7 +282,7 @@ fs.watch('config/glass_config.json', function (event, filename) {
*/
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
const wss = new WebSocket.Server({port: 8080});
options.interval = 300;
var tail_dhcp_log = new tail_module(
@ -293,7 +293,7 @@ var tail_dhcp_log = new tail_module(
dhcp_requests = {};
tail_dhcp_log.on("line", function(data) {
tail_dhcp_log.on("line", function (data) {
if (listening_to_log_file) {
wss.broadcast_event(data, 'dhcp_log_subscription');
}
@ -344,16 +344,18 @@ tail_dhcp_log.on("line", function(data) {
}
});
const purge_request_data = setInterval(function() {
const purge_request_data = setInterval(function () {
for (var key in dhcp_requests) {
if(dhcp_requests[key].request_count <= 10)
if (dhcp_requests[key].request_count <= 10)
delete dhcp_requests[key];
}
}, 600 * 1000); /* 10 Minutes */
}, 600 * 1000);
/* 10 Minutes */
const purge_request_data_hour = setInterval(function() {
const purge_request_data_hour = setInterval(function () {
dhcp_requests = {};
}, 3600 * 1000); /* 60 Minutes */
}, 3600 * 1000);
/* 60 Minutes */
wss.on('connection', function connection(ws) {
socket_clients++;
@ -386,8 +388,8 @@ function isJson(str) {
return true;
}
function ws_event_subscribers(event){
if(typeof wss === "undefined")
function ws_event_subscribers(event) {
if (typeof wss === "undefined")
return false;
var is_listening = false;
@ -417,17 +419,17 @@ wss.on('connection', function connection(ws) {
ws.event_subscription = [];
ws.on('message', function incoming(data) {
if(data != "" && isJson(data)) {
if (data != "" && isJson(data)) {
var json = JSON.parse(data);
if(typeof json["event_subscription"] !== "undefined"){
if (typeof json["event_subscription"] !== "undefined") {
console.log("[WS] Incoming Subscription '%s'", json['event_subscription']);
ws.event_subscription[json["event_subscription"]] = 1;
}
if(typeof json["event_unsubscribe"] !== "undefined"){
if (typeof json["event_unsubscribe"] !== "undefined") {
console.log("[WS] event_unsubscribe '%s'", json['event_unsubscribe']);
delete ws.event_subscription[json["event_unsubscribe"]];
}
if(typeof json["all_events"] !== "undefined"){
if (typeof json["all_events"] !== "undefined") {
console.log("[WS] event_unsubscribe '%s'", json['event_unsubscribe']);
ws.event_subscription = [];
}
@ -448,7 +450,7 @@ wss.broadcast = function broadcast(data) {
wss.broadcast_event = function broadcast(data, event) {
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
if(client.event_subscription[event])
if (client.event_subscription[event])
client.send(JSON.stringify({"event": event, "data": data}));
}
});
@ -506,12 +508,12 @@ function slack_message(message) {
alert_status = [];
alert_status['leases_per_minute'] = 0;
setTimeout(function(){
setTimeout(function () {
console.log("[Glass Server] Alert loop started");
alert_check_timer = setInterval(function(){
alert_check_timer = setInterval(function () {
// console.log("[Timer] Alert Timer check");
if(glass_config.leases_per_minute_threshold > 0) {
if (glass_config.leases_per_minute_threshold > 0) {
// console.log("[Timer] lpm: %s lpm_th: %s", leases_per_minute, glass_config.leases_per_minute_threshold);
if (leases_per_minute <= glass_config.leases_per_minute_threshold && alert_status['leases_per_minute'] == 0) {
alert_status['leases_per_minute'] = 1;
@ -544,10 +546,10 @@ setTimeout(function(){
alert_status_networks_warning = [];
alert_status_networks_critical = [];
alert_subnet_check_timer = setInterval(function(){
alert_subnet_check_timer = setInterval(function () {
// console.log("[Timer] Alert Timer check - subnets");
if(glass_config.shared_network_warning_threshold > 0 || glass_config.shared_network_critical_threshold > 0) {
if (glass_config.shared_network_warning_threshold > 0 || glass_config.shared_network_critical_threshold > 0) {
const execSync = require('child_process').execSync;
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);
@ -555,18 +557,18 @@ setTimeout(function(){
/*
* Iterate through Shared Networks
*/
for ( var i = 0; i < dhcp_data['shared-networks'].length; i++) {
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))
if (isNaN(utilization))
utilization = 0;
/* Initialize these array buckets */
if(typeof alert_status_networks_warning[dhcp_data['shared-networks'][i].location] === "undefined")
if (typeof alert_status_networks_warning[dhcp_data['shared-networks'][i].location] === "undefined")
alert_status_networks_warning[dhcp_data['shared-networks'][i].location] = 0;
if(typeof alert_status_networks_critical[dhcp_data['shared-networks'][i].location] === "undefined")
if (typeof alert_status_networks_critical[dhcp_data['shared-networks'][i].location] === "undefined")
alert_status_networks_critical[dhcp_data['shared-networks'][i].location] = 0;
/*
@ -579,13 +581,12 @@ setTimeout(function(){
*/
/* Check Warnings */
if(glass_config.shared_network_warning_threshold > 0) {
if (glass_config.shared_network_warning_threshold > 0) {
if (
utilization >= glass_config.shared_network_warning_threshold &&
utilization <= glass_config.shared_network_critical_threshold &&
alert_status_networks_warning[dhcp_data['shared-networks'][i].location] == 0
)
{
) {
alert_status_networks_warning[dhcp_data['shared-networks'][i].location] = 1;
slack_message(":warning: WARNING: DHCP shared network utilization (" + dhcp_data['shared-networks'][i].location + ") " +
@ -603,8 +604,7 @@ setTimeout(function(){
else if (
utilization <= glass_config.shared_network_warning_threshold &&
alert_status_networks_warning[dhcp_data['shared-networks'][i].location] == 1
)
{
) {
alert_status_networks_warning[dhcp_data['shared-networks'][i].location] = 0;
slack_message(":white_check_mark: CLEAR: Warning DHCP shared network utilization (" + dhcp_data['shared-networks'][i].location + ") " +
@ -622,12 +622,11 @@ setTimeout(function(){
}
/* Check Critical */
if(glass_config.shared_network_critical_threshold > 0) {
if (glass_config.shared_network_critical_threshold > 0) {
if (
utilization >= glass_config.shared_network_critical_threshold &&
alert_status_networks_critical[dhcp_data['shared-networks'][i].location] == 0
)
{
) {
alert_status_networks_critical[dhcp_data['shared-networks'][i].location] = 1;
slack_message(":fire: CRITICAL: DHCP shared network utilization (" + dhcp_data['shared-networks'][i].location + ") " +
"Current: (" + utilization + "%) " +
@ -644,8 +643,7 @@ setTimeout(function(){
else if (
utilization <= glass_config.shared_network_critical_threshold &&
alert_status_networks_critical[dhcp_data['shared-networks'][i].location] == 1
)
{
) {
alert_status_networks_critical[dhcp_data['shared-networks'][i].location] = 0;
slack_message(":white_check_mark: CLEAR: Critical DHCP shared network utilization (" + dhcp_data['shared-networks'][i].location + ") " +
"Current: (" + utilization + "%) " +
@ -689,10 +687,10 @@ function email_alert(alert_title, alert_message) {
/* E-Mail Template Load */
console.log("[Glass Server] Sending E-Mail Alert...\n");
if(typeof glass_config.email_alert_to === "undefined" && typeof glass_config.sms_alert_to === "undefined")
if (typeof glass_config.email_alert_to === "undefined" && typeof glass_config.sms_alert_to === "undefined")
return false;
if (glass_config.email_alert_to == "" && glass_config.sms_alert_to != ""){
if (glass_config.email_alert_to == "" && glass_config.sms_alert_to != "") {
console.log("[Glass Server] No email_to specified - returning...");
return false;
}
@ -700,13 +698,13 @@ function email_alert(alert_title, alert_message) {
/* Write on top of E-Mail Template */
email_body = email_body.replace("[body_content_placeholder]", alert_message);
email_body = email_body.replace("[alert_title]", alert_title);
email_body = email_body.replace("[local_time]", new Date().toString() );
email_body = email_body.replace("[local_time]", new Date().toString());
/* Clean extra commas etc. */
glass_config.email_alert_to = glass_config.email_alert_to.replace(/^[,\s]+|[,\s]+$/g, '').replace(/,[,\s]*,/g, ',');
/* Send regular HTML E-Mails */
if(glass_config.email_alert_to.trim() != "") {
if (glass_config.email_alert_to.trim() != "") {
var mailOptions = {
from: "Glass Alerting Monitor glass@noreply.com",
to: glass_config.email_alert_to,
@ -724,7 +722,7 @@ function email_alert(alert_title, alert_message) {
}
/* Send SMS */
if(glass_config.sms_alert_to.trim() != "") {
if (glass_config.sms_alert_to.trim() != "") {
var mailOptions = {
from: "Glass Alerting Monitor glass@noreply.com",
to: glass_config.sms_alert_to,