From 46fd9d76718cea926ae0b97fa475be7b04109e34 Mon Sep 17 00:00:00 2001 From: Mark Nellemann Date: Wed, 17 May 2023 20:36:40 +0200 Subject: [PATCH] Modifications to support to InfluxDB v2.x --- README.md | 17 ++++++++++++- doc/hmci.toml | 17 +++++++++---- doc/readme-grafana.md | 1 + .../java/biz/nellemann/hmci/InfluxClient.java | 24 ++++++++++++++----- .../hmci/dto/toml/InfluxConfiguration.java | 4 ++++ 5 files changed, 52 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 49228a8..c36ecac 100644 --- a/README.md +++ b/README.md @@ -149,7 +149,7 @@ Use the gradle build tool, which will download all required dependencies: ### Local Testing -#### InfluxDB +#### InfluxDB v1.x Start a InfluxDB container: @@ -163,6 +163,18 @@ Create the *hmci* database: docker exec -i influxdb influx -execute "CREATE DATABASE hmci" ``` +#### InfluxDB v2.x + +Start a InfluxDB container: + +```shell +docker pull influxdb:latest +docker run --name=influxdb --rm -d -p 8086:8086 influxdb:latest +``` + +- Then use the Web UI to create an initial user (for the web UI), an organization and bucket: http://localhost:8086/ +- Then create an API token with RW access to your bucket. + #### Grafana @@ -174,4 +186,7 @@ docker run --name grafana --link influxdb:influxdb --rm -d -p 3000:3000 grafana/ Setup Grafana to connect to the InfluxDB container by defining a new datasource on URL *http://influxdb:8086* named *hmci*. +If you are [connecting](https://docs.influxdata.com/influxdb/v2.7/tools/grafana/) to InfluxDB v2.x, then add a custom http header, enter bucket as database and disable authorization. +- Authorization = Token abcdef_random_token_from_nfluxdb== + Import dashboards from the [doc/dashboards/](doc/dashboards/) folder. diff --git a/doc/hmci.toml b/doc/hmci.toml index e798ad2..64cccf2 100644 --- a/doc/hmci.toml +++ b/doc/hmci.toml @@ -3,14 +3,23 @@ ### ### Define one InfluxDB to save metrics into +### There must be only one and it should be named [influx] ### +# InfluxDB v1.x example +#[influx] +#url = "http://localhost:8086" +#username = "root" +#password = "" +#database = "hmci" + + +# InfluxDB v2.x example [influx] url = "http://localhost:8086" -username = "root" -password = "" -database = "hmci" - +org = "myOrg" +token = "rAnd0mT0k3nG3neRaT3dByInF1uxDb==" +bucket = "hmci" ### diff --git a/doc/readme-grafana.md b/doc/readme-grafana.md index 36d3a17..cc2c218 100644 --- a/doc/readme-grafana.md +++ b/doc/readme-grafana.md @@ -7,6 +7,7 @@ When installed Grafana listens on [http://localhost:3000](http://localhost:3000) - Configure Grafana to use InfluxDB as a new datasource - Name the datasource **hmci** to make it obvious what it contains. - You would typically use *http://localhost:8086* without any credentials. + - For InfluxDB add a custom header: Authorization = Token myTokenFromInfluxDB - The name of the database would be *hmci* (or another name you used when creating it) - **NOTE:** set *Min time interval* to *30s* or *1m* depending on your HMCi *refresh* setting. diff --git a/src/main/java/biz/nellemann/hmci/InfluxClient.java b/src/main/java/biz/nellemann/hmci/InfluxClient.java index 5d49660..90803b7 100644 --- a/src/main/java/biz/nellemann/hmci/InfluxClient.java +++ b/src/main/java/biz/nellemann/hmci/InfluxClient.java @@ -36,9 +36,9 @@ public final class InfluxClient { private final static Logger log = LoggerFactory.getLogger(InfluxClient.class); final private String url; + final private String org; // v2 only final private String token; - final private String org; - final private String database; // Bucket in v2 + final private String bucket; // Bucket in v2, Database in v1 private InfluxDBClient influxDBClient; @@ -47,9 +47,21 @@ public final class InfluxClient { InfluxClient(InfluxConfiguration config) { this.url = config.url; - this.token = config.username + ":" + config.password; - this.org = "hmci"; // In InfluxDB 1.x, there is no concept of organization. - this.database = config.database; + if(config.org != null) { + this.org = config.org; + } else { + this.org = "hmci"; // In InfluxDB 1.x, there is no concept of organization. + } + if(config.token != null) { + this.token = config.token; + } else { + this.token = config.username + ":" + config.password; + } + if(config.bucket != null) { + this.bucket = config.bucket; + } else { + this.bucket = config.database; + } } @@ -66,7 +78,7 @@ public final class InfluxClient { do { try { log.debug("Connecting to InfluxDB - {}", url); - influxDBClient = InfluxDBClientFactory.create(url, token.toCharArray(), org, database); + influxDBClient = InfluxDBClientFactory.create(url, token.toCharArray(), org, bucket); influxDBClient.version(); // This ensures that we actually try to connect to the db Runtime.getRuntime().addShutdownHook(new Thread(influxDBClient::close)); diff --git a/src/main/java/biz/nellemann/hmci/dto/toml/InfluxConfiguration.java b/src/main/java/biz/nellemann/hmci/dto/toml/InfluxConfiguration.java index a07b85e..e315cfc 100644 --- a/src/main/java/biz/nellemann/hmci/dto/toml/InfluxConfiguration.java +++ b/src/main/java/biz/nellemann/hmci/dto/toml/InfluxConfiguration.java @@ -3,6 +3,10 @@ package biz.nellemann.hmci.dto.toml; public class InfluxConfiguration { public String url; + public String org; + public String token; + public String bucket; + public String username; public String password; public String database;