Search in sources :

Example 1 with OperatingSystemMXBean

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;
            }
        });
    }
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Gauge(org.apache.flink.metrics.Gauge) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 2 with OperatingSystemMXBean

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;
}
Also used : Resource(org.springframework.core.io.Resource) TreeMap(java.util.TreeMap) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 3 with OperatingSystemMXBean

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;
}
Also used : MBeanInfo(javax.management.MBeanInfo) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) ObjectName(javax.management.ObjectName)

Example 4 with OperatingSystemMXBean

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);
}
Also used : Status(com.alibaba.dubbo.common.status.Status) Method(java.lang.reflect.Method) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 5 with OperatingSystemMXBean

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;
}
Also used : UnknownHostException(java.net.UnknownHostException) RuntimeMXBean(java.lang.management.RuntimeMXBean) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Aggregations

OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)89 RuntimeMXBean (java.lang.management.RuntimeMXBean)27 Method (java.lang.reflect.Method)20 IOException (java.io.IOException)15 MemoryMXBean (java.lang.management.MemoryMXBean)12 MemoryUsage (java.lang.management.MemoryUsage)8 ThreadMXBean (java.lang.management.ThreadMXBean)8 HashMap (java.util.HashMap)8 UnixOperatingSystemMXBean (com.sun.management.UnixOperatingSystemMXBean)7 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)6 Status (com.alibaba.dubbo.common.status.Status)5 File (java.io.File)5 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)5 Test (org.junit.Test)5 Test (org.testng.annotations.Test)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 UnknownHostException (java.net.UnknownHostException)4 Date (java.util.Date)4 ExtendedOperatingSystemMXBeanImpl (com.ibm.lang.management.internal.ExtendedOperatingSystemMXBeanImpl)3