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 pf4jVersion=3.6.0
slf4jVersion=1.7.30 slf4jVersion=1.7.31
camelVersion=3.10.0 camelVersion=3.11.0
picocliVersion=4.6.1 picocliVersion=4.6.1
oshiVersion=5.7.5 oshiVersion=5.7.5

View file

@ -2,7 +2,9 @@ package sysmon.server;
import org.apache.camel.main.Main; import org.apache.camel.main.Main;
import org.influxdb.InfluxDB; import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBException;
import org.influxdb.InfluxDBFactory; import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Query;
import picocli.CommandLine; import picocli.CommandLine;
import java.io.IOException; 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>") @CommandLine.Option(names = { "-p", "--influxdb-pass" }, description = "InfluxDB Password (default: ${DEFAULT-VALUE}).", defaultValue = "", paramLabel = "<pass>")
private String influxPass; private String influxPass;
//@CommandLine.Option(names = { "-d", "--influxdb-db" }, description = "InfluxDB Database (default: ${DEFAULT-VALUE}).", defaultValue = "", paramLabel = "<name>") @CommandLine.Option(names = { "-d", "--influxdb-db" }, description = "InfluxDB Database (default: ${DEFAULT-VALUE}).", defaultValue = "sysmon", paramLabel = "<db>")
//private String influxName = "sysmon"; private String influxName;
@CommandLine.Option(names = { "-H", "--server-host" }, description = "Server listening address (default: ${DEFAULT-VALUE}).", paramLabel = "<addr>") @CommandLine.Option(names = { "-H", "--server-host" }, description = "Server listening address (default: ${DEFAULT-VALUE}).", paramLabel = "<addr>")
private String listenHost = "0.0.0.0"; private String listenHost = "0.0.0.0";
@ -42,20 +44,21 @@ public class Application implements Callable<Integer> {
@Override @Override
public Integer call() throws IOException { public Integer call() throws IOException {
InfluxDB influxDB = InfluxDBFactory.connect(influxUrl.toString(), influxUser, influxPass);
/* /*
Properties properties = new Properties(); try {
properties.put("http.host", listenHost); influxDB.query(new Query("CREATE DATABASE " + influxName));
properties.put("http.port", listenPort); } catch (InfluxDBException e) {
*/ System.err.println(e.getMessage());
InfluxDB influxConnectionBean = InfluxDBFactory.connect(influxUrl.toString(), influxUser, influxPass); return -1;
}*/
Main main = new Main(); Main main = new Main();
main.bind("myInfluxConnection", influxConnectionBean); main.bind("myInfluxConnection", influxDB);
main.bind("http.host", listenHost); main.bind("http.host", listenHost);
main.bind("http.port", listenPort); main.bind("http.port", listenPort);
//main.bind("properties", properties);
main.bind("threads", threads); main.bind("threads", threads);
//main.bind("influxdb_name", influxName); main.bind("dbname", influxName);
main.configure().addRoutesBuilder(ServerRouteBuilder.class); main.configure().addRoutesBuilder(ServerRouteBuilder.class);
// now keep the application running until the JVM is terminated (ctrl + c or sigterm) // 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.Exchange;
import org.apache.camel.builder.RouteBuilder; import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.influxdb.InfluxDbConstants;
import org.apache.camel.model.rest.RestBindingMode; import org.apache.camel.model.rest.RestBindingMode;
import org.apache.camel.spi.Registry; import org.apache.camel.spi.Registry;
import sysmon.shared.MetricResult; import sysmon.shared.MetricResult;
@ -11,7 +12,9 @@ public class ServerRouteBuilder extends RouteBuilder {
@Override @Override
public void configure() throws Exception { 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") restConfiguration().component("netty-http")
.bindingMode(RestBindingMode.auto) .bindingMode(RestBindingMode.auto)
@ -38,11 +41,11 @@ public class ServerRouteBuilder extends RouteBuilder {
.to("seda:inbound") .to("seda:inbound")
.endRest(); .endRest();
fromF("seda:inbound?concurrentConsumers=%s", registry.lookupByNameAndType("threads", Integer.class)) fromF("seda:inbound?concurrentConsumers=%s", threads)
.log(">>> metric: ${header.hostname} - ${body}") .log(">>> metric: ${header.hostname} - ${body}")
.doTry() .doTry()
.process(new MetricResultToPointProcessor()) .process(new MetricResultToPointProcessor())
.toF("influxdb://ref.myInfluxConnection?databaseName=%s&retentionPolicy=autogen", "sysmon") .toF("influxdb://ref.myInfluxConnection?databaseName=%s&retentionPolicy=autogen", dbname)
.doCatch(Exception.class) .doCatch(Exception.class)
.log("Error storing metric to InfluxDB: ${exception}") .log("Error storing metric to InfluxDB: ${exception}")
.end(); .end();