Search in sources :

Example 1 with JmxAttributeSample

use of org.opennms.netmgt.jmx.samples.JmxAttributeSample in project opennms by OpenNMS.

the class DefaultJmxCollector method collect.

private void collect(MBeanServerConnection concreteConnection, JmxCollection jmxCollection, JmxSampleProcessor sampleProcessor) {
    if (jmxCollection == null || jmxCollection.getMbeanCount() < 1) {
        logger.info("No MBeans to collect.");
        return;
    }
    try {
        for (Mbean eachMbean : jmxCollection.getMbeans()) {
            logger.debug("Collecting MBean (objectname={}, wildcard={})", eachMbean.getObjectname(), isWildcard(eachMbean.getObjectname()));
            final Collection<ObjectName> objectNames = getObjectNames(concreteConnection, eachMbean.getObjectname());
            for (ObjectName eachObjectName : objectNames) {
                logger.debug("Collecting ObjectName {}", eachObjectName);
                boolean collect = canBeCollected(concreteConnection, eachObjectName, eachMbean.getKeyfield(), eachMbean.getExclude());
                if (collect) {
                    List<String> attributeNames = extractAttributeNames(eachMbean);
                    List<Attribute> attributes = getAttributes(concreteConnection, eachObjectName, attributeNames);
                    for (Attribute eachAttribute : attributes) {
                        if (eachAttribute.getValue() instanceof CompositeData) {
                            CompositeData compositeData = (CompositeData) eachAttribute.getValue();
                            for (CompMember eachCompositeMember : getCompositeMembers(eachMbean, eachAttribute.getName())) {
                                JmxCompositeSample sample = new JmxCompositeSample(eachMbean, eachAttribute, compositeData, eachCompositeMember);
                                logger.debug("Collected sample {}", sample);
                                sampleProcessor.process(sample);
                            }
                        } else {
                            JmxAttributeSample sample = new JmxAttributeSample(eachMbean, eachAttribute);
                            logger.debug("Collected sample {}", sample);
                            sampleProcessor.process(sample);
                        }
                    }
                } else {
                    logger.debug("Skip ObjectName {}", eachObjectName);
                }
            }
        }
    } catch (JMException e) {
        logger.error("Could not collect data", e);
    } catch (IOException e) {
        logger.error("Could not communicate with MBeanServer", e);
    }
}
Also used : Attribute(javax.management.Attribute) CompositeData(javax.management.openmbean.CompositeData) JmxCompositeSample(org.opennms.netmgt.jmx.samples.JmxCompositeSample) IOException(java.io.IOException) ObjectName(javax.management.ObjectName) CompMember(org.opennms.netmgt.config.collectd.jmx.CompMember) Mbean(org.opennms.netmgt.config.collectd.jmx.Mbean) JMException(javax.management.JMException) JmxAttributeSample(org.opennms.netmgt.jmx.samples.JmxAttributeSample)

Example 2 with JmxAttributeSample

use of org.opennms.netmgt.jmx.samples.JmxAttributeSample in project opennms by OpenNMS.

the class JMXCollector method collect.

@Override
public CollectionSet collect(CollectionAgent agent, Map<String, Object> map) {
    final Map<String, String> stringMap = JmxUtils.convertToUnmodifiableStringMap(map);
    final InetAddress ipaddr = agent.getAddress();
    final JmxCollection jmxCollection = (JmxCollection) map.get(JMX_COLLECTION_KEY);
    final MBeanServer mBeanServer = (MBeanServer) map.get(JMX_MBEAN_SERVER_KEY);
    final String collectionName = ParameterMap.getKeyedString(map, ParameterName.COLLECTION.toString(), serviceName);
    final String port = ParameterMap.getKeyedString(map, ParameterName.PORT.toString(), null);
    final String friendlyName = ParameterMap.getKeyedString(map, ParameterName.FRIENDLY_NAME.toString(), port);
    final String collDir = JmxUtils.getCollectionDirectory(stringMap, friendlyName, serviceName);
    final int retries = ParameterMap.getKeyedInteger(map, ParameterName.RETRY.toString(), 3);
    InetAddress ipAddr = agent.getAddress();
    int nodeID = agent.getNodeId();
    // Retrieve the name of the JMX data collector
    final String hostAddress = InetAddressUtils.str(ipAddr);
    LOG.debug("initialize: InetAddress={}, collectionName={}", hostAddress, collectionName);
    JMXNodeInfo nodeInfo = new JMXNodeInfo(nodeID);
    LOG.debug("nodeInfo: {} {} {}", 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 = JMXDataCollectionConfigDao.getAttributeMap(jmxCollection, serviceName(), hostAddress);
    nodeInfo.setAttributeMap(attrMap);
    Map<String, JMXDataSource> dsList = buildDataSourceList(collectionName, attrMap);
    nodeInfo.setDsMap(dsList);
    nodeInfo.setMBeans(JMXDataCollectionConfigDao.getMBeanInfo(jmxCollection));
    // Metrics collected from JMX are currently modeled as node level resources,
    // but live in a sub-directory set to the service name
    final NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId(), collDir);
    // Used to gather the results
    final CollectionSetBuilder collectionSetBuilder = new CollectionSetBuilder(agent);
    LOG.debug("connecting to {} on node ID {}", InetAddressUtils.str(ipaddr), nodeInfo.getNodeId());
    try {
        // create config for JmxCollector
        final JmxCollectorConfig config = new JmxCollectorConfig();
        config.setAgentAddress(InetAddressUtils.str(ipaddr));
        config.setConnectionName(getConnectionName());
        config.setRetries(retries);
        config.setServiceProperties(stringMap);
        config.setJmxCollection(jmxCollection);
        final DefaultJmxCollector jmxCollector = new DefaultJmxCollector();
        jmxCollector.collect(config, mBeanServer, new JmxSampleProcessor() {

            @Override
            public void process(JmxAttributeSample attributeSample) {
                final String objectName = attributeSample.getMbean().getObjectname();
                final String attributeName = attributeSample.getCollectedAttribute().getName();
                final String dsKey = objectName + "|" + attributeName;
                final JMXDataSource ds = nodeInfo.getDsMap().get(dsKey);
                if (ds == null) {
                    LOG.info("Could not find datasource for {}. Skipping.", dsKey);
                    return;
                }
                addNumericAttributeToCollectionSet(ds, attributeSample);
            }

            @Override
            public void process(JmxCompositeSample compositeSample) {
                final String objectName = compositeSample.getMbean().getObjectname();
                final String attributeName = compositeSample.getCollectedAttribute().getName();
                final String dsKey = objectName + "|" + attributeName + "|" + compositeSample.getCompositeKey();
                final JMXDataSource ds = nodeInfo.getDsMap().get(dsKey);
                if (ds == null) {
                    LOG.info("Could not find datasource for {}. Skipping.", dsKey);
                    return;
                }
                addNumericAttributeToCollectionSet(ds, compositeSample);
            }

            private void addNumericAttributeToCollectionSet(JMXDataSource ds, AbstractJmxSample sample) {
                final String groupName = fixGroupName(JmxUtils.getGroupName(stringMap, sample.getMbean()));
                // Only numeric data comes back from JMX in data collection
                final String valueAsString = sample.getCollectedValueAsString();
                final Double value = NumericAttributeUtils.parseNumericValue(valueAsString);
                // Construct the metric identifier (used by NRTG)
                String metricId = groupName;
                metricId = metricId.replace("_type_", ":type=");
                metricId = metricId.replace("_", ".");
                metricId = metricId.concat(".");
                metricId = metricId.concat(ds.getName());
                metricId = "JMX_".concat(metricId);
                collectionSetBuilder.withIdentifiedNumericAttribute(nodeResource, groupName, ds.getName(), value, ds.getType(), metricId);
            }
        });
    } catch (final Exception e) {
        LOG.debug("{} Collector.collect: IOException while collecting address: {}", serviceName, agent.getAddress(), e);
    }
    return collectionSetBuilder.build();
}
Also used : DefaultJmxCollector(org.opennms.netmgt.jmx.impl.DefaultJmxCollector) CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) JmxCollectorConfig(org.opennms.netmgt.jmx.JmxCollectorConfig) JmxSampleProcessor(org.opennms.netmgt.jmx.JmxSampleProcessor) JmxCompositeSample(org.opennms.netmgt.jmx.samples.JmxCompositeSample) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) JmxCollection(org.opennms.netmgt.config.collectd.jmx.JmxCollection) AbstractJmxSample(org.opennms.netmgt.jmx.samples.AbstractJmxSample) List(java.util.List) JmxAttributeSample(org.opennms.netmgt.jmx.samples.JmxAttributeSample) InetAddress(java.net.InetAddress) MBeanServer(org.opennms.netmgt.config.jmx.MBeanServer)

Aggregations

JmxAttributeSample (org.opennms.netmgt.jmx.samples.JmxAttributeSample)2 JmxCompositeSample (org.opennms.netmgt.jmx.samples.JmxCompositeSample)2 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 List (java.util.List)1 Attribute (javax.management.Attribute)1 JMException (javax.management.JMException)1 ObjectName (javax.management.ObjectName)1 CompositeData (javax.management.openmbean.CompositeData)1 CollectionSetBuilder (org.opennms.netmgt.collection.support.builder.CollectionSetBuilder)1 NodeLevelResource (org.opennms.netmgt.collection.support.builder.NodeLevelResource)1 CompMember (org.opennms.netmgt.config.collectd.jmx.CompMember)1 JmxCollection (org.opennms.netmgt.config.collectd.jmx.JmxCollection)1 Mbean (org.opennms.netmgt.config.collectd.jmx.Mbean)1 MBeanServer (org.opennms.netmgt.config.jmx.MBeanServer)1 JmxCollectorConfig (org.opennms.netmgt.jmx.JmxCollectorConfig)1 JmxSampleProcessor (org.opennms.netmgt.jmx.JmxSampleProcessor)1 DefaultJmxCollector (org.opennms.netmgt.jmx.impl.DefaultJmxCollector)1 AbstractJmxSample (org.opennms.netmgt.jmx.samples.AbstractJmxSample)1