More cleanup.

This commit is contained in:
Mark Nellemann 2021-05-06 16:31:02 +02:00
parent d77e110387
commit b1edee145a
16 changed files with 60 additions and 126 deletions

View file

@ -12,10 +12,9 @@ import org.sysmon.shared.MetricResult;
import java.util.List;
public class MyRouteBuilder extends RouteBuilder {
private static final Logger log = LoggerFactory.getLogger(MyRouteBuilder.class);
public class AgentRouteBuilder extends RouteBuilder {
private static final Logger log = LoggerFactory.getLogger(AgentRouteBuilder.class);
@Override
public void configure() throws Exception {
@ -33,11 +32,9 @@ public class MyRouteBuilder extends RouteBuilder {
// Setup Camel route for this extension
from("timer:collect?period=10000")
.bean(ext, "getMetrics")
//.setHeader("ext", constant(ext.getName()))
.doTry()
.process(new MetricProcessor())
.choice()
.when(exchangeProperty("skip").isEqualTo(true))
//.doTry()
.process(new MetricEnrichProcessor())
.choice().when(exchangeProperty("skip").isEqualTo(true))
.stop()
.otherwise()
.to("seda:metrics");
@ -48,12 +45,14 @@ public class MyRouteBuilder extends RouteBuilder {
from("seda:metrics")
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
//.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
.doTry()
//.process(new MetricProcessor())
.marshal().json(JsonLibrary.Jackson, MetricResult.class)
.to("http://127.0.0.1:9925/metrics")
.doCatch(Exception.class)
.log("Error sending metric to collector: ${exception}")
.log("Error: ${exception.message}")
//.log("Error sending metric to collector: ${body}")
.end();
}

View file

@ -17,7 +17,7 @@ public class Application {
Main main = new Main();
// and add the routes (you can specify multiple classes)
main.configure().addRoutesBuilder(MyRouteBuilder.class);
main.configure().addRoutesBuilder(AgentRouteBuilder.class);
// now keep the application running until the JVM is terminated (ctrl + c or sigterm)
try {

View file

@ -5,19 +5,21 @@ import org.apache.camel.Processor;
import org.sysmon.shared.MetricResult;
public class MetricProcessor implements Processor {
public class MetricEnrichProcessor implements Processor {
// TODO: Read hostname from future configuration
private final static String hostname = "saruman";
public void process(Exchange exchange) throws Exception {
MetricResult result = exchange.getIn().getBody(MetricResult.class);
if(result.getMeasurementList().size() < 1) {
result.setHostname(hostname);
// We make sure MetricResults with no measurements are not sent further down the line
if(result.getMeasurements().size() < 1) {
exchange.setProperty("skip", true);
}
exchange.getIn().setHeader("component", result.getName());
// TODO: Read hostname from configuration
result.setHostname("sauron");
exchange.getIn().setBody(result);
}

View file

@ -1,18 +0,0 @@
package org.sysmon.agent;
import org.apache.camel.AggregationStrategy;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.sysmon.shared.MetricResult;
public class MyAggregationStrategy implements AggregationStrategy {
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
Message newIn = newExchange.getIn();
MetricResult oldBody = oldExchange.getIn().getBody(MetricResult.class);
String newBody = newIn.getBody(String.class);
newIn.setBody(oldBody + newBody);
return newExchange;
}
}

View file

@ -17,14 +17,14 @@
# to configure camel main
# here you can configure options on camel main (see MainConfigurationProperties class)
camel.main.name = SysMon-Agent
camel.main.name = sysmon-agent
# enable tracing
#camel.main.tracing = true
# bean introspection to log reflection based configuration
camel.main.beanIntrospectionExtendedStatistics=true
camel.main.beanIntrospectionLoggingLevel=INFO
#camel.main.beanIntrospectionExtendedStatistics=true
#camel.main.beanIntrospectionLoggingLevel=INFO
# run in lightweight mode to be tiny as possible
camel.main.lightweight = true
@ -39,4 +39,4 @@ camel.main.lightweight = true
# camel.main.auto-startup = false
# configure beans
camel.beans.metricProcessor = #class:org.sysmon.agent.MetricProcessor
#camel.beans.metricProcessor = #class:org.sysmon.agent.MetricProcessor

View file

@ -1,13 +1,8 @@
package org.sysmon.collector;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson.JacksonDataFormat;
import org.apache.camel.model.dataformat.JsonLibrary;
import org.apache.camel.model.rest.RestBindingMode;
import org.sysmon.collector.processor.MetricResultToPointProcessor;
import org.sysmon.shared.MetricResult;
import org.sysmon.shared.dto.MetricMessageDTO;
public class CollectorRouteBuilder extends RouteBuilder {

View file

@ -1,9 +1,9 @@
package org.sysmon.collector.processor;
package org.sysmon.collector;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.influxdb.dto.Point;
import org.sysmon.shared.MetricMeasurement;
import org.sysmon.shared.MeasurementPair;
import org.sysmon.shared.MetricResult;
import java.util.List;
@ -20,8 +20,8 @@ public class MetricResultToPointProcessor implements Processor {
.time(metricResult.getTimestamp(), TimeUnit.MILLISECONDS)
.tag("hostname", metricResult.getHostname());
List<MetricMeasurement> measurements = metricResult.getMeasurementList();
for(MetricMeasurement measurement : measurements) {
List<MeasurementPair> measurements = metricResult.getMeasurements();
for(MeasurementPair measurement : measurements) {
if(measurement.getValue() instanceof Number) {
Number num = (Number) measurement.getValue();
builder.addField(measurement.getName(), num);

View file

@ -2,10 +2,7 @@ package org.sysmon.collector.bean;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sysmon.shared.MetricResult;
import org.sysmon.shared.dto.MetricMessageDTO;
public class IncomingMetricProcessor implements Processor {

View file

@ -23,8 +23,8 @@ camel.main.name = sysmon-collector
#camel.main.tracing = true
# bean introspection to log reflection based configuration
camel.main.beanIntrospectionExtendedStatistics=true
camel.main.beanIntrospectionLoggingLevel=INFO
#camel.main.beanIntrospectionExtendedStatistics=true
#camel.main.beanIntrospectionLoggingLevel=INFO
# run in lightweight mode to be tiny as possible
camel.main.lightweight = true
@ -39,5 +39,5 @@ camel.main.lightweight = true
# camel.main.auto-startup = false
# configure beans
camel.beans.incomingMetricProcessor = #class:org.sysmon.collector.bean.IncomingMetricProcessor
camel.beans.hello = #class:org.sysmon.collector.bean.Hello
#camel.beans.incomingMetricProcessor = #class:org.sysmon.collector.bean.IncomingMetricProcessor
#camel.beans.hello = #class:org.sysmon.collector.bean.Hello

View file

@ -4,7 +4,7 @@ import org.pf4j.Extension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sysmon.shared.MetricExtension;
import org.sysmon.shared.MetricMeasurement;
import org.sysmon.shared.MeasurementPair;
import org.sysmon.shared.MetricResult;
import org.sysmon.shared.PluginHelper;
@ -39,7 +39,7 @@ public class AixProcessorExtension implements MetricExtension {
List<String> mpstat = PluginHelper.executeCommand("mpstat", "-a");
List<AixProcessorStat> processorStats = processCommandOutput(mpstat);
for(AixProcessorStat stat : processorStats) {
result.addMetricMeasurement(new MetricMeasurement(String.format("cpu%d", stat.getCpuNum()), stat.getUtilizationPercentage()));
result.addMetricMeasurement(new MeasurementPair(String.format("cpu%d", stat.getCpuNum()), stat.getUtilizationPercentage()));
}
return result;

View file

@ -2,7 +2,7 @@ package org.sysmon.plugins.sysmon_linux;
import org.pf4j.Extension;
import org.sysmon.shared.MetricExtension;
import org.sysmon.shared.MetricMeasurement;
import org.sysmon.shared.MeasurementPair;
import org.sysmon.shared.MetricResult;
import java.io.IOException;
@ -76,9 +76,9 @@ public class LinuxDiskExtension implements MetricExtension {
}
private List<MetricMeasurement> calculate() {
private List<MeasurementPair> calculate() {
List<MetricMeasurement> measurementList = new ArrayList<>();
List<MeasurementPair> measurementList = new ArrayList<>();
if(previousDiskStats == null || previousDiskStats.size() != currentDiskStats.size()) {
return measurementList;
@ -101,7 +101,7 @@ public class LinuxDiskExtension implements MetricExtension {
if(!ignore.get()) {
long timeSpendDoingIo = curStat.getTimeSpentOnIo() - preStat.getTimeSpentOnIo();
// TODO: Calculate differences for wanted disk io stats
measurementList.add(new MetricMeasurement(curStat.getDevice() + "-iotime", timeSpendDoingIo));
measurementList.add(new MeasurementPair(curStat.getDevice() + "-iotime", timeSpendDoingIo));
}
}

View file

@ -2,7 +2,7 @@ package org.sysmon.plugins.sysmon_linux;
import org.pf4j.Extension;
import org.sysmon.shared.MetricExtension;
import org.sysmon.shared.MetricMeasurement;
import org.sysmon.shared.MeasurementPair;
import org.sysmon.shared.MetricResult;
import java.io.IOException;
@ -49,9 +49,9 @@ public class LinuxMemoryExtension implements MetricExtension {
}
private List<MetricMeasurement> readProcFile() throws IOException {
private List<MeasurementPair> readProcFile() throws IOException {
List<MetricMeasurement> measurementList = new ArrayList<>();
List<MeasurementPair> measurementList = new ArrayList<>();
List<String> allLines = Files.readAllLines(Paths.get("/proc/meminfo"), StandardCharsets.UTF_8);
for (String line : allLines) {
@ -59,7 +59,7 @@ public class LinuxMemoryExtension implements MetricExtension {
if (line.startsWith("Mem")) {
Matcher matcher = pattern.matcher(line);
if (matcher.find() && matcher.groupCount() == 2) {
measurementList.add(new MetricMeasurement(matcher.group(1), matcher.group(2)));
measurementList.add(new MeasurementPair(matcher.group(1), matcher.group(2)));
}
}
}

View file

@ -5,7 +5,7 @@ import org.pf4j.Extension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sysmon.shared.MetricExtension;
import org.sysmon.shared.MetricMeasurement;
import org.sysmon.shared.MeasurementPair;
import org.sysmon.shared.MetricResult;
import java.io.IOException;
@ -55,9 +55,9 @@ public class LinuxProcessorExtension implements MetricExtension {
}
private List<MetricMeasurement> calculateDifference() {
private List<MeasurementPair> calculateDifference() {
List<MetricMeasurement> measurementList = new ArrayList<>();
List<MeasurementPair> measurementList = new ArrayList<>();
if(previousProcessorStats == null || previousProcessorStats.size() != currentProcessorStats.size()) {
return measurementList;
@ -73,7 +73,7 @@ public class LinuxProcessorExtension implements MetricExtension {
float percentUsage = (float) (workTimeDiff - idleTimeDiff) / workTimeDiff;
Integer pct = (int) (percentUsage * 100);
measurementList.add(new MetricMeasurement(curStat.getCpuName(), pct));
measurementList.add(new MeasurementPair(curStat.getCpuName(), pct));
}

View file

@ -1,13 +1,17 @@
package org.sysmon.shared;
public class MetricMeasurement {
import java.io.Serializable;
public class MeasurementPair implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private Object value;
public MetricMeasurement() { }
public MeasurementPair() { }
public MetricMeasurement(String name, Object value) {
public MeasurementPair(String name, Object value) {
this.name = name;
this.value = value;
}

View file

@ -12,7 +12,7 @@ public class MetricResult implements Serializable {
private String name;
private Long timestamp; // epoch milli
private String hostname;
private List<MetricMeasurement> measurementList = new ArrayList<>();
private List<MeasurementPair> measurements = new ArrayList<>();
public MetricResult() {
@ -23,12 +23,12 @@ public class MetricResult implements Serializable {
this.timestamp = Instant.now().toEpochMilli();
}
public void setMetricMeasurementList(List<MetricMeasurement> measurementList) {
this.measurementList = measurementList;
public void setMetricMeasurementList(List<MeasurementPair> measurementList) {
this.measurements = measurementList;
}
public void addMetricMeasurement(MetricMeasurement measurement) {
measurementList.add(measurement);
public void addMetricMeasurement(MeasurementPair measurement) {
measurements.add(measurement);
}
public void setHostname(String hostname) {
@ -55,13 +55,13 @@ public class MetricResult implements Serializable {
return hostname;
}
public List<MetricMeasurement> getMeasurementList() {
return measurementList;
public List<MeasurementPair> getMeasurements() {
return measurements;
}
public String toString() {
StringBuilder sb = new StringBuilder(String.format("%s - %s\n", timestamp.toString(), name));
for(MetricMeasurement mm : measurementList) {
for(MeasurementPair mm : measurements) {
sb.append(mm.toString()).append("\n");
}

View file

@ -1,45 +0,0 @@
package org.sysmon.shared.dto;
import java.util.Objects;
public class MetricMessageDTO {
private String msg;
private long id;
public MetricMessageDTO() {
// empty constructor is required bu Jackson for deserialization
}
public MetricMessageDTO(String msg, long id) {
Objects.requireNonNull(msg);
this.msg = msg;
this.id = id;
}
public String getMsg() {
return msg;
}
public long getId() {
return id;
}
public void setMsg(String msg) {
this.msg = msg;
}
public void setId(long id) {
this.id = id;
}
@Override
public String toString() {
return "MetricMessageDTO{" +
"msg='" + msg + '\'' +
", id=" + id +
'}';
}
}