diff --git a/src/helper/influx-checks.js b/src/helper/influx-checks.js index 66bdfc5..34769bb 100644 --- a/src/helper/influx-checks.js +++ b/src/helper/influx-checks.js @@ -1,5 +1,6 @@ const logger = require.main.require("./helper/logger.js")("influx-checks"); +const { InfluxDB, Point } = require('@influxdata/influxdb-client') const Influx = require('@influxdata/influxdb-client-apis'); @@ -34,9 +35,43 @@ function checkBucket(influxDb, options){ }); } +function checkWriteApi(influxDb, options){ + return new Promise((resolve, reject) => { + const writeApi = influxDb.getWriteApi(options.org, options.bucket); // Get WriteAPI + writeApi.writePoint(new Point("test").tag("ConnectionTest", true).intField("value", 0).timestamp(0)) // Write dummy-point + writeApi.close() + .catch((err) => { + logger.error("Could not get writeApi:"); + logger.error(`Error [${err.code}]:`, err.message); + if(err.code == "not found") logger.fatal("No write-permission?"); + reject(); + }).then((res) => { + logger.debug("Writing ok"); + + // Delete the connection-text-point + new Influx.DeleteAPI(influxDb).postDelete({ + "body": { + "start": 0, + "end": 1, + "predicate": "ConnectionTest==true" + }, + "org": options.org, + "bucket": options.bucket + }) + .catch((err) => { + logger.error("Could not delete ConnectionTest-Point:"); + logger.error(`Error [${err.code}]:`, err.message); + reject(); + }) + .then(resolve()) + }); + }); +} + // Specify exports module.exports = { checkHealth, checkBucket, + checkWriteApi, }; \ No newline at end of file diff --git a/src/main.js b/src/main.js index 0eb1fac..7956c86 100644 --- a/src/main.js +++ b/src/main.js @@ -31,6 +31,10 @@ if(errorMsg){ org: env.INFLUX_ORG, name: env.INFLUX_BUCKET })}) + .then((res) => {return InfluxChecks.checkWriteApi(influxDb, { + org: env.INFLUX_ORG, + name: env.INFLUX_BUCKET + })}) .catch((err) => { if(err) { logger.error("Error whilst checking influx:");