@Test public void testJmxBeans() throws Exception { final int NUM_SERVERS = 3; final MiniRaftClusterWithSimulatedRpc cluster = MiniRaftClusterWithSimulatedRpc.FACTORY.newCluster(3, new RaftProperties()); cluster.start(); waitForLeader(cluster); MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer(); Set<ObjectInstance> objectInstances = platformMBeanServer.queryMBeans(new ObjectName("Ratis:*"), null); Assert.assertEquals(NUM_SERVERS, objectInstances.size()); for (ObjectInstance instance : objectInstances) { Object groupId = platformMBeanServer.getAttribute(instance.getObjectName(), "GroupId"); Assert.assertEquals(cluster.getGroupId().toString(), groupId); } cluster.shutdown(); }
public ObjectInstance registerMBean(Object object, ObjectName name) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException { // ------------------------------ // ------------------------------ Class<?> theClass = object.getClass(); Introspector.checkCompliance(theClass); final String infoClassName = getNewMBeanClassName(object); checkMBeanPermission(infoClassName, null, name, "registerMBean"); checkMBeanTrustPermission(theClass); return registerObject(infoClassName, object, name); }
/** * 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; }
/** * Call <code>checkCreate(className)</code>, then forward this method to the * wrapped object. */ public ObjectInstance createMBean(String className, ObjectName name) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException { checkCreate(className); SecurityManager sm = System.getSecurityManager(); if (sm == null) { Object object = getMBeanServer().instantiate(className); checkClassLoader(object); return getMBeanServer().registerMBean(object, name); } else { return getMBeanServer().createMBean(className, name); } }
/** * Call <code>checkCreate(className)</code>, then forward this method to the * wrapped object. */ public ObjectInstance createMBean(String className, ObjectName name, Object params[], String signature[]) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException { checkCreate(className); SecurityManager sm = System.getSecurityManager(); if (sm == null) { Object object = getMBeanServer().instantiate(className, params, signature); checkClassLoader(object); return getMBeanServer().registerMBean(object, name); } else { return getMBeanServer().createMBean(className, name, params, signature); } }
public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) { Set<ObjectInstance> instances = newSet(); if (name == null) name = ObjectName.WILDCARD; if (query == null) query = trueQuery; MBeanServer oldMBS = QueryEval.getMBeanServer(); try { query.setMBeanServer(this); for (ObjectName n : mbeans.keySet()) { if (name.apply(n)) { try { if (query.apply(n)) instances.add(getObjectInstance(n)); } catch (Exception e) { // OK: Ignore this MBean in the result } } } } finally { query.setMBeanServer(oldMBS); } return instances; }
public ObjectInstance getObjectInstance(ObjectName name) throws InstanceNotFoundException, IOException { if (logger.debugOn()) logger.debug("getObjectInstance", "name=" + name); final ClassLoader old = pushDefaultClassLoader(); try { return connection.getObjectInstance(name, delegationSubject); } catch (IOException ioe) { communicatorAdmin.gotIOException(ioe); return connection.getObjectInstance(name, delegationSubject); } finally { popDefaultClassLoader(old); } }
public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) throws IOException { if (logger.debugOn()) logger.debug("queryMBeans", "name=" + name + ", query=" + query); final MarshalledObject<QueryExp> sQuery = new MarshalledObject<QueryExp>(query); final ClassLoader old = pushDefaultClassLoader(); try { return connection.queryMBeans(name, sQuery, delegationSubject); } catch (IOException ioe) { communicatorAdmin.gotIOException(ioe); return connection.queryMBeans(name, sQuery, delegationSubject); } finally { popDefaultClassLoader(old); } }
public ObjectInstance createMBean(String className, ObjectName name, Object[] params, String[] signature) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException { try { return createMBean(className, name, null, true, params, signature); } catch (InstanceNotFoundException e) { /* Can only happen if loaderName doesn't exist, but we just passed null, so we shouldn't get this exception. */ throw EnvHelp.initCause( new IllegalArgumentException("Unexpected exception: " + e), e); } }
/** * Call <code>checkCreate(className)</code>, then forward this method to the * wrapped object. */ public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException { checkCreate(className); SecurityManager sm = System.getSecurityManager(); if (sm == null) { Object object = getMBeanServer().instantiate(className, loaderName); checkClassLoader(object); return getMBeanServer().registerMBean(object, name); } else { return getMBeanServer().createMBean(className, name, loaderName); } }
public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName, Object[] params, String[] signature) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException { return createMBean(className, name, loaderName, false, params, signature); }
@Override public synchronized ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException { throw new UnsupportedOperationException("Not supported by PogamutMBeanServer yet..."); }
public ObjectInstance createMBean( String className, ObjectName name, Object[] params, String[] signature) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException { try { return createMBean(className, name, clrName, params, signature); } catch (InstanceNotFoundException ex) { throw new RuntimeException(ex); // can't happen } }
@Override public synchronized ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName, Object[] params, String[] signature) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException { throw new UnsupportedOperationException("Not supported by PogamutMBeanServer yet..."); }
@Override public synchronized ObjectInstance registerMBean(Object object, ObjectName name) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException { ObjectInstance obj = mbs.registerMBean(object, name); mBeans.add(new MBean1(name, object)); unregisteredMBeans.remove(new MBean1(name, object)); return obj; }
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; } } } } }
public void init(String host, int port) throws Exception { int iport = 0; String shost = null; mBeanServer = Registry.getRegistry(null, null).getMBeanServer(); String onStr = "*:type=ThreadPool,*"; ObjectName objectName = new ObjectName(onStr); Set<ObjectInstance> set = mBeanServer.queryMBeans(objectName, null); Iterator<ObjectInstance> iterator = set.iterator(); while (iterator.hasNext()) { ObjectInstance oi = iterator.next(); objName = oi.getObjectName(); String name = objName.getKeyProperty("name"); /* Name are: * http-8080 * jk-10.33.144.3-8009 * jk-jfcpc%2F10.33.144.3-8009 */ String [] elenames = name.split("-"); String sport = elenames[elenames.length-1]; iport = Integer.parseInt(sport); String [] shosts = elenames[1].split("%2F"); shost = shosts[0]; if (port==0 && host==null) break; /* Take the first one */ if (host==null && iport==port) break; /* Only port done */ if (shost.compareTo(host) == 0) break; /* Done port and host are the expected ones */ } if (objName == null) throw(new Exception("Can't find connector for " + host + ":" + port)); this.port = iport; this.host = shost; }
public static Map<String, Map<String, String>> getAllMBeans() { Map<String, Map<String, String>> mbeanMap = Maps.newHashMap(); Set<ObjectInstance> queryMBeans = null; try { queryMBeans = mbeanServer.queryMBeans(null, null); } catch (Exception ex) { LOG.error("Could not get Mbeans for monitoring", ex); Throwables.propagate(ex); } for (ObjectInstance obj : queryMBeans) { try { if (!obj.getObjectName().toString().startsWith("org.apache.flume")) { continue; } MBeanAttributeInfo[] attrs = mbeanServer.getMBeanInfo(obj.getObjectName()).getAttributes(); String[] strAtts = new String[attrs.length]; for (int i = 0; i < strAtts.length; i++) { strAtts[i] = attrs[i].getName(); } AttributeList attrList = mbeanServer.getAttributes(obj.getObjectName(), strAtts); String component = obj.getObjectName().toString().substring( obj.getObjectName().toString().indexOf('=') + 1); Map<String, String> attrMap = Maps.newHashMap(); for (Object attr : attrList) { Attribute localAttr = (Attribute) attr; if (localAttr.getName().equalsIgnoreCase("type")) { component = localAttr.getValue() + "." + component; } attrMap.put(localAttr.getName(), localAttr.getValue().toString()); } mbeanMap.put(component, attrMap); } catch (Exception e) { LOG.error("Unable to poll JMX for metrics.", e); } } return mbeanMap; }
private Set<ObjectInstance> queryMBeansImpl(ObjectName name, QueryExp query) { // Query the MBeans on the repository // Set<NamedObject> list = repository.query(name, query); return (objectInstancesFromFilteredNamedObjects(list, query)); }
public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) { SecurityManager sm = System.getSecurityManager(); if (sm != null) { // Check if the caller has the right to invoke 'queryMBeans' // checkMBeanPermission((String) null, null, null, "queryMBeans"); // Perform query without "query". // Set<ObjectInstance> list = queryMBeansImpl(name, null); // Check if the caller has the right to invoke 'queryMBeans' // on each specific classname/objectname in the list. // Set<ObjectInstance> allowedList = new HashSet<ObjectInstance>(list.size()); for (ObjectInstance oi : list) { try { checkMBeanPermission(oi.getClassName(), null, oi.getObjectName(), "queryMBeans"); allowedList.add(oi); } catch (SecurityException e) { // OK: Do not add this ObjectInstance to the list } } // Apply query to allowed MBeans only. // return filterListOfObjectInstances(allowedList, query); } else { // Perform query. // return queryMBeansImpl(name, query); } }
public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException { return createMBean(className, name, loaderName, (Object[]) null, (String[]) null); }
private ConfigRegistryJMXClient(final MBeanServer configMBeanServer, final ObjectName configRegistryON) { this.configMBeanServer = configMBeanServer; this.configRegistryON = configRegistryON; Set<ObjectInstance> searchResult = configMBeanServer.queryMBeans(configRegistryON, null); if (searchResult.size() != 1) { throw new IllegalStateException("Config registry not found"); } configRegistryMXBeanProxy = JMX.newMXBeanProxy(configMBeanServer, configRegistryON, ConfigRegistryMXBean.class, false); }
public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException, IOException { if (logger.debugOn()) logger.debug("createMBean(String,ObjectName,ObjectName)", "className=" + className + ", name=" + name + ", loaderName=" + loaderName + ")"); final ClassLoader old = pushDefaultClassLoader(); try { return connection.createMBean(className, name, loaderName, delegationSubject); } catch (IOException ioe) { communicatorAdmin.gotIOException(ioe); return connection.createMBean(className, name, loaderName, delegationSubject); } finally { popDefaultClassLoader(old); } }
public static void main(String[] args) throws Exception { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); for (Class<?> c : new Class<?>[] { Spume.class, Thing.class, XSpume.class, XThing.class }) { for (ObjectName n : new ObjectName[] {null, new ObjectName("a:b=c")}) { System.out.println("Class " + c.getName() + " with name " + n + "..."); ObjectName realName = new ObjectName("a:type=" + c.getName()); Constructor<?> constr = c.getConstructor(ObjectName.class); Object mbean = constr.newInstance(realName); ObjectInstance oi; String what = "Registering MBean of type " + c.getName() + " under name " + "<" + n + ">: "; try { oi = mbs.registerMBean(mbean, n); } catch (Exception e) { e.printStackTrace(); fail(what + " got " + e); continue; } ObjectName registeredName = oi.getObjectName(); if (!registeredName.equals(realName)) fail(what + " registered as " + registeredName); if (!mbs.isRegistered(realName)) { fail(what + " not registered as expected"); } mbs.unregisterMBean(registeredName); } } System.err.flush(); if (failures == 0) System.out.println("TEST PASSED"); else throw new Exception("TEST FAILED: " + failure); }
/** * Call <code>checkWrite()</code>, then forward this method to the * wrapped object. */ public ObjectInstance registerMBean(Object object, ObjectName name) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException { checkWrite(); return getMBeanServer().registerMBean(object, name); }
static void checkMBeanPermission( final MBeanServer mbs, final ObjectName name, final String actions) throws InstanceNotFoundException, SecurityException { SecurityManager sm = System.getSecurityManager(); if (sm != null) { AccessControlContext acc = AccessController.getContext(); ObjectInstance oi; try { oi = AccessController.doPrivileged( new PrivilegedExceptionAction<ObjectInstance>() { public ObjectInstance run() throws InstanceNotFoundException { return mbs.getObjectInstance(name); } }); } catch (PrivilegedActionException e) { throw (InstanceNotFoundException) extractException(e); } String classname = oi.getClassName(); MBeanPermission perm = new MBeanPermission( classname, null, name, actions); sm.checkPermission(perm, acc); } }
public ObjectInstance createMBean(String className, ObjectName name) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException, IOException { if (logger.debugOn()) logger.debug("createMBean(String,ObjectName)", "className=" + className + ", name=" + name); final ClassLoader old = pushDefaultClassLoader(); try { return connection.createMBean(className, name, delegationSubject); } catch (IOException ioe) { communicatorAdmin.gotIOException(ioe); return connection.createMBean(className, name, delegationSubject); } finally { popDefaultClassLoader(old); } }
public ObjectInstance createMBean( String className, ObjectName name, ObjectName loaderName, Object[] params, String[] signature) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException { Object mbean = instantiate(className, loaderName, params, signature); return registerMBean(mbean, name); }
@Override public void onEvent(ActiveMQJmxMonitor.NotificationType notificationType, ActiveMQJmxMonitor.State state, ObjectInstance objectInstance) { if (notificationType == ActiveMQJmxMonitor.NotificationType.SLOW_CONSUMER) { if (state == ActiveMQJmxMonitor.State.START) slowConsumers++; else if (state == ActiveMQJmxMonitor.State.END) slowConsumers--; } else if (notificationType == ActiveMQJmxMonitor.NotificationType.PRODUCER_BLOCKED) { if (state == ActiveMQJmxMonitor.State.START) blockedProducers++; else if (state == ActiveMQJmxMonitor.State.END) blockedProducers--; } }
@Override public ObjectInstance createMBean(String className, ObjectName name, Object[] params, String[] signature) throws ReflectionException, InstanceAlreadyExistsException, MBeanException, NotCompliantMBeanException { checkDomain(name); return mbs.createMBean(className, name, params, signature); }
public ObjectInstance createMBean(String className, ObjectName name, ObjectName loaderName, Object params[], String signature[]) throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException, IOException { if (logger.debugOn()) logger.debug( "createMBean(String,ObjectName,ObjectName,Object[],String[])", "className=" + className + ", name=" + name + ", loaderName=" + loaderName + ", signature=" + strings(signature)); final MarshalledObject<Object[]> sParams = new MarshalledObject<Object[]>(params); final ClassLoader old = pushDefaultClassLoader(); try { return connection.createMBean(className, name, loaderName, sParams, signature, delegationSubject); } catch (IOException ioe) { communicatorAdmin.gotIOException(ioe); return connection.createMBean(className, name, loaderName, sParams, signature, delegationSubject); } finally { popDefaultClassLoader(old); } }