From f43616c86bab4abdd2ca2aa2d8764396378550af Mon Sep 17 00:00:00 2001 From: Akkadius Date: Wed, 6 Sep 2017 03:50:11 -0500 Subject: [PATCH] Critical/Warning shared network utilization alerting implementation --- app.js | 86 +++++++++++++++++++++++++++++++++++++++- config/glass_config.json | 10 ++--- 2 files changed, 90 insertions(+), 6 deletions(-) diff --git a/app.js b/app.js index 969336d..13fdee0 100644 --- a/app.js +++ b/app.js @@ -378,4 +378,88 @@ alert_check_timer = setInterval(function(){ slack_message(":white_check_mark: CLEAR: DHCP leases per minute have returned to above the critical threshold (" + glass_config.leases_per_minute_threshold.toLocaleString('en') + ") Current LP/s (" + leases_per_minute.toLocaleString('en') + ")"); } } -}, (60 * 1000)); \ No newline at end of file +}, (60 * 1000)); + +alert_status_networks_warning = []; +alert_status_networks_critical = []; + +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) { + 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); + + /* + * Iterate through Shared Networks + */ + 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; + + + /* Initialize these array buckets */ + 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") + alert_status_networks_critical[dhcp_data['shared-networks'][i].location] = 0; + + console.log("Location: %s", dhcp_data['shared-networks'][i].location); + console.log("Used: %s", dhcp_data['shared-networks'][i].used.toLocaleString('en')); + console.log("Defined: %s", dhcp_data['shared-networks'][i].defined.toLocaleString('en')); + console.log("Free: %s", dhcp_data['shared-networks'][i].free.toLocaleString('en')); + console.log("Utilization: %s", utilization); + console.log(" \n"); + + /* Check Warnings */ + 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 + ") Current: (" + utilization + ") Threshold: (" + glass_config.shared_network_warning_threshold + ")"); + } + 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 + ") Current: (" + utilization + ") Threshold: (" + glass_config.shared_network_warning_threshold + ")"); + } + } + + /* Check Critical */ + 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 + ") Threshold: (" + glass_config.shared_network_critical_threshold + ")"); + } + 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 + ") Threshold: (" + glass_config.shared_network_critical_threshold + ")"); + } + } + } + } +}, (5 * 1000)); + +function round(num, places) { + var multiplier = Math.pow(10, places); + return Math.round(num * multiplier) / multiplier; +} \ No newline at end of file diff --git a/config/glass_config.json b/config/glass_config.json index a1196c3..9ea9574 100644 --- a/config/glass_config.json +++ b/config/glass_config.json @@ -4,9 +4,9 @@ "leases_file": "/var/lib/dhcp/dhcpd.leases", "log_file": "/var/log/dhcp.log", "config_file": "/etc/dhcp/dhcpd.conf", - "shared_network_critical_threshold": 95, - "shared_network_warning_threshold": 80, - "leases_per_minute_threshold": 0, - "slack_webhook_url": "", - "slack_alert_channel": "" + "shared_network_critical_threshold": "95", + "shared_network_warning_threshold": "80", + "slack_webhook_url": "https://hooks.slack.com/services/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", + "slack_alert_channel": "#alerting", + "leases_per_minute_threshold": "1000" }