Initial work on power readings.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Mark Nellemann 2023-03-15 16:21:07 +01:00
parent fb94b9e563
commit 11a22e84ba
3 changed files with 113 additions and 3 deletions

View file

@ -1,4 +1,4 @@
version = 1.1.2
version = 1.1.3
pf4jVersion = 3.7.0
slf4jVersion = 2.0.6
camelVersion = 3.14.7

View file

@ -0,0 +1,112 @@
package sysmon.plugins.base;
import org.pf4j.Extension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.hardware.PowerSource;
import sysmon.shared.Measurement;
import sysmon.shared.MetricExtension;
import sysmon.shared.MetricResult;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
@Extension
public class BasePowerExtension implements MetricExtension {
private static final Logger log = LoggerFactory.getLogger(BasePowerExtension.class);
// Extension details
private final String name = "base_power";
private final String description = "Base Power Metrics";
// Configuration / Options
private boolean enabled = true;
private boolean threaded = false;
private String interval = "30s";
private HardwareAbstractionLayer hardwareAbstractionLayer;
private List<PowerSource> powerSources;
@Override
public boolean isEnabled() {
return enabled;
}
@Override
public boolean isThreaded() {
return threaded;
}
@Override
public boolean isSupported() {
hardwareAbstractionLayer = BasePlugin.getHardwareAbstractionLayer();
return hardwareAbstractionLayer != null;
}
@Override
public String getName() {
return name;
}
@Override
public String getInterval() {
return interval;
}
@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("threaded")) {
threaded = (boolean) map.get("threaded");
}
if (map.containsKey("interval")) {
interval = (String) map.get("interval");
}
}
@Override
public MetricResult getMetrics() {
if(powerSources == null) {
powerSources = hardwareAbstractionLayer.getPowerSources();
}
ArrayList<Measurement> measurementList = new ArrayList<>();
powerSources.forEach((source) -> {
log.info("name: {}", source.getName());
log.info("amp: {}", source.getAmperage());
log.info("voltage: {}", source.getVoltage());
TreeMap<String, String> tagsMap = new TreeMap<String, String>() {
{
put("name", source.getName());
}
};
TreeMap<String, Object> fieldsMap = new TreeMap<String, Object>() {
{
put("amperage", source.getAmperage());
put("voltage", source.getVoltage());
}
};
log.debug("getMetrics() - tags: {}, fields: {}", tagsMap, fieldsMap);
measurementList.add(new Measurement(tagsMap, fieldsMap));
});
return new MetricResult(name, measurementList);
}
}

View file

@ -14,5 +14,3 @@ public class PowerPlugin extends Plugin {
}
}