Make influx database name configurable.

This commit is contained in:
Mark Nellemann 2021-06-29 14:02:01 +02:00
parent 126f256c6c
commit 3dd568a95e
3 changed files with 22 additions and 16 deletions

View file

@ -1,6 +1,6 @@
version=0.0.5
version=0.0.6
pf4jVersion=3.6.0
slf4jVersion=1.7.30
camelVersion=3.10.0
slf4jVersion=1.7.31
camelVersion=3.11.0
picocliVersion=4.6.1
oshiVersion=5.7.5

View file

@ -2,7 +2,9 @@ package sysmon.server;
import org.apache.camel.main.Main;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBException;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Query;
import picocli.CommandLine;
import java.io.IOException;
@ -21,8 +23,8 @@ public class Application implements Callable<Integer> {
@CommandLine.Option(names = { "-p", "--influxdb-pass" }, description = "InfluxDB Password (default: ${DEFAULT-VALUE}).", defaultValue = "", paramLabel = "<pass>")
private String influxPass;
//@CommandLine.Option(names = { "-d", "--influxdb-db" }, description = "InfluxDB Database (default: ${DEFAULT-VALUE}).", defaultValue = "", paramLabel = "<name>")
//private String influxName = "sysmon";
@CommandLine.Option(names = { "-d", "--influxdb-db" }, description = "InfluxDB Database (default: ${DEFAULT-VALUE}).", defaultValue = "sysmon", paramLabel = "<db>")
private String influxName;
@CommandLine.Option(names = { "-H", "--server-host" }, description = "Server listening address (default: ${DEFAULT-VALUE}).", paramLabel = "<addr>")
private String listenHost = "0.0.0.0";
@ -42,20 +44,21 @@ public class Application implements Callable<Integer> {
@Override
public Integer call() throws IOException {
InfluxDB influxDB = InfluxDBFactory.connect(influxUrl.toString(), influxUser, influxPass);
/*
Properties properties = new Properties();
properties.put("http.host", listenHost);
properties.put("http.port", listenPort);
*/
InfluxDB influxConnectionBean = InfluxDBFactory.connect(influxUrl.toString(), influxUser, influxPass);
try {
influxDB.query(new Query("CREATE DATABASE " + influxName));
} catch (InfluxDBException e) {
System.err.println(e.getMessage());
return -1;
}*/
Main main = new Main();
main.bind("myInfluxConnection", influxConnectionBean);
main.bind("myInfluxConnection", influxDB);
main.bind("http.host", listenHost);
main.bind("http.port", listenPort);
//main.bind("properties", properties);
main.bind("threads", threads);
//main.bind("influxdb_name", influxName);
main.bind("dbname", influxName);
main.configure().addRoutesBuilder(ServerRouteBuilder.class);
// now keep the application running until the JVM is terminated (ctrl + c or sigterm)

View file

@ -2,6 +2,7 @@ package sysmon.server;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.influxdb.InfluxDbConstants;
import org.apache.camel.model.rest.RestBindingMode;
import org.apache.camel.spi.Registry;
import sysmon.shared.MetricResult;
@ -11,7 +12,9 @@ public class ServerRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
Registry registry = getContext().getRegistry();
final Registry registry = getContext().getRegistry();
final String dbname = registry.lookupByNameAndType("dbname", String.class);
final Integer threads = registry.lookupByNameAndType("threads", Integer.class);
restConfiguration().component("netty-http")
.bindingMode(RestBindingMode.auto)
@ -38,11 +41,11 @@ public class ServerRouteBuilder extends RouteBuilder {
.to("seda:inbound")
.endRest();
fromF("seda:inbound?concurrentConsumers=%s", registry.lookupByNameAndType("threads", Integer.class))
fromF("seda:inbound?concurrentConsumers=%s", threads)
.log(">>> metric: ${header.hostname} - ${body}")
.doTry()
.process(new MetricResultToPointProcessor())
.toF("influxdb://ref.myInfluxConnection?databaseName=%s&retentionPolicy=autogen", "sysmon")
.toF("influxdb://ref.myInfluxConnection?databaseName=%s&retentionPolicy=autogen", dbname)
.doCatch(Exception.class)
.log("Error storing metric to InfluxDB: ${exception}")
.end();