public static List<MbeanAttributeValue> getMBeanAttributeValues(String mbeanExpr, String attributeExpr) { List<MbeanAttributeValue> values = new ArrayList<>(); MBeanServer server = ManagementFactory.getPlatformMBeanServer(); try { Set<ObjectName> mbeanNames = server.queryNames(new ObjectName(mbeanExpr), null); for (ObjectName mbeanName: mbeanNames) { MBeanInfo mBeanInfo = server.getMBeanInfo(mbeanName); MBeanAttributeInfo[] attributeInfos = mBeanInfo.getAttributes(); for (MBeanAttributeInfo attributeInfo: attributeInfos) { if (attributeInfo.getName().equals(attributeExpr) || attributeExpr.length() == 0 || attributeExpr.equals("*")) { double value = (Double) server.getAttribute(mbeanName, attributeInfo.getName()); values.add(new MbeanAttributeValue(mbeanName.getCanonicalName(), attributeInfo.getName(), value)); } } } } catch (Exception e) { LOG.error("fail to retrieve value for " + mbeanExpr + ":" + attributeExpr, e); } return values; }
void registerLayoutMBean(Layout layout) { if(layout == null) return; String name = getAppenderName(appender)+",layout="+layout.getClass().getName(); cat.debug("Adding LayoutMBean:"+name); ObjectName objectName = null; try { LayoutDynamicMBean appenderMBean = new LayoutDynamicMBean(layout); objectName = new ObjectName("log4j:appender="+name); if (!server.isRegistered(objectName)) { registerMBean(appenderMBean, objectName); dAttributes.add(new MBeanAttributeInfo("appender=" + name, "javax.management.ObjectName", "The " + name + " layout.", true, true, false)); } } catch(JMException e) { cat.error("Could not add DynamicLayoutMBean for ["+name+"].", e); } catch(java.beans.IntrospectionException e) { cat.error("Could not add DynamicLayoutMBean for ["+name+"].", e); } catch(RuntimeException e) { cat.error("Could not add DynamicLayoutMBean for ["+name+"].", e); } }
private static void printJMXStats() { MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); ObjectName groupMulticast = getGroupMulticastObjectName(mBeanServer); if (groupMulticast == null) { System.err.println("Unable to find the GROUP_MULTICAST protocol"); return ; } try { System.out.println("======== JMX STATS ========="); for (MBeanAttributeInfo mBeanAttributeInfo : mBeanServer.getMBeanInfo(groupMulticast).getAttributes()) { String attribute = mBeanAttributeInfo.getName(); String type = mBeanAttributeInfo.getType(); if (!type.equals("double") && !type.equals("int")) { continue; } System.out.println(attribute + "=" + mBeanServer.getAttribute(groupMulticast, attribute)); } System.out.println("======== JMX STATS ========="); } catch (Exception e) { System.err.println("Error collecting stats" + e.getLocalizedMessage()); } }
private Map<String,Object> toNeo4jValue( ObjectName name, MBeanAttributeInfo[] attributes ) throws JMException { HashMap<String,Object> out = new HashMap<>(); for ( MBeanAttributeInfo attribute : attributes ) { if ( attribute.isReadable() ) { out.put( attribute.getName(), toNeo4jValue( name, attribute ) ); } } return out; }
private static Set<String> getAttributesNames(ObjectName name) { try { final Set<String> result = new HashSet<String>(); final MBeanInfo mBeanInfo = MBEAN_SERVER.getMBeanInfo(name); final MBeanAttributeInfo[] attributes = mBeanInfo.getAttributes(); for (final MBeanAttributeInfo attribute : attributes) { if (attribute.isReadable()) { result.add(attribute.getName()); } } return result; } catch (final JMException e) { return Collections.emptySet(); } } }
@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); assertEquals(MetricsMBeanImpl.class.getName(), mBeanInfo.getClassName()); MBeanOperationInfo[] oops = mBeanInfo.getOperations(); boolean resetFound = false; for (MBeanOperationInfo op : oops) { if ("reset".equals(op.getName())) { resetFound = true; break; 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());
@Before public void setup() throws Throwable { jmxServer = mock( MBeanServer.class ); beanName = new ObjectName( "org.neo4j:chevyMakesTheTruck=bobMcCoshMakesTheDifference" ); attributeName = "name"; when( jmxServer.queryNames( new ObjectName( "*:*" ), null ) ) .thenReturn( asSet( beanName ) ); when( jmxServer.getMBeanInfo( beanName ) ) .thenReturn( new MBeanInfo( "org.neo4j.SomeMBean", "This is a description", new MBeanAttributeInfo[]{ new MBeanAttributeInfo( attributeName, "someType", "This is the attribute desc.", true, false, false ) }, null, null, null ) ); } }
pairs.put("name", "Master"); pairs.put("sub", "Balancer"); target = new ObjectName("Hadoop", pairs); MBeanInfo beanInfo = mb.getMBeanInfo(target); for (MBeanAttributeInfo attrInfo : beanInfo.getAttributes()) { existingAttrs.add(attrInfo.getName()); LOG.warn("Failed to get bean!!! " + target, e); if (mb != null) { Set<ObjectInstance> instances = mb.queryMBeans(null, null); Iterator<ObjectInstance> iterator = instances.iterator(); System.out.println("MBean Found:");
public String describe(ObjectName bean) throws Exception { MBeanInfo mbinfo = mserver.getMBeanInfo(bean); StringBuilder sb = new StringBuilder(); sb.append(bean); sb.append('\n'); sb.append(mbinfo.getClassName()); sb.append('\n'); sb.append(" - " + mbinfo.getDescription()); sb.append('\n'); for(MBeanAttributeInfo ai: mbinfo.getAttributes()) { sb.append(" (A) "); sb.append(ai.getName()).append(" : ").append(toPrintableType(ai.getType())).append(""); if (!ai.isReadable()) { sb.append(" - WRITEONLY"); else if (ai.isWritable()) { sb.append(" - WRITEABLE"); if (!ai.getName().equals(ai.getDescription())) { sb.append(" - " + ai.getDescription()); sb.append('\n'); if (oi.getSignature().length > 0) { sb.setLength(sb.length() - 2);
MBeanInfo info = null; try { info = mbs.getMBeanInfo(objectName); } catch (Exception e) { log.error("Could not get MBeanInfo object for {}", className, e); return; log.info("CLASSNAME: \t" + info.getClassName()); log.info("DESCRIPTION: \t" + info.getDescription()); log.info("ATTRIBUTES"); MBeanAttributeInfo[] attrInfo = info.getAttributes(); if (attrInfo.length > 0) { for (int i = 0; i < attrInfo.length; i++) { log.info(" ** NAME: \t" + attrInfo[i].getName()); log.info(" DESCR: \t" + attrInfo[i].getDescription()); log.info(" TYPE: \t" + attrInfo[i].getType() + "\tREAD: " + attrInfo[i].isReadable() + "\tWRITE: " + attrInfo[i].isWritable()); MBeanConstructorInfo[] constrInfo = info.getConstructors(); for (int i = 0; i < constrInfo.length; i++) { log.info(" ** NAME: \t" + constrInfo[i].getName()); log.info(" DESCR: \t" + constrInfo[i].getDescription()); log.info(" PARAM: \t" + constrInfo[i].getSignature().length + " parameter(s)"); if (opInfo.length > 0) { for (int i = 0; i < opInfo.length; i++) { log.info(" ** NAME: \t" + opInfo[i].getName()); log.info(" DESCR: \t" + opInfo[i].getDescription()); log.info(" PARAM: \t" + opInfo[i].getSignature().length + " parameter(s)");
final String mbeanName = getParam(req, JMX_MBEAN); try { final ObjectName name = new ObjectName(mbeanName); final MBeanInfo info = this.server.getMBeanInfo(name); ret.put("attributes", info.getAttributes()); ret.put("description", info.getDescription()); } catch (final Exception e) { logger.error(e); final ObjectName name = new ObjectName(mbeanName); final Object obj = this.server.getMBeanAttribute(name, attribute); ret.put("value", obj); final String mbeanName = getParam(req, JMX_MBEAN); try { final ObjectName name = new ObjectName(mbeanName); final MBeanInfo info = this.server.getMBeanInfo(name); final MBeanAttributeInfo[] mbeanAttrs = info.getAttributes(); final Map<String, Object> attributes = new TreeMap<>(); final Object obj = this.server.getMBeanAttribute(name, attrInfo.getName()); attributes.put(attrInfo.getName(), obj);
public List<String> getAttributeNames(String objectName) throws Exception { if (mxserver == null) return null; MBeanAttributeInfo[] attrs = mxserver.getMBeanInfo(new ObjectName(objectName)).getAttributes(); List<String> names = new ArrayList<String>(); for (int i = 0; i < attrs.length; i++) { names.add(attrs[i].getName()); } return names; }
protected static String listOptions(MBeanServerConnection mbsc, ObjectInstance instance) throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException { StringBuffer result = new StringBuffer(); MBeanInfo info = mbsc.getMBeanInfo(instance.getObjectName()); MBeanAttributeInfo[] attributes = info.getAttributes(); if (attributes.length > 0) { result.append("Attributes:"); result.append("\n"); for (int i = 0; i < attributes.length; i++) { result.append(' ' + attributes[i].getName() + ": " + attributes[i].getDescription() + " (type=" + attributes[i].getType() + ")"); result.append("\n"); result.append(' ' + operations[i].getName() + ": " + operations[i].getDescription() + "\n Parameters " + params != null ? params.length : 0 + ", return type=" + operations[i].getReturnType() + paramsStrBuffer.toString());
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); } } }
TreeElement root = new TreeElement(info.getClassName()); overflow(Overflow.HIDDEN). rightCellPadding(1); Descriptor descriptorInfo = info.getDescriptor(); if (descriptorInfo != null) { for (String fieldName : descriptorInfo.getFieldNames()) { rightCellPadding(1). add(new RowElement().style(Decoration.bold.fg(Color.black).bg(Color.white)).add("NAME", "TYPE", "DESCRIPTION")); for (MBeanAttributeInfo attributeInfo : info.getAttributes()) { attributes.row(attributeInfo.getName(), attributeInfo.getType(), attributeInfo.getDescription()); overflow(Overflow.HIDDEN). rightCellPadding(1); MBeanParameterInfo[] parameterInfos = operationInfo.getSignature(); for (MBeanParameterInfo parameterInfo : parameterInfos) { signature.row(parameterInfo.getName(), parameterInfo.getType(), parameterInfo.getDescription()); TreeElement operation = new TreeElement(operationInfo.getName()); String impact; switch (operationInfo.getImpact()) { case MBeanOperationInfo.ACTION: impact = "ACTION";
final Set<ObjectName> names = connection.queryNames(null, null); final List<ObjectName> ksqlObjects = new LinkedList<>(); for (final ObjectName n : names) { if (n.toString().startsWith("io.confluent.ksql.metrics:type=ksql-engine")) { ksqlObjects.add(n); final MBeanInfo info; try { info = connection.getMBeanInfo(n); } catch (final Exception error) { throw new PrintMetricsException( ); final MBeanAttributeInfo[] attributes = info.getAttributes(); for (final MBeanAttributeInfo attributeInfo : attributes) { final Object attribute; try { attribute = connection.getAttribute(n, attributeInfo.getName()); } catch (final Exception error) { throw new PrintMetricsException( ); System.out.println(attributeInfo.getName() + ": " + attribute);
MBeanInfo info = server.getMBeanInfo(this.objectName); MBeanAttributeInfo[] attributeInfo = info.getAttributes(); this.allowedAttributes = new HashMap<>(attributeInfo.length); for (MBeanAttributeInfo infoEle : attributeInfo) { this.allowedAttributes.put(infoEle.getName(), infoEle); MBeanOperationInfo[] operationInfo = info.getOperations(); this.allowedOperations = new HashMap<>(operationInfo.length); for (MBeanOperationInfo infoEle : operationInfo) { Class<?>[] paramTypes = JmxUtils.parameterInfoToTypes(infoEle.getSignature(), this.beanClassLoader); this.allowedOperations.put(new MethodCacheKey(infoEle.getName(), paramTypes), infoEle);
MBeanAttributeInfo[] attrInfos = info.getAttributes(); if (!attr.isReadable()) { logScrape(mbeanName, attr, "not readable"); continue; name2AttrInfo.put(attr.getName(), attr); attributes = beanConn.getAttributes(mbeanName, name2AttrInfo.keySet().toArray(new String[0])); } catch (Exception e) { logScrape(mbeanName, name2AttrInfo.keySet(), "Fail: " + e); logScrape(mbeanName, attr, "process"); processBeanValue( mbeanName.getDomain(), jmxMBeanPropertyCache.getKeyPropertyList(mbeanName), new LinkedList<String>(), attr.getName(), attr.getType(), attr.getDescription(), attribute.getValue() );
Set<ObjectInstance> queryMBeans = null; try { queryMBeans = mbeanServer.queryMBeans(null, null); } catch (Exception ex) { LOG.error("Could not get Mbeans for monitoring", ex); 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();