Search in sources :

Example 21 with CollectionSet

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

the class NMS7963IT method testHttpCollection.

/**
 * Test HTTP Data Collection with XPath
 *
 * @throws Exception the exception
 */
@Test
@JUnitHttpServer(port = 10342, https = false, webapps = { @Webapp(context = "/junit", path = "src/test/resources/test-webapp") })
public void testHttpCollection() throws Exception {
    File configFile = new File("src/test/resources/http-datacollection-config.xml");
    XmlDataCollectionConfig config = JaxbUtils.unmarshal(XmlDataCollectionConfig.class, configFile);
    XmlDataCollection collection = config.getDataCollectionByName("NMS-7963");
    RrdRepository repository = createRrdRepository(collection.getXmlRrd());
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("collection", "NMS-7963");
    DefaultXmlCollectionHandler collector = new DefaultXmlCollectionHandler();
    collector.setRrdRepository(repository);
    collector.setServiceName("HTTP");
    CollectionSet collectionSet = XmlCollectorTestUtils.doCollect(m_nodeDao, collector, m_collectionAgent, collection, parameters);
    Assert.assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
    ServiceParameters serviceParams = new ServiceParameters(new HashMap<String, Object>());
    CollectionSetVisitor persister = m_persisterFactory.createGroupPersister(serviceParams, repository, false, false);
    collectionSet.visit(persister);
    RrdDb jrb = new RrdDb(new File(getSnmpRoot(), "1/xml-retrv-wipo-data.jrb"));
    Assert.assertNotNull(jrb);
    Assert.assertEquals(1, jrb.getDsCount());
    Datasource ds = jrb.getDatasource("xml-wipo-paco");
    Assert.assertNotNull(ds);
    Assert.assertEquals(new Double(903), Double.valueOf(ds.getLastValue()));
}
Also used : Datasource(org.jrobin.core.Datasource) XmlDataCollectionConfig(org.opennms.protocols.xml.config.XmlDataCollectionConfig) HashMap(java.util.HashMap) CollectionSetVisitor(org.opennms.netmgt.collection.api.CollectionSetVisitor) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) XmlDataCollection(org.opennms.protocols.xml.config.XmlDataCollection) RrdDb(org.jrobin.core.RrdDb) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) File(java.io.File) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 22 with CollectionSet

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

the class NodeLevelDataOnMultipleNodesTest method executeCollectorTest.

/**
 * Executes collector test.
 *
 * @param nodeId the node id
 * @param ipAddress the IP address
 * @param xmlSampleFileName the XML sample file name
 * @param parameters the parameters
 * @param expectedFiles the expected amount of JRB files
 * @throws Exception the exception
 */
public void executeCollectorTest(int nodeId, String ipAddress, String xmlSampleFileName, Map<String, Object> parameters, int expectedFiles) throws Exception {
    MockDocumentBuilder.setXmlFileName(xmlSampleFileName);
    CollectionAgent collectionAgent = new MockCollectionAgent(nodeId, "mynode", InetAddrUtils.addr(ipAddress));
    CollectionSet collectionSet = XmlCollectorTestUtils.doCollect(m_collector, collectionAgent, parameters);
    Assert.assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
    ServiceParameters serviceParams = new ServiceParameters(new HashMap<String, Object>());
    CollectionSetVisitor persister = m_persisterFactory.createGroupPersister(serviceParams, createRrdRepository((String) parameters.get("collection")), false, false);
    collectionSet.visit(persister);
    Assert.assertEquals(expectedFiles, FileUtils.listFiles(new File(getSnmpRoot(), Integer.toString(nodeId)), new String[] { getRrdExtension() }, true).size());
}
Also used : CollectionSetVisitor(org.opennms.netmgt.collection.api.CollectionSetVisitor) MockCollectionAgent(org.opennms.core.collection.test.MockCollectionAgent) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) MockCollectionAgent(org.opennms.core.collection.test.MockCollectionAgent) File(java.io.File) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet)

Example 23 with CollectionSet

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

the class CollectableService method doCollection.

/**
 * Perform data collection.
 */
private void doCollection() throws CollectionException {
    LOG.info("run: starting new collection for {}/{}/{}/{}", m_nodeId, getHostAddress(), m_spec.getServiceName(), m_spec.getPackageName());
    CollectionSet result = null;
    try {
        result = m_spec.collect(m_agent);
        if (result != null) {
            Collectd.instrumentation().beginPersistingServiceData(m_spec.getPackageName(), m_nodeId, getHostAddress(), m_spec.getServiceName());
            try {
                CollectionSetVisitor persister = m_persisterFactory.createPersister(m_params, m_repository, result.ignorePersist(), false, false);
                if (Boolean.getBoolean(USE_COLLECTION_START_TIME_SYS_PROP)) {
                    final ConstantTimeKeeper timeKeeper = new ConstantTimeKeeper(new Date(m_lastScheduledCollectionTime));
                    // Wrap the persister visitor such that calls to CollectionResource.getTimeKeeper() return the given timeKeeper
                    persister = wrapResourcesWithTimekeeper(persister, timeKeeper);
                }
                result.visit(persister);
            } finally {
                Collectd.instrumentation().endPersistingServiceData(m_spec.getPackageName(), m_nodeId, getHostAddress(), m_spec.getServiceName());
            }
            /*
                         * Do the thresholding; this could be made more generic (listeners being passed the collectionset), but frankly, why bother?
                         * The first person who actually needs to configure that sort of thing on the fly can code it up.
                         */
            if (m_thresholdVisitor != null) {
                if (m_thresholdVisitor.isNodeInOutage()) {
                    LOG.info("run: the threshold processing will be skipped because the node {} is on a scheduled outage.", m_nodeId);
                } else if (m_thresholdVisitor.hasThresholds()) {
                    // Required to reinitialize the counters.
                    m_thresholdVisitor.setCounterReset(result.ignorePersist());
                    result.visit(m_thresholdVisitor);
                }
            }
            if (!CollectionStatus.SUCCEEDED.equals(result.getStatus())) {
                throw new CollectionFailed(result.getStatus());
            }
        }
    } catch (CollectionException e) {
        LOG.warn("run: failed collection for {}/{}/{}/{}", m_nodeId, getHostAddress(), m_spec.getServiceName(), m_spec.getPackageName());
        throw e;
    } catch (Throwable t) {
        LOG.warn("run: failed collection for {}/{}/{}/{}", m_nodeId, getHostAddress(), m_spec.getServiceName(), m_spec.getPackageName());
        throw new CollectionException("An undeclared throwable was caught during data collection for interface " + m_nodeId + "/" + getHostAddress() + "/" + m_spec.getServiceName(), t);
    }
    LOG.info("run: finished collection for {}/{}/{}/{}", m_nodeId, getHostAddress(), m_spec.getServiceName(), m_spec.getPackageName());
}
Also used : CollectionException(org.opennms.netmgt.collection.api.CollectionException) CollectionSetVisitor(org.opennms.netmgt.collection.api.CollectionSetVisitor) CollectionFailed(org.opennms.netmgt.collection.api.CollectionFailed) ConstantTimeKeeper(org.opennms.netmgt.collection.support.ConstantTimeKeeper) Date(java.util.Date) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet)

Example 24 with CollectionSet

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

the class StressCommand method generateAndPersistCollectionSets.

private Callable<Void> generateAndPersistCollectionSets(ServiceParameters params, RrdRepository repository, int generatorThreadId, int sleepTimeInMillisBetweenNodes) {
    return new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            for (int nodeId = 0; nodeId < numberOfNodes; nodeId++) {
                if (nodeId % numberOfGeneratorThreads != generatorThreadId) {
                    // A different generator will handle this node
                    continue;
                }
                // Build the node resource
                CollectionAgent agent = new MockCollectionAgent(nodeId);
                NodeLevelResource nodeResource = new NodeLevelResource(nodeId);
                // Don't reuse the persister instance across nodes to help simulate collectd's actual behavior
                Persister persister = persisterFactory.createPersister(params, repository);
                for (int interfaceId = 0; interfaceId < numberOfInterfacesPerNode; interfaceId++) {
                    // Return immediately if the abort flag is set
                    if (abort.get()) {
                        return null;
                    }
                    // Build the interface resource
                    InterfaceLevelResource interfaceResource = new InterfaceLevelResource(nodeResource, "tap" + interfaceId);
                    // Generate the collection set
                    CollectionSet collectionSet = generateCollectionSet(agent, nodeId, interfaceId, interfaceResource);
                    // Persist
                    collectionSet.visit(persister);
                }
                Thread.sleep(sleepTimeInMillisBetweenNodes);
            }
            return null;
        }
    };
}
Also used : InterfaceLevelResource(org.opennms.netmgt.collection.support.builder.InterfaceLevelResource) Persister(org.opennms.netmgt.collection.api.Persister) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) Callable(java.util.concurrent.Callable) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet)

Example 25 with CollectionSet

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

the class JdbcCollectorTest method canCollectEmptyCollection.

@Test
public void canCollectEmptyCollection() throws Exception {
    JdbcDataCollection collection = new JdbcDataCollection();
    CollectionSet collectionSet = collect(collection);
    assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
    assertEquals(0, CollectionSetUtils.getAttributesByName(collectionSet).size());
}
Also used : JdbcDataCollection(org.opennms.netmgt.config.jdbc.JdbcDataCollection) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) 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