
com.icthh.xm.commons.metric.OperatingSystemGaugeSet Maven / Gradle / Ivy
package com.icthh.xm.commons.metric;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricSet;
import javax.management.Attribute;
import javax.management.InstanceNotFoundException;
import javax.management.ReflectionException;
import java.lang.management.OperatingSystemMXBean;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static java.lang.management.ManagementFactory.getPlatformMBeanServer;
import static java.util.Collections.emptyList;
/**
* A set of gauges for operating system settings.
*/
public class OperatingSystemGaugeSet implements MetricSet {
private final OperatingSystemMXBean operatingSystemMXBean;
public OperatingSystemGaugeSet(OperatingSystemMXBean mxBean) {
this.operatingSystemMXBean = mxBean;
}
@Override
public Map getMetrics() {
final Map gauges = new HashMap<>();
gauges.put("committedVirtualMemorySize", (Gauge) () -> invokeLong("CommittedVirtualMemorySize"));
gauges.put("totalSwapSpaceSize", (Gauge) () -> invokeLong("TotalSwapSpaceSize"));
gauges.put("freeSwapSpaceSize", (Gauge) () -> invokeLong("FreeSwapSpaceSize"));
gauges.put("processCpuTime", (Gauge) () -> invokeLong("ProcessCpuTime"));
gauges.put("freePhysicalMemorySize", (Gauge) () -> invokeLong("FreePhysicalMemorySize"));
gauges.put("totalPhysicalMemorySize", (Gauge) () -> invokeLong("TotalPhysicalMemorySize"));
gauges.put("fileDescriptor.usage", (Gauge) () -> invokeRatio("OpenFileDescriptorCount", "MaxFileDescriptorCount"));
gauges.put("systemCpuLoad", (Gauge) () -> invokeDouble("SystemCpuLoad"));
gauges.put("processCpuLoad", (Gauge) () -> invokeDouble("ProcessCpuLoad"));
return gauges;
}
private Optional getOSAttribute(String attributeName) {
String[] attributesNames = {attributeName};
List attributes;
try {
attributes = getPlatformMBeanServer().getAttributes(operatingSystemMXBean.getObjectName(), attributesNames)
.asList();
} catch (ReflectionException | InstanceNotFoundException ex) {
attributes = emptyList();
}
return attributes.stream().filter(it -> attributeName.equals(it.getName())).findAny();
}
private long invokeLong(String attributeName) {
return getOSAttribute(attributeName).map(value -> (long) value.getValue()).orElse(0L);
}
private double invokeDouble(String attributeName) {
return getOSAttribute(attributeName).map(value -> (double) value.getValue()).orElse(0d);
}
private double invokeRatio(String numeratorAttributeName, String denominatorAttributeName) {
Optional numerator = getOSAttribute(numeratorAttributeName).map(it -> (long) it.getValue());
Optional denominator = getOSAttribute(denominatorAttributeName).map(it -> (long) it.getValue())
.filter(it -> it != 0);
return numerator.map(numValue -> denominator.map(denValue -> 1.0 * numValue / denValue)
.orElse(Double.NaN))
.orElse(Double.NaN);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy