More work on Linux network metrics.

This commit is contained in:
Mark Nellemann 2021-05-28 08:07:04 +02:00
parent 9f8191d0e5
commit e7560831e8
7 changed files with 143 additions and 146 deletions

View file

@ -4,8 +4,8 @@ plugins {
id "nebula.ospackage" version "8.5.6"
}
subprojects {
apply plugin: 'java'
apply plugin: 'groovy'
@ -20,6 +20,7 @@ subprojects {
}
annotationProcessor(group: 'org.pf4j', name: 'pf4j', version: "${pf4jVersion}")
implementation "org.slf4j:slf4j-api:${slf4jVersion}"
}
jar {
@ -32,9 +33,17 @@ subprojects {
'Plugin-Description': "${pluginDescription}"
)
}
}
task copyJar(type: Copy, dependsOn:[jar]) {
task fatJar(type: Jar) {
duplicatesStrategy(DuplicatesStrategy.INCLUDE)
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
task copyJar(type: Copy, dependsOn:[fatJar]) {
from jar // here it automatically reads jar file produced from jar task
into "../output/"
}

View file

@ -3,6 +3,8 @@ package sysmon.plugins.os_aix;
import org.pf4j.Extension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import oshi.SystemInfo;
import oshi.hardware.HardwareAbstractionLayer;
import sysmon.shared.Measurement;
import sysmon.shared.MetricExtension;
import sysmon.shared.MetricResult;
@ -11,11 +13,15 @@ import sysmon.shared.PluginHelper;
import java.util.List;
import java.util.Map;
@Extension
public class AixDiskExtension implements MetricExtension {
private static final Logger log = LoggerFactory.getLogger(AixProcessorExtension.class);
private final SystemInfo systemInfo;
private final HardwareAbstractionLayer hardwareAbstractionLayer;
@Override
public boolean isSupported() {
@ -32,6 +38,15 @@ public class AixDiskExtension implements MetricExtension {
return true;
}
public AixDiskExtension() {
systemInfo = new SystemInfo();
hardwareAbstractionLayer = systemInfo.getHardware();
}
@Override
public String getName() {
return "aix-disk";
@ -50,6 +65,15 @@ public class AixDiskExtension implements MetricExtension {
@Override
public MetricResult getMetrics() {
long writeBytes = hardwareAbstractionLayer.getDiskStores().get(0).getWriteBytes();
log.warn(String.format("Disk 0 - Write Bytes: %d", writeBytes));
long readBytes = hardwareAbstractionLayer.getDiskStores().get(0).getReadBytes();
log.warn(String.format("Disk 0 - Read Bytes: %d", readBytes));
long memAvailable = hardwareAbstractionLayer.getMemory().getAvailable();
log.warn(String.format("Memory - Available: %d", memAvailable));
List<String> iostat = PluginHelper.executeCommand("iostat -d 1 1");
AixDiskStat diskStat = processCommandOutput(iostat);

View file

@ -49,25 +49,25 @@ public class LinuxDiskProcLine {
== =====================================
*/
private Long readsCompleted = 0L; // successfully
//private Long readsMerged = 0L;
private Long sectorsRead = 0L; // 512 bytes pr. sector
private Long timeSpentReading = 0L; // ms
private Long writesCompleted = 0L; // successfully
//private Long writesMerged = 0L;
private Long sectorsWritten = 0L; // 512 bytes pr. sector
private Long timeSpentWriting = 0L; // ms
//private Long ioInProgress = 0L;
private Long timeSpentOnIo = 0L; // ms
//private Long timeSpentOnIoWeighted = 0L;
private long readsCompleted; // successfully
//private long readsMerged;
private long sectorsRead; // 512 bytes pr. sector
private long timeSpentReading; // ms
private long writesCompleted; // successfully
//private long writesMerged;
private long sectorsWritten; // 512 bytes pr. sector
private long timeSpentWriting; // ms
//private long ioInProgress;
private long timeSpentOnIo; // ms
//private long timeSpentOnIoWeighted;
//private Long discardsCompleted = 0L; // successfully
//private Long discardsMerged = 0L;
//private Long sectorsDiscarded = 0L; // 512 bytes pr. sector
//private Long timeSpentDiscarding = 0L; // ms
//private long discardsCompleted; // successfully
//private long discardsMerged;
//private long sectorsDiscarded; // 512 bytes pr. sector
//private long timeSpentDiscarding; // ms
//private Long flushRequestsCompleted = 0L;
//private Long timeSpentFlushing = 0L; // ms
//private long flushRequestsCompleted;
//private long timeSpentFlushing; // ms
public LinuxDiskProcLine(List<String> procLines) {

View file

@ -1,90 +0,0 @@
package sysmon.plugins.os_linux;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LinuxNetworkDevProcLine {
private static final Logger log = LoggerFactory.getLogger(LinuxNetworkDevProcLine.class);
private static final Pattern pattern1 = Pattern.compile("^\\s+([a-z]{2,}[0-9]+):.*");
private static final Pattern pattern2 = Pattern.compile("^\\s+([a-z]{2,}[0-9]+):\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)");
private long rxBytes;
private long rxPackets;
private long rxErrs;
private long txBytes;
private long txPackets;
private long txErrs;
/*
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
env2: 657010764 483686 0 0 0 0 0 0 55416850 431020 0 0 0 0 0 0
env3: 6900272 41836 0 0 0 0 0 0 7667444 41849 0 0 0 0 0 0
lo: 3098805 14393 0 0 0 0 0 0 3098805 14393 0 0 0 0 0 0
*/
public LinuxNetworkDevProcLine(List<String> procLines) {
Matcher matcher1;
Matcher matcher2;
for(String procLine : procLines) {
matcher1 = pattern1.matcher(procLine);
if(matcher1.matches()) {
if(matcher1.group(1).equals("lo")) {
continue;
}
matcher2 = pattern2.matcher(procLine);
if(matcher2.matches() && matcher2.groupCount() == 17) {
rxBytes += Long.parseLong(matcher2.group(2));
rxPackets += Long.parseLong(matcher2.group(3));
rxErrs += Long.parseLong(matcher2.group(4));
txBytes += Long.parseLong(matcher2.group(10));
txPackets += Long.parseLong(matcher2.group(11));
txErrs += Long.parseLong(matcher2.group(12));
}
}
}
}
public long getRxBytes() {
return rxBytes;
}
public long getRxPackets() {
return rxPackets;
}
public long getRxErrs() {
return rxErrs;
}
public long getTxBytes() {
return txBytes;
}
public long getTxPackets() {
return txPackets;
}
public long getTxErrs() {
return txErrs;
}
}

View file

@ -1,21 +1,91 @@
package sysmon.plugins.os_linux;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LinuxNetworkDevStat {
private final long rxBytes;
private final long rxPackets;
private final long txBytes;
private final long txPackets;
private static final Logger log = LoggerFactory.getLogger(LinuxNetworkDevStat.class);
private static final Pattern pattern1 = Pattern.compile("^\\s+([a-z]{2,}[0-9]+):.*");
private static final Pattern pattern2 = Pattern.compile("^\\s+([a-z]{2,}[0-9]+):\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)");
private long rxBytes;
private long rxPackets;
private long rxErrs;
private long txBytes;
private long txPackets;
private long txErrs;
/*
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
env2: 657010764 483686 0 0 0 0 0 0 55416850 431020 0 0 0 0 0 0
env3: 6900272 41836 0 0 0 0 0 0 7667444 41849 0 0 0 0 0 0
lo: 3098805 14393 0 0 0 0 0 0 3098805 14393 0 0 0 0 0 0
*/
public LinuxNetworkDevStat(List<String> procLines) {
Matcher matcher1;
Matcher matcher2;
for(String procLine : procLines) {
matcher1 = pattern1.matcher(procLine);
if(matcher1.matches()) {
if(matcher1.group(1).equals("lo")) {
continue;
}
matcher2 = pattern2.matcher(procLine);
if(matcher2.matches() && matcher2.groupCount() == 17) {
rxBytes += Long.parseLong(matcher2.group(2));
rxPackets += Long.parseLong(matcher2.group(3));
rxErrs += Long.parseLong(matcher2.group(4));
txBytes += Long.parseLong(matcher2.group(10));
txPackets += Long.parseLong(matcher2.group(11));
txErrs += Long.parseLong(matcher2.group(12));
}
}
}
}
public LinuxNetworkDevStat(LinuxNetworkDevProcLine previous, LinuxNetworkDevProcLine current) {
rxBytes = current.getRxBytes() - previous.getRxBytes();
rxPackets = current.getRxPackets() - previous.getRxPackets();
txBytes = current.getTxBytes() - previous.getTxBytes();
txPackets = current.getTxPackets() - previous.getTxPackets();
public long getRxBytes() {
return rxBytes;
}
public long getRxPackets() {
return rxPackets;
}
public long getRxErrs() {
return rxErrs;
}
public long getTxBytes() {
return txBytes;
}
public long getTxPackets() {
return txPackets;
}
public long getTxErrs() {
return txErrs;
}
@ -32,5 +102,4 @@ public class LinuxNetworkDevStat {
return fields;
}
}

View file

@ -45,23 +45,12 @@ public class LinuxNetworkExtension implements MetricExtension {
@Override
public MetricResult getMetrics() {
// LinuxNetworkDevStat = 2 x reading from /proc/net/dev ?
LinuxNetworkDevProcLine proc1 = processDevOutput(PluginHelper.readFile("/proc/net/dev"));
try {
Thread.sleep(1 * 1000); // TODO: Configure sample collect time
} catch (InterruptedException e) {
log.warn("getMetrics() - sleep interrupted");
return null;
}
LinuxNetworkDevProcLine proc2 = processDevOutput(PluginHelper.readFile("/proc/net/dev"));
// LinuxNetworkSockStat = 1 x reading from /proc/net/sockstats
LinuxNetworkSockStat stat = processSockOutput(PluginHelper.readFile("/proc/net/sockstat"));
Map<String, String> tagsMap = stat.getTags();
Map<String, Object> fieldsMap = stat.getFields();
LinuxNetworkSockStat sockStat = processSockOutput(PluginHelper.readFile("/proc/net/sockstat"));
LinuxNetworkDevStat devStat = processDevOutput(PluginHelper.readFile("/proc/net/dev"));
Map<String, String> tagsMap = sockStat.getTags();
Map<String, Object> fieldsMap = sockStat.getFields();
fieldsMap.putAll(devStat.getFields());
return new MetricResult("network", new Measurement(tagsMap, fieldsMap));
}
@ -70,8 +59,8 @@ public class LinuxNetworkExtension implements MetricExtension {
return new LinuxNetworkSockStat(inputLines);
}
protected LinuxNetworkDevProcLine processDevOutput(List<String> inputLines) {
return new LinuxNetworkDevProcLine(inputLines);
protected LinuxNetworkDevStat processDevOutput(List<String> inputLines) {
return new LinuxNetworkDevStat(inputLines);
}

View file

@ -1,8 +1,4 @@
import spock.lang.Specification
import sysmon.plugins.os_linux.LinuxDiskExtension
import sysmon.plugins.os_linux.LinuxDiskProcLine
import sysmon.plugins.os_linux.LinuxDiskStat
import sysmon.plugins.os_linux.LinuxNetworkDevProcLine
import sysmon.plugins.os_linux.LinuxNetworkDevStat
import sysmon.plugins.os_linux.LinuxNetworkExtension
import sysmon.plugins.os_linux.LinuxNetworkSockStat
@ -40,7 +36,7 @@ class LinuxNetworkTest extends Specification {
when:
LinuxNetworkExtension extension = new LinuxNetworkExtension()
LinuxNetworkDevProcLine procLine = extension.processDevOutput(lines)
LinuxNetworkDevStat procLine = extension.processDevOutput(lines)
then:
procLine.getRxBytes() == 663911036L
@ -51,14 +47,15 @@ class LinuxNetworkTest extends Specification {
procLine.getTxErrs() == 0L
}
/*
void "test dev utilization"() {
setup:
def testFile1 = new File(getClass().getResource('/proc_net_dev1.txt').toURI())
def testFile2 = new File(getClass().getResource('/proc_net_dev2.txt').toURI())
LinuxNetworkExtension extension = new LinuxNetworkExtension()
LinuxNetworkDevProcLine procLine1 = extension.processDevOutput(testFile1.readLines())
LinuxNetworkDevProcLine procLine2 = extension.processDevOutput(testFile2.readLines())
LinuxNetworkDevStat procLine1 = extension.processDevOutput(testFile1.readLines())
LinuxNetworkDevStat procLine2 = extension.processDevOutput(testFile2.readLines())
when:
LinuxNetworkDevStat networkDevStat = new LinuxNetworkDevStat(procLine1, procLine2)
@ -68,7 +65,6 @@ class LinuxNetworkTest extends Specification {
networkDevStat.getFields().get("rxBytes") == 31501L
networkDevStat.getFields().get("txBytes") == 46460L
networkDevStat.getFields().get("txPackets") == 341L
}
}*/
}