use of org.opennms.netmgt.collection.api.CollectionSetVisitor 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());
}
use of org.opennms.netmgt.collection.api.CollectionSetVisitor in project opennms by OpenNMS.
the class CollectableService method wrapResourcesWithTimekeeper.
public static CollectionSetVisitor wrapResourcesWithTimekeeper(CollectionSetVisitor visitor, TimeKeeper timeKeeper) {
// Wrap the given visitor and intercept the calls to visit the resources
final CollectionSetVisitor wrappedVisitor = new CollectionSetVisitorWrapper(visitor) {
private CollectionResource wrappedResource;
private CollectionAttribute wrappedAttribute;
private AttributeGroup wrappedGroup;
@Override
public void visitResource(CollectionResource resource) {
// Wrap the given resource and return the custom timekeeper
wrappedResource = new CollectionResourceWrapper(resource) {
@Override
public TimeKeeper getTimeKeeper() {
return timeKeeper;
}
};
visitor.visitResource(wrappedResource);
}
@Override
public void completeResource(CollectionResource resource) {
visitor.completeResource(wrappedResource);
}
@Override
public void visitAttribute(CollectionAttribute attribute) {
// Wrap the given attribute and return the custom resource
wrappedAttribute = new CollectionAttributeWrapper(attribute) {
@Override
public CollectionResource getResource() {
return wrappedResource;
}
};
visitor.visitAttribute(wrappedAttribute);
}
@Override
public void completeAttribute(CollectionAttribute attribute) {
visitor.completeAttribute(wrappedAttribute);
}
@Override
public void visitGroup(AttributeGroup group) {
// Wrap the given attribute group and return the custom resource
wrappedGroup = new AttributeGroupWrapper(group) {
@Override
public CollectionResource getResource() {
return wrappedResource;
}
};
visitor.visitGroup(wrappedGroup);
}
@Override
public void completeGroup(AttributeGroup group) {
visitor.completeGroup(wrappedGroup);
}
};
return wrappedVisitor;
}
use of org.opennms.netmgt.collection.api.CollectionSetVisitor in project opennms by OpenNMS.
the class TcaCollectorIT method testCollector.
/**
* Test collector.
*
* @throws Exception the exception
*/
@Test
public void testCollector() throws Exception {
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("collection", "default");
// Create Collection Set
TcaCollector collector = new TcaCollector();
collector.setConfigDao(m_configDao);
collector.setResourceStorageDao(m_resourceStorageDao);
collector.setResourceTypesDao(m_resourceTypesDao);
collector.setLocationAwareSnmpClient(m_client);
CollectionSetVisitor persister = m_persisterFactory.createOneToOnePersister(new ServiceParameters(parameters), collector.getRrdRepository("default"), false, false);
// Setup SNMP Value Handling
SnmpValueFactory valFac = SnmpUtils.getValueFactory();
SnmpObjId peer1 = SnmpObjId.get(".1.3.6.1.4.1.27091.3.1.6.1.2.171.19.37.60");
SnmpObjId peer2 = SnmpObjId.get(".1.3.6.1.4.1.27091.3.1.6.1.2.171.19.38.70");
// Collect and Persist Data - Step 1
CollectionSet collectionSet = collector.collect(m_collectionAgent, parameters);
validateCollectionSet(collectionSet);
collectionSet.visit(persister);
// Generate new SNMP Data
StringBuffer sb = new StringBuffer("|25|");
long ts = 1327451787l;
for (int i = 0; i < 25; i++) {
sb.append(ts++);
sb.append(",12,-1,12,-2,1|");
}
// Get Current Values
SnmpValue v1a = SnmpUtils.get(m_collectionAgent.getAgentConfig(), peer1);
SnmpValue v2a = SnmpUtils.get(m_collectionAgent.getAgentConfig(), peer2);
// Set New Values
SnmpUtils.set(m_collectionAgent.getAgentConfig(), peer1, valFac.getOctetString(sb.toString().getBytes()));
SnmpUtils.set(m_collectionAgent.getAgentConfig(), peer2, valFac.getOctetString(sb.toString().getBytes()));
// Validate New Values
SnmpValue v1b = SnmpUtils.get(m_collectionAgent.getAgentConfig(), peer1);
SnmpValue v2b = SnmpUtils.get(m_collectionAgent.getAgentConfig(), peer2);
Assert.assertFalse(v1a.toDisplayString().equals(v1b.toDisplayString()));
Assert.assertFalse(v2a.toDisplayString().equals(v2b.toDisplayString()));
// Collect and Persist Data - Step 2
collectionSet = collector.collect(m_collectionAgent, parameters);
validateCollectionSet(collectionSet);
collectionSet.visit(persister);
// Validate Persisted Data
Path pathToJrbFile = getSnmpRoot().toPath().resolve(Paths.get("1", TcaCollectionHandler.RESOURCE_TYPE_NAME, "171.19.37.60", TcaCollectionHandler.INBOUND_DELAY + m_rrdStrategy.getDefaultFileExtension()));
RrdDb jrb = new RrdDb(pathToJrbFile.toString());
// According with the Fixed Step
Assert.assertEquals(1, jrb.getArchive(0).getArcStep());
// According with the Sample Data
Assert.assertEquals(ts - 1, jrb.getArchive(0).getEndTime());
Robin inboundDelay = jrb.getArchive(0).getRobin(0);
for (int i = inboundDelay.getSize() - 49; i < inboundDelay.getSize() - 25; i++) {
Assert.assertEquals(new Double(11), Double.valueOf(inboundDelay.getValue(i)));
}
for (int i = inboundDelay.getSize() - 24; i < inboundDelay.getSize(); i++) {
Assert.assertEquals(new Double(12), Double.valueOf(inboundDelay.getValue(i)));
}
}
use of org.opennms.netmgt.collection.api.CollectionSetVisitor in project opennms by OpenNMS.
the class LatencyStoringServiceMonitorAdaptor method persistLatencySamples.
private void persistLatencySamples(MonitoredService service, Map<String, Number> entries, File rrdRepositoryRoot, String rrdBaseName) {
RrdRepository repository = new RrdRepository();
repository.setStep(m_pollerConfig.getStep(m_pkg));
repository.setRraList(m_pollerConfig.getRRAList(m_pkg));
repository.setHeartBeat(repository.getStep() * HEARTBEAT_STEP_MULTIPLIER);
repository.setRrdBaseDir(rrdRepositoryRoot);
// When making calls directly to RrdUtils#createRrd() and RrdUtils#updateRrd(),
// the behavior was as follows:
// 1) All samples get written to response/${ipAddr}/${rrdBaseName}.rrd
// This happens whether or not storeByGroup is enabled.
// 2) If multiple entries are present, the DSs are created in the same order that they
// appear in the map
LatencyCollectionResource latencyResource = new LatencyCollectionResource(service.getSvcName(), service.getIpAddr(), service.getNodeLocation());
for (final Entry<String, Number> entry : entries.entrySet()) {
final String ds = entry.getKey();
final Number value = entry.getValue() != null ? entry.getValue() : Double.NaN;
LatencyCollectionAttributeType latencyType = new LatencyCollectionAttributeType(rrdBaseName, ds);
latencyResource.addAttribute(new LatencyCollectionAttribute(latencyResource, latencyType, ds, value.doubleValue()));
}
ServiceParameters params = new ServiceParameters(Collections.emptyMap());
CollectionSetVisitor persister = m_persisterFactory.createPersister(params, repository, false, true, true);
SingleResourceCollectionSet collectionSet = new SingleResourceCollectionSet(latencyResource, new Date());
collectionSet.setStatus(CollectionStatus.SUCCEEDED);
collectionSet.visit(persister);
}
use of org.opennms.netmgt.collection.api.CollectionSetVisitor 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()));
}
Aggregations