2021-06-10 06:55:03 +00:00
|
|
|
package sysmon.plugins.os_linux;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import sysmon.shared.Measurement;
|
|
|
|
import sysmon.shared.MetricExtension;
|
|
|
|
import sysmon.shared.MetricResult;
|
|
|
|
import sysmon.shared.PluginHelper;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2021-09-02 19:40:22 +00:00
|
|
|
import java.util.HashMap;
|
2021-06-10 06:55:03 +00:00
|
|
|
import java.util.Map;
|
2022-11-07 12:33:35 +00:00
|
|
|
import java.util.TreeMap;
|
2021-06-10 06:55:03 +00:00
|
|
|
|
2021-06-11 00:21:32 +00:00
|
|
|
// Disabled
|
|
|
|
//@Extension
|
2021-06-10 06:55:03 +00:00
|
|
|
public class LinuxNetstatExtension implements MetricExtension {
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(LinuxNetstatExtension.class);
|
|
|
|
|
2021-09-10 10:15:33 +00:00
|
|
|
// Extension details
|
|
|
|
private final String name = "linux_network_netstat";
|
|
|
|
private final String provides = "network_netstat";
|
|
|
|
private final String description = "Linux Netstat Metrics";
|
|
|
|
|
|
|
|
// Configuration / Options
|
|
|
|
private boolean enabled = true;
|
2021-11-06 19:32:54 +00:00
|
|
|
private boolean threaded = false;
|
2021-09-10 10:15:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isEnabled() {
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
2021-11-06 19:32:54 +00:00
|
|
|
@Override
|
|
|
|
public boolean isThreaded() {
|
|
|
|
return threaded;
|
|
|
|
}
|
|
|
|
|
2021-06-10 06:55:03 +00:00
|
|
|
@Override
|
|
|
|
public boolean isSupported() {
|
|
|
|
|
|
|
|
if(!System.getProperty("os.name").toLowerCase().contains("linux")) {
|
|
|
|
log.warn("Requires Linux.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-16 11:34:51 +00:00
|
|
|
if(PluginHelper.notExecutable("netstat")) {
|
2021-06-10 06:55:03 +00:00
|
|
|
log.warn("Requires the 'netstat' command.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
2021-09-10 10:15:33 +00:00
|
|
|
return name;
|
2021-06-10 06:55:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-06 13:08:52 +00:00
|
|
|
@Override
|
|
|
|
public String getInterval() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-06-10 06:55:03 +00:00
|
|
|
@Override
|
|
|
|
public String getProvides() {
|
2021-09-10 10:15:33 +00:00
|
|
|
return provides;
|
2021-06-10 06:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDescription() {
|
2021-09-10 10:15:33 +00:00
|
|
|
return description;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setConfiguration(Map<String, Object> map) {
|
|
|
|
if (map.containsKey("enabled")) {
|
|
|
|
enabled = (boolean) map.get("enabled");
|
|
|
|
}
|
2021-11-06 19:32:54 +00:00
|
|
|
if(map.containsKey("threaded")) {
|
|
|
|
threaded = (boolean) map.get("threaded");
|
|
|
|
}
|
2021-06-10 06:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public MetricResult getMetrics() throws Exception {
|
|
|
|
|
2022-11-07 12:33:35 +00:00
|
|
|
TreeMap<String, String> tagsMap;
|
|
|
|
TreeMap<String, Object> fieldsMap;
|
2021-06-10 06:55:03 +00:00
|
|
|
|
|
|
|
try (InputStream inputStream = PluginHelper.executeCommand("netstat -s")) {
|
|
|
|
LinuxNetstatParser parser = processCommandOutput(inputStream);
|
|
|
|
tagsMap = parser.getTags();
|
|
|
|
fieldsMap = parser.getFields();
|
|
|
|
}
|
|
|
|
|
2021-09-10 10:15:33 +00:00
|
|
|
return new MetricResult(name, new Measurement(tagsMap, fieldsMap));
|
2021-06-10 06:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected LinuxNetstatParser processCommandOutput(InputStream input) throws IOException {
|
|
|
|
return new LinuxNetstatParser(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|