Search in sources :

Example 6 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project jcollectd by collectd.

the class MBeanCollector method collect.

public void collect(MBeanQuery query, ObjectName name) throws Exception {
    MBeanServerConnection conn = _sender.getMBeanServerConnection();
    String plugin = query.getPlugin();
    if (plugin == null) {
        plugin = name.getDomain();
    }
    Map<String, MBeanAttributeInfo> attrInfo = null;
    if (_useDescriptors) {
        MBeanInfo info = conn.getMBeanInfo(name);
        attrInfo = new HashMap<String, MBeanAttributeInfo>();
        for (MBeanAttributeInfo ainfo : info.getAttributes()) {
            attrInfo.put(ainfo.getName(), ainfo);
        }
    }
    for (MBeanAttribute attr : query.getAttributes()) {
        String attrName = attr.getAttributeName();
        Object obj;
        try {
            obj = conn.getAttribute(name, attrName);
        } catch (Exception e) {
            //XXX remove attr for future collection e.g. UnsupportedOperation
            continue;
        }
        if (_useDescriptors) {
            //e.g. spring @ManagedMetric(metricType = MetricType.COUNTER)
            try {
                Descriptor descriptor = (Descriptor) _getDescriptor.invoke(attrInfo.get(attrName), (Object[]) null);
                Object type = descriptor.getFieldValue(_metricTypeField);
                if (TypesDB.NAME_COUNTER.equals(type)) {
                    if (attr.getTypeName().equals(TypesDB.NAME_GAUGE)) {
                        attr.setTypeName(TypesDB.NAME_COUNTER);
                    }
                    attr.setDataType(Network.DS_TYPE_COUNTER);
                }
            } catch (Exception e) {
            }
        }
        if (obj instanceof CompositeData) {
            CompositeData data = (CompositeData) obj;
            String key = attr.getCompositeKey();
            if (key == null) {
                //no key specified; collect all
                Set<String> keys = data.getCompositeType().keySet();
                for (String ckey : keys) {
                    obj = data.get(ckey);
                    if (!isNumber(obj)) {
                        continue;
                    }
                    dispatch(query, plugin, attrName + "." + ckey, name, attr, (Number) obj);
                }
                continue;
            } else {
                obj = data.get(key);
            }
        }
        if (!isNumber(obj)) {
            continue;
        }
        dispatch(query, plugin, attr.getName(), name, attr, (Number) obj);
    }
    _sender.flush();
}
Also used : MBeanInfo(javax.management.MBeanInfo) CompositeData(javax.management.openmbean.CompositeData) Descriptor(javax.management.Descriptor) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Example 7 with MBeanServerConnection

use of javax.management.MBeanServerConnection in project druid by alibaba.

the class DruidStat method getDataSourceIds.

@SuppressWarnings("all")
public static List<Integer> getDataSourceIds(Option option) throws Exception {
    String address = loadManagementAgentAndGetAddress(option.getPid());
    JMXServiceURL jmxUrl = new JMXServiceURL(address);
    JMXConnector jmxc = JMXConnectorFactory.connect(jmxUrl);
    MBeanServerConnection jmxConn = jmxc.getMBeanServerConnection();
    List<Map<String, Object>> content = (List<Map<String, Object>>) invokeService(jmxConn, Option.DATA_SOURCE);
    TabledDataPrinter.printDataSourceData(content, option);
    List<Integer> result = new ArrayList<Integer>();
    for (Map<String, Object> dsStat : content) {
        Integer id = (Integer) dsStat.get("Identity");
        result.add(id);
    }
    return result;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) MBeanServerConnection(javax.management.MBeanServerConnection)

Example 8 with MBeanServerConnection

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

the class Server method execute.

public Iterable<Result> execute(Query query) throws Exception {
    JMXConnection jmxConnection = pool.borrowObject(this);
    try {
        ImmutableList.Builder<Result> results = ImmutableList.builder();
        MBeanServerConnection connection = jmxConnection.getMBeanServerConnection();
        for (ObjectName queryName : query.queryNames(connection)) {
            results.addAll(query.fetchResults(connection, queryName));
        }
        pool.returnObject(this, jmxConnection);
        return results.build();
    } catch (Exception e) {
        // since we will invalidate the connection in the pool, prevent connection leaks
        try {
            jmxConnection.close();
        } catch (IOException | RuntimeException re) {
            // drop these, we don't really know what caused the original exception.
            logger.warn("An error occurred trying to close a JMX Connection during error handling.", re);
        }
        pool.invalidateObject(this, jmxConnection);
        throw e;
    }
}
Also used : JMXConnection(com.googlecode.jmxtrans.connections.JMXConnection) ImmutableList(com.google.common.collect.ImmutableList) MBeanServerConnection(javax.management.MBeanServerConnection) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ObjectName(javax.management.ObjectName)

Example 9 with MBeanServerConnection

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

the class ServerTests method testConnectionRepoolingSkippedOnError_andConnectionIsClosed.

@Test
public void testConnectionRepoolingSkippedOnError_andConnectionIsClosed() throws Exception {
    @SuppressWarnings("unchecked") GenericKeyedObjectPool<JmxConnectionProvider, JMXConnection> pool = mock(GenericKeyedObjectPool.class);
    Server server = Server.builder().setHost("host.example.net").setPort("4321").setLocal(true).setPool(pool).build();
    MBeanServerConnection mBeanConn = mock(MBeanServerConnection.class);
    JMXConnection conn = mock(JMXConnection.class);
    when(conn.getMBeanServerConnection()).thenReturn(mBeanConn);
    when(pool.borrowObject(server)).thenReturn(conn);
    Query query = mock(Query.class);
    IOException e = mock(IOException.class);
    when(query.queryNames(mBeanConn)).thenThrow(e);
    try {
        server.execute(query);
        fail("No exception got throws");
    } catch (IOException e2) {
        if (e != e2) {
            fail("Wrong exception thrown (" + e + " instead of mock");
        }
    }
    verify(conn).close();
    verify(pool, never()).returnObject(server, conn);
    InOrder orderVerifier = inOrder(pool);
    orderVerifier.verify(pool).borrowObject(server);
    orderVerifier.verify(pool).invalidateObject(server, conn);
}
Also used : JMXConnection(com.googlecode.jmxtrans.connections.JMXConnection) InOrder(org.mockito.InOrder) JmxConnectionProvider(com.googlecode.jmxtrans.connections.JmxConnectionProvider) IOException(java.io.IOException) MBeanServerConnection(javax.management.MBeanServerConnection) Test(org.junit.Test)

Example 10 with MBeanServerConnection

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

the class JMXConnectionTest method serverConnectionIsExposed.

@Test
public void serverConnectionIsExposed() {
    MBeanServerConnection mBeanServerConnection = mock(MBeanServerConnection.class);
    JMXConnection jmxConnection = new JMXConnection(null, mBeanServerConnection);
    assertThat(jmxConnection.getMBeanServerConnection()).isSameAs(mBeanServerConnection);
}
Also used : JMXConnection(com.googlecode.jmxtrans.connections.JMXConnection) MBeanServerConnection(javax.management.MBeanServerConnection) Test(org.junit.Test)

Aggregations

MBeanServerConnection (javax.management.MBeanServerConnection)125 JMXConnector (javax.management.remote.JMXConnector)84 ObjectName (javax.management.ObjectName)73 JMXServiceURL (javax.management.remote.JMXServiceURL)59 JMXConnectorServer (javax.management.remote.JMXConnectorServer)38 Test (org.junit.Test)35 IOException (java.io.IOException)31 MBeanServer (javax.management.MBeanServer)28 HashMap (java.util.HashMap)23 Attribute (javax.management.Attribute)15 NotificationListener (javax.management.NotificationListener)13 MalformedURLException (java.net.MalformedURLException)12 ArrayList (java.util.ArrayList)12 Notification (javax.management.Notification)12 MalformedObjectNameException (javax.management.MalformedObjectNameException)11 Map (java.util.Map)10 List (java.util.List)8 RemoteException (java.rmi.RemoteException)7 LocateRegistry (java.rmi.registry.LocateRegistry)7 Registry (java.rmi.registry.Registry)7