From c09c6c29fbe4fd0d75785f4f41755a8326f331db Mon Sep 17 00:00:00 2001 From: Ruakij Date: Thu, 9 Dec 2021 17:15:10 +0100 Subject: [PATCH] Move InfluxDB to if-block --- src/main.js | 59 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/src/main.js b/src/main.js index 610922e..fd2199e 100644 --- a/src/main.js +++ b/src/main.js @@ -14,6 +14,7 @@ const { RegexBlockStream } = require("./streamHandler/RegexBlockStream.js"); const { PacketStreamFactory } = require("./streamHandler/PacketStreamFactory.js"); const { PacketInfluxPointFactory } = require("./streamHandler/PacketInfluxPointFactory.js"); const { InfluxPointWriter } = require("./streamHandler/InfluxPointWriter.js"); +const { InfluxDbLineProtocolWriter } = require("./streamHandler/InfluxDbLineProtocolWriter.js"); const userHelper = require("./helper/userHelper.js"); @@ -42,31 +43,41 @@ if(errorMsg){ } (async function() { - logger.info("Setup Influx.."); - const influxDb = new InfluxDB({url: env.INFLUX_URL, token: env.INFLUX_TOKEN}); - - await InfluxChecks.checkHealth(influxDb) - .then((res) => {return InfluxChecks.checkBucket(influxDb, { - org: env.INFLUX_ORG, - name: env.INFLUX_BUCKET - });}) - .then((res) => {return InfluxChecks.checkWriteApi(influxDb, { - org: env.INFLUX_ORG, - bucket: env.INFLUX_BUCKET - });}) - .catch((err) => { - if(err) { - logger.error("Error whilst checking influx:"); - logger.error(err); - } - logger.fatal("Setup influx failed!"); - exit(1); - }); + let pointWriter; + if(!env.USE_INFLUXDB_LINEPROTOCOL){ + logger.info("Setup Influx.."); + const influxDb = new InfluxDB({url: env.INFLUX_URL, token: env.INFLUX_TOKEN}); + + await InfluxChecks.checkHealth(influxDb) + .then((res) => {return InfluxChecks.checkBucket(influxDb, { + org: env.INFLUX_ORG, + name: env.INFLUX_BUCKET + });}) + .then((res) => {return InfluxChecks.checkWriteApi(influxDb, { + org: env.INFLUX_ORG, + bucket: env.INFLUX_BUCKET + });}) + .catch((err) => { + if(err) { + logger.error("Error whilst checking influx:"); + logger.error(err); + } + logger.fatal("Setup influx failed!"); + exit(1); + }); + + logger.debug("Get WriteApi & set default-hostname to", `'${env.HOSTNAME}'`); + const influxWriteApi = influxDb.getWriteApi(env.INFLUX_ORG, env.INFLUX_BUCKET, "us"); + //influxWriteApi.useDefaultTags({"hostname": env.HOSTNAME}); - logger.debug("Get WriteApi & set default-hostname to", `'${env.HOSTNAME}'`); - const influxWriteApi = influxDb.getWriteApi(env.INFLUX_ORG, env.INFLUX_BUCKET, "us"); - //influxWriteApi.useDefaultTags({"hostname": env.HOSTNAME}); - logger.info("Influx ok"); + pointWriter = new InfluxPointWriter(influxWriteApi); + + logger.info("Influx ok"); + } + else { + logger.info("Setup Influxdb-LineProtocol.."); + + } logger.info("Starting tcpdump.."); const TCPDUMP_BASECMD = "tcpdump -vvv -e -n -X -s0 -i";