/** Check if CompositeAttributes will be collected */ @Test public void collectJvmDefaultComposites() { String mBeansObjectName = "java.lang:type=GarbageCollector,name=PS MarkSweep"; Map<String, BeanInfo> mBeans = new HashMap<String, BeanInfo>(); BeanInfo beanInfo = new BeanInfo(); beanInfo.setObjectName(mBeansObjectName); List<String> attributes = new ArrayList<String>(); attributes.add("CollectionCount"); attributes.add("LastGcInfo"); beanInfo.setAttributes(attributes); List<String> compositeAttributes = new ArrayList<String>(); compositeAttributes.add("LastGcInfo"); beanInfo.setCompositeAttributes(compositeAttributes); mBeans.put("first", beanInfo); jmxNodeInfo.setMBeans(mBeans); Map<String, JMXDataSource> dataSourceMap = new HashMap<String, JMXDataSource>(); dataSourceMap.put(mBeansObjectName + "|CollectionCount", new JMXDataSource()); // ToDo Tak set the JmxDataSource type to composite? dataSourceMap.put(mBeansObjectName + "|LastGcInfo", new JMXDataSource()); jmxNodeInfo.setDsMap(dataSourceMap); CollectionSet collectionSet = jmxCollector.collect(collectionAgent, null, null); assertEquals( "Collection of one Jvm default value run successfully", 1, collectionSet.getStatus()); }
/** This test is just a prove of concept. */ @Test public void collectTwoBasicValues() { String mBeansObjectName = "org.opennms.netmgt.collectd.jmxhelper:type=JmxTest"; Map<String, BeanInfo> mBeans = new HashMap<String, BeanInfo>(); BeanInfo beanInfo = new BeanInfo(); beanInfo.setObjectName(mBeansObjectName); List<String> attributes = new ArrayList<String>(); attributes.add("X"); attributes.add("Name"); // TODO Tak: Test attributes that will return null is the next step // attributes.add("NullString"); beanInfo.setAttributes(attributes); mBeans.put("first", beanInfo); jmxNodeInfo.setMBeans(mBeans); Map<String, JMXDataSource> dataSourceMap = new HashMap<String, JMXDataSource>(); dataSourceMap.put(mBeansObjectName + "|X", new JMXDataSource()); dataSourceMap.put(mBeansObjectName + "|Name", new JMXDataSource()); // dataSourceMap.put("org.opennms.netmgt.collectd.jmxhelper:type=JmxTest|NullString", new // JMXDataSource()); jmxNodeInfo.setDsMap(dataSourceMap); CollectionSet collectionSet = jmxCollector.collect(collectionAgent, null, null); assertEquals("Collection of two dummy values run successfully", 1, collectionSet.getStatus()); }
/** * {@inheritDoc} * * <p>Perform data collection. */ @Override public CollectionSet collect(CollectionAgent agent, EventProxy eproxy, Map<String, Object> map) { InetAddress ipaddr = agent.getAddress(); JMXNodeInfo nodeInfo = agent.getAttribute(NODE_INFO_KEY); Map<String, BeanInfo> mbeans = nodeInfo.getMBeans(); String collDir = serviceName; boolean useMbeanForRrds = ParameterMap.getKeyedBoolean(map, "use-mbean-name-for-rrds", false); String port = ParameterMap.getKeyedString(map, "port", null); String friendlyName = ParameterMap.getKeyedString(map, "friendly-name", port); if (useFriendlyName) { collDir = friendlyName; } JMXCollectionSet collectionSet = new JMXCollectionSet(agent, collDir); collectionSet.setCollectionTimestamp(new Date()); JMXCollectionResource collectionResource = collectionSet.getResource(); ConnectionWrapper connection = null; LogUtils.debugf( this, "collecting %s on node ID %d", InetAddressUtils.str(ipaddr), nodeInfo.getNodeId()); try { connection = getMBeanServerConnection(map, ipaddr); if (connection == null) { return collectionSet; } MBeanServerConnection mbeanServer = connection.getMBeanServer(); int retry = ParameterMap.getKeyedInteger(map, "retry", 3); for (int attempts = 0; attempts <= retry; attempts++) { try { /* * Iterate over the mbeans, for each object name perform a * getAttributes, the update the RRD. */ for (Iterator<BeanInfo> iter = mbeans.values().iterator(); iter.hasNext(); ) { BeanInfo beanInfo = iter.next(); String mbeanName = beanInfo.getMbeanName(); String objectName = beanInfo.getObjectName(); String excludeList = beanInfo.getExcludes(); // All JMX collected values are per node String obj = useMbeanForRrds ? mbeanName : objectName; AttributeGroupType attribGroupType = new AttributeGroupType(fixGroupName(obj), "all"); List<String> attribNames = beanInfo.getAttributeNames(); List<String> compAttribNames = beanInfo.getCompositeAttributeNames(); for (String compAttribName : compAttribNames) { if (attribNames.contains(compAttribName)) { attribNames.remove(compAttribName); String[] ac = compAttribName.split("\\|", -1); String attrName = ac[0]; if (!attribNames.contains(attrName)) { attribNames.add(attrName); } } } // log.debug(" JMXCollector: processed the following attributes: " + // attribNames.toString()); // log.debug(" JMXCollector: processed the following Composite Attributes: " + // compAttribNames.toString()); String[] attrNames = attribNames.toArray(new String[attribNames.size()]); if (objectName.indexOf("*") == -1) { LogUtils.debugf( this, "%s Collector - getAttributes: %s, # attributes: %d, # composite attribute members: %d", serviceName, objectName, attrNames.length, compAttribNames.size()); try { ObjectName oName = new ObjectName(objectName); if (mbeanServer.isRegistered(oName)) { AttributeList attrList = mbeanServer.getAttributes(oName, attrNames); Map<String, JMXDataSource> dsMap = nodeInfo.getDsMap(); for (Object attribute : attrList) { List<String> compositeMemberKeys = new ArrayList<String>(); Boolean isComposite = false; Attribute attrib = (Attribute) attribute; for (String compAttrName : compAttribNames) { String[] attribKeys = compAttrName.split("\\|", -1); if (attrib.getName().equals(attribKeys[0])) { compositeMemberKeys.add(attribKeys[1]); isComposite = true; } } if (isComposite) { try { CompositeData cd = (CompositeData) attrib.getValue(); for (String key : compositeMemberKeys) { /* value = cd.get(key); log.debug(" JMXCollector - got CompositeData: " + objectName + "|" + attrib.getName() + "|" + key + " |-> " + cd.get(key).toString()); */ JMXDataSource ds = dsMap.get(objectName + "|" + attrib.getName() + "|" + key); JMXCollectionAttributeType attribType = new JMXCollectionAttributeType(ds, null, null, attribGroupType); collectionResource.setAttributeValue(attribType, cd.get(key).toString()); } } catch (final ClassCastException cce) { LogUtils.debugf( this, cce, "%s Collection - getAttributes (try CompositeData) - ERROR: Failed to cast attribute value to type CompositeData!", serviceName); } } else { // this is a normal attribute, so fallback to default handler JMXDataSource ds = dsMap.get(objectName + "|" + attrib.getName()); JMXCollectionAttributeType attribType = new JMXCollectionAttributeType(ds, null, null, attribGroupType); collectionResource.setAttributeValue( attribType, attrib.getValue().toString()); } } } } catch (final InstanceNotFoundException e) { LogUtils.errorf(this, e, "Unable to retrieve attributes from %s", objectName); } } else { /* * This section is for ObjectNames that use the * '*' wildcard */ Set<ObjectName> mbeanSet = getObjectNames(mbeanServer, objectName); for (Iterator<ObjectName> objectNameIter = mbeanSet.iterator(); objectNameIter.hasNext(); ) { ObjectName oName = objectNameIter.next(); LogUtils.debugf( this, "%s Collector - getAttributesWC: %s, # attributes: %d, alias: %s", serviceName, oName, attrNames.length, beanInfo.getKeyAlias()); try { if (excludeList == null) { // the exclude list doesn't apply if (mbeanServer.isRegistered(oName)) { AttributeList attrList = mbeanServer.getAttributes(oName, attrNames); Map<String, JMXDataSource> dsMap = nodeInfo.getDsMap(); for (Object attribute : attrList) { Attribute attrib = (Attribute) attribute; JMXDataSource ds = dsMap.get(objectName + "|" + attrib.getName()); JMXCollectionAttributeType attribType = new JMXCollectionAttributeType( ds, oName.getKeyProperty(beanInfo.getKeyField()), beanInfo.getKeyAlias(), attribGroupType); collectionResource.setAttributeValue( attribType, attrib.getValue().toString()); } } } else { /* * filter out calls if the key field * matches an entry in the exclude * list */ String keyName = oName.getKeyProperty(beanInfo.getKeyField()); boolean found = false; StringTokenizer st = new StringTokenizer(excludeList, ","); while (st.hasMoreTokens()) { if (keyName.equals(st.nextToken())) { found = true; break; } } if (!found) { if (mbeanServer.isRegistered(oName)) { AttributeList attrList = mbeanServer.getAttributes(oName, attrNames); Map<String, JMXDataSource> dsMap = nodeInfo.getDsMap(); for (Object attribute : attrList) { Attribute attrib = (Attribute) attribute; JMXDataSource ds = dsMap.get(objectName + "|" + attrib.getName()); JMXCollectionAttributeType attribType = new JMXCollectionAttributeType( ds, oName.getKeyProperty(beanInfo.getKeyField()), beanInfo.getKeyAlias(), attribGroupType); collectionResource.setAttributeValue( attribType, attrib.getValue().toString()); } } } } } catch (final InstanceNotFoundException e) { LogUtils.errorf(this, e, "Error retrieving attributes for %s", oName); } } } } break; } catch (final Exception e) { LogUtils.debugf( this, e, "%s Collector.collect: IOException while collecting address: %s", serviceName, agent.getAddress()); } } } catch (final Exception e) { LogUtils.errorf(this, e, "Error getting MBeanServer"); } finally { if (connection != null) { connection.close(); } } collectionSet.setStatus(ServiceCollector.COLLECTION_SUCCEEDED); return collectionSet; }