use of org.opennms.netmgt.collection.api.CollectionAgent in project opennms by OpenNMS.
the class JdbcCollectorTest method collect.
public CollectionSet collect(JdbcDataCollection collection, ResultSet resultSet, ResourceType... resourceTypes) throws Exception {
final int nodeId = 1;
JdbcDataCollectionConfig config = new JdbcDataCollectionConfig();
config.setRrdRepository(tempFolder.getRoot().getAbsolutePath());
JdbcDataCollectionConfigDao jdbcCollectionDao = mock(JdbcDataCollectionConfigDao.class);
when(jdbcCollectionDao.getConfig()).thenReturn(config);
when(jdbcCollectionDao.getDataCollectionByName(null)).thenReturn(collection);
ResourceTypeMapper.getInstance().setResourceTypeMapper((name) -> {
for (ResourceType resourceType : resourceTypes) {
if (resourceType.getName().equals(name)) {
return resourceType;
}
}
return null;
});
MyJdbcCollector jdbcCollector = new MyJdbcCollector();
jdbcCollector.setJdbcCollectionDao(jdbcCollectionDao);
jdbcCollector.initialize();
CollectionAgent agent = mock(CollectionAgent.class);
when(agent.getNodeId()).thenReturn(nodeId);
when(agent.getAddress()).thenReturn(InetAddressUtils.ONE_TWENTY_SEVEN);
when(agent.getStorageResourcePath()).thenReturn(ResourcePath.get("snmp", Integer.toString(nodeId)));
JdbcAgentState jdbcAgentState = mock(JdbcAgentState.class);
when(jdbcAgentState.groupIsAvailable(any(String.class))).thenReturn(true);
when(jdbcAgentState.executeJdbcQuery(anyObject(), anyObject())).thenReturn(resultSet);
jdbcCollector.setJdbcAgentState(jdbcAgentState);
Map<String, Object> params = new HashMap<>();
params.putAll(jdbcCollector.getRuntimeAttributes(agent, params));
CollectionSet collectionSet = jdbcCollector.collect(agent, params);
return collectionSet;
}
use of org.opennms.netmgt.collection.api.CollectionAgent in project opennms by OpenNMS.
the class NewtsPersisterIT method canPersist.
@Test
public void canPersist() throws InterruptedException {
ServiceParameters params = new ServiceParameters(Collections.emptyMap());
RrdRepository repo = new RrdRepository();
// Only the last element of the path matters here
repo.setRrdBaseDir(Paths.get("a", "path", "that", "ends", "with", "snmp").toFile());
Persister persister = m_persisterFactory.createPersister(params, repo);
int nodeId = 1;
CollectionAgent agent = mock(CollectionAgent.class);
when(agent.getStorageResourcePath()).thenReturn(ResourcePath.get(Integer.toString(nodeId)));
NodeLevelResource nodeLevelResource = new NodeLevelResource(nodeId);
// Build a collection set with a single sample
Timestamp now = Timestamp.now();
CollectionSet collectionSet = new CollectionSetBuilder(agent).withNumericAttribute(nodeLevelResource, "metrics", "metric", 900, AttributeType.GAUGE).withTimestamp(now.asDate()).build();
// Persist
collectionSet.visit(persister);
// Wait for the sample(s) to be flushed
Thread.sleep(5 * 1000);
// Fetch the (persisted) sample
Resource resource = new Resource("snmp:1:metrics");
Timestamp end = Timestamp.now();
Results<Sample> samples = m_sampleRepository.select(Context.DEFAULT_CONTEXT, resource, Optional.of(now), Optional.of(end));
assertEquals(1, samples.getRows().size());
Row<Sample> row = samples.getRows().iterator().next();
assertEquals(900, row.getElement("metric").getValue().doubleValue(), 0.00001);
}
use of org.opennms.netmgt.collection.api.CollectionAgent in project opennms by OpenNMS.
the class SnmpCollectionSet method collect.
void collect() throws CollectionException {
// XXX Should we have a call to hasDataToCollect here?
try {
// now collect the data
CollectionAgent agent = getCollectionAgent();
logStartedWalker();
CompletableFuture<CollectionTracker> future = m_client.walk(getAgentConfig(), getTracker()).withDescription("SnmpCollectors for " + agent.getHostAddress()).withLocation(getCollectionAgent().getLocationName()).withTimeToLive(m_snmpCollection.getServiceParameters().getServiceInterval()).execute();
// wait for collection to finish
try {
future.get();
} finally {
logFinishedWalker();
}
// Execute POST Updates (add custom parameters)
SnmpPropertyExtenderProcessor processor = new SnmpPropertyExtenderProcessor();
processor.process(this, m_snmpCollection.getName(), m_agent.getSysObjectId(), m_agent.getHostAddress());
m_status = CollectionStatus.SUCCEEDED;
} catch (InterruptedException | ExecutionException e) {
throw RpcExceptionUtils.handleException(e, new RpcExceptionHandler<CollectionException>() {
@Override
public CollectionException onInterrupted(Throwable t) {
Thread.currentThread().interrupt();
return new CollectionUnknown(String.format("Collection of SNMP data for interface %s at location %s was interrupted.", getCollectionAgent().getHostAddress(), getCollectionAgent().getLocationName()), t);
}
@Override
public CollectionException onTimedOut(Throwable t) {
return new CollectionUnknown(String.format("No response received when remotely collecting SNMP data" + " for interface %s at location %s.", getCollectionAgent().getHostAddress(), getCollectionAgent().getLocationName()), t);
}
@Override
public CollectionException onRejected(Throwable t) {
return new CollectionUnknown(String.format("The request to remotely collect SNMP data" + " for interface %s at location %s was rejected.", getCollectionAgent().getHostAddress(), getCollectionAgent().getLocationName()), t);
}
@Override
public CollectionException onUnknown(Throwable t) {
if (t instanceof SnmpAgentTimeoutException) {
return new CollectionTimedOut(t.getMessage());
} else if (t.getCause() != null && t.getCause() instanceof SnmpAgentTimeoutException) {
return new CollectionTimedOut(t.getCause().getMessage());
}
return new CollectionWarning(String.format("Unexpected exception when collecting SNMP data for interface %s at location %s.", getCollectionAgent().getHostAddress(), getCollectionAgent().getLocationName()), t);
}
});
}
}
use of org.opennms.netmgt.collection.api.CollectionAgent 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;
}
};
}
use of org.opennms.netmgt.collection.api.CollectionAgent in project opennms by OpenNMS.
the class CollectorClientRpcModule method execute.
@Override
public CompletableFuture<CollectorResponseDTO> execute(CollectorRequestDTO request) {
final String className = request.getClassName();
final ServiceCollector collector = serviceCollectorRegistry.getCollectorByClassName(className);
if (collector == null) {
throw new IllegalArgumentException("No collector found with class name '" + className + "'.");
}
return CompletableFuture.supplyAsync(new Supplier<CollectorResponseDTO>() {
@Override
public CollectorResponseDTO get() {
Logging.putPrefix("collectd");
final CollectionAgent agent = request.getAgent();
final Map<String, Object> parameters = request.getParameters(collector);
return new CollectorResponseDTO(collector.collect(agent, parameters));
}
}, executor);
}
Aggregations