Search in sources :

Example 1 with MBeanInfo

use of javax.management.MBeanInfo in project flink by apache.

the class JMXReporterTest method testMeterReporting.

/**
	 * Tests that meters are properly reported via the JMXReporter.
	 */
@Test
public void testMeterReporting() throws Exception {
    MetricRegistry registry = null;
    String meterName = "meter";
    try {
        Configuration config = new Configuration();
        config.setString(ConfigConstants.METRICS_REPORTERS_LIST, "jmx_test");
        config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "jmx_test." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, JMXReporter.class.getName());
        registry = new MetricRegistry(MetricRegistryConfiguration.fromConfiguration(config));
        TaskManagerMetricGroup metricGroup = new TaskManagerMetricGroup(registry, "localhost", "tmId");
        TestMeter meter = new TestMeter();
        metricGroup.meter(meterName, meter);
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        ObjectName objectName = new ObjectName(JMX_DOMAIN_PREFIX + "taskmanager." + meterName, JMXReporter.generateJmxTable(metricGroup.getAllVariables()));
        MBeanInfo info = mBeanServer.getMBeanInfo(objectName);
        MBeanAttributeInfo[] attributeInfos = info.getAttributes();
        assertEquals(2, attributeInfos.length);
        assertEquals(meter.getRate(), mBeanServer.getAttribute(objectName, "Rate"));
        assertEquals(meter.getCount(), mBeanServer.getAttribute(objectName, "Count"));
    } finally {
        if (registry != null) {
            registry.shutdown();
        }
    }
}
Also used : TestMeter(org.apache.flink.metrics.util.TestMeter) MetricRegistryConfiguration(org.apache.flink.runtime.metrics.MetricRegistryConfiguration) Configuration(org.apache.flink.configuration.Configuration) MBeanInfo(javax.management.MBeanInfo) MetricRegistry(org.apache.flink.runtime.metrics.MetricRegistry) TaskManagerMetricGroup(org.apache.flink.runtime.metrics.groups.TaskManagerMetricGroup) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 2 with MBeanInfo

use of javax.management.MBeanInfo in project hadoop by apache.

the class MBeanInfoBuilder method get.

MBeanInfo get() {
    curRecNo = 0;
    for (MetricsRecordImpl rec : recs) {
        for (MetricsTag t : rec.tags()) {
            attrs.add(newAttrInfo("tag." + t.name(), t.description(), "java.lang.String"));
        }
        for (AbstractMetric m : rec.metrics()) {
            m.visit(this);
        }
        ++curRecNo;
    }
    MetricsSystemImpl.LOG.debug(attrs);
    MBeanAttributeInfo[] attrsArray = new MBeanAttributeInfo[attrs.size()];
    return new MBeanInfo(name, description, attrs.toArray(attrsArray), null, null, // no ops/ctors/notifications
    null);
}
Also used : MBeanInfo(javax.management.MBeanInfo) AbstractMetric(org.apache.hadoop.metrics2.AbstractMetric) MetricsTag(org.apache.hadoop.metrics2.MetricsTag) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Example 3 with MBeanInfo

use of javax.management.MBeanInfo in project hbase by apache.

the class JSONBean method write.

/**
   * @param mBeanServer
   * @param qry
   * @param attribute
   * @param description
   * @return Return non-zero if failed to find bean. 0
   * @throws IOException
   */
private static int write(final JsonGenerator jg, final MBeanServer mBeanServer, ObjectName qry, String attribute, final boolean description) throws IOException {
    LOG.trace("Listing beans for " + qry);
    Set<ObjectName> names = null;
    names = mBeanServer.queryNames(qry, null);
    jg.writeArrayFieldStart("beans");
    Iterator<ObjectName> it = names.iterator();
    while (it.hasNext()) {
        ObjectName oname = it.next();
        MBeanInfo minfo;
        String code = "";
        String descriptionStr = null;
        Object attributeinfo = null;
        try {
            minfo = mBeanServer.getMBeanInfo(oname);
            code = minfo.getClassName();
            if (description)
                descriptionStr = minfo.getDescription();
            String prs = "";
            try {
                if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
                    prs = "modelerType";
                    code = (String) mBeanServer.getAttribute(oname, prs);
                }
                if (attribute != null) {
                    prs = attribute;
                    attributeinfo = mBeanServer.getAttribute(oname, prs);
                }
            } catch (RuntimeMBeanException e) {
                // so no need to log them as errors all the time.
                if (e.getCause() instanceof UnsupportedOperationException) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Getting attribute " + prs + " of " + oname + " threw " + e);
                    }
                } else {
                    LOG.error("Getting attribute " + prs + " of " + oname + " threw an exception", e);
                }
                return 0;
            } catch (AttributeNotFoundException e) {
                // If the modelerType attribute was not found, the class name is used
                // instead.
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            } catch (MBeanException e) {
                // The code inside the attribute getter threw an exception so log it,
                // and fall back on the class name
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            } catch (RuntimeException e) {
                // For some reason even with an MBeanException available to them
                // Runtime exceptionscan still find their way through, so treat them
                // the same as MBeanException
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            } catch (ReflectionException e) {
                // This happens when the code inside the JMX bean (setter?? from the
                // java docs) threw an exception, so log it and fall back on the
                // class name
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            }
        } catch (InstanceNotFoundException e) {
            //Ignored for some reason the bean was not found so don't output it
            continue;
        } catch (IntrospectionException e) {
            // This is an internal error, something odd happened with reflection so
            // log it and don't output the bean.
            LOG.error("Problem while trying to process JMX query: " + qry + " with MBean " + oname, e);
            continue;
        } catch (ReflectionException e) {
            // This happens when the code inside the JMX bean threw an exception, so
            // log it and don't output the bean.
            LOG.error("Problem while trying to process JMX query: " + qry + " with MBean " + oname, e);
            continue;
        }
        jg.writeStartObject();
        jg.writeStringField("name", oname.toString());
        if (description && descriptionStr != null && descriptionStr.length() > 0) {
            jg.writeStringField("description", descriptionStr);
        }
        jg.writeStringField("modelerType", code);
        if (attribute != null && attributeinfo == null) {
            jg.writeStringField("result", "ERROR");
            jg.writeStringField("message", "No attribute with name " + attribute + " was found.");
            jg.writeEndObject();
            jg.writeEndArray();
            jg.close();
            return -1;
        }
        if (attribute != null) {
            writeAttribute(jg, attribute, descriptionStr, attributeinfo);
        } else {
            MBeanAttributeInfo[] attrs = minfo.getAttributes();
            for (int i = 0; i < attrs.length; i++) {
                writeAttribute(jg, mBeanServer, oname, description, attrs[i]);
            }
        }
        jg.writeEndObject();
    }
    jg.writeEndArray();
    return 0;
}
Also used : RuntimeMBeanException(javax.management.RuntimeMBeanException) ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) MBeanInfo(javax.management.MBeanInfo) InstanceNotFoundException(javax.management.InstanceNotFoundException) IntrospectionException(javax.management.IntrospectionException) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ObjectName(javax.management.ObjectName) RuntimeMBeanException(javax.management.RuntimeMBeanException) MBeanException(javax.management.MBeanException)

Example 4 with MBeanInfo

use of javax.management.MBeanInfo in project hive by apache.

the class JMXJsonServlet method listBeans.

// --------------------------------------------------------- Private Methods
private void listBeans(JsonGenerator jg, ObjectName qry, String attribute, HttpServletResponse response) throws IOException {
    LOG.debug("Listing beans for " + qry);
    Set<ObjectName> names = null;
    names = mBeanServer.queryNames(qry, null);
    jg.writeArrayFieldStart("beans");
    Iterator<ObjectName> it = names.iterator();
    while (it.hasNext()) {
        ObjectName oname = it.next();
        MBeanInfo minfo;
        String code = "";
        Object attributeinfo = null;
        try {
            minfo = mBeanServer.getMBeanInfo(oname);
            code = minfo.getClassName();
            String prs = "";
            try {
                if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
                    prs = "modelerType";
                    code = (String) mBeanServer.getAttribute(oname, prs);
                }
                if (attribute != null) {
                    prs = attribute;
                    attributeinfo = mBeanServer.getAttribute(oname, prs);
                }
            } catch (AttributeNotFoundException e) {
                // If the modelerType attribute was not found, the class name is used
                // instead.
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            } catch (MBeanException e) {
                // The code inside the attribute getter threw an exception so log it,
                // and fall back on the class name
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            } catch (RuntimeException e) {
                // For some reason even with an MBeanException available to them
                // Runtime exceptions can still find their way through, so treat them
                // the same as MBeanException
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            } catch (ReflectionException e) {
                // This happens when the code inside the JMX bean (setter?? from the
                // java docs) threw an exception, so log it and fall back on the 
                // class name
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            }
        } catch (InstanceNotFoundException e) {
            //Ignored for some reason the bean was not found so don't output it
            continue;
        } catch (IntrospectionException e) {
            // This is an internal error, something odd happened with reflection so
            // log it and don't output the bean.
            LOG.error("Problem while trying to process JMX query: " + qry + " with MBean " + oname, e);
            continue;
        } catch (ReflectionException e) {
            // This happens when the code inside the JMX bean threw an exception, so
            // log it and don't output the bean.
            LOG.error("Problem while trying to process JMX query: " + qry + " with MBean " + oname, e);
            continue;
        }
        jg.writeStartObject();
        jg.writeStringField("name", oname.toString());
        jg.writeStringField("modelerType", code);
        if ((attribute != null) && (attributeinfo == null)) {
            jg.writeStringField("result", "ERROR");
            jg.writeStringField("message", "No attribute with name " + attribute + " was found.");
            jg.writeEndObject();
            jg.writeEndArray();
            jg.close();
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        if (attribute != null) {
            writeAttribute(jg, attribute, attributeinfo);
        } else {
            MBeanAttributeInfo[] attrs = minfo.getAttributes();
            for (int i = 0; i < attrs.length; i++) {
                writeAttribute(jg, oname, attrs[i]);
            }
        }
        jg.writeEndObject();
    }
    jg.writeEndArray();
}
Also used : ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) MBeanInfo(javax.management.MBeanInfo) InstanceNotFoundException(javax.management.InstanceNotFoundException) IntrospectionException(javax.management.IntrospectionException) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ObjectName(javax.management.ObjectName) RuntimeMBeanException(javax.management.RuntimeMBeanException) MBeanException(javax.management.MBeanException)

Example 5 with MBeanInfo

use of javax.management.MBeanInfo in project hive by apache.

the class TestLegacyMetrics method testMetricsMBean.

@Test
public void testMetricsMBean() throws Exception {
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final ObjectName oname = new ObjectName("org.apache.hadoop.hive.common.metrics:type=MetricsMBean");
    MBeanInfo mBeanInfo = mbs.getMBeanInfo(oname);
    // check implementation class:
    assertEquals(MetricsMBeanImpl.class.getName(), mBeanInfo.getClassName());
    // check reset operation:
    MBeanOperationInfo[] oops = mBeanInfo.getOperations();
    boolean resetFound = false;
    for (MBeanOperationInfo op : oops) {
        if ("reset".equals(op.getName())) {
            resetFound = true;
            break;
        }
    }
    assertTrue(resetFound);
    // add metric with a non-null value:
    Attribute attr = new Attribute("fooMetric", Long.valueOf(-77));
    mbs.setAttribute(oname, attr);
    mBeanInfo = mbs.getMBeanInfo(oname);
    MBeanAttributeInfo[] attrinuteInfos = mBeanInfo.getAttributes();
    assertEquals(1, attrinuteInfos.length);
    boolean attrFound = false;
    for (MBeanAttributeInfo info : attrinuteInfos) {
        if ("fooMetric".equals(info.getName())) {
            assertEquals("java.lang.Long", info.getType());
            assertTrue(info.isReadable());
            assertTrue(info.isWritable());
            assertFalse(info.isIs());
            attrFound = true;
            break;
        }
    }
    assertTrue(attrFound);
    // check metric value:
    Object v = mbs.getAttribute(oname, "fooMetric");
    assertEquals(Long.valueOf(-77), v);
    // reset the bean:
    Object result = mbs.invoke(oname, "reset", new Object[0], new String[0]);
    assertNull(result);
    // the metric value must be zeroed:
    v = mbs.getAttribute(oname, "fooMetric");
    assertEquals(Long.valueOf(0), v);
}
Also used : MBeanInfo(javax.management.MBeanInfo) Attribute(javax.management.Attribute) MBeanOperationInfo(javax.management.MBeanOperationInfo) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Aggregations

MBeanInfo (javax.management.MBeanInfo)121 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)74 ObjectName (javax.management.ObjectName)64 MBeanOperationInfo (javax.management.MBeanOperationInfo)32 MBeanServer (javax.management.MBeanServer)22 Test (org.junit.Test)21 IntrospectionException (javax.management.IntrospectionException)16 ReflectionException (javax.management.ReflectionException)16 ArrayList (java.util.ArrayList)15 Attribute (javax.management.Attribute)14 InstanceNotFoundException (javax.management.InstanceNotFoundException)13 MBeanParameterInfo (javax.management.MBeanParameterInfo)12 MBeanNotificationInfo (javax.management.MBeanNotificationInfo)10 IOException (java.io.IOException)9 AttributeNotFoundException (javax.management.AttributeNotFoundException)9 MalformedObjectNameException (javax.management.MalformedObjectNameException)9 HashMap (java.util.HashMap)8 RuntimeMBeanException (javax.management.RuntimeMBeanException)8 AttributeList (javax.management.AttributeList)7 MBeanConstructorInfo (javax.management.MBeanConstructorInfo)7