2021-05-21 09:08:43 +00:00
|
|
|
package sysmon.client;
|
2021-05-04 12:23:34 +00:00
|
|
|
|
|
|
|
import org.apache.camel.Exchange;
|
|
|
|
import org.apache.camel.builder.RouteBuilder;
|
2021-05-06 08:57:35 +00:00
|
|
|
import org.apache.camel.model.dataformat.JsonLibrary;
|
2021-05-07 15:53:47 +00:00
|
|
|
import org.apache.camel.spi.Registry;
|
2021-05-04 12:23:34 +00:00
|
|
|
import org.pf4j.JarPluginManager;
|
|
|
|
import org.pf4j.PluginManager;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2021-05-21 09:08:43 +00:00
|
|
|
import sysmon.shared.MetricExtension;
|
|
|
|
import sysmon.shared.MetricResult;
|
2021-05-04 12:23:34 +00:00
|
|
|
|
2021-05-11 13:37:23 +00:00
|
|
|
import java.nio.file.Path;
|
2021-05-13 17:28:31 +00:00
|
|
|
import java.nio.file.Paths;
|
2021-05-11 13:37:23 +00:00
|
|
|
import java.util.ArrayList;
|
2021-05-04 12:23:34 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2021-05-07 15:53:47 +00:00
|
|
|
public class ClientRouteBuilder extends RouteBuilder {
|
2021-05-04 12:23:34 +00:00
|
|
|
|
2021-05-07 15:53:47 +00:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(ClientRouteBuilder.class);
|
2021-05-04 12:23:34 +00:00
|
|
|
|
|
|
|
@Override
|
2021-06-10 06:55:03 +00:00
|
|
|
public void configure() {
|
2021-05-04 12:23:34 +00:00
|
|
|
|
2021-05-07 15:53:47 +00:00
|
|
|
Registry registry = getContext().getRegistry();
|
|
|
|
|
2021-05-13 17:28:31 +00:00
|
|
|
Path[] pluginpaths = { Paths.get(registry.lookupByNameAndType("pluginPath", String.class)) };
|
2021-05-11 13:37:23 +00:00
|
|
|
PluginManager pluginManager = new JarPluginManager(pluginpaths);
|
2021-05-04 12:23:34 +00:00
|
|
|
pluginManager.loadPlugins();
|
|
|
|
pluginManager.startPlugins();
|
|
|
|
|
2021-05-11 13:37:23 +00:00
|
|
|
List<String> providers = new ArrayList<>();
|
2021-05-04 12:23:34 +00:00
|
|
|
List<MetricExtension> metricExtensions = pluginManager.getExtensions(MetricExtension.class);
|
|
|
|
for (MetricExtension ext : metricExtensions) {
|
2021-05-11 13:37:23 +00:00
|
|
|
|
2021-05-04 12:23:34 +00:00
|
|
|
if(ext.isSupported()) {
|
2021-05-11 13:37:23 +00:00
|
|
|
|
|
|
|
String provides = ext.getProvides();
|
|
|
|
if(providers.contains(provides)) {
|
|
|
|
log.warn("Skipping extension (already provided): " + ext.getName());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-04 12:23:34 +00:00
|
|
|
log.info(">>> Enabling extension: " + ext.getDescription());
|
2021-05-11 13:37:23 +00:00
|
|
|
providers.add(provides);
|
2021-05-04 12:23:34 +00:00
|
|
|
|
2021-06-10 06:55:03 +00:00
|
|
|
|
|
|
|
// TODO: Make timer thread configurable
|
|
|
|
|
2021-05-04 12:23:34 +00:00
|
|
|
// Setup Camel route for this extension
|
2021-05-13 19:53:28 +00:00
|
|
|
// a unique timer name gives the timer it's own thread, otherwise it's a shared thread for other timers with same name.
|
2021-06-10 06:55:03 +00:00
|
|
|
//from("timer:"+provides+"?fixedRate=true&period=30s")
|
|
|
|
from("timer:extensions?fixedRate=true&period=30s")
|
2021-05-04 12:23:34 +00:00
|
|
|
.bean(ext, "getMetrics")
|
2021-05-06 14:31:02 +00:00
|
|
|
//.doTry()
|
2021-05-08 16:55:37 +00:00
|
|
|
.process(new MetricEnrichProcessor(registry))
|
2021-05-06 14:31:02 +00:00
|
|
|
.choice().when(exchangeProperty("skip").isEqualTo(true))
|
2021-05-31 12:44:48 +00:00
|
|
|
.log("Skipping empty measurement.")
|
2021-05-06 14:31:02 +00:00
|
|
|
.stop()
|
2021-05-06 11:26:53 +00:00
|
|
|
.otherwise()
|
2021-09-01 14:16:47 +00:00
|
|
|
.log(">>> ${body}")
|
2021-06-10 06:55:03 +00:00
|
|
|
.to("seda:metrics?discardWhenFull=true");
|
2021-05-27 20:43:56 +00:00
|
|
|
} else {
|
2021-06-10 06:55:03 +00:00
|
|
|
log.info(">>> Skipping extension (not supported here): " + ext.getDescription());
|
2021-05-04 12:23:34 +00:00
|
|
|
}
|
2021-06-10 06:55:03 +00:00
|
|
|
|
2021-05-04 12:23:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-10 06:55:03 +00:00
|
|
|
// TODO: Make 'concurrentConsumers' configurable
|
|
|
|
from("seda:metrics?concurrentConsumers=1")
|
2021-05-04 12:23:34 +00:00
|
|
|
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
|
2021-05-06 14:31:02 +00:00
|
|
|
//.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
|
2021-05-04 15:54:47 +00:00
|
|
|
.doTry()
|
2021-05-06 11:26:53 +00:00
|
|
|
//.process(new MetricProcessor())
|
2021-05-06 08:57:35 +00:00
|
|
|
.marshal().json(JsonLibrary.Jackson, MetricResult.class)
|
2021-05-07 15:53:47 +00:00
|
|
|
.to((String)registry.lookupByName("myServerUrl"))
|
2021-05-04 15:54:47 +00:00
|
|
|
.doCatch(Exception.class)
|
2021-05-06 14:31:02 +00:00
|
|
|
.log("Error: ${exception.message}")
|
|
|
|
//.log("Error sending metric to collector: ${body}")
|
2021-05-04 15:54:47 +00:00
|
|
|
.end();
|
2021-05-04 12:23:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-23 09:07:07 +00:00
|
|
|
|
2021-05-11 07:05:00 +00:00
|
|
|
}
|