Search in sources :

Example 31 with CollectionSet

use of org.opennms.netmgt.collection.api.CollectionSet in project opennms by OpenNMS.

the class CollectorRequestBuilderImpl method execute.

@Override
public CompletableFuture<CollectionSet> execute() {
    if (serviceCollector == null) {
        throw new IllegalArgumentException("Collector or collector class name is required.");
    } else if (agent == null) {
        throw new IllegalArgumentException("Agent is required.");
    }
    final RpcTarget target = client.getRpcTargetHelper().target().withNodeId(agent.getNodeId()).withLocation(agent.getLocationName()).withSystemId(systemId).withServiceAttributes(attributes).withLocationOverride((s) -> serviceCollector.getEffectiveLocation(s)).build();
    CollectorRequestDTO request = new CollectorRequestDTO();
    request.setLocation(target.getLocation());
    request.setSystemId(target.getSystemId());
    request.setClassName(serviceCollector.getClass().getCanonicalName());
    request.setTimeToLiveMs(ttlInMs);
    // Retrieve the runtime attributes, which may include attributes
    // such as the agent details and other state related attributes
    // which should be included in the request
    final Map<String, Object> runtimeAttributes = serviceCollector.getRuntimeAttributes(agent, attributes);
    final Map<String, Object> allAttributes = new HashMap<>();
    allAttributes.putAll(attributes);
    allAttributes.putAll(runtimeAttributes);
    // Only marshal these if the request is being executed at another location.
    if (MonitoringLocationUtils.isDefaultLocationName(request.getLocation())) {
        // As-is
        request.setAgent(agent);
        request.addAttributes(allAttributes);
    } else {
        // Marshal
        request.setAgent(new CollectionAgentDTO(agent));
        final Map<String, String> marshaledParms = serviceCollector.marshalParameters(allAttributes);
        marshaledParms.forEach(request::addAttribute);
        request.setAttributesNeedUnmarshaling(true);
    }
    // Execute the request
    return client.getDelegate().execute(request).thenApply(CollectorResponseDTO::getCollectionSet);
}
Also used : ServiceCollector(org.opennms.netmgt.collection.api.ServiceCollector) Objects(java.util.Objects) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) CollectorRequestBuilder(org.opennms.netmgt.collection.api.CollectorRequestBuilder) MonitoringLocationUtils(org.opennms.netmgt.dao.api.MonitoringLocationUtils) CollectionAgentDTO(org.opennms.netmgt.collection.dto.CollectionAgentDTO) Map(java.util.Map) RpcTarget(org.opennms.core.rpc.api.RpcTarget) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) HashMap(java.util.HashMap) CollectionAgentDTO(org.opennms.netmgt.collection.dto.CollectionAgentDTO) RpcTarget(org.opennms.core.rpc.api.RpcTarget)

Example 32 with CollectionSet

use of org.opennms.netmgt.collection.api.CollectionSet 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)

Example 33 with CollectionSet

use of org.opennms.netmgt.collection.api.CollectionSet in project opennms by OpenNMS.

the class JMXCollectorTest method collectJvmDefaultComposites.

/**
 * 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<>();
    attributes.add("CollectionCount");
    attributes.add("LastGcInfo");
    beanInfo.setAttributes(attributes);
    List<String> compositeAttributes = new ArrayList<>();
    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());
    dataSourceMap.put(mBeansObjectName + "|LastGcInfo", new JMXDataSource());
    jmxNodeInfo.setDsMap(dataSourceMap);
    CollectionSet collectionSet = jmxCollector.collect(collectionAgent, Collections.emptyMap());
    assertEquals("Collection of one Jvm default value failed", CollectionStatus.SUCCEEDED, collectionSet.getStatus());
}
Also used : HashMap(java.util.HashMap) BeanInfo(org.opennms.netmgt.config.BeanInfo) ArrayList(java.util.ArrayList) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) JmxTest(org.opennms.netmgt.collectd.jmxhelper.JmxTest) Test(org.junit.Test)

Example 34 with CollectionSet

use of org.opennms.netmgt.collection.api.CollectionSet in project opennms by OpenNMS.

the class JMXCollectorTest method collect.

private Map<String, Map<String, CollectionAttribute>> collect(String collectionName) {
    final Map<String, Object> parms = new HashMap<String, Object>();
    parms.put(ParameterName.COLLECTION.toString(), collectionName);
    parms.putAll(jmxCollector.getRuntimeAttributes(collectionAgent, parms));
    // start collection
    final CollectionSet collectionSet = jmxCollector.collect(collectionAgent, parms);
    assertEquals("Collection: " + collectionName + " failed", CollectionStatus.SUCCEEDED, collectionSet.getStatus());
    return CollectionSetUtils.getAttributesByNameByGroup(collectionSet);
}
Also used : HashMap(java.util.HashMap) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet)

Example 35 with CollectionSet

use of org.opennms.netmgt.collection.api.CollectionSet in project opennms by OpenNMS.

the class JMXCollectorTest method collectJvmMbeansWithWildCard.

@Test
public void collectJvmMbeansWithWildCard() {
    final Map<String, Object> parms = new HashMap<String, Object>();
    parms.put(ParameterName.COLLECTION.toString(), "collectBasicJvmValues");
    parms.putAll(jmxCollector.getRuntimeAttributes(collectionAgent, parms));
    CollectionSet collectionSet = jmxCollector.collect(collectionAgent, parms);
    assertEquals("Collection of jvm values failed", CollectionStatus.SUCCEEDED, collectionSet.getStatus());
    ResourceType rt = mock(ResourceType.class, RETURNS_DEEP_STUBS);
    when(rt.getName()).thenReturn("jvm");
    when(rt.getStorageStrategy().getClazz()).thenReturn(MockStorageStrategy.class.getCanonicalName());
    when(rt.getPersistenceSelectorStrategy().getClazz()).thenReturn(MockPersistenceSelectorStrategy.class.getCanonicalName());
    ResourceTypeMapper.getInstance().setResourceTypeMapper((name) -> rt);
    Map<String, Map<String, CollectionAttribute>> attributesByNameByGroup = CollectionSetUtils.getAttributesByNameByGroup(collectionSet);
    // 2 attributes from the defined Numeric attributes and 3 string
    // attributes, domain, type, name
    assertEquals(5, attributesByNameByGroup.get("java_lang_type_GarbageCollector_name__").size());
    Map<String, CollectionAttribute> attributes = attributesByNameByGroup.get("java_lang_type_GarbageCollector_name__");
    CollectionAttribute attribute1 = attributes.get("domain");
    CollectionAttribute attribute2 = attributes.get("type");
    assertEquals("java.lang", attribute1.getStringValue());
    assertEquals("GarbageCollector", attribute2.getStringValue());
}
Also used : CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) HashMap(java.util.HashMap) ResourceType(org.opennms.netmgt.collection.api.ResourceType) HashMap(java.util.HashMap) Map(java.util.Map) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) JmxTest(org.opennms.netmgt.collectd.jmxhelper.JmxTest) Test(org.junit.Test)

Aggregations

CollectionSet (org.opennms.netmgt.collection.api.CollectionSet)52 Test (org.junit.Test)30 HashMap (java.util.HashMap)17 CollectionAgent (org.opennms.netmgt.collection.api.CollectionAgent)17 CollectionSetBuilder (org.opennms.netmgt.collection.support.builder.CollectionSetBuilder)11 NodeLevelResource (org.opennms.netmgt.collection.support.builder.NodeLevelResource)11 CollectionSetVisitor (org.opennms.netmgt.collection.api.CollectionSetVisitor)10 ServiceParameters (org.opennms.netmgt.collection.api.ServiceParameters)10 File (java.io.File)9 RrdRepository (org.opennms.netmgt.rrd.RrdRepository)8 RrdDb (org.jrobin.core.RrdDb)7 JUnitCollector (org.opennms.core.collection.test.JUnitCollector)7 OnmsNode (org.opennms.netmgt.model.OnmsNode)7 JUnitHttpServer (org.opennms.core.test.http.annotations.JUnitHttpServer)6 JUnitSnmpAgent (org.opennms.core.test.snmp.annotations.JUnitSnmpAgent)6 Map (java.util.Map)5 Datasource (org.jrobin.core.Datasource)5 Date (java.util.Date)4 GenericTypeResource (org.opennms.netmgt.collection.support.builder.GenericTypeResource)4 ResourcePath (org.opennms.netmgt.model.ResourcePath)4