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()));
}
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;
}
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()));
}
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);
}
}
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;
}
Aggregations