From d1fc7582acac79bd8d6d2ef25484789486c93311 Mon Sep 17 00:00:00 2001 From: Mark Nellemann Date: Fri, 7 May 2021 12:39:55 +0200 Subject: [PATCH] Refactoring of component names. Plugins tests. --- .gitignore | 1 + {agent => client}/build.gradle | 4 +- .../org/sysmon/client}/AgentRouteBuilder.java | 5 +- .../java/org/sysmon/client}/Application.java | 2 +- .../sysmon/client}/MetricEnrichProcessor.java | 8 +- .../src/main/resources/application.properties | 4 +- .../main/resources/simplelogger.properties | 0 .../groovy/org/sysmon/client}/AppTest.groovy | 2 +- .../test/resources/simplelogger.properties | 0 plugins/.gitignore | 1 - plugins/build.gradle | 4 +- .../sysmon_aix/AixProcessorExtension.java | 2 +- .../src/test/groovy/AixProcessorTest.groovy | 27 ++++++ .../sysmon-aix/src/test/resources/mpstat1.txt | 15 +++ .../sysmon-aix/src/test/resources/mpstat2.txt | 15 +++ .../sysmon_linux/LinuxDiskExtension.java | 2 +- .../sysmon_linux/LinuxMemoryExtension.java | 2 +- .../sysmon_linux/LinuxProcessorExtension.java | 53 ++++------ .../sysmon_linux/LinuxProcessorProcLine.java | 95 ++++++++++++++++++ .../sysmon_linux/LinuxProcessorStat.java | 97 +++---------------- .../src/test/groovy/LinuxProcessorTest.groovy | 45 +++++++++ .../sysmon-linux/src/test/resources/proc1.txt | 9 ++ .../sysmon-linux/src/test/resources/proc2.txt | 9 ++ {collector => server}/build.gradle | 2 +- .../java/org/sysmon/server}/Application.java | 3 +- .../sysmon/server}/CollectorRouteBuilder.java | 2 +- .../server}/MetricResultToPointProcessor.java | 2 +- .../java/org/sysmon/server}/bean/Hello.java | 2 +- .../server}/bean/IncomingMetricProcessor.java | 2 +- .../src/main/resources/application.properties | 6 +- .../main/resources/simplelogger.properties | 0 .../test/resources/simplelogger.properties | 0 settings.gradle | 2 +- .../java/org/sysmon/shared/MetricResult.java | 5 +- 34 files changed, 280 insertions(+), 148 deletions(-) rename {agent => client}/build.gradle (90%) rename {agent/src/main/java/org/sysmon/agent => client/src/main/java/org/sysmon/client}/AgentRouteBuilder.java (94%) rename {agent/src/main/java/org/sysmon/agent => client/src/main/java/org/sysmon/client}/Application.java (98%) rename {agent/src/main/java/org/sysmon/agent => client/src/main/java/org/sysmon/client}/MetricEnrichProcessor.java (86%) rename {agent => client}/src/main/resources/application.properties (94%) rename {agent => client}/src/main/resources/simplelogger.properties (100%) rename {agent/src/test/groovy/org/sysmon/agent => client/src/test/groovy/org/sysmon/client}/AppTest.groovy (75%) rename {agent => client}/src/test/resources/simplelogger.properties (100%) delete mode 100644 plugins/.gitignore create mode 100644 plugins/sysmon-aix/src/test/groovy/AixProcessorTest.groovy create mode 100644 plugins/sysmon-aix/src/test/resources/mpstat1.txt create mode 100644 plugins/sysmon-aix/src/test/resources/mpstat2.txt create mode 100644 plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxProcessorProcLine.java create mode 100644 plugins/sysmon-linux/src/test/groovy/LinuxProcessorTest.groovy create mode 100644 plugins/sysmon-linux/src/test/resources/proc1.txt create mode 100644 plugins/sysmon-linux/src/test/resources/proc2.txt rename {collector => server}/build.gradle (94%) rename {collector/src/main/java/org/sysmon/collector => server/src/main/java/org/sysmon/server}/Application.java (89%) rename {collector/src/main/java/org/sysmon/collector => server/src/main/java/org/sysmon/server}/CollectorRouteBuilder.java (97%) rename {collector/src/main/java/org/sysmon/collector => server/src/main/java/org/sysmon/server}/MetricResultToPointProcessor.java (97%) rename {collector/src/main/java/org/sysmon/collector => server/src/main/java/org/sysmon/server}/bean/Hello.java (94%) rename {collector/src/main/java/org/sysmon/collector => server/src/main/java/org/sysmon/server}/bean/IncomingMetricProcessor.java (93%) rename {collector => server}/src/main/resources/application.properties (89%) rename {collector => server}/src/main/resources/simplelogger.properties (100%) rename {collector => server}/src/test/resources/simplelogger.properties (100%) diff --git a/.gitignore b/.gitignore index 0839577..dcf7985 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,6 @@ .classpath .project .gradle +output build bin diff --git a/agent/build.gradle b/client/build.gradle similarity index 90% rename from agent/build.gradle rename to client/build.gradle index e94b6ba..ce03075 100644 --- a/agent/build.gradle +++ b/client/build.gradle @@ -20,11 +20,11 @@ dependencies { application { // Define the main class for the application. - mainClassName = 'org.sysmon.agent.Application' + mainClassName = 'org.sysmon.client.Application' } run { - systemProperty 'pf4j.pluginsDir', '../plugins/test/' + systemProperty 'pf4j.pluginsDir', '../plugins/output/' } tasks.named('test') { diff --git a/agent/src/main/java/org/sysmon/agent/AgentRouteBuilder.java b/client/src/main/java/org/sysmon/client/AgentRouteBuilder.java similarity index 94% rename from agent/src/main/java/org/sysmon/agent/AgentRouteBuilder.java rename to client/src/main/java/org/sysmon/client/AgentRouteBuilder.java index f004be3..79ba904 100644 --- a/agent/src/main/java/org/sysmon/agent/AgentRouteBuilder.java +++ b/client/src/main/java/org/sysmon/client/AgentRouteBuilder.java @@ -1,4 +1,4 @@ -package org.sysmon.agent; +package org.sysmon.client; import org.apache.camel.Exchange; import org.apache.camel.builder.RouteBuilder; @@ -30,11 +30,12 @@ public class AgentRouteBuilder extends RouteBuilder { log.info(">>> Enabling extension: " + ext.getDescription()); // Setup Camel route for this extension - from("timer:collect?period=10000") + from("timer:collect?period=5000") .bean(ext, "getMetrics") //.doTry() .process(new MetricEnrichProcessor()) .choice().when(exchangeProperty("skip").isEqualTo(true)) + .log("Skipping: ${body}") .stop() .otherwise() .to("seda:metrics"); diff --git a/agent/src/main/java/org/sysmon/agent/Application.java b/client/src/main/java/org/sysmon/client/Application.java similarity index 98% rename from agent/src/main/java/org/sysmon/agent/Application.java rename to client/src/main/java/org/sysmon/client/Application.java index f55ff44..7346b7e 100644 --- a/agent/src/main/java/org/sysmon/agent/Application.java +++ b/client/src/main/java/org/sysmon/client/Application.java @@ -1,7 +1,7 @@ /* * This Java source file was generated by the Gradle 'init' task. */ -package org.sysmon.agent; +package org.sysmon.client; import org.apache.camel.main.Main; import org.slf4j.Logger; diff --git a/agent/src/main/java/org/sysmon/agent/MetricEnrichProcessor.java b/client/src/main/java/org/sysmon/client/MetricEnrichProcessor.java similarity index 86% rename from agent/src/main/java/org/sysmon/agent/MetricEnrichProcessor.java rename to client/src/main/java/org/sysmon/client/MetricEnrichProcessor.java index 4ba1fbb..55705a5 100644 --- a/agent/src/main/java/org/sysmon/agent/MetricEnrichProcessor.java +++ b/client/src/main/java/org/sysmon/client/MetricEnrichProcessor.java @@ -1,4 +1,4 @@ -package org.sysmon.agent; +package org.sysmon.client; import org.apache.camel.Exchange; import org.apache.camel.Processor; @@ -12,13 +12,15 @@ public class MetricEnrichProcessor implements Processor { public void process(Exchange exchange) throws Exception { MetricResult result = exchange.getIn().getBody(MetricResult.class); - result.setHostname(hostname); // We make sure MetricResults with no measurements are not sent further down the line - if(result.getMeasurements().size() < 1) { + if(result == null || result.getMeasurements().size() < 1) { exchange.setProperty("skip", true); + return; } + result.setHostname(hostname); + exchange.getIn().setHeader("component", result.getName()); exchange.getIn().setBody(result); } diff --git a/agent/src/main/resources/application.properties b/client/src/main/resources/application.properties similarity index 94% rename from agent/src/main/resources/application.properties rename to client/src/main/resources/application.properties index 2969166..3789f73 100644 --- a/agent/src/main/resources/application.properties +++ b/client/src/main/resources/application.properties @@ -17,7 +17,7 @@ # to configure camel main # here you can configure options on camel main (see MainConfigurationProperties class) -camel.main.name = sysmon-agent +camel.main.name = sysmon-client # enable tracing #camel.main.tracing = 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.client.MetricProcessor diff --git a/agent/src/main/resources/simplelogger.properties b/client/src/main/resources/simplelogger.properties similarity index 100% rename from agent/src/main/resources/simplelogger.properties rename to client/src/main/resources/simplelogger.properties diff --git a/agent/src/test/groovy/org/sysmon/agent/AppTest.groovy b/client/src/test/groovy/org/sysmon/client/AppTest.groovy similarity index 75% rename from agent/src/test/groovy/org/sysmon/agent/AppTest.groovy rename to client/src/test/groovy/org/sysmon/client/AppTest.groovy index f95a586..78e8dd8 100644 --- a/agent/src/test/groovy/org/sysmon/agent/AppTest.groovy +++ b/client/src/test/groovy/org/sysmon/client/AppTest.groovy @@ -1,4 +1,4 @@ -package org.sysmon.agent +package org.sysmon.client import spock.lang.Specification diff --git a/agent/src/test/resources/simplelogger.properties b/client/src/test/resources/simplelogger.properties similarity index 100% rename from agent/src/test/resources/simplelogger.properties rename to client/src/test/resources/simplelogger.properties diff --git a/plugins/.gitignore b/plugins/.gitignore deleted file mode 100644 index 30d74d2..0000000 --- a/plugins/.gitignore +++ /dev/null @@ -1 +0,0 @@ -test \ No newline at end of file diff --git a/plugins/build.gradle b/plugins/build.gradle index bf0fdff..87b4191 100644 --- a/plugins/build.gradle +++ b/plugins/build.gradle @@ -29,7 +29,7 @@ subprojects { task copyJar(type: Copy, dependsOn:[jar]) { from jar // here it automatically reads jar file produced from jar task - into "../test/" + into "../output/" } tasks.assemble.dependsOn { @@ -46,7 +46,7 @@ subprojects { } task customCleanUp(type:Delete) { - delete "test" + delete "output" //delete files("${buildDir}/test/*.jar") } diff --git a/plugins/sysmon-aix/src/main/java/org/sysmon/plugins/sysmon_aix/AixProcessorExtension.java b/plugins/sysmon-aix/src/main/java/org/sysmon/plugins/sysmon_aix/AixProcessorExtension.java index 2740ce0..f02a0df 100644 --- a/plugins/sysmon-aix/src/main/java/org/sysmon/plugins/sysmon_aix/AixProcessorExtension.java +++ b/plugins/sysmon-aix/src/main/java/org/sysmon/plugins/sysmon_aix/AixProcessorExtension.java @@ -39,7 +39,7 @@ public class AixProcessorExtension implements MetricExtension { List mpstat = PluginHelper.executeCommand("mpstat", "-a"); List processorStats = processCommandOutput(mpstat); for(AixProcessorStat stat : processorStats) { - result.addMetricMeasurement(new MeasurementPair(String.format("cpu%d", stat.getCpuNum()), stat.getUtilizationPercentage())); + result.addMeasurement(new MeasurementPair(String.format("cpu%d", stat.getCpuNum()), stat.getUtilizationPercentage())); } return result; diff --git a/plugins/sysmon-aix/src/test/groovy/AixProcessorTest.groovy b/plugins/sysmon-aix/src/test/groovy/AixProcessorTest.groovy new file mode 100644 index 0000000..cee6460 --- /dev/null +++ b/plugins/sysmon-aix/src/test/groovy/AixProcessorTest.groovy @@ -0,0 +1,27 @@ +import org.sysmon.plugins.sysmon_aix.AixProcessorExtension +import org.sysmon.plugins.sysmon_aix.AixProcessorStat +import spock.lang.Specification + +class AixProcessorTest extends Specification { + + void "test mpstat output processing"() { + + setup: + def testFile = new File(getClass().getResource('/mpstat1.txt').toURI()) + List lines = testFile.readLines("UTF-8") + + when: + AixProcessorExtension extension = new AixProcessorExtension() + List stats = extension.processCommandOutput(lines) + + then: + stats[0].getCombinedWorkTime() == 85.1f + stats[0].getCombinedTime() == 100.0f + stats[0].getSystemTime() == 18.4f + stats[0].getUserTime() == 66.7f + stats[0].getWaitTime() == 0.0f + stats[0].getIdleTime() == 14.9f + + } + +} diff --git a/plugins/sysmon-aix/src/test/resources/mpstat1.txt b/plugins/sysmon-aix/src/test/resources/mpstat1.txt new file mode 100644 index 0000000..80f39e7 --- /dev/null +++ b/plugins/sysmon-aix/src/test/resources/mpstat1.txt @@ -0,0 +1,15 @@ + +System configuration: lcpu=8 ent=0.2 mode=Uncapped + +cpu min maj mpcs mpcr dev soft dec ph cs ics bound rq push S3pull S3grd S0rd S1rd S2rd S3rd S4rd S5rd sysc us sy wa id pc %ec ilcs vlcs S3hrd S4hrd S5hrd +cpu min maj mpcs mpcr dev soft dec ph cs ics bound rq push S3pull S3grd S0rd S1rd S2rd S3rd S4rd S5rd sysc us sy wa id pc %ec ilcs vlcs S3hrd S4hrd S5hrd %nsp + 0 22324631 12995 2450 1 117140 14071941 37427759 4 25044259 445796 1 3 0 0 0 99.9 0.1 0.0 0.0 0.0 0.0 77548535 66.7 18.4 0.0 14.9 0.00 0.1 237843 38774212 100.0 0.0 0.0 150 + 1 275936 176 7 350 68318 1505 3604407 41 31072 25403 0 0 0 0 0 24.5 75.5 0.0 0.0 0.0 0.0 608541 0.2 0.8 0.0 99.0 0.00 0.0 916 3701367 100.0 0.0 0.0 150 + 2 4713 1 0 351 58162 833 2400472 13 24152 23991 0 0 0 0 0 0.7 99.3 0.0 0.0 0.0 0.0 1445 0.0 1.0 0.0 99.0 0.00 0.0 163 2483263 100.0 0.0 0.0 150 + 3 4587 2 0 351 57777 836 2400544 17 24094 23987 0 0 0 0 0 0.7 99.3 0.0 0.0 0.0 0.0 1199 0.0 0.4 0.0 99.6 0.00 0.0 167 2482920 100.0 0.0 0.0 150 + 4 5 0 0 351 56903 784 2400095 16 23999 23965 0 0 0 0 0 0.0 100.0 0.0 0.0 0.0 0.0 7 0.0 1.0 0.0 99.0 0.00 0.0 153 2481522 100.0 0.0 0.0 150 + 5 13 0 0 351 57171 815 2399248 12 23965 23962 0 0 0 0 0 0.0 100.0 0.0 0.0 0.0 0.0 0 0.0 0.4 0.0 99.6 0.00 0.0 145 2480922 100.0 0.0 0.0 150 + 6 23529 102 0 351 56443 805 2401503 6 24217 24124 0 0 0 0 0 0.9 99.1 0.0 0.0 0.0 0.0 461 0.0 1.0 0.0 99.0 0.00 0.0 162 2482459 100.0 0.0 0.0 150 + 7 1523 75 0 351 56335 783 4150673 11 24209 24040 0 0 0 0 0 1.1 98.9 0.0 0.0 0.0 0.0 471 0.0 0.9 0.0 99.1 0.00 0.0 479 4231232 100.0 0.0 0.0 150 + U - - - - - - - - - - - - - - - - - - - - - - - - 0.0 99.9 0.25 99.8 - - - - - +ALL 22634937 13351 2457 2457 528249 14078302 57184701 120 25219967 615268 1 3 0 0 0 99.3 0.7 0.0 0.0 0.0 0.0 78160659 0.0 0.0 0.0 99.9 0.00 0.2 240028 59117897 100.0 0.0 0.0 0 diff --git a/plugins/sysmon-aix/src/test/resources/mpstat2.txt b/plugins/sysmon-aix/src/test/resources/mpstat2.txt new file mode 100644 index 0000000..0807efe --- /dev/null +++ b/plugins/sysmon-aix/src/test/resources/mpstat2.txt @@ -0,0 +1,15 @@ + +System configuration: lcpu=8 ent=0.2 mode=Uncapped + +cpu min maj mpcs mpcr dev soft dec ph cs ics bound rq push S3pull S3grd S0rd S1rd S2rd S3rd S4rd S5rd sysc us sy wa id pc %ec ilcs vlcs S3hrd S4hrd S5hrd +cpu min maj mpcs mpcr dev soft dec ph cs ics bound rq push S3pull S3grd S0rd S1rd S2rd S3rd S4rd S5rd sysc us sy wa id pc %ec ilcs vlcs S3hrd S4hrd S5hrd %nsp + 0 22334111 14138 2450 1 117391 14078152 37441241 4 25056579 446411 1 4 0 0 0 99.9 0.1 0.0 0.0 0.0 0.0 77590574 66.7 18.4 0.0 14.9 0.00 0.1 237925 38789423 100.0 0.0 0.0 150 + 1 275936 176 7 350 68538 1505 3605599 41 31080 25411 0 0 0 0 0 24.5 75.5 0.0 0.0 0.0 0.0 608541 0.2 0.8 0.0 99.0 0.00 0.0 916 3702787 100.0 0.0 0.0 150 + 2 4713 1 0 351 58375 833 2401267 13 24160 23999 0 0 0 0 0 0.7 99.3 0.0 0.0 0.0 0.0 1445 0.0 1.0 0.0 99.0 0.00 0.0 163 2484279 100.0 0.0 0.0 150 + 3 4587 2 0 351 57986 836 2401339 17 24102 23995 0 0 0 0 0 0.6 99.4 0.0 0.0 0.0 0.0 1199 0.0 0.4 0.0 99.6 0.00 0.0 167 2483932 100.0 0.0 0.0 150 + 4 5 0 0 351 57114 786 2400891 16 24007 23973 0 0 0 0 0 0.0 100.0 0.0 0.0 0.0 0.0 7 0.0 1.0 0.0 99.0 0.00 0.0 154 2482537 100.0 0.0 0.0 150 + 5 13 0 0 351 57379 815 2400045 12 23973 23970 0 0 0 0 0 0.0 100.0 0.0 0.0 0.0 0.0 0 0.0 0.4 0.0 99.6 0.00 0.0 145 2481936 100.0 0.0 0.0 150 + 6 23529 102 0 351 56651 805 2402300 6 24225 24132 0 0 0 0 0 0.9 99.1 0.0 0.0 0.0 0.0 461 0.0 1.0 0.0 99.0 0.00 0.0 162 2483472 100.0 0.0 0.0 150 + 7 1523 75 0 351 56544 783 4152029 11 24217 24048 0 0 0 0 0 1.1 98.9 0.0 0.0 0.0 0.0 471 0.0 0.9 0.0 99.1 0.00 0.0 479 4232805 100.0 0.0 0.0 150 + U - - - - - - - - - - - - - - - - - - - - - - - - 0.0 99.9 0.25 99.8 - - - - - +ALL 22644417 14494 2457 2457 529978 14084515 57204711 120 25232343 615939 1 4 0 0 0 99.3 0.7 0.0 0.0 0.0 0.0 78202698 0.0 0.0 0.0 99.9 0.00 0.2 240111 59141171 100.0 0.0 0.0 0 diff --git a/plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxDiskExtension.java b/plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxDiskExtension.java index 44d3e01..b66df06 100644 --- a/plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxDiskExtension.java +++ b/plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxDiskExtension.java @@ -46,7 +46,7 @@ public class LinuxDiskExtension implements MetricExtension { try { copyCurrentValues(); readProcFile(); - result.setMetricMeasurementList(calculate()); + result.addMeasurements(calculate()); } catch (IOException e) { e.printStackTrace(); } diff --git a/plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxMemoryExtension.java b/plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxMemoryExtension.java index 26e8cd3..97aa5bb 100644 --- a/plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxMemoryExtension.java +++ b/plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxMemoryExtension.java @@ -40,7 +40,7 @@ public class LinuxMemoryExtension implements MetricExtension { MetricResult result = new MetricResult("memory"); try { - result.setMetricMeasurementList(readProcFile()); + result.addMeasurements(readProcFile()); } catch (IOException e) { e.printStackTrace(); } diff --git a/plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxProcessorExtension.java b/plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxProcessorExtension.java index 5479a4f..87e815f 100644 --- a/plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxProcessorExtension.java +++ b/plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxProcessorExtension.java @@ -4,8 +4,8 @@ package org.sysmon.plugins.sysmon_linux; import org.pf4j.Extension; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.sysmon.shared.MetricExtension; import org.sysmon.shared.MeasurementPair; +import org.sysmon.shared.MetricExtension; import org.sysmon.shared.MetricResult; import java.io.IOException; @@ -20,8 +20,8 @@ public class LinuxProcessorExtension implements MetricExtension { private static final Logger log = LoggerFactory.getLogger(LinuxProcessorExtension.class); - private List currentProcessorStats; - private List previousProcessorStats; + private List currentProcessorProc; + private List previousProcessorProc; @Override @@ -43,44 +43,25 @@ public class LinuxProcessorExtension implements MetricExtension { @Override public MetricResult getMetrics() { - if(currentProcessorStats != null && currentProcessorStats.size() > 0) { - previousProcessorStats = new ArrayList<>(currentProcessorStats); + if(currentProcessorProc != null && currentProcessorProc.size() > 0) { + previousProcessorProc = new ArrayList<>(currentProcessorProc); } + currentProcessorProc = processFileOutput(readProcFile()); MetricResult result = new MetricResult("processor"); - currentProcessorStats = processFileOutput(readProcFile()); - result.setMetricMeasurementList(calculateDifference()); + if(previousProcessorProc == null || previousProcessorProc.size() != currentProcessorProc.size()) { + return result; + } + + for(int i = 0; i < currentProcessorProc.size(); i++) { + LinuxProcessorStat stat = new LinuxProcessorStat(currentProcessorProc.get(i), previousProcessorProc.get(i)); + result.addMeasurement(stat.getMeasurements()); + } return result; } - private List calculateDifference() { - - List measurementList = new ArrayList<>(); - - if(previousProcessorStats == null || previousProcessorStats.size() != currentProcessorStats.size()) { - return measurementList; - } - - for(int i = 0; i < currentProcessorStats.size(); i++) { - - LinuxProcessorStat curStat = currentProcessorStats.get(i); - LinuxProcessorStat preStat = previousProcessorStats.get(i); - - long workTimeDiff = curStat.getCombinedTime() - preStat.getCombinedTime(); - long idleTimeDiff = curStat.getCombinedIdleTime() - preStat.getCombinedIdleTime(); - float percentUsage = (float) (workTimeDiff - idleTimeDiff) / workTimeDiff; - - Integer pct = (int) (percentUsage * 100); - measurementList.add(new MeasurementPair(curStat.getCpuName(), pct)); - - } - - return measurementList; - - } - protected List readProcFile() { @@ -95,12 +76,12 @@ public class LinuxProcessorExtension implements MetricExtension { } - protected List processFileOutput(List inputLines) { + protected List processFileOutput(List inputLines) { - List processorStats = new ArrayList<>(); + List processorStats = new ArrayList<>(); for(String line : inputLines) { if(line.matches("^cpu\\d+.*")) { - processorStats.add(new LinuxProcessorStat(line)); + processorStats.add(new LinuxProcessorProcLine(line)); } } diff --git a/plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxProcessorProcLine.java b/plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxProcessorProcLine.java new file mode 100644 index 0000000..05a3522 --- /dev/null +++ b/plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxProcessorProcLine.java @@ -0,0 +1,95 @@ +package org.sysmon.plugins.sysmon_linux; + +public class LinuxProcessorProcLine { + + private final String cpuName; + private final Long userTime; + private final Long niceTime; + private final Long systemTime; + private final Long idleTime; + private final Long ioWaitTime; + private final Long irqTime; + private final Long softIrqTime; + private final Long stealTime; + private final Long guestTime; + private final Long guestNiceTime; + + + public LinuxProcessorProcLine(String procString) { + + String[] splitStr = procString.trim().split("\\s+"); + if(splitStr.length != 11) { + throw new UnsupportedOperationException("Linux proc CPU string error: " + procString); + } + + this.cpuName = splitStr[0]; + this.userTime = Long.parseLong(splitStr[1]); + this.niceTime = Long.parseLong(splitStr[2]); + this.systemTime = Long.parseLong(splitStr[3]); + this.idleTime = Long.parseLong(splitStr[4]); + this.ioWaitTime = Long.parseLong(splitStr[5]); + this.irqTime = Long.parseLong(splitStr[6]); + this.softIrqTime = Long.parseLong(splitStr[7]); + this.stealTime = Long.parseLong(splitStr[8]); + this.guestTime = Long.parseLong(splitStr[9]); + this.guestNiceTime = Long.parseLong(splitStr[10]); + + } + + public String getCpuName() { + return cpuName; + } + + public Long getUserTime() { + return userTime; + } + + public Long getNiceTime() { + return niceTime; + } + + public Long getSystemTime() { + return systemTime; + } + + public Long getIdleTime() { + return idleTime; + } + + public Long getIoWaitTime() { + return ioWaitTime; + } + + public Long getIrqTime() { + return irqTime; + } + + public Long getSoftIrqTime() { + return softIrqTime; + } + + public Long getStealTime() { + return stealTime; + } + + public Long getGuestTime() { + return guestTime; + } + + public Long getGuestNiceTime() { + return guestNiceTime; + } + + public Long getCombinedIdleTime() { + return idleTime + ioWaitTime; + } + + public Long getCombinedWorkTime() { + return userTime + niceTime + systemTime + irqTime + softIrqTime + stealTime + guestTime + guestNiceTime; + } + + public Long getCombinedTime() { + return getCombinedIdleTime() + getCombinedWorkTime(); + } + +} diff --git a/plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxProcessorStat.java b/plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxProcessorStat.java index 2ef5742..df8fecc 100644 --- a/plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxProcessorStat.java +++ b/plugins/sysmon-linux/src/main/java/org/sysmon/plugins/sysmon_linux/LinuxProcessorStat.java @@ -1,95 +1,30 @@ package org.sysmon.plugins.sysmon_linux; +import org.sysmon.shared.MeasurementPair; + +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.List; + public class LinuxProcessorStat { private final String cpuName; - private final Long userTime; - private final Long niceTime; - private final Long systemTime; - private final Long idleTime; - private final Long ioWaitTime; - private final Long irqTime; - private final Long softIrqTime; - private final Long stealTime; - private final Long guestTime; - private final Long guestNiceTime; + private final float utilizationPercentage; + public LinuxProcessorStat(LinuxProcessorProcLine current, LinuxProcessorProcLine previous) { + cpuName = current.getCpuName(); - LinuxProcessorStat(String procString) { - - String[] splitStr = procString.trim().split("\\s+"); - if(splitStr.length != 11) { - throw new UnsupportedOperationException("Linux proc CPU string error: " + procString); - } - - this.cpuName = splitStr[0]; - this.userTime = Long.parseLong(splitStr[1]); - this.niceTime = Long.parseLong(splitStr[2]); - this.systemTime = Long.parseLong(splitStr[3]); - this.idleTime = Long.parseLong(splitStr[4]); - this.ioWaitTime = Long.parseLong(splitStr[5]); - this.irqTime = Long.parseLong(splitStr[6]); - this.softIrqTime = Long.parseLong(splitStr[7]); - this.stealTime = Long.parseLong(splitStr[8]); - this.guestTime = Long.parseLong(splitStr[9]); - this.guestNiceTime = Long.parseLong(splitStr[10]); + long workTimeDiff = current.getCombinedTime() - previous.getCombinedTime(); + long idleTimeDiff = current.getCombinedIdleTime() - previous.getCombinedIdleTime(); + float utilization = (float) (workTimeDiff - idleTimeDiff) / workTimeDiff; + utilizationPercentage = (utilization * 100); } - public String getCpuName() { - return cpuName; + + public MeasurementPair getMeasurements() { + return new MeasurementPair(cpuName, utilizationPercentage); } - public Long getUserTime() { - return userTime; - } - - public Long getNiceTime() { - return niceTime; - } - - public Long getSystemTime() { - return systemTime; - } - - public Long getIdleTime() { - return idleTime; - } - - public Long getIoWaitTime() { - return ioWaitTime; - } - - public Long getIrqTime() { - return irqTime; - } - - public Long getSoftIrqTime() { - return softIrqTime; - } - - public Long getStealTime() { - return stealTime; - } - - public Long getGuestTime() { - return guestTime; - } - - public Long getGuestNiceTime() { - return guestNiceTime; - } - - public Long getCombinedIdleTime() { - return idleTime + ioWaitTime; - } - - public Long getCombinedWorkTime() { - return userTime + niceTime + systemTime + irqTime + softIrqTime + stealTime + guestTime + guestNiceTime; - } - - public Long getCombinedTime() { - return getCombinedIdleTime() + getCombinedWorkTime(); - } } diff --git a/plugins/sysmon-linux/src/test/groovy/LinuxProcessorTest.groovy b/plugins/sysmon-linux/src/test/groovy/LinuxProcessorTest.groovy new file mode 100644 index 0000000..7f1c52f --- /dev/null +++ b/plugins/sysmon-linux/src/test/groovy/LinuxProcessorTest.groovy @@ -0,0 +1,45 @@ +import org.sysmon.plugins.sysmon_linux.LinuxProcessorExtension +import org.sysmon.plugins.sysmon_linux.LinuxProcessorProcLine +import org.sysmon.plugins.sysmon_linux.LinuxProcessorStat +import spock.lang.Specification + +class LinuxProcessorTest extends Specification { + + void "test proc file processing"() { + + setup: + def testFile = new File(getClass().getResource('/proc1.txt').toURI()) + List lines = testFile.readLines("UTF-8") + + when: + LinuxProcessorExtension extension = new LinuxProcessorExtension() + List procLines = extension.processFileOutput(lines) + + then: + procLines[0].getSystemTime() == 4686l + procLines[0].getUserTime() == 27477l + procLines[0].getIdleTime() == 281276l + procLines[0].getIoWaitTime() == 252l + + } + + + void "test processor utlization"() { + + setup: + def testFile1 = new File(getClass().getResource('/proc1.txt').toURI()) + def testFile2 = new File(getClass().getResource('/proc2.txt').toURI()) + LinuxProcessorProcLine processorProcLine1 = new LinuxProcessorProcLine(testFile1.readLines().get(1)) + LinuxProcessorProcLine processorProcLine2 = new LinuxProcessorProcLine(testFile2.readLines().get(1)) + + when: + LinuxProcessorStat processorStat = new LinuxProcessorStat(processorProcLine1, processorProcLine2) + + then: + processorStat.getMeasurements().getName() == "cpu0" + processorStat.getMeasurements().getValue() == 42.13362f + + } + + +} diff --git a/plugins/sysmon-linux/src/test/resources/proc1.txt b/plugins/sysmon-linux/src/test/resources/proc1.txt new file mode 100644 index 0000000..e642692 --- /dev/null +++ b/plugins/sysmon-linux/src/test/resources/proc1.txt @@ -0,0 +1,9 @@ +cpu 209203 14 38118 2255635 1987 0 431 0 0 0 +cpu0 27477 3 4686 281276 252 0 4 0 0 0 +cpu1 25578 1 4701 283363 243 0 12 0 0 0 +cpu2 21817 0 4598 284376 279 0 377 0 0 0 +cpu3 26782 5 4810 281794 296 0 3 0 0 0 +cpu4 25568 0 4813 281555 237 0 6 0 0 0 +cpu5 25460 0 5313 282740 210 0 20 0 0 0 +cpu6 28989 1 4390 280404 220 0 3 0 0 0 +cpu7 27530 0 4804 280125 247 0 2 0 0 0 diff --git a/plugins/sysmon-linux/src/test/resources/proc2.txt b/plugins/sysmon-linux/src/test/resources/proc2.txt new file mode 100644 index 0000000..f35dbe6 --- /dev/null +++ b/plugins/sysmon-linux/src/test/resources/proc2.txt @@ -0,0 +1,9 @@ +cpu 211854 14 38286 2260226 2000 0 434 0 0 0 +cpu0 27848 3 4706 281811 254 0 4 0 0 0 +cpu1 25908 1 4713 283951 244 0 12 0 0 0 +cpu2 22137 0 4625 284942 281 0 378 0 0 0 +cpu3 27193 5 4828 282288 298 0 3 0 0 0 +cpu4 25874 0 4836 282159 239 0 6 0 0 0 +cpu5 25756 0 5337 283350 211 0 22 0 0 0 +cpu6 29300 1 4418 280991 221 0 3 0 0 0 +cpu7 27834 0 4819 280731 249 0 2 0 0 0 diff --git a/collector/build.gradle b/server/build.gradle similarity index 94% rename from collector/build.gradle rename to server/build.gradle index 3025071..2472626 100644 --- a/collector/build.gradle +++ b/server/build.gradle @@ -17,7 +17,7 @@ dependencies { application { // Define the main class for the application. - mainClassName = 'org.sysmon.collector.Application' + mainClassName = 'org.sysmon.server.Application' } tasks.named('test') { diff --git a/collector/src/main/java/org/sysmon/collector/Application.java b/server/src/main/java/org/sysmon/server/Application.java similarity index 89% rename from collector/src/main/java/org/sysmon/collector/Application.java rename to server/src/main/java/org/sysmon/server/Application.java index ee3e6d5..2c65c36 100644 --- a/collector/src/main/java/org/sysmon/collector/Application.java +++ b/server/src/main/java/org/sysmon/server/Application.java @@ -1,7 +1,6 @@ -package org.sysmon.collector; +package org.sysmon.server; import org.apache.camel.main.Main; -import org.apache.camel.support.SimpleRegistry; import org.influxdb.InfluxDB; import org.influxdb.InfluxDBFactory; diff --git a/collector/src/main/java/org/sysmon/collector/CollectorRouteBuilder.java b/server/src/main/java/org/sysmon/server/CollectorRouteBuilder.java similarity index 97% rename from collector/src/main/java/org/sysmon/collector/CollectorRouteBuilder.java rename to server/src/main/java/org/sysmon/server/CollectorRouteBuilder.java index 09feed2..8f6128b 100644 --- a/collector/src/main/java/org/sysmon/collector/CollectorRouteBuilder.java +++ b/server/src/main/java/org/sysmon/server/CollectorRouteBuilder.java @@ -1,4 +1,4 @@ -package org.sysmon.collector; +package org.sysmon.server; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.model.rest.RestBindingMode; diff --git a/collector/src/main/java/org/sysmon/collector/MetricResultToPointProcessor.java b/server/src/main/java/org/sysmon/server/MetricResultToPointProcessor.java similarity index 97% rename from collector/src/main/java/org/sysmon/collector/MetricResultToPointProcessor.java rename to server/src/main/java/org/sysmon/server/MetricResultToPointProcessor.java index 69a5271..4cad344 100644 --- a/collector/src/main/java/org/sysmon/collector/MetricResultToPointProcessor.java +++ b/server/src/main/java/org/sysmon/server/MetricResultToPointProcessor.java @@ -1,4 +1,4 @@ -package org.sysmon.collector; +package org.sysmon.server; import org.apache.camel.Exchange; import org.apache.camel.Processor; diff --git a/collector/src/main/java/org/sysmon/collector/bean/Hello.java b/server/src/main/java/org/sysmon/server/bean/Hello.java similarity index 94% rename from collector/src/main/java/org/sysmon/collector/bean/Hello.java rename to server/src/main/java/org/sysmon/server/bean/Hello.java index 44e2c83..a27a1f7 100644 --- a/collector/src/main/java/org/sysmon/collector/bean/Hello.java +++ b/server/src/main/java/org/sysmon/server/bean/Hello.java @@ -1,4 +1,4 @@ -package org.sysmon.collector.bean; +package org.sysmon.server.bean; import org.apache.camel.Exchange; import org.apache.camel.Processor; diff --git a/collector/src/main/java/org/sysmon/collector/bean/IncomingMetricProcessor.java b/server/src/main/java/org/sysmon/server/bean/IncomingMetricProcessor.java similarity index 93% rename from collector/src/main/java/org/sysmon/collector/bean/IncomingMetricProcessor.java rename to server/src/main/java/org/sysmon/server/bean/IncomingMetricProcessor.java index 09f0c38..2b40de2 100644 --- a/collector/src/main/java/org/sysmon/collector/bean/IncomingMetricProcessor.java +++ b/server/src/main/java/org/sysmon/server/bean/IncomingMetricProcessor.java @@ -1,4 +1,4 @@ -package org.sysmon.collector.bean; +package org.sysmon.server.bean; import org.apache.camel.Exchange; import org.apache.camel.Processor; diff --git a/collector/src/main/resources/application.properties b/server/src/main/resources/application.properties similarity index 89% rename from collector/src/main/resources/application.properties rename to server/src/main/resources/application.properties index 7dde120..cc7923e 100644 --- a/collector/src/main/resources/application.properties +++ b/server/src/main/resources/application.properties @@ -17,7 +17,7 @@ # to configure camel main # here you can configure options on camel main (see MainConfigurationProperties class) -camel.main.name = sysmon-collector +camel.main.name = sysmon-server # enable tracing #camel.main.tracing = 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.server.bean.IncomingMetricProcessor +#camel.beans.hello = #class:org.sysmon.server.bean.Hello diff --git a/collector/src/main/resources/simplelogger.properties b/server/src/main/resources/simplelogger.properties similarity index 100% rename from collector/src/main/resources/simplelogger.properties rename to server/src/main/resources/simplelogger.properties diff --git a/collector/src/test/resources/simplelogger.properties b/server/src/test/resources/simplelogger.properties similarity index 100% rename from collector/src/test/resources/simplelogger.properties rename to server/src/test/resources/simplelogger.properties diff --git a/settings.gradle b/settings.gradle index 6985859..95503d4 100644 --- a/settings.gradle +++ b/settings.gradle @@ -8,7 +8,7 @@ */ rootProject.name = 'sysmon' -include('shared', 'agent', 'collector', 'plugins') +include('shared', 'client', 'server', 'plugins') new File(rootDir, "plugins").listFiles().each { diff --git a/shared/src/main/java/org/sysmon/shared/MetricResult.java b/shared/src/main/java/org/sysmon/shared/MetricResult.java index 11abb61..e5679a7 100644 --- a/shared/src/main/java/org/sysmon/shared/MetricResult.java +++ b/shared/src/main/java/org/sysmon/shared/MetricResult.java @@ -15,7 +15,6 @@ public class MetricResult implements Serializable { private List measurements = new ArrayList<>(); public MetricResult() { - } public MetricResult(String name) { @@ -23,11 +22,11 @@ public class MetricResult implements Serializable { this.timestamp = Instant.now().toEpochMilli(); } - public void setMetricMeasurementList(List measurementList) { + public void addMeasurements(List measurementList) { this.measurements = measurementList; } - public void addMetricMeasurement(MeasurementPair measurement) { + public void addMeasurement(MeasurementPair measurement) { measurements.add(measurement); }