Search in sources :

Example 1 with MBeanServer

use of org.opennms.netmgt.config.jmx.MBeanServer in project opennms by OpenNMS.

the class JmxUtils method getMBeanServer.

public static MBeanServer getMBeanServer(JmxConfigDao jmxConfigDao, String address, Map<String, String> parameters) {
    Objects.requireNonNull(address);
    Objects.requireNonNull(parameters);
    if (jmxConfigDao != null && jmxConfigDao.getConfig() != null) {
        try {
            final JmxConnectionConfig config = JmxConnectionConfigBuilder.buildFrom(address, parameters).build();
            final int port = new JMXServiceURL(config.getUrl()).getPort();
            final MBeanServer mBeanServer = jmxConfigDao.getConfig().lookupMBeanServer(address, port);
            return mBeanServer;
        } catch (MalformedURLException e) {
            LOG.warn("Unexpected exception: {}", e.getMessage(), e);
        }
    }
    // not found or exception
    return null;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MalformedURLException(java.net.MalformedURLException) JmxConnectionConfig(org.opennms.netmgt.jmx.connection.JmxConnectionConfig) MBeanServer(org.opennms.netmgt.config.jmx.MBeanServer)

Example 2 with MBeanServer

use of org.opennms.netmgt.config.jmx.MBeanServer in project opennms by OpenNMS.

the class JMXCollector method getRuntimeAttributes.

@Override
public Map<String, Object> getRuntimeAttributes(CollectionAgent agent, Map<String, Object> parameters) {
    final Map<String, Object> runtimeAttributes = new HashMap<>();
    // Retrieve the name of the JMX data collector
    final String collectionName = ParameterMap.getKeyedString(parameters, ParameterName.COLLECTION.toString(), serviceName);
    final JmxCollection jmxCollection = m_jmxDataCollectionConfigDao.getJmxCollection(collectionName);
    if (jmxCollection == null) {
        throw new IllegalArgumentException(String.format("JMXCollector: No collection found with name '%s'.", collectionName));
    }
    runtimeAttributes.put(JMX_COLLECTION_KEY, jmxCollection);
    // Retrieve the agent config.
    final Map<String, String> parameterStringMap = new HashMap<String, String>();
    for (Map.Entry<String, Object> eachEntry : parameters.entrySet()) {
        if (eachEntry.getValue() instanceof String) {
            parameterStringMap.put(eachEntry.getKey(), (String) eachEntry.getValue());
        }
    }
    final MBeanServer mBeanServer = JmxUtils.getMBeanServer(m_jmxConfigDao, agent.getHostAddress(), parameterStringMap);
    if (mBeanServer != null) {
        runtimeAttributes.put(JMX_MBEAN_SERVER_KEY, mBeanServer);
    }
    return runtimeAttributes;
}
Also used : HashMap(java.util.HashMap) JmxCollection(org.opennms.netmgt.config.collectd.jmx.JmxCollection) HashMap(java.util.HashMap) Map(java.util.Map) ParameterMap(org.opennms.core.utils.ParameterMap) MBeanServer(org.opennms.netmgt.config.jmx.MBeanServer)

Example 3 with MBeanServer

use of org.opennms.netmgt.config.jmx.MBeanServer in project opennms by OpenNMS.

the class JMXCollectorComplianceTest method getRequiredBeans.

@Override
public Map<String, Object> getRequiredBeans() {
    MBeanServer mbeanServer = new MBeanServer();
    JmxConfigDao jmxConfigDao = mock(JmxConfigDao.class, RETURNS_DEEP_STUBS);
    final String host = InetAddrUtils.str(InetAddrUtils.getLocalHostAddress());
    when(jmxConfigDao.getConfig().lookupMBeanServer(host, JmxServerConnector.DEFAULT_OPENNMS_JMX_PORT)).thenReturn(mbeanServer);
    JmxCollection collection = new JmxCollection();
    JMXDataCollectionConfigDao jmxCollectionDao = mock(JMXDataCollectionConfigDao.class, RETURNS_DEEP_STUBS);
    when(jmxCollectionDao.getJmxCollection(COLLECTION)).thenReturn(collection);
    return new ImmutableMap.Builder<String, Object>().put("jmxConfigDao", jmxConfigDao).put("jmxDataCollectionConfigDao", jmxCollectionDao).build();
}
Also used : JMXDataCollectionConfigDao(org.opennms.netmgt.config.JMXDataCollectionConfigDao) JmxCollection(org.opennms.netmgt.config.collectd.jmx.JmxCollection) JmxConfigDao(org.opennms.netmgt.dao.jmx.JmxConfigDao) ImmutableMap(com.google.common.collect.ImmutableMap) MBeanServer(org.opennms.netmgt.config.jmx.MBeanServer)

Example 4 with MBeanServer

use of org.opennms.netmgt.config.jmx.MBeanServer 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);
    // This parent resource used for generic resource
    final NodeLevelResource parentResource = new NodeLevelResource(agent.getNodeId());
    // 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, ObjectName objectName) {
                final String mbeanObjectName = attributeSample.getMbean().getObjectname();
                final String attributeName = attributeSample.getCollectedAttribute().getName();
                final String dsKey = mbeanObjectName + "|" + attributeName;
                final JMXDataSource ds = nodeInfo.getDsMap().get(dsKey);
                if (ds == null) {
                    LOG.info("Could not find datasource for {}. Skipping.", dsKey);
                    return;
                }
                String resourceType = attributeSample.getMbean().getResourceType();
                if (resourceType != null) {
                    final String parsedObjectName = fixGroupName(objectName.getCanonicalName());
                    final Resource resource = new DeferredGenericTypeResource(parentResource, resourceType, parsedObjectName);
                    addNumericAttributeToCollectionSet(ds, attributeSample, resource);
                    addStringAttributesToCollectionSet(ds, attributeSample, resource, objectName);
                } else {
                    addNumericAttributeToCollectionSet(ds, attributeSample, nodeResource);
                }
            }

            @Override
            public void process(JmxCompositeSample compositeSample, ObjectName objectName) {
                final String mbeanObjectName = compositeSample.getMbean().getObjectname();
                final String attributeName = compositeSample.getCollectedAttribute().getName();
                final String dsKey = mbeanObjectName + "|" + attributeName + "|" + compositeSample.getCompositeKey();
                final JMXDataSource ds = nodeInfo.getDsMap().get(dsKey);
                if (ds == null) {
                    LOG.info("Could not find datasource for {}. Skipping.", dsKey);
                    return;
                }
                String resourceType = compositeSample.getMbean().getResourceType();
                if (resourceType != null) {
                    final String parsedObjectName = fixGroupName(objectName.getCanonicalName());
                    final Resource resource = new DeferredGenericTypeResource(parentResource, resourceType, parsedObjectName);
                    addNumericAttributeToCollectionSet(ds, compositeSample, resource);
                    addStringAttributesToCollectionSet(ds, compositeSample, resource, objectName);
                } else {
                    addNumericAttributeToCollectionSet(ds, compositeSample, nodeResource);
                }
            }

            private void addStringAttributesToCollectionSet(JMXDataSource ds, AbstractJmxSample sample, Resource resource, ObjectName objectName) {
                final String groupName = fixGroupName(JmxUtils.getGroupName(stringMap, sample.getMbean()));
                final String domain = objectName.getDomain();
                final Hashtable<String, String> properties = objectName.getKeyPropertyList();
                properties.forEach((key, value) -> collectionSetBuilder.withStringAttribute(resource, groupName, key, value));
                if (domain != null) {
                    collectionSetBuilder.withStringAttribute(resource, groupName, "domain", objectName.getDomain());
                }
            }

            private void addNumericAttributeToCollectionSet(JMXDataSource ds, AbstractJmxSample sample, Resource resource) {
                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(resource, 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 : CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) AlphaNumeric(org.opennms.core.utils.AlphaNumeric) JmxCollection(org.opennms.netmgt.config.collectd.jmx.JmxCollection) AbstractJmxSample(org.opennms.netmgt.jmx.samples.AbstractJmxSample) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) DeferredGenericTypeResource(org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource) NumericAttributeUtils(org.opennms.netmgt.collection.support.NumericAttributeUtils) InetAddress(java.net.InetAddress) DefaultJmxCollector(org.opennms.netmgt.jmx.impl.DefaultJmxCollector) MBeanServer(org.opennms.netmgt.config.jmx.MBeanServer) Map(java.util.Map) JmxUtils(org.opennms.netmgt.jmx.JmxUtils) JMXDataCollectionConfigDao(org.opennms.netmgt.config.JMXDataCollectionConfigDao) SimpleEntry(java.util.AbstractMap.SimpleEntry) Hashtable(java.util.Hashtable) ParameterMap(org.opennms.core.utils.ParameterMap) JmxCollectorConfig(org.opennms.netmgt.jmx.JmxCollectorConfig) JmxCompositeSample(org.opennms.netmgt.jmx.samples.JmxCompositeSample) Resource(org.opennms.netmgt.collection.support.builder.Resource) JmxAttributeSample(org.opennms.netmgt.jmx.samples.JmxAttributeSample) Logger(org.slf4j.Logger) InetAddressUtils(org.opennms.core.utils.InetAddressUtils) AbstractRemoteServiceCollector(org.opennms.netmgt.collection.api.AbstractRemoteServiceCollector) ObjectName(javax.management.ObjectName) JmxConfigDao(org.opennms.netmgt.dao.jmx.JmxConfigDao) Collectors(java.util.stream.Collectors) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) BeanUtils(org.opennms.core.spring.BeanUtils) Objects(java.util.Objects) List(java.util.List) JmxSampleProcessor(org.opennms.netmgt.jmx.JmxSampleProcessor) Stream(java.util.stream.Stream) ParameterName(org.opennms.netmgt.collection.api.ServiceParameters.ParameterName) Attrib(org.opennms.netmgt.config.collectd.jmx.Attrib) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) JmxConnectors(org.opennms.netmgt.jmx.connection.JmxConnectors) Collections(java.util.Collections) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) 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) MBeanServer(org.opennms.netmgt.config.jmx.MBeanServer) DefaultJmxCollector(org.opennms.netmgt.jmx.impl.DefaultJmxCollector) JmxCollectorConfig(org.opennms.netmgt.jmx.JmxCollectorConfig) Hashtable(java.util.Hashtable) JmxSampleProcessor(org.opennms.netmgt.jmx.JmxSampleProcessor) JmxCompositeSample(org.opennms.netmgt.jmx.samples.JmxCompositeSample) DeferredGenericTypeResource(org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource) Resource(org.opennms.netmgt.collection.support.builder.Resource) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) ObjectName(javax.management.ObjectName) DeferredGenericTypeResource(org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource) JmxAttributeSample(org.opennms.netmgt.jmx.samples.JmxAttributeSample) InetAddress(java.net.InetAddress)

Aggregations

MBeanServer (org.opennms.netmgt.config.jmx.MBeanServer)4 JmxCollection (org.opennms.netmgt.config.collectd.jmx.JmxCollection)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ParameterMap (org.opennms.core.utils.ParameterMap)2 JMXDataCollectionConfigDao (org.opennms.netmgt.config.JMXDataCollectionConfigDao)2 JmxConfigDao (org.opennms.netmgt.dao.jmx.JmxConfigDao)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 InetAddress (java.net.InetAddress)1 MalformedURLException (java.net.MalformedURLException)1 SimpleEntry (java.util.AbstractMap.SimpleEntry)1 Collections (java.util.Collections)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 ObjectName (javax.management.ObjectName)1 JMXServiceURL (javax.management.remote.JMXServiceURL)1 BeanUtils (org.opennms.core.spring.BeanUtils)1