/** * {@inheritDoc} * * <p>Responsible for performing all necessary initialization for the specified interface in * preparation for data collection. */ @Override public void initialize(CollectionAgent agent, Map<String, Object> parameters) { InetAddress ipAddr = agent.getAddress(); int nodeID = agent.getNodeId(); // Retrieve the name of the JMX data collector String collectionName = ParameterMap.getKeyedString(parameters, "collection", serviceName); final String hostAddress = InetAddressUtils.str(ipAddr); LogUtils.debugf( this, "initialize: InetAddress=%s, collectionName=%s", hostAddress, collectionName); JMXNodeInfo nodeInfo = new JMXNodeInfo(nodeID); LogUtils.debugf(this, "nodeInfo: %s %d %s", hostAddress, nodeID, agent); /* * Retrieve list of MBean objects to be collected from the * remote agent which are to be stored in the node-level RRD file. * These objects pertain to the node itself not any individual * interfaces. */ Map<String, List<Attrib>> attrMap = JMXDataCollectionConfigFactory.getInstance() .getAttributeMap(collectionName, serviceName, hostAddress); nodeInfo.setAttributeMap(attrMap); Map<String, JMXDataSource> dsList = buildDataSourceList(collectionName, attrMap); nodeInfo.setDsMap(dsList); nodeInfo.setMBeans(JMXDataCollectionConfigFactory.getInstance().getMBeanInfo(collectionName)); // Add the JMXNodeInfo object as an attribute of the interface agent.setAttribute(NODE_INFO_KEY, nodeInfo); agent.setAttribute("collectionName", collectionName); }
@Before public void setUp() throws Exception { jmxNodeInfo = new JMXNodeInfo(0); jmxCollector = new JMXCollectorImpl(); platformMBeanServer = ManagementFactory.getPlatformMBeanServer(); ObjectName objectName = new ObjectName("org.opennms.netmgt.collectd.jmxhelper:type=JmxTest"); JmxTestMBean testMBean = new JmxTest(); platformMBeanServer.registerMBean(testMBean, objectName); collectionAgent = EasyMock.createMock(CollectionAgent.class); EasyMock.expect(collectionAgent.getAddress()).andReturn(InetAddress.getLocalHost()).anyTimes(); EasyMock.expect( collectionAgent.getAttribute("org.opennms.netmgt.collectd.JMXCollector.nodeInfo")) .andReturn(jmxNodeInfo) .anyTimes(); EasyMock.expect(collectionAgent.getNodeId()).andReturn(0).anyTimes(); EasyMock.expect(collectionAgent.getStorageDir()).andReturn(new File("")).anyTimes(); EasyMock.replay(collectionAgent); FileInputStream configFileStream = new FileInputStream("src/test/resources/etc/JmxCollectorConfigTest.xml"); logger.debug("ConfigFileStream check '{}'", configFileStream.available()); jmxConfigFactory = new JMXDataCollectionConfigFactory(configFileStream); JMXDataCollectionConfigFactory.setInstance(jmxConfigFactory); }
/** {@inheritDoc} */ @Override public void release(final CollectionAgent agent) { final WmiAgentState nodeState = m_scheduledNodes.get((Integer) agent.getNodeId()); if (nodeState != null) { m_scheduledNodes.remove((Integer) agent.getNodeId()); } }
/** {@inheritDoc} */ @Override public void initialize(final CollectionAgent agent, final Map<String, Object> parameters) { LOG.debug("initialize: Initializing WMI collection for agent: {}", agent); final Integer scheduledNodeKey = new Integer(agent.getNodeId()); WmiAgentState nodeState = m_scheduledNodes.get(scheduledNodeKey); if (nodeState != null) { LOG.info( "initialize: Not scheduling interface for WMI collection: {}", nodeState.getAddress()); final StringBuffer sb = new StringBuffer(); sb.append("initialize service: "); sb.append(" for address: "); sb.append(nodeState.getAddress()); sb.append(" already scheduled for collection on node: "); sb.append(agent); LOG.debug(sb.toString()); throw new IllegalStateException(sb.toString()); } else { nodeState = new WmiAgentState(agent.getInetAddress(), parameters); LOG.info("initialize: Scheduling interface for collection: {}", nodeState.getAddress()); m_scheduledNodes.put(scheduledNodeKey, nodeState); } }
JMXCollectionResource(CollectionAgent agent, String resourceName) { super(agent); m_resourceName = resourceName; m_nodeId = agent.getNodeId(); }
/** * {@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; }
/** {@inheritDoc} */ @Override public CollectionSet collect( final CollectionAgent agent, final EventProxy eproxy, final Map<String, Object> parameters) { String collectionName = ParameterMap.getKeyedString( parameters, "collection", ParameterMap.getKeyedString(parameters, "wmi-collection", null)); // Find attributes to collect - check groups in configuration. For each, // check scheduled nodes to see if that group should be collected final WmiCollection collection = WmiDataCollectionConfigFactory.getInstance().getWmiCollection(collectionName); final WmiAgentState agentState = m_scheduledNodes.get(agent.getNodeId()); // Load the attribute group types. loadAttributeGroupList(collection); // Load the attribute types. loadAttributeTypeList(collection); // Create a new collection set. final WmiCollectionSet collectionSet = new WmiCollectionSet(agent); collectionSet.setCollectionTimestamp(new Date()); // Iterate through the WMI collection groups. for (final Wpm wpm : collection.getWpms().getWpm()) { // A wpm consists of a list of attributes, identified by name if (agentState.shouldCheckAvailability(wpm.getName(), wpm.getRecheckInterval())) { if (!isGroupAvailable(agentState, wpm)) { continue; } } if (agentState.groupIsAvailable(wpm.getName())) { WmiClient client = null; // Collect the data try { // Tell the agent to connect agentState.connect(wpm.getWmiNamespace()); // And retrieve the client object for working. client = (WmiClient) agentState.getWmiClient(); // Retrieve the WbemObjectSet from the class defined on the group. final OnmsWbemObjectSet wOS = client.performInstanceOf(wpm.getWmiClass()); // If we received a WbemObjectSet result, lets go through it and collect it. if (wOS != null) { // Go through each object (class instance) in the object set. for (int i = 0; i < wOS.count(); i++) { // Create a new collection resource. WmiCollectionResource resource = null; // Fetch our WBEM Object final OnmsWbemObject obj = wOS.get(i); // If this is multi-instance, fetch the instance name and store it. if (wOS.count() > 1) { // Fetch the value of the key value. e.g. Name. final OnmsWbemProperty prop = obj.getWmiProperties().getByName(wpm.getKeyvalue()); final Object propVal = prop.getWmiValue(); String instance = null; if (propVal instanceof String) { instance = (String) propVal; } else { instance = propVal.toString(); } resource = new WmiMultiInstanceCollectionResource(agent, instance, wpm.getResourceType()); } else { resource = new WmiSingleInstanceCollectionResource(agent); } for (final Attrib attrib : wpm.getAttrib()) { final OnmsWbemProperty prop = obj.getWmiProperties().getByName(attrib.getWmiObject()); final WmiCollectionAttributeType attribType = m_attribTypeList.get(attrib.getName()); resource.setAttributeValue(attribType, prop.getWmiValue().toString()); } collectionSet.getResources().add(resource); } } } catch (final WmiException e) { LOG.info("unable to collect params for wpm '{}'", wpm.getName(), e); } finally { if (client != null) { try { client.disconnect(); } catch (final WmiException e) { LOG.warn("An error occurred disconnecting while collecting from WMI.", e); } } } } } collectionSet.setStatus(ServiceCollector.COLLECTION_SUCCEEDED); return collectionSet; }