Search in sources :

Example 1 with XmlDataCollection

use of org.opennms.protocols.xml.config.XmlDataCollection 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 2 with XmlDataCollection

use of org.opennms.protocols.xml.config.XmlDataCollection in project opennms by OpenNMS.

the class XmlCollector method parseCollection.

public XmlDataCollection parseCollection(XmlDataCollection collection, XmlCollectionHandler handler, CollectionAgent agent, Map<String, Object> parameters) {
    // Clone the collection and perform token replacement in the source url and request using the handler
    XmlDataCollection preparsedCollection = collection.clone();
    // Remove blacklisted properties from the map
    Map<String, String> filteredParameters = filterParameters(parameters);
    for (XmlSource source : preparsedCollection.getXmlSources()) {
        final String originalUrlStr = source.getUrl();
        final String parsedUrlStr = handler.parseUrl(m_nodeDao, originalUrlStr, agent, collection.getXmlRrd().getStep(), filteredParameters);
        LOG.debug("parseCollection: original url: '{}', parsed url: '{}' ", originalUrlStr, parsedUrlStr);
        source.setUrl(parsedUrlStr);
        final Request originalRequest = source.getRequest();
        final Request parsedRequest = handler.parseRequest(m_nodeDao, originalRequest, agent, collection.getXmlRrd().getStep(), filteredParameters);
        LOG.debug("parseCollection: original request: '{}', parsed request: '{}' ", originalRequest, parsedRequest);
        source.setRequest(parsedRequest);
    }
    return preparsedCollection;
}
Also used : XmlDataCollection(org.opennms.protocols.xml.config.XmlDataCollection) Request(org.opennms.protocols.xml.config.Request) XmlSource(org.opennms.protocols.xml.config.XmlSource)

Example 3 with XmlDataCollection

use of org.opennms.protocols.xml.config.XmlDataCollection in project opennms by OpenNMS.

the class HttpDataCollectionIT method testCssSelectorHttpCollection.

/**
     * Test HTTP Data Collection with CSS Selector
     *
     * @throws Exception the exception
     */
@Test
@JUnitHttpServer(port = 10342, https = false, webapps = { @Webapp(context = "/junit", path = "src/test/resources/test-webapp") })
public void testCssSelectorHttpCollection() throws Exception {
    File configFile = new File("src/test/resources/http-datacollection-config.xml");
    XmlDataCollectionConfig config = JaxbUtils.unmarshal(XmlDataCollectionConfig.class, configFile);
    XmlDataCollection collection = config.getDataCollectionByName("Http-Market");
    RrdRepository repository = createRrdRepository(collection.getXmlRrd());
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("collection", "Http-Market");
    HttpCollectionHandler collector = new HttpCollectionHandler();
    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/market.jrb"));
    Assert.assertNotNull(jrb);
    Assert.assertEquals(2, jrb.getDsCount());
    Datasource ds = jrb.getDatasource("nasdaq");
    Assert.assertNotNull(ds);
    Assert.assertEquals(new Double(3578.30), 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) HttpCollectionHandler(org.opennms.protocols.http.collector.HttpCollectionHandler) 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 4 with XmlDataCollection

use of org.opennms.protocols.xml.config.XmlDataCollection in project opennms by OpenNMS.

the class XmlCollector method collect.

/* (non-Javadoc)
     * @see org.opennms.netmgt.collectd.ServiceCollector#collect(org.opennms.netmgt.collectd.CollectionAgent, org.opennms.netmgt.model.events.EventProxy, java.util.Map)
     */
@Override
public CollectionSet collect(CollectionAgent agent, Map<String, Object> parameters) throws CollectionException {
    final String rrdRepositoryPath = ParameterMap.getKeyedString(parameters, RRD_REPOSITORY_PATH_KEY, null);
    final XmlDataCollection collection = (XmlDataCollection) parameters.get(XML_DATACOLLECTION_KEY);
    final String serviceName = ParameterMap.getKeyedString(parameters, "SERVICE", "XML");
    final String handlerClass = ParameterMap.getKeyedString(parameters, "handler-class", "org.opennms.protocols.xml.collector.DefaultXmlCollectionHandler");
    final XmlCollectionHandlerKey key = new XmlCollectionHandlerKey(serviceName, handlerClass);
    try {
        // Filling XML CollectionSet
        RrdRepository rrdRepository = XmlDataCollectionConfig.buildRrdRepository(rrdRepositoryPath, collection);
        XmlCollectionHandler handler = m_handlers.get(key);
        handler.setRrdRepository(rrdRepository);
        return handler.collect(agent, collection, parameters);
    } catch (Exception e) {
        throw new CollectionException("Can't collect XML data because " + e.getMessage(), e);
    }
}
Also used : XmlDataCollection(org.opennms.protocols.xml.config.XmlDataCollection) CollectionException(org.opennms.netmgt.collection.api.CollectionException) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) CollectionException(org.opennms.netmgt.collection.api.CollectionException) ExecutionException(java.util.concurrent.ExecutionException) CollectionInitializationException(org.opennms.netmgt.collection.api.CollectionInitializationException)

Example 5 with XmlDataCollection

use of org.opennms.protocols.xml.config.XmlDataCollection in project opennms by OpenNMS.

the class XmlCollector method getRuntimeAttributes.

@Override
public Map<String, Object> getRuntimeAttributes(CollectionAgent agent, Map<String, Object> parameters) {
    final Map<String, Object> runtimeAttributes = new HashMap<>();
    // Construct the handler
    LOG.debug("getRuntimeAttributes: initializing XML collection handling using {} for collection agent {}", parameters, agent);
    String serviceName = ParameterMap.getKeyedString(parameters, "SERVICE", "XML");
    String handlerClass = ParameterMap.getKeyedString(parameters, "handler-class", "org.opennms.protocols.xml.collector.DefaultXmlCollectionHandler");
    XmlCollectionHandlerKey key = new XmlCollectionHandlerKey(serviceName, handlerClass);
    XmlCollectionHandler handler;
    try {
        handler = m_handlers.get(key);
    } catch (ExecutionException e) {
        throw new RuntimeException(e);
    }
    // Retrieve the XML Collection
    String collectionName = ParameterMap.getKeyedString(parameters, "collection", ParameterMap.getKeyedString(parameters, "xml-collection", null));
    if (collectionName == null) {
        throw new IllegalArgumentException("Parameter collection is required for the XML Collector!");
    }
    LOG.debug("getRuntimeAttributes: collecting XML data using collection {} for {}", collectionName, agent);
    XmlDataCollection collection = m_xmlCollectionDao.getDataCollectionByName(collectionName);
    if (collection == null) {
        throw new IllegalArgumentException("XML Collection " + collectionName + " does not exist.");
    }
    // Parse the collection attributes before adding it in the map
    runtimeAttributes.put(XML_DATACOLLECTION_KEY, parseCollection(collection, handler, agent, parameters));
    runtimeAttributes.put(RRD_REPOSITORY_PATH_KEY, m_xmlCollectionDao.getConfig().getRrdRepository());
    return runtimeAttributes;
}
Also used : XmlDataCollection(org.opennms.protocols.xml.config.XmlDataCollection) HashMap(java.util.HashMap) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

XmlDataCollection (org.opennms.protocols.xml.config.XmlDataCollection)10 RrdRepository (org.opennms.netmgt.rrd.RrdRepository)7 HashMap (java.util.HashMap)6 XmlDataCollectionConfig (org.opennms.protocols.xml.config.XmlDataCollectionConfig)6 File (java.io.File)5 Datasource (org.jrobin.core.Datasource)5 RrdDb (org.jrobin.core.RrdDb)5 Test (org.junit.Test)5 JUnitHttpServer (org.opennms.core.test.http.annotations.JUnitHttpServer)5 CollectionSet (org.opennms.netmgt.collection.api.CollectionSet)5 CollectionSetVisitor (org.opennms.netmgt.collection.api.CollectionSetVisitor)5 ServiceParameters (org.opennms.netmgt.collection.api.ServiceParameters)5 ExecutionException (java.util.concurrent.ExecutionException)2 CollectionException (org.opennms.netmgt.collection.api.CollectionException)1 CollectionInitializationException (org.opennms.netmgt.collection.api.CollectionInitializationException)1 HttpCollectionHandler (org.opennms.protocols.http.collector.HttpCollectionHandler)1 DefaultJsonCollectionHandler (org.opennms.protocols.json.collector.DefaultJsonCollectionHandler)1 Request (org.opennms.protocols.xml.config.Request)1 XmlRrd (org.opennms.protocols.xml.config.XmlRrd)1 XmlSource (org.opennms.protocols.xml.config.XmlSource)1