Search in sources :

Example 6 with XmlDataCollection

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

the class HttpDataCollectionIT method testPostRequestHttpCollection.

/**
 * Test HTTP Data Collection with a POST Request
 *
 * @throws Exception the exception
 */
@Test
@JUnitHttpServer(port = 10342, https = false, webapps = { @Webapp(context = "/junit", path = "src/test/resources/test-webapp") })
public void testPostRequestHttpCollection() 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-Person-Stats");
    RrdRepository repository = createRrdRepository(collection.getXmlRrd());
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("collection", "Http-Person-Stats");
    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/person-stats.jrb"));
    Assert.assertNotNull(jrb);
    Assert.assertEquals(3, jrb.getDsCount());
    Datasource ds = jrb.getDatasource("contributions");
    Assert.assertNotNull(ds);
    Assert.assertEquals(new Double(500), 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 7 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 8 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)

Example 9 with XmlDataCollection

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

the class XmlCollectorComplianceTest method getRequiredBeans.

public Map<String, Object> getRequiredBeans() {
    XmlDataCollectionConfig config = mock(XmlDataCollectionConfig.class);
    when(config.getRrdRepository()).thenReturn("target");
    when(config.buildRrdRepository(COLLECTION)).thenReturn(new RrdRepository());
    XmlRrd xmlRrd = new XmlRrd();
    xmlRrd.setStep(300);
    XmlDataCollection collection = new XmlDataCollection();
    collection.setXmlRrd(xmlRrd);
    XmlDataCollectionConfigDao xmlDataCollectionConfigDao = mock(XmlDataCollectionConfigDao.class);
    when(xmlDataCollectionConfigDao.getDataCollectionByName(COLLECTION)).thenReturn(collection);
    when(xmlDataCollectionConfigDao.getConfig()).thenReturn(config);
    return new ImmutableMap.Builder<String, Object>().put("xmlDataCollectionConfigDao", xmlDataCollectionConfigDao).build();
}
Also used : XmlRrd(org.opennms.protocols.xml.config.XmlRrd) XmlDataCollection(org.opennms.protocols.xml.config.XmlDataCollection) XmlDataCollectionConfig(org.opennms.protocols.xml.config.XmlDataCollectionConfig) XmlDataCollectionConfigDao(org.opennms.protocols.xml.dao.XmlDataCollectionConfigDao) RrdRepository(org.opennms.netmgt.rrd.RrdRepository)

Example 10 with XmlDataCollection

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

the class XmlCollectorTestUtils method doCollect.

public static CollectionSet doCollect(NodeDao nodeDao, XmlCollectionHandler handler, CollectionAgent agent, XmlDataCollection collection, Map<String, Object> parameters) throws CollectionException {
    ResourceTypeMapper.getInstance().setResourceTypeMapper(type -> getResourceType(type));
    XmlCollector collector = new XmlCollector();
    collector.setNodeDao(nodeDao);
    XmlDataCollection parsedCollection = collector.parseCollection(collection, handler, agent, parameters);
    return handler.collect(agent, parsedCollection, parameters);
}
Also used : XmlDataCollection(org.opennms.protocols.xml.config.XmlDataCollection)

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