private ImmutableMap<String, Optional<Object>> getAttributes(Set<String> uniqueColumnNames, String name) throws JMException { ObjectName objectName = new ObjectName(name); String[] columnNamesArray = uniqueColumnNames.toArray(new String[uniqueColumnNames.size()]); ImmutableMap.Builder<String, Optional<Object>> attributes = ImmutableMap.builder(); for (Attribute attribute : mbeanServer.getAttributes(objectName, columnNamesArray).asList()) { attributes.put(attribute.getName(), Optional.ofNullable(attribute.getValue())); } return attributes.build(); }
private static List<Attribute> batchLoadAttributes( MBeanServerConnection server, ObjectName objectName, List<String> matchingNames) throws InstanceNotFoundException, ReflectionException, IOException { final String[] namesArray = matchingNames.toArray(new String[matchingNames.size()]); return server.getAttributes(objectName, namesArray).asList(); }
@Override public AttributeList setAttributes(AttributeList arg0) { AttributeList attributesSet = new AttributeList(); for (Attribute attr : arg0.asList()) { try { setAttribute(attr); attributesSet.add(attr); } catch (AttributeNotFoundException e) { // ignore exception - we simply don't add this attribute // back in to the resultant set. } catch (InvalidAttributeValueException e) { // ditto } catch (MBeanException e) { // likewise } catch (ReflectionException e) { // and again, one last time. } } return attributesSet; }
@Override public long[] getFileDecriptorInfo() { MBeanServer mbsc = MBeans.getMBeanServer(); AttributeList attributes; try { attributes = mbsc.getAttributes( new ObjectName("java.lang:type=OperatingSystem"), new String[]{"OpenFileDescriptorCount", "MaxFileDescriptorCount"}); List<Attribute> attrList = attributes.asList(); long openFdCount = (Long)attrList.get(0).getValue(); long maxFdCount = (Long)attrList.get(1).getValue(); long[] fdCounts = { openFdCount, maxFdCount}; return fdCounts; } catch (Exception e) { LogFactory.getLog(SdkMBeanRegistrySupport.class).debug( "Failed to retrieve file descriptor info", e); } return null; }
private synchronized NameValueMap getCachedAttributes(ObjectName objName, Set<String> attrNames) throws InstanceNotFoundException, ReflectionException, IOException { NameValueMap values = cachedValues.get(objName); if (values != null && values.keySet().containsAll(attrNames)) { return values; } attrNames = new TreeSet<String>(attrNames); Set<String> oldNames = cachedNames.get(objName); if (oldNames != null) { attrNames.addAll(oldNames); } values = new NameValueMap(); final AttributeList attrs = conn.getAttributes(objName, attrNames.toArray(new String[attrNames.size()])); for (Attribute attr : attrs.asList()) { values.put(attr.getName(), attr.getValue()); } cachedValues.put(objName, values); cachedNames.put(objName, attrNames); return values; }
private Map<String, String> getMBeanValues(MBeanServerConnection cnx, ObjectName on, String ... attributeNames) throws InstanceNotFoundException, IOException, ReflectionException, IntrospectionException { if (attributeNames == null) { MBeanInfo info = cnx.getMBeanInfo( on ); MBeanAttributeInfo[] attributeArray = info.getAttributes(); int i = 0; attributeNames = new String[attributeArray.length]; for (MBeanAttributeInfo ai : attributeArray) attributeNames[i++] = ai.getName(); } AttributeList attributes = cnx.getAttributes(on, attributeNames); Map<String, String> values = new HashMap<String, String>(); for (javax.management.Attribute attribute : attributes.asList()) { Object value = attribute.getValue(); values.put(attribute.getName(), value == null ? "" : value.toString()); } return values; }
private static void fetchThreadPoolMetrics(long now, MBeanServer mbs, List<Metric> metrics) throws JMException { final ObjectName threadPoolName = new ObjectName("Catalina:type=ThreadPool,*"); final Set<ObjectName> names = mbs.queryNames(threadPoolName, null); if (names == null) { return; } for (ObjectName name : names) { AttributeList list = mbs.getAttributes(name, THREAD_POOL_ATTRS); // determine whether the shared threadPool is used boolean isUsed = true; for (Attribute a : list.asList()) { if (a.getName().equals("maxThreads")) { Number v = (Number) a.getValue(); isUsed = v.doubleValue() >= 0.0; break; } } if (isUsed) { // only add the attributes if the metric is used. for (Attribute a : list.asList()) { addMetric(metrics, toGauge(now, name, a)); } } } }
@Override public AttributeList setAttributes(ObjectName name, AttributeList attributes) throws InstanceNotFoundException, ReflectionException { // call setAttribute instead to use the authorization logic for (Attribute attribute : attributes.asList()) { try { setAttribute(name, attribute); } catch (Exception e) { throw new GemFireSecurityException("error setting attribute " + attribute + " of " + name, e); } } return attributes; }
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); } } }
private synchronized NameValueMap getCachedAttributes( ObjectName objName, Set<String> attrNames) throws InstanceNotFoundException, ReflectionException, IOException { NameValueMap values = cachedValues.get(objName); if (values != null && values.keySet().containsAll(attrNames)) { return values; } attrNames = new TreeSet<String>(attrNames); Set<String> oldNames = cachedNames.get(objName); if (oldNames != null) { attrNames.addAll(oldNames); } values = new NameValueMap(); final AttributeList attrs = conn.getAttributes( objName, attrNames.toArray(new String[attrNames.size()])); for (Attribute attr : attrs.asList()) { values.put(attr.getName(), attr.getValue()); } cachedValues.put(objName, values); cachedNames.put(objName, attrNames); return values; }
private static void fetchExecutorMetrics(long now, MBeanServer mbs, List<Metric> metrics) throws JMException { final ObjectName executorName = new ObjectName("Catalina:type=Executor,*"); final Set<ObjectName> names = mbs.queryNames(executorName, null); if (names == null) { return; } for (ObjectName name : names) { AttributeList list = mbs.getAttributes(name, EXECUTOR_ATTRS); for (Attribute a : list.asList()) { addMetric(metrics, a.getName().equals("completedTaskCount") ? toCounter(now, name, a) : toGauge(now, name, a)); } } }
private static void fetchRequestProcessorMetrics(long now, MBeanServer mbs, List<Metric> metrics) throws JMException { final ObjectName globalName = new ObjectName("Catalina:type=GlobalRequestProcessor,*"); final Set<ObjectName> names = mbs.queryNames(globalName, null); if (names == null) { return; } for (ObjectName name : names) { AttributeList list = mbs.getAttributes(name, GLOBAL_REQ_ATTRS); for (Attribute a : list.asList()) { // the only gauge here is maxTime addMetric(metrics, a.getName().equals("maxTime") ? toGauge(now, name, a) : toCounter(now, name, a)); } } }
public Iterable<Result> fetchResults(MBeanServerConnection mbeanServer, ObjectName queryName) throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException { ObjectInstance oi = mbeanServer.getObjectInstance(queryName); List<String> attributes; if (attr.isEmpty()) { attributes = new ArrayList<>(); MBeanInfo info = mbeanServer.getMBeanInfo(queryName); for (MBeanAttributeInfo attrInfo : info.getAttributes()) { attributes.add(attrInfo.getName()); } } else { attributes = attr; } try { if (!attributes.isEmpty()) { logger.debug("Executing queryName [{}] from query [{}]", queryName.getCanonicalName(), this); AttributeList al = mbeanServer.getAttributes(queryName, attributes.toArray(new String[attributes.size()])); return new JmxResultProcessor(this, oi, al.asList(), oi.getClassName(), queryName.getDomain()).getResults(); } } catch (UnmarshalException ue) { if ((ue.getCause() != null) && (ue.getCause() instanceof ClassNotFoundException)) { logger.debug("Bad unmarshall, continuing. This is probably ok and due to something like this: " + "http://ehcache.org/xref/net/sf/ehcache/distribution/RMICacheManagerPeerListener.html#52", ue.getMessage()); } else { throw ue; } } return ImmutableList.of(); }
public void execute(OperationContext context, ModelNode operation) throws OperationFailedException { MBeanServer mbs = TransactionExtension.getMBeanServer(context); final Resource resource = context.readResource(PathAddress.EMPTY_ADDRESS); try { final ObjectName on = LogStoreResource.getObjectName(resource); final ModelNode model = resource.getModel().clone(); AttributeList attributes = mbs.getAttributes(on, LogStoreConstants.PARTICIPANT_JMX_NAMES); for (javax.management.Attribute attribute : attributes.asList()) { String modelName = LogStoreConstants.jmxNameToModelName(LogStoreConstants.MODEL_TO_JMX_PARTICIPANT_NAMES, attribute.getName()); if (modelName != null) { ModelNode aNode = model.get(modelName); Object value = attribute.getValue(); if (aNode != null) aNode.set(value == null ? "" : value.toString()); } } // Replace the model resource.writeModel(model); } catch (Exception e) { throw new OperationFailedException("JMX error: " + e.getMessage()); } context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER); } }
@Test public void testGetAttributes() throws Exception { sensor.record(3.5); sensor.record(4.0); AttributeList attributeList = getAttributes(countMetricName, countMetricName.name(), sumMetricName.name()); List<Attribute> attributes = attributeList.asList(); assertEquals(2, attributes.size()); for (Attribute attribute : attributes) { if (countMetricName.name().equals(attribute.getName())) assertEquals(2.0, attribute.getValue()); else if (sumMetricName.name().equals(attribute.getName())) assertEquals(7.5, attribute.getValue()); else fail("Unexpected attribute returned: " + attribute.getName()); } }
@Test public void testGetAttributesWithUnknown() throws Exception { sensor.record(3.5); sensor.record(4.0); AttributeList attributeList = getAttributes(countMetricName, countMetricName.name(), sumMetricName.name(), "name"); List<Attribute> attributes = attributeList.asList(); assertEquals(2, attributes.size()); for (Attribute attribute : attributes) { if (countMetricName.name().equals(attribute.getName())) assertEquals(2.0, attribute.getValue()); else if (sumMetricName.name().equals(attribute.getName())) assertEquals(7.5, attribute.getValue()); else fail("Unexpected attribute returned: " + attribute.getName()); } }
@Override public long[] getFileDecriptorInfo() { MBeanServer mbsc = MBeans.getMBeanServer(); AttributeList attributes; try { attributes = mbsc.getAttributes( new ObjectName("java.lang:type=OperatingSystem"), new String[]{"OpenFileDescriptorCount", "MaxFileDescriptorCount"}); List<Attribute> attrList = attributes.asList(); long openFdCount = (Long)attrList.get(0).getValue(); long maxFdCount = (Long)attrList.get(1).getValue(); long[] fdCounts = { openFdCount, maxFdCount}; return fdCounts; } catch (Exception e) { LogFactory.getLog(SdkMBeanRegistrySupport.class).debug( "Failed to retrieve file descriptor info", e); } return null; }
private long getOpenFileDecriptorCount() throws Exception { MBeanServer mbsc = getMBeanServer(); AttributeList attributes; attributes = mbsc.getAttributes( new ObjectName("java.lang:type=OperatingSystem"), new String[] { "OpenFileDescriptorCount" }); List<Attribute> attrList = attributes.asList(); return (Long) attrList.get(0).getValue(); }
return; for (Attribute attribute : attributes.asList()) { MBeanAttributeInfo attr = name2AttrInfo.get(attribute.getName()); logScrape(mbeanName, attr, "process");
public static Map<Object, List> queryMBeansAsMap(MBeanServerConnection jmxConnection, List queryList, Set attributes) throws Exception { Map<Object, List> answer = new HashMap<Object, List>(); List<AttributeList> mbeans = queryMBeans(jmxConnection, queryList, attributes); for (AttributeList mbean : mbeans) { for(Attribute attr: mbean.asList()) { if (attr.getName().equals(MBeansAttributeQueryFilter.KEY_OBJECT_NAME_ATTRIBUTE)) { answer.put(attr.getValue(), mbean); } } } return answer; }