Search in sources :

Example 6 with AttributeList

use of javax.management.AttributeList in project jmxtrans by jmxtrans.

the class JmxResultProcessorTest method canReadTabularData.

@Test
public void canReadTabularData() throws MalformedObjectNameException, AttributeNotFoundException, MBeanException, ReflectionException, InstanceNotFoundException {
    ObjectInstance runtime = getRuntime();
    AttributeList attr = ManagementFactory.getPlatformMBeanServer().getAttributes(runtime.getObjectName(), new String[] { "SystemProperties" });
    List<Result> results = new JmxResultProcessor(dummyQueryWithResultAlias(), runtime, attr.asList(), runtime.getClassName(), TEST_DOMAIN_NAME).getResults();
    assertThat(results.size()).isGreaterThan(2);
    Optional<Result> result = from(results).firstMatch(new ByAttributeName("SystemProperties.java.version"));
    assertThat(result.isPresent()).isTrue();
    assertThat(result.get().getAttributeName()).isEqualTo("SystemProperties.java.version");
    Map<String, Object> values = result.get().getValues();
    assertThat(values).hasSize(2);
    Object objectValue = result.get().getValues().get("key");
    assertThat(objectValue).isInstanceOf(String.class);
    String key = (String) objectValue;
    assertThat(key).isEqualTo("java.version");
}
Also used : AttributeList(javax.management.AttributeList) ObjectInstance(javax.management.ObjectInstance) JmxResultProcessor(com.googlecode.jmxtrans.model.JmxResultProcessor) Result(com.googlecode.jmxtrans.model.Result) Test(org.junit.Test)

Example 7 with AttributeList

use of javax.management.AttributeList in project jmxtrans by jmxtrans.

the class TreeWalker method walkTree.

public void walkTree(MBeanServerConnection connection) throws Exception {
    // key here is null, null returns everything!
    Set<ObjectName> mbeans = connection.queryNames(null, null);
    for (ObjectName name : mbeans) {
        MBeanInfo info = connection.getMBeanInfo(name);
        MBeanAttributeInfo[] attrs = info.getAttributes();
        String[] attrNames = new String[attrs.length];
        for (int i = 0; i < attrs.length; i++) {
            attrNames[i] = attrs[i].getName();
        }
        try {
            AttributeList attributes = connection.getAttributes(name, attrNames);
            for (Attribute attribute : attributes.asList()) {
                output(name.getCanonicalName() + "%" + attribute.getName(), attribute.getValue());
            }
        } catch (Exception e) {
            log.error("error getting " + name + ":" + e.getMessage(), e);
        }
    }
}
Also used : MBeanInfo(javax.management.MBeanInfo) Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) IOException(java.io.IOException) ObjectName(javax.management.ObjectName)

Example 8 with AttributeList

use of javax.management.AttributeList in project neo4j by neo4j.

the class Neo4jManager method getConfiguration.

public Map<String, Object> getConfiguration() {
    final String[] keys;
    final AttributeList attributes;
    try {
        MBeanAttributeInfo[] keyInfo = server.getMBeanInfo(config).getAttributes();
        keys = new String[keyInfo.length];
        for (int i = 0; i < keys.length; i++) {
            keys[i] = keyInfo[i].getName();
        }
        attributes = server.getAttributes(config, keys);
    } catch (Exception e) {
        throw new IllegalStateException("Could not access the configuration bean", e);
    }
    Map<String, Object> configuration = new HashMap<>();
    for (int i = 0; i < keys.length; i++) {
        configuration.put(keys[i], attributes.get(i));
    }
    return configuration;
}
Also used : HashMap(java.util.HashMap) AttributeList(javax.management.AttributeList) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) AttributeNotFoundException(javax.management.AttributeNotFoundException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException)

Example 9 with AttributeList

use of javax.management.AttributeList in project twitter4j by yusuke.

the class MBeansTest method testAPIStatisticsOpenMBean.

/**
     * Tests exposure of API statistics via a dynamic MBean
     */
public void testAPIStatisticsOpenMBean() throws Exception {
    APIStatistics stats = new APIStatistics(5);
    APIStatisticsOpenMBean openMBean = new APIStatisticsOpenMBean(stats);
    // sanity check to ensure metadata accurately describes dynamic attributes
    MBeanInfo info = openMBean.getMBeanInfo();
    assertEquals(5, info.getAttributes().length);
    assertEquals(1, info.getOperations().length);
    List<String> attrNames = new ArrayList<String>();
    for (MBeanAttributeInfo attr : info.getAttributes()) {
        assertNotNull(openMBean.getAttribute(attr.getName()));
        attrNames.add(attr.getName());
    }
    AttributeList attrList = openMBean.getAttributes(attrNames.toArray(new String[attrNames.size()]));
    assertNotNull(attrList);
    assertEquals(5, attrList.size());
    // check stats (empty case)
    Long callCount = (Long) openMBean.getAttribute("callCount");
    assertEquals(0, callCount.longValue());
    Long errorCount = (Long) openMBean.getAttribute("errorCount");
    assertEquals(0, callCount.longValue());
    Long totalTime = (Long) openMBean.getAttribute("totalTime");
    assertEquals(0, totalTime.longValue());
    Long averageTime = (Long) openMBean.getAttribute("averageTime");
    assertEquals(0, averageTime.longValue());
    // check table (empty case)
    TabularData table = (TabularData) openMBean.getAttribute("statisticsTable");
    assertTrue(table.isEmpty());
    stats.methodCalled("foo", 100, true);
    // check stats (populated case)
    callCount = (Long) openMBean.getAttribute("callCount");
    assertEquals(1, callCount.longValue());
    errorCount = (Long) openMBean.getAttribute("errorCount");
    assertEquals(0, errorCount.longValue());
    totalTime = (Long) openMBean.getAttribute("totalTime");
    assertEquals(100, totalTime.longValue());
    averageTime = (Long) openMBean.getAttribute("averageTime");
    assertEquals(100, averageTime.longValue());
    // check table (populated  case)
    table = (TabularData) openMBean.getAttribute("statisticsTable");
    assertFalse(table.isEmpty());
    assertEquals(1, table.keySet().size());
    CompositeData data = table.get(new Object[] { "foo" });
    assertNotNull(data);
    String[] columnNames = new String[] { "methodName", "callCount", "totalTime", "avgTime" };
    Object[] columnValues = data.getAll(columnNames);
    assertEquals(columnNames.length, columnValues.length);
    assertEquals("foo", columnValues[0]);
    assertEquals(1, ((Long) columnValues[1]).longValue());
    assertEquals(100, ((Long) columnValues[2]).longValue());
    assertEquals(100, ((Long) columnValues[3]).longValue());
    // check reset
    openMBean.invoke("reset", new Object[0], new String[0]);
    checkCalculator(stats, 0, 0, 0, 0);
    assertFalse(stats.getInvocationStatistics().iterator().hasNext());
}
Also used : MBeanInfo(javax.management.MBeanInfo) AttributeList(javax.management.AttributeList) CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) TabularData(javax.management.openmbean.TabularData)

Example 10 with AttributeList

use of javax.management.AttributeList in project midpoint by Evolveum.

the class SystemInfoPanel method fillCpuUsage.

private void fillCpuUsage(SystemInfoDto dto) throws Exception {
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem");
    AttributeList list = mbs.getAttributes(name, new String[] { "ProcessCpuLoad" });
    if (list.isEmpty()) {
        dto.cpuUsage = Double.NaN;
        return;
    }
    Attribute att = (Attribute) list.get(0);
    Double value = (Double) att.getValue();
    if (value == -1.0) {
        // usually takes a couple of seconds before we get real values
        dto.cpuUsage = Double.NaN;
        return;
    }
    dto.cpuUsage = ((int) (value * 1000) / 10.0);
}
Also used : Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Aggregations

AttributeList (javax.management.AttributeList)56 Attribute (javax.management.Attribute)46 ReflectionException (javax.management.ReflectionException)22 AttributeNotFoundException (javax.management.AttributeNotFoundException)16 InstanceNotFoundException (javax.management.InstanceNotFoundException)16 MBeanException (javax.management.MBeanException)15 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)14 ObjectName (javax.management.ObjectName)14 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)10 MBeanServer (javax.management.MBeanServer)8 IOException (java.io.IOException)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 MBeanInfo (javax.management.MBeanInfo)7 RuntimeOperationsException (javax.management.RuntimeOperationsException)7 HashMap (java.util.HashMap)6 MalformedObjectNameException (javax.management.MalformedObjectNameException)6 ListenerNotFoundException (javax.management.ListenerNotFoundException)5 MBeanRegistrationException (javax.management.MBeanRegistrationException)5 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)5 ArrayList (java.util.ArrayList)4