Improvements to network and disk, plus some cleanup and refactoring.

This commit is contained in:
Mark Nellemann 2021-09-11 21:55:48 +02:00
parent 203c5daf3e
commit 731d8b8d10
13 changed files with 213 additions and 58 deletions

View file

@ -37,7 +37,7 @@ def projectName = "sysmon-client"
application { application {
// Define the main class for the application. // Define the main class for the application.
mainClass.set('sysmon.client.Application') mainClass.set('sysmon.client.Application')
applicationDefaultJvmArgs = [ "-server", "-Xms16m", "-Xmx32m", "-XX:+UseG1GC" ] applicationDefaultJvmArgs = [ "-server", "-Xmx64m", "-XX:+UseG1GC" ]
} }
run { run {

View file

@ -1,7 +1,9 @@
# Configuration for sysmon-client # Configuration for sysmon-client
[extension.base_filesystem]
[extension.base_disk] [extension.base_disk]
enabled = false enabled = true
[extension.base_process] [extension.base_process]
enabled = true enabled = true

View file

@ -4,6 +4,7 @@
package sysmon.client; package sysmon.client;
import org.apache.camel.main.Main; import org.apache.camel.main.Main;
import org.slf4j.impl.SimpleLogger;
import picocli.CommandLine; import picocli.CommandLine;
import java.io.File; import java.io.File;
@ -28,6 +29,10 @@ public class Application implements Callable<Integer> {
@CommandLine.Option(names = { "-c", "--conf" }, description = "Configuration file [default: '/etc/sysmon-client.toml'].", paramLabel = "<file>", defaultValue = "/etc/sysmon-client.toml") @CommandLine.Option(names = { "-c", "--conf" }, description = "Configuration file [default: '/etc/sysmon-client.toml'].", paramLabel = "<file>", defaultValue = "/etc/sysmon-client.toml")
private File configurationFile; private File configurationFile;
@CommandLine.Option(names = { "-d", "--debug" }, description = "Enable debugging (default: ${DEFAULT_VALUE}).")
private boolean enableDebug = false;
public static void main(String... args) { public static void main(String... args) {
int exitCode = new CommandLine(new Application()).execute(args); int exitCode = new CommandLine(new Application()).execute(args);
System.exit(exitCode); System.exit(exitCode);
@ -39,6 +44,10 @@ public class Application implements Callable<Integer> {
Configuration configuration = new Configuration(); Configuration configuration = new Configuration();
if(enableDebug) {
System.setProperty(SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "DEBUG");
}
if(hostname == null || hostname.isEmpty()) { if(hostname == null || hostname.isEmpty()) {
try { try {
hostname = InetAddress.getLocalHost().getHostName(); hostname = InetAddress.getLocalHost().getHostName();

View file

@ -55,13 +55,10 @@ public class ClientRouteBuilder extends RouteBuilder {
log.info(">>> Enabling extension: " + ext.getDescription()); log.info(">>> Enabling extension: " + ext.getDescription());
providers.add(provides); providers.add(provides);
// TODO: Make timer thread configurable ?
// Setup Camel route for this extension // Setup Camel route for this extension
// a unique timer name gives the timer it's own thread, otherwise it's a shared thread for other timers with same name. // a unique timer name gives the timer it's own thread, otherwise it's a shared thread for other timers with same name.
//from("timer:"+provides+"?fixedRate=true&period=30s") //from("timer:extensions?fixedRate=true&period=30s")
from("timer:extensions?fixedRate=true&period=30s") from("timer:"+provides+"?fixedRate=true&period=30s")
.bean(ext, "getMetrics") .bean(ext, "getMetrics")
//.doTry() //.doTry()
.outputType(MetricResult.class) .outputType(MetricResult.class)

View file

@ -46,10 +46,12 @@ public final class Configuration {
String key = String.format("extension.%s", extName); String key = String.format("extension.%s", extName);
TomlTable table = result.getTableOrEmpty(key); TomlTable table = result.getTableOrEmpty(key);
table.keySet().forEach( k -> { table.keySet().forEach( k -> {
if(table.isString(k)) { if(table.isBoolean(k)) {
map.put(k, table.getString(k));
} else if(table.isBoolean(k)) {
map.put(k, table.getBoolean(k)); map.put(k, table.getBoolean(k));
} else if(table.isString(k)) {
map.put(k, table.getString(k));
} else if(table.isLong(k)) {
map.put(k, table.getLong(k));
} else if(table.isDouble(k)) { } else if(table.isDouble(k)) {
map.put(k, table.getDouble(k)); map.put(k, table.getDouble(k));
} else if(table.isArray(k)) { } else if(table.isArray(k)) {

View file

@ -1,4 +1,4 @@
version=0.0.10 version=0.0.11
pf4jVersion=3.6.0 pf4jVersion=3.6.0
slf4jVersion=1.7.32 slf4jVersion=1.7.32
camelVersion=3.11.1 camelVersion=3.11.1

View file

@ -37,7 +37,7 @@ public class AixProcessorExtension implements MetricExtension {
String osArch = System.getProperty("os.arch").toLowerCase(); String osArch = System.getProperty("os.arch").toLowerCase();
if(!osArch.startsWith("ppc64")) { if(!osArch.startsWith("ppc64")) {
log.warn("Requires CPU Architecture ppc64 or ppc64le, this is: " + osArch); log.debug("Requires CPU Architecture ppc64 or ppc64le, this is: " + osArch);
return false; return false;
} }
@ -81,6 +81,8 @@ public class AixProcessorExtension implements MetricExtension {
AixProcessorStat processorStat = processCommandOutput(buf); AixProcessorStat processorStat = processCommandOutput(buf);
tagsMap = processorStat.getTags(); tagsMap = processorStat.getTags();
fieldsMap = processorStat.getFields(); fieldsMap = processorStat.getFields();
} catch (IOException e) {
log.error("lparstat error", e);
} }
return new MetricResult(name, new Measurement(tagsMap, fieldsMap)); return new MetricResult(name, new Measurement(tagsMap, fieldsMap));

View file

@ -37,6 +37,24 @@ Metrics reported are:
- **iotime** - Time spent on IO in milliseconds. - **iotime** - Time spent on IO in milliseconds.
- **queue** - Lenght of IO queue. - **queue** - Lenght of IO queue.
## Filesystem Extension
### Metrics
-
- **free_bytes** - Free bytes for filesystem.
- **total_bytes** - Total bytes for filesystem.
- **free_inoed** - Free inodes for filesystem.
- **total_inodes** - Total inodes for filesystem.
### Configuration
```toml
[extension.base_filesystem]
enabled = true
exclude_type = [ "tmpfs", "ahafs" ]
exclude_mount = [ "/boot/efi" ]
```
## Process Extension ## Process Extension
Reports metrics on one or more running processes. Reports metrics on one or more running processes.

View file

@ -9,6 +9,7 @@ import sysmon.shared.Measurement;
import sysmon.shared.MetricExtension; import sysmon.shared.MetricExtension;
import sysmon.shared.MetricResult; import sysmon.shared.MetricResult;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -65,33 +66,32 @@ public class BaseDiskExtension implements MetricExtension {
@Override @Override
public MetricResult getMetrics() { public MetricResult getMetrics() {
long writeBytes = 0L; ArrayList<Measurement> measurementList = new ArrayList<>();
long readBytes = 0L;
long transferTime = 0L;
long queueLength = 0L;
HashMap<String, String> tagsMap = new HashMap<>();
HashMap<String, Object> fieldsMap = new HashMap<>();
List<HWDiskStore> diskStores = hardwareAbstractionLayer.getDiskStores(); List<HWDiskStore> diskStores = hardwareAbstractionLayer.getDiskStores();
for(HWDiskStore store : diskStores) { for(HWDiskStore store : diskStores) {
String name = store.getName(); String name = store.getName();
if (name.matches("hdisk[0-9]+") || name.matches("/dev/x?[sv]d[a-z]") || name.matches("/dev/nvme[0-9]n[0-9]")) { if (name.matches("hdisk[0-9]+") || name.matches("/dev/x?[sv]d[a-z]") || name.matches("/dev/nvme[0-9]n[0-9]")) {
log.debug("Using device: " + name);
writeBytes += store.getWriteBytes(); HashMap<String, String> tagsMap = new HashMap<String, String>() {{
readBytes += store.getReadBytes(); put("name", name);
transferTime += store.getTransferTime(); }};
queueLength = store.getCurrentQueueLength();
} HashMap<String, Object> fieldsMap = new HashMap<String, Object>() {{
put("read", store.getReadBytes());
put("write", store.getWriteBytes());
put("iotime", store.getTransferTime());
put("queue", store.getCurrentQueueLength());
}};
measurementList.add(new Measurement(tagsMap, fieldsMap));
} }
fieldsMap.put("reads", readBytes); }
fieldsMap.put("writes", writeBytes);
fieldsMap.put("iotime", transferTime); return new MetricResult(name, measurementList);
fieldsMap.put("queue", queueLength);
log.debug(fieldsMap.toString());
return new MetricResult(name, new Measurement(tagsMap, fieldsMap));
} }
} }

View file

@ -0,0 +1,132 @@
package sysmon.plugins.os_base;
import org.pf4j.Extension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import oshi.SystemInfo;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OSFileStore;
import sysmon.shared.Measurement;
import sysmon.shared.MetricExtension;
import sysmon.shared.MetricResult;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Extension
public class BaseFilesystemExtension implements MetricExtension {
private static final Logger log = LoggerFactory.getLogger(BaseDiskExtension.class);
// Extension details
private final String name = "base_filesystem";
private final String provides = "filesystem";
private final String description = "Base Filesystem Metrics";
// Configuration / Options
private boolean enabled = true;
private List<?> excludeType = new ArrayList<String>() {{
add("tmpfs");
add("ahafs");
}};
private List<?> excludeMount = new ArrayList<String>() {{
add("/boot/efi");
}};
private HardwareAbstractionLayer hardwareAbstractionLayer;
private SystemInfo systemInfo;
@Override
public boolean isEnabled() {
return enabled;
}
@Override
public boolean isSupported() {
systemInfo = BasePlugin.getSystemInfo();
//hardwareAbstractionLayer = BasePlugin.getHardwareAbstractionLayer();
return systemInfo != null;
}
@Override
public String getName() {
return name;
}
@Override
public String getProvides() {
return provides;
}
@Override
public String getDescription() {
return description;
}
@Override
public void setConfiguration(Map<String, Object> map) {
if (map.containsKey("enabled")) {
enabled = (boolean) map.get("enabled");
}
if(map.containsKey("exclude_type")) {
excludeType = (List<?>) map.get("exclude_type");
}
if(map.containsKey("exclude_mount")) {
excludeMount = (List<?>) map.get("exclude_mount");
}
}
@Override
public MetricResult getMetrics() {
ArrayList<String> alreadyProcessed = new ArrayList<>();
ArrayList<Measurement> measurementList = new ArrayList<>();
List<OSFileStore> fileStores = systemInfo.getOperatingSystem().getFileSystem().getFileStores(true);
for(OSFileStore store : fileStores) {
String name = store.getName();
String type = store.getType();
String mount = store.getMount();
if(excludeType.contains(type)) {
log.debug("Excluding type: " + type);
continue;
}
if(excludeMount.contains(mount)) {
log.debug("Excluding mount: " + mount);
continue;
}
if(alreadyProcessed.contains(name)) {
log.debug("Skipping name: " + name);
continue;
}
alreadyProcessed.add(name);
HashMap<String, String> tagsMap = new HashMap<String, String>() {{
put("name", name);
put("type", type);
put("mount", mount);
}};
HashMap<String, Object> fieldsMap = new HashMap<String, Object>() {{
put("free_bytes", store.getFreeSpace());
put("total_bytes", store.getTotalSpace());
put("free_inodes", store.getFreeInodes());
put("total_inodes", store.getTotalInodes());
}};
measurementList.add(new Measurement(tagsMap, fieldsMap));
}
return new MetricResult(name, measurementList);
}
}

View file

@ -9,6 +9,7 @@ import sysmon.shared.Measurement;
import sysmon.shared.MetricExtension; import sysmon.shared.MetricExtension;
import sysmon.shared.MetricResult; import sysmon.shared.MetricResult;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -65,37 +66,29 @@ public class BaseNetworkExtension implements MetricExtension {
@Override @Override
public MetricResult getMetrics() { public MetricResult getMetrics() {
long rxBytes = 0L; ArrayList<Measurement> measurementList = new ArrayList<>();
long rxPackets = 0L;
long rxErrs = 0L;
long txBytes = 0L;
long txPackets = 0L;
long txErrs = 0L;
HashMap<String, String> tagsMap = new HashMap<>();
HashMap<String, Object> fieldsMap = new HashMap<>();
List<NetworkIF> interfaces = hardwareAbstractionLayer.getNetworkIFs(); List<NetworkIF> interfaces = hardwareAbstractionLayer.getNetworkIFs();
for(NetworkIF netif : interfaces) { for(NetworkIF netif : interfaces) {
//String name = netif.getName();
//log.warn("Device: " + name); HashMap<String, String> tagsMap = new HashMap<String, String>() {{
rxPackets += netif.getPacketsRecv(); put("name", netif.getName());
txPackets += netif.getPacketsSent(); }};
rxBytes += netif.getBytesRecv();
txBytes += netif.getBytesSent(); HashMap<String, Object> fieldsMap = new HashMap<String, Object>() {{
rxErrs += netif.getInErrors(); put("rx_pkts", netif.getPacketsRecv());
txErrs += netif.getOutErrors(); put("tx_pkts", netif.getPacketsSent());
put("rx_bytes", netif.getBytesRecv());
put("tx_bytes", netif.getBytesSent());
put("rx_errs", netif.getInErrors());
put("tx_errs", netif.getOutErrors());
}};
measurementList.add(new Measurement(tagsMap, fieldsMap));
} }
fieldsMap.put("rxPackets", rxPackets);
fieldsMap.put("txPackets", txPackets);
fieldsMap.put("rxBytes", rxBytes);
fieldsMap.put("txBytes", txBytes);
fieldsMap.put("rxErrors", rxErrs);
fieldsMap.put("txErrors", txErrs);
log.debug(fieldsMap.toString()); return new MetricResult(name, measurementList);
return new MetricResult(name, new Measurement(tagsMap, fieldsMap));
} }
} }

View file

@ -35,7 +35,7 @@ public class LinuxSockstatExtension implements MetricExtension {
public boolean isSupported() { public boolean isSupported() {
if(!System.getProperty("os.name").toLowerCase().contains("linux")) { if(!System.getProperty("os.name").toLowerCase().contains("linux")) {
log.warn("Requires Linux."); log.debug("Requires Linux.");
return false; return false;
} }

View file

@ -28,7 +28,7 @@ def projectName = "sysmon-server"
application { application {
// Define the main class for the application. // Define the main class for the application.
mainClass.set('sysmon.server.Application') mainClass.set('sysmon.server.Application')
applicationDefaultJvmArgs = [ "-server", "-Xms64m", "-Xmx128m", "-XX:+UseG1GC" ] applicationDefaultJvmArgs = [ "-server", "-Xmx128m", "-XX:+UseG1GC" ]
} }
tasks.named('test') { tasks.named('test') {