From b590ccfaf9cb9ccf404cd7d73a7751e076740877 Mon Sep 17 00:00:00 2001 From: "mn.albeschenko" Date: Wed, 12 May 2021 17:01:18 +0300 Subject: [PATCH] Fix async start bug(OUI), fix 1. When Glass starting, OUI base not loaded, cause concurency. Therefore the vendor is not defined. 2. When you have failover dhcp cluster, there is many junk records. --- app.js | 17 +++++++++------- core/lease-parser.js | 8 +++++++- core/oui-reader.js | 48 +++++++++++++++++++++++--------------------- 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/app.js b/app.js index e77586f..fb12acc 100644 --- a/app.js +++ b/app.js @@ -125,14 +125,17 @@ let dhcp_log_watcher = require('./core/dhcp-log-watcher'); let app_timers = require('./core/app-timers'); /** - * Run routines + * Run routines in seq order */ -oui_reader.initOuiDatabase(); -dhcp_leases.parseLeasesFileOnce(glass_config); -dhcp_leases.startLeaseListener(glass_config); -dhcp_leases.setLeasesCleanTimer(); -glass_config_watcher.init(); -dhcp_log_watcher.init(glass_config); +(async () => { + await oui_reader.initOuiDatabase(); + dhcp_leases.parseLeasesFileOnce(glass_config); + dhcp_leases.startLeaseListener(glass_config); + dhcp_leases.setLeasesCleanTimer(); + glass_config_watcher.init(); + dhcp_log_watcher.init(glass_config); +})(); + /** * Timers diff --git a/core/lease-parser.js b/core/lease-parser.js index e6aee90..d7618b6 100644 --- a/core/lease-parser.js +++ b/core/lease-parser.js @@ -77,13 +77,19 @@ module.exports = { /* Mac OUI Lookup */ var mac_oui = dhcp_lease_data[ip_address].mac.split(":").join("").toUpperCase().slice(0, 6); - dhcp_lease_data[ip_address].mac_oui_vendor = ''; if (typeof oui_data[mac_oui] !== "undefined") { dhcp_lease_data[ip_address].mac_oui_vendor = oui_data[mac_oui]; } } } + if (/binding/i.test(lines[l])) { + /* Need to ignore binding, when it is not first. Cause "next binding state expired" in Leases. */ + if (line_data_arg[0] == "binding" && line_data_arg[2] != "active;") { + delete dhcp_lease_data[ip_address]; + break; + } + } if (/hostname/i.test(lines[l])) { if (typeof line_data_arg[1] !== "undefined") dhcp_lease_data[ip_address].host = line_data_arg[1].replace(/;/gi, '').replace(/"/gi, '').trim(); diff --git a/core/oui-reader.js b/core/oui-reader.js index f82e04e..8978aae 100644 --- a/core/oui-reader.js +++ b/core/oui-reader.js @@ -1,27 +1,29 @@ var fs = require('fs'); module.exports = { - initOuiDatabase: function () { - var oui_database_file = "bin/oui_table.txt"; - - if (fs.existsSync(oui_database_file)) { - fs.readFile(oui_database_file, 'utf8', function (err, data) { - if (err) { - return console.log(err); - } - else { - lines = data.split("\n"); - for (l = 0; l < lines.length; l++) { - /* Trim whitespaces at each ends of the line */ - lines[l] = lines[l].trim(); - var oui_line_data = lines[l].split(":::"); - - if (typeof oui_line_data[1] !== "undefined") - oui_data[oui_line_data[0].trim()] = oui_line_data[1].trim(); + initOuiDatabase: async function () { + return new Promise((resolve) => { + var oui_database_file = "bin/oui_table.txt"; + if (fs.existsSync(oui_database_file)) { + fs.readFile(oui_database_file, 'utf8', function (err, data) { + if (err) { + console.log(err); + } + else { + lines = data.split("\n"); + for (l = 0; l < lines.length; l++) { + /* Trim whitespaces at each ends of the line */ + lines[l] = lines[l].trim(); + var oui_line_data = lines[l].split(":::"); + + 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"); + resolve() } - console.log("[Glass Server] OUI Database Loaded"); - } - }); - } - }, -}; \ No newline at end of file + }); + } + }) + } +} \ No newline at end of file