Search in sources :

Example 6 with CollectionAgent

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;
}
Also used : JdbcDataCollectionConfigDao(org.opennms.netmgt.dao.JdbcDataCollectionConfigDao) HashMap(java.util.HashMap) ResourceType(org.opennms.netmgt.config.datacollection.ResourceType) Matchers.anyObject(org.mockito.Matchers.anyObject) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) JdbcDataCollectionConfig(org.opennms.netmgt.config.jdbc.JdbcDataCollectionConfig) JdbcAgentState(org.opennms.netmgt.collectd.jdbc.JdbcAgentState) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet)

Example 7 with CollectionAgent

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);
}
Also used : CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) Sample(org.opennms.newts.api.Sample) Resource(org.opennms.newts.api.Resource) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) Timestamp(org.opennms.newts.api.Timestamp) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) Persister(org.opennms.netmgt.collection.api.Persister) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) Test(org.junit.Test)

Example 8 with CollectionAgent

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);
            }
        });
    }
}
Also used : RpcExceptionHandler(org.opennms.core.rpc.api.RpcExceptionHandler) SnmpAgentTimeoutException(org.opennms.netmgt.snmp.SnmpAgentTimeoutException) CollectionTracker(org.opennms.netmgt.snmp.CollectionTracker) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with CollectionAgent

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;
        }
    };
}
Also used : InterfaceLevelResource(org.opennms.netmgt.collection.support.builder.InterfaceLevelResource) Persister(org.opennms.netmgt.collection.api.Persister) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) Callable(java.util.concurrent.Callable) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet)

Example 10 with CollectionAgent

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);
}
Also used : ServiceCollector(org.opennms.netmgt.collection.api.ServiceCollector) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) Map(java.util.Map)

Aggregations

CollectionAgent (org.opennms.netmgt.collection.api.CollectionAgent)16 CollectionSet (org.opennms.netmgt.collection.api.CollectionSet)13 NodeLevelResource (org.opennms.netmgt.collection.support.builder.NodeLevelResource)9 Test (org.junit.Test)8 CollectionSetBuilder (org.opennms.netmgt.collection.support.builder.CollectionSetBuilder)8 Definition (org.opennms.netmgt.config.wsman.Definition)7 OnmsNode (org.opennms.netmgt.model.OnmsNode)7 Map (java.util.Map)6 WSManClientFactory (org.opennms.core.wsman.WSManClientFactory)6 CollectionInitializationException (org.opennms.netmgt.collection.api.CollectionInitializationException)6 Collection (org.opennms.netmgt.config.wsman.Collection)6 WSManConfigDao (org.opennms.netmgt.dao.WSManConfigDao)6 WSManDataCollectionConfigDao (org.opennms.netmgt.dao.WSManDataCollectionConfigDao)6 NodeDao (org.opennms.netmgt.dao.api.NodeDao)6 List (java.util.List)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Supplier (java.util.function.Supplier)5 Collectors (java.util.stream.Collectors)5 CollectionException (org.opennms.netmgt.collection.api.CollectionException)5 GenericTypeResource (org.opennms.netmgt.collection.support.builder.GenericTypeResource)5