From 4ddbe3f06f2cb8c204dfabe35c992ed30de3f209 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 29 Nov 2021 15:09:21 +0100 Subject: [PATCH 1/4] Implemented userHelper methods to detect if streams have data before timeout --- src/helper/userHelper.js | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/helper/userHelper.js diff --git a/src/helper/userHelper.js b/src/helper/userHelper.js new file mode 100644 index 0000000..50a1624 --- /dev/null +++ b/src/helper/userHelper.js @@ -0,0 +1,46 @@ +// This file specifies functions to help a user with e.g. configuration-errors + +function detectStreamData(stream, timeout = 5000){ + return new Promise((resolve, reject) => { + let timeoutHandler; + if(timeout){ + timeoutHandler = setTimeout(() => { + reject('timeout'); + remListeners(); + }, + timeout); + } + + function remListeners(){ + stream.removeListener('error', errorHandler); + stream.removeListener('data', dataHandler); + if(timeoutHandler) clearTimeout(timeoutHandler); + } + + function errorHandler(err) { + remListeners(); + } + function dataHandler(data) { + resolve(data); + remListeners(); + } + + stream.on('error', errorHandler); + stream.on('data', dataHandler); + }); +} + +function detectStreamsData(streams, timeout = 5000){ + let promises = []; + streams.forEach((stream) => { + promises.push(detectStreamData(stream, timeout)); + }) + return promises; +} + + +// Specify exports +module.exports = { + detectStreamData, + detectStreamsData, +}; \ No newline at end of file From c28bbaaadad0c5adff8656f19f81d5c86ef22f9d Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 29 Nov 2021 15:09:59 +0100 Subject: [PATCH 2/4] Moved Stream-creations outside --- src/main.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main.js b/src/main.js index 512b8a4..12f2f69 100644 --- a/src/main.js +++ b/src/main.js @@ -62,12 +62,15 @@ if(errorMsg){ let proc = exec(cmd); logger.debug("Creating & Attaching streams.."); let regexBlockStream = new RegexBlockStream(/^\d{2}:\d{2}:\d{2}.\d{6}.*(\n( {4,8}|\t\t?).*)+\n/gm); + let packetStreamFactory = new PacketStreamFactory(); + let packetInfluxPointFactory = new PacketInfluxPointFactory(); + let influxPointWriter = new InfluxPointWriter(influxDb, env.INFLUX_ORG, env.INFLUX_BUCKET); proc.stdout .setEncoding("utf8") .pipe(regexBlockStream) - .pipe(new PacketStreamFactory()) - .pipe(new PacketInfluxPointFactory()) - .pipe(new InfluxPointWriter(influxDb, env.INFLUX_ORG, env.INFLUX_BUCKET)); + .pipe(packetStreamFactory) + .pipe(packetInfluxPointFactory) + .pipe(influxPointWriter); logger.debug("Attaching error-logger.."); const loggerTcpdump = logFactory("tcpdump"); From 8211f55b89a847832376711209f51fcf07d10ade Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 29 Nov 2021 15:10:42 +0100 Subject: [PATCH 3/4] Added check for tcpdump-process stdout --- src/main.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main.js b/src/main.js index 12f2f69..bfd719d 100644 --- a/src/main.js +++ b/src/main.js @@ -14,6 +14,9 @@ const { PacketStreamFactory } = require("./streamHandler/PacketStreamFactory.js" const { PacketInfluxPointFactory } = require("./streamHandler/PacketInfluxPointFactory.js"); const { InfluxPointWriter } = require("./streamHandler/InfluxPointWriter.js"); +const userHelper = require("./helper/userHelper.js"); + + /// Setup ENVs const env = process.env; // Defaults @@ -87,6 +90,14 @@ if(errorMsg){ loggerTcpdump.error(err); }); + userHelper.detectStreamData(proc.stdout, 10000) // Expect tcpdump-logs to have data after max. 10s + .then(() => { + loggerTcpdump.debug("Got first data"); + }) + .catch((err) => { + if(err == 'timeout') loggerTcpdump.warn("No data after 10s! Wrong configuration?"); + }); + logger.debug("Attaching exit-handler.."); proc.on("exit", (code) => { loggerTcpdump.debug(`tcpdump exited code: ${code}`); From 16388c73e55011adc1ab32ca8e73f77f468faf71 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 29 Nov 2021 15:11:14 +0100 Subject: [PATCH 4/4] Added additional check for packetStream --- src/main.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main.js b/src/main.js index bfd719d..2a0a0a6 100644 --- a/src/main.js +++ b/src/main.js @@ -90,9 +90,17 @@ if(errorMsg){ loggerTcpdump.error(err); }); + const loggerPacketStream = logFactory("PacketStreamFactory"); userHelper.detectStreamData(proc.stdout, 10000) // Expect tcpdump-logs to have data after max. 10s .then(() => { loggerTcpdump.debug("Got first data"); + userHelper.detectStreamData(packetStreamFactory, 10000) // Expect then to have packets after further 10s + .then(() => { + loggerPacketStream.debug("Got first packet"); + }) + .catch((err) => { + if(err == 'timeout') loggerPacketStream.warn("No packets"); + }); }) .catch((err) => { if(err == 'timeout') loggerTcpdump.warn("No data after 10s! Wrong configuration?");