diff --git a/client/build.gradle b/client/build.gradle index ce03075..7b60692 100644 --- a/client/build.gradle +++ b/client/build.gradle @@ -1,5 +1,9 @@ plugins { id 'application' + + id "com.github.johnrengelman.shadow" version "6.1.0" + id "net.nemerosa.versioning" version "2.14.0" + id "nebula.ospackage" version "8.4.1" } dependencies { @@ -9,6 +13,9 @@ dependencies { annotationProcessor(group: 'org.pf4j', name: 'pf4j', version: "${pf4jVersion}") implementation group: 'org.pf4j', name: 'pf4j', version: "${pf4jVersion}" + annotationProcessor "info.picocli:picocli-codegen:${picocliVersion}" + implementation "info.picocli:picocli:${picocliVersion}" + implementation group: 'org.apache.camel', name: 'camel-core', version: camelVersion implementation group: 'org.apache.camel', name: 'camel-main', version: camelVersion implementation group: 'org.apache.camel', name: 'camel-http', version: camelVersion @@ -30,4 +37,55 @@ run { tasks.named('test') { // Use junit platform for unit tests. useJUnitPlatform() -} \ No newline at end of file +} + + +apply plugin: 'nebula.ospackage' +ospackage { + packageName = 'sysmon-client' + release = '1' + user = 'root' + packager = "Mark Nellemann " + + into '/opt/sysmon-client' + + from(shadowJar.outputs.files) { + into 'lib' + } + + from('build/scriptsShadow') { + into 'bin' + } + + from('doc/') { + into 'doc' + } + + from(['README.md', 'LICENSE']) { + into 'doc' + } + +} + +buildRpm { + dependsOn startShadowScripts + os = "LINUX" +} + +buildDeb { + dependsOn startShadowScripts +} + +jar { + manifest { + attributes( + 'Created-By' : "Gradle ${gradle.gradleVersion}", + 'Build-OS' : "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}", + 'Build-Jdk' : "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})", + 'Build-User' : System.properties['user.name'], + 'Build-Version' : versioning.info.tag ?: (versioning.info.branch + "-" + versioning.info.build), + 'Build-Revision' : versioning.info.commit, + 'Build-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ss.SSSZ").toString(), + ) + } +} diff --git a/client/src/main/java/org/sysmon/client/Application.java b/client/src/main/java/org/sysmon/client/Application.java index 7346b7e..249ffe2 100644 --- a/client/src/main/java/org/sysmon/client/Application.java +++ b/client/src/main/java/org/sysmon/client/Application.java @@ -4,62 +4,42 @@ package org.sysmon.client; import org.apache.camel.main.Main; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import picocli.CommandLine; -public class Application { +import java.io.IOException; +import java.net.URL; +import java.util.concurrent.Callable; - private static final Logger log = LoggerFactory.getLogger(Application.class); +@CommandLine.Command(name = "sysmon-client", mixinStandardHelpOptions = true) +public class Application implements Callable { - public static void main(String[] args) { + @CommandLine.Option(names = { "-s", "--server-url" }, description = "Server URL [default: 'http://127.0.0.1:9925/metrics'].", defaultValue = "http://127.0.0.1:9925/metrics", paramLabel = "") + private URL serverUrl; + + + public static void main(String... args) { + int exitCode = new CommandLine(new Application()).execute(args); + System.exit(exitCode); + } + + + @Override + public Integer call() throws IOException { - // use Camels Main class Main main = new Main(); - - // and add the routes (you can specify multiple classes) - main.configure().addRoutesBuilder(AgentRouteBuilder.class); + main.bind("myServerUrl", serverUrl.toString()); + main.configure().addRoutesBuilder(ClientRouteBuilder.class); // now keep the application running until the JVM is terminated (ctrl + c or sigterm) try { - main.run(args); - } catch(Exception e) { + main.run(); + } catch (Exception e) { System.err.println(e.getMessage()); } - } - -/* - public static void main(String[] args) throws InterruptedException { - - // create the plugin manager - PluginManager pluginManager = new JarPluginManager(); // or "new ZipPluginManager() / new DefaultPluginManager()" - - // start and load all plugins of application - pluginManager.loadPlugins(); - pluginManager.startPlugins(); - - - List metricExtensions = pluginManager.getExtensions(MetricExtension.class); - log.info(String.format("Found %d extensions for extension point '%s':", metricExtensions.size(), MetricExtension.class.getName())); - for (MetricExtension ext : metricExtensions) { - if(ext.isSupported()) { - log.info(">>> " + ext.getGreeting()); - - // TODO: Setup camel - } - } - - - AtomicBoolean keepRunning = new AtomicBoolean(true); - Thread shutdownHook = new Thread(() -> { - keepRunning.set(false); - pluginManager.stopPlugins(); - System.out.println("Stopping sysmon, please wait ..."); - }); - Runtime.getRuntime().addShutdownHook(shutdownHook); - + return 0; } - */ } + diff --git a/client/src/main/java/org/sysmon/client/AgentRouteBuilder.java b/client/src/main/java/org/sysmon/client/ClientRouteBuilder.java similarity index 87% rename from client/src/main/java/org/sysmon/client/AgentRouteBuilder.java rename to client/src/main/java/org/sysmon/client/ClientRouteBuilder.java index 79ba904..7b4760d 100644 --- a/client/src/main/java/org/sysmon/client/AgentRouteBuilder.java +++ b/client/src/main/java/org/sysmon/client/ClientRouteBuilder.java @@ -3,6 +3,7 @@ package org.sysmon.client; import org.apache.camel.Exchange; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.model.dataformat.JsonLibrary; +import org.apache.camel.spi.Registry; import org.pf4j.JarPluginManager; import org.pf4j.PluginManager; import org.slf4j.Logger; @@ -12,13 +13,15 @@ import org.sysmon.shared.MetricResult; import java.util.List; -public class AgentRouteBuilder extends RouteBuilder { +public class ClientRouteBuilder extends RouteBuilder { - private static final Logger log = LoggerFactory.getLogger(AgentRouteBuilder.class); + private static final Logger log = LoggerFactory.getLogger(ClientRouteBuilder.class); @Override public void configure() throws Exception { + Registry registry = getContext().getRegistry(); + PluginManager pluginManager = new JarPluginManager(); pluginManager.loadPlugins(); pluginManager.startPlugins(); @@ -50,7 +53,7 @@ public class AgentRouteBuilder extends RouteBuilder { .doTry() //.process(new MetricProcessor()) .marshal().json(JsonLibrary.Jackson, MetricResult.class) - .to("http://127.0.0.1:9925/metrics") + .to((String)registry.lookupByName("myServerUrl")) .doCatch(Exception.class) .log("Error: ${exception.message}") //.log("Error sending metric to collector: ${body}") diff --git a/gradle.properties b/gradle.properties index c4590bc..5e2fb28 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,4 @@ pf4jVersion=3.6.0 slf4jVersion=1.7.30 -camelVersion=3.7.3 \ No newline at end of file +camelVersion=3.7.3 +picocliVersion=4.6.1 \ No newline at end of file diff --git a/server/build.gradle b/server/build.gradle index 2472626..9c4d46e 100644 --- a/server/build.gradle +++ b/server/build.gradle @@ -1,11 +1,18 @@ plugins { id 'application' + + id "com.github.johnrengelman.shadow" version "6.1.0" + id "net.nemerosa.versioning" version "2.14.0" + id "nebula.ospackage" version "8.4.1" } dependencies { testImplementation project(':shared') implementation project(':shared') + annotationProcessor "info.picocli:picocli-codegen:${picocliVersion}" + implementation "info.picocli:picocli:${picocliVersion}" + implementation group: 'org.apache.camel', name: 'camel-core', version: camelVersion implementation group: 'org.apache.camel', name: 'camel-main', version: camelVersion implementation group: 'org.apache.camel', name: 'camel-rest', version: camelVersion @@ -24,3 +31,53 @@ tasks.named('test') { // Use junit platform for unit tests. useJUnitPlatform() } + +apply plugin: 'nebula.ospackage' +ospackage { + packageName = 'sysmon-server' + release = '1' + user = 'root' + packager = "Mark Nellemann " + + into '/opt/sysmon-server' + + from(shadowJar.outputs.files) { + into 'lib' + } + + from('build/scriptsShadow') { + into 'bin' + } + + from('doc/') { + into 'doc' + } + + from(['README.md', 'LICENSE']) { + into 'doc' + } + +} + +buildRpm { + dependsOn startShadowScripts + os = "LINUX" +} + +buildDeb { + dependsOn startShadowScripts +} + +jar { + manifest { + attributes( + 'Created-By' : "Gradle ${gradle.gradleVersion}", + 'Build-OS' : "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}", + 'Build-Jdk' : "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})", + 'Build-User' : System.properties['user.name'], + 'Build-Version' : versioning.info.tag ?: (versioning.info.branch + "-" + versioning.info.build), + 'Build-Revision' : versioning.info.commit, + 'Build-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ss.SSSZ").toString(), + ) + } +} diff --git a/server/src/main/java/org/sysmon/server/Application.java b/server/src/main/java/org/sysmon/server/Application.java index 2c65c36..a614cde 100644 --- a/server/src/main/java/org/sysmon/server/Application.java +++ b/server/src/main/java/org/sysmon/server/Application.java @@ -3,25 +3,54 @@ package org.sysmon.server; import org.apache.camel.main.Main; import org.influxdb.InfluxDB; import org.influxdb.InfluxDBFactory; +import picocli.CommandLine; -public class Application { +import java.io.IOException; +import java.net.URL; +import java.util.concurrent.Callable; - public static void main(String[] args) { +@CommandLine.Command(name = "sysmon-server", mixinStandardHelpOptions = true) +public class Application implements Callable { - InfluxDB influxConnectionBean = InfluxDBFactory.connect("http://localhost:8086", "root", ""); + @CommandLine.Option(names = { "-i", "--influxdb-url" }, description = "InfluxDB URL [default: 'http://localhost:8086'].", defaultValue = "http://localhost:8086", paramLabel = "") + private URL influxUrl; + + @CommandLine.Option(names = { "-u", "--influxdb-user" }, description = "InfluxDB User [default: 'root'].", defaultValue = "root", paramLabel = "") + private String influxUser; + + @CommandLine.Option(names = { "-p", "--influxdb-pass" }, description = "InfluxDB Password [default: ''].", defaultValue = "", paramLabel = "") + private String influxPass; + + @CommandLine.Option(names = { "-l", "--listen-port" }, description = "Listening port [default: '9925'].", defaultValue = "9925", paramLabel = "") + private String listenPort; + + + public static void main(String... args) { + int exitCode = new CommandLine(new Application()).execute(args); + System.exit(exitCode); + } + + + @Override + public Integer call() throws IOException { + + InfluxDB influxConnectionBean = InfluxDBFactory.connect(influxUrl.toString(), influxUser, influxPass); Main main = new Main(); main.bind("myInfluxConnection", influxConnectionBean); - main.configure().addRoutesBuilder(CollectorRouteBuilder.class); + main.bind("myListenPort", Integer.parseInt(listenPort)); + main.configure().addRoutesBuilder(ServerRouteBuilder.class); // now keep the application running until the JVM is terminated (ctrl + c or sigterm) try { - main.run(args); + main.run(); } catch (Exception e) { System.err.println(e.getMessage()); } + return 0; + } } diff --git a/server/src/main/java/org/sysmon/server/CollectorRouteBuilder.java b/server/src/main/java/org/sysmon/server/ServerRouteBuilder.java similarity index 85% rename from server/src/main/java/org/sysmon/server/CollectorRouteBuilder.java rename to server/src/main/java/org/sysmon/server/ServerRouteBuilder.java index 8f6128b..a66d324 100644 --- a/server/src/main/java/org/sysmon/server/CollectorRouteBuilder.java +++ b/server/src/main/java/org/sysmon/server/ServerRouteBuilder.java @@ -2,17 +2,20 @@ package org.sysmon.server; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.model.rest.RestBindingMode; +import org.apache.camel.spi.Registry; import org.sysmon.shared.MetricResult; -public class CollectorRouteBuilder extends RouteBuilder { +public class ServerRouteBuilder extends RouteBuilder { @Override public void configure() throws Exception { + Registry registry = getContext().getRegistry(); + restConfiguration().component("jetty") .bindingMode(RestBindingMode.auto) .host("127.0.0.1") - .port(9925); + .port((Integer) registry.lookupByName("myListenPort")); rest() .get("/")