use of java.lang.management.OperatingSystemMXBean in project flink by apache.
the class MetricUtils method instantiateCPUMetrics.
private static void instantiateCPUMetrics(MetricGroup metrics) {
try {
final OperatingSystemMXBean mxBean = ManagementFactory.getOperatingSystemMXBean();
final Method fetchCPULoadMethod = Class.forName("com.sun.management.OperatingSystemMXBean").getMethod("getProcessCpuLoad");
// verify that we can invoke the method
fetchCPULoadMethod.invoke(mxBean);
final Method fetchCPUTimeMethod = Class.forName("com.sun.management.OperatingSystemMXBean").getMethod("getProcessCpuTime");
// verify that we can invoke the method
fetchCPUTimeMethod.invoke(mxBean);
metrics.gauge("Load", new Gauge<Double>() {
@Override
public Double getValue() {
try {
return (Double) fetchCPULoadMethod.invoke(mxBean);
} catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException ignored) {
return -1.0;
}
}
});
metrics.gauge("Time", new Gauge<Long>() {
@Override
public Long getValue() {
try {
return (Long) fetchCPUTimeMethod.invoke(mxBean);
} catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException ignored) {
return -1L;
}
}
});
} catch (ClassNotFoundException | InvocationTargetException | SecurityException | NoSuchMethodException | IllegalArgumentException | IllegalAccessException ignored) {
LOG.warn("Cannot access com.sun.management.OperatingSystemMXBean.getProcessCpuLoad()" + " - CPU load metrics will not be available.");
// make sure that a metric still exists for the given name
metrics.gauge("Load", new Gauge<Double>() {
@Override
public Double getValue() {
return -1.0;
}
});
metrics.gauge("Time", new Gauge<Long>() {
@Override
public Long getValue() {
return -1L;
}
});
}
}
use of java.lang.management.OperatingSystemMXBean in project opennms by OpenNMS.
the class OSReportPlugin method getEntries.
@Override
public Map<String, Resource> getEntries() {
final Map<String, Resource> map = new TreeMap<String, Resource>();
map.put("Name", getResourceFromProperty("os.name"));
map.put("Architecture", getResourceFromProperty("os.arch"));
map.put("Version", getResourceFromProperty("os.version"));
map.put("Distribution", map.get("Name"));
OperatingSystemMXBean osBean = getBean(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME, OperatingSystemMXBean.class);
if (osBean == null) {
LOG.info("falling back to local VM OperatingSystemMXBean");
osBean = ManagementFactory.getOperatingSystemMXBean();
}
LOG.trace("bean = {}", osBean);
addGetters(osBean, map);
File lsb = new File("/bin/lsb_release");
File solaris = new File("/var/sadm/softinfo/INST_RELEASE");
if (lsb.exists()) {
final String text = slurpCommand(new String[] { "/bin/lsb_release", "-a" });
final Map<String, String> distMap = splitMultilineString(": +", text);
for (final Map.Entry<String, String> entry : distMap.entrySet()) {
map.put("Distribution " + entry.getKey(), getResource(entry.getValue()));
}
} else if (solaris.exists()) {
map.put("Distribution OS", getResource("Solaris"));
final String solarisText = slurp(solaris);
final Map<String, String> distMap = splitMultilineString("=", solarisText);
for (final Map.Entry<String, String> entry : distMap.entrySet()) {
map.put("Distribution " + entry.getKey().toLowerCase().replaceFirst("^.", entry.getKey().substring(0, 1).toUpperCase()), getResource(entry.getValue()));
}
if (map.containsKey("Distribution OS")) {
map.put("Distribution", map.remove("Distribution OS"));
}
File isainfo = new File("/usr/bin/isainfo");
if (isainfo.exists()) {
final String arch = slurpCommand(new String[] { "/usr/bin/isainfo", "-n" });
if (arch != null) {
map.put("Instruction Set", getResource(arch));
}
}
} else {
for (final Map.Entry<String, String> entry : m_oses.entrySet()) {
final String description = slurp(new File(entry.getKey()));
if (description != null) {
map.put("Distribution", getResource(entry.getValue()));
map.put("Description", getResource(description.trim()));
}
}
}
if (map.containsKey("Distribution Distributor ID")) {
map.put("Distribution", map.remove("Distribution Distributor ID"));
}
if (map.containsKey("Distribution Description")) {
map.put("Description", map.remove("Distribution Description"));
}
return map;
}
use of java.lang.management.OperatingSystemMXBean in project jdk8u_jdk by JetBrains.
the class MXBeanInteropTest1 method doOperatingSystemMXBeanTest.
private final int doOperatingSystemMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0;
System.out.println("---- OperatingSystemMXBean");
try {
ObjectName operationName = new ObjectName(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME);
MBeanInfo mbInfo = mbsc.getMBeanInfo(operationName);
errorCount += checkNonEmpty(mbInfo);
System.out.println("getMBeanInfo\t\t" + mbInfo);
OperatingSystemMXBean operation = null;
operation = JMX.newMXBeanProxy(mbsc, operationName, OperatingSystemMXBean.class);
System.out.println("getArch\t\t" + operation.getArch());
System.out.println("getAvailableProcessors\t\t" + operation.getAvailableProcessors());
System.out.println("getName\t\t" + operation.getName());
System.out.println("getVersion\t\t" + operation.getVersion());
System.out.println("---- OK\n");
} catch (Exception e) {
Utils.printThrowable(e, true);
errorCount++;
System.out.println("---- ERROR\n");
}
return errorCount;
}
use of java.lang.management.OperatingSystemMXBean in project dubbo by alibaba.
the class LoadStatusChecker method check.
public Status check() {
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
double load;
try {
Method method = OperatingSystemMXBean.class.getMethod("getSystemLoadAverage", new Class<?>[0]);
load = (Double) method.invoke(operatingSystemMXBean, new Object[0]);
} catch (Throwable e) {
load = -1;
}
int cpu = operatingSystemMXBean.getAvailableProcessors();
return new Status(load < 0 ? Status.Level.UNKNOWN : (load < cpu ? Status.Level.OK : Status.Level.WARN), "Load: " + load + " / CPU: " + cpu);
}
use of java.lang.management.OperatingSystemMXBean in project gerrit by GerritCodeReview.
the class GetSummary method getJvmSummary.
private JvmSummaryInfo getJvmSummary() {
OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
JvmSummaryInfo jvmSummary = new JvmSummaryInfo();
jvmSummary.vmVendor = runtimeBean.getVmVendor();
jvmSummary.vmName = runtimeBean.getVmName();
jvmSummary.vmVersion = runtimeBean.getVmVersion();
jvmSummary.osName = osBean.getName();
jvmSummary.osVersion = osBean.getVersion();
jvmSummary.osArch = osBean.getArch();
jvmSummary.user = System.getProperty("user.name");
try {
jvmSummary.host = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
// Ignored
}
jvmSummary.currentWorkingDirectory = path(Paths.get(".").toAbsolutePath().getParent());
jvmSummary.site = path(sitePath);
return jvmSummary;
}
Aggregations