Modifications to support to InfluxDB v2.x

This commit is contained in:
Mark Nellemann 2023-05-17 20:36:40 +02:00
parent 8f4fbc6a93
commit 46fd9d7671
5 changed files with 52 additions and 11 deletions

View file

@ -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.

View file

@ -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"
###

View file

@ -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.

View file

@ -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));

View file

@ -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;