public void sense()

in metrics-core/src/main/java/software/amazon/swage/metrics/jmx/sensor/OperatingSystemSensor.java [60:100]


    public void sense(final MetricContext metricContext)
    throws SenseException
    {
        OperatingSystemMXBean mxBean = ManagementFactory.getOperatingSystemMXBean();

        double load = mxBean.getSystemLoadAverage();
        if (load >= 0) {
            metricContext.record(SYSTEM_LOAD, load, Unit.NONE);
        }


        // This is the same bean, but the attributes we want aren't on the
        // OperatingSystemMXBean interface.
        ObjectName on;
        try {
            on = new ObjectName(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME);
        } catch (MalformedObjectNameException e) {
            // This should never happen
            throw new IllegalStateException("JVM-provided constant name is malformed");
        }

        final MBeanServerConnection connection = ManagementFactory.getPlatformMBeanServer();
        try {
            //TODO: find a better way to do this
            Long max = (Long) connection.getAttribute(on, "MaxFileDescriptorCount");
            metricContext.record(FILE_DESCRIPTORS_MAX, max, Unit.NONE);

            Long open = (Long) connection.getAttribute(on, "OpenFileDescriptorCount");
            metricContext.record(FILE_DESCRIPTORS_OPEN, open, Unit.NONE);

            if (open >= 0) {
                double use_percent = 100.0 * ((double) open / (double) max);
                metricContext.record(FILE_DESCRIPTORS_USED, use_percent, Unit.PERCENT);
            }
        }
        catch (MBeanException|AttributeNotFoundException|InstanceNotFoundException|ReflectionException|IOException e)
        {
            throw new SenseException("Failure reading OpenFiledDescriptorCount from OS MBean", e);
        }

    }