Search in sources :

Example 1 with CollectionTracker

use of org.opennms.netmgt.snmp.CollectionTracker in project opennms by OpenNMS.

the class SnmpProxyRpcModule method walk.

private CompletableFuture<Collection<SnmpResponseDTO>> walk(SnmpRequestDTO request, List<SnmpWalkRequestDTO> walks) {
    final CompletableFuture<Collection<SnmpResponseDTO>> future = new CompletableFuture<>();
    final Map<String, SnmpResponseDTO> responsesByCorrelationId = new LinkedHashMap<>();
    final List<Collectable> trackers = new ArrayList<>(walks.size());
    for (final SnmpWalkRequestDTO walk : walks) {
        CollectionTracker tracker;
        if (walk.getInstance() != null) {
            if (walk.getOids().size() != 1) {
                future.completeExceptionally(new IllegalArgumentException("Single instance requests must have a single OID."));
                return future;
            }
            final SnmpObjId oid = walk.getOids().get(0);
            tracker = new SingleInstanceTracker(oid, new SnmpInstId(walk.getInstance())) {

                @Override
                protected void storeResult(SnmpResult res) {
                    addResult(res, walk.getCorrelationId(), responsesByCorrelationId);
                }
            };
        } else {
            final Collection<Collectable> columnTrackers = walk.getOids().stream().map(oid -> SnmpObjId.get(oid)).map(objId -> new ColumnTracker(objId)).collect(Collectors.toList());
            tracker = new AggregateTracker(columnTrackers) {

                @Override
                protected void storeResult(SnmpResult res) {
                    addResult(res, walk.getCorrelationId(), responsesByCorrelationId);
                }
            };
        }
        if (walk.getMaxRepetitions() != null) {
            tracker.setMaxRepetitions(walk.getMaxRepetitions());
        }
        trackers.add(tracker);
    }
    AggregateTracker aggregate = new AggregateTracker(trackers);
    final SnmpWalker walker = SnmpUtils.createWalker(request.getAgent(), request.getDescription(), aggregate);
    walker.setCallback(new SnmpWalkCallback() {

        @Override
        public void complete(SnmpWalker tracker, Throwable t) {
            try {
                if (t != null) {
                    future.completeExceptionally(t);
                } else {
                    future.complete(responsesByCorrelationId.values());
                }
            } finally {
                // Close the tracker using a separate thread
                // This allows the SnmpWalker to clean up properly instead
                // of interrupting execution as it's executing the callback
                REAPER_EXECUTOR.submit(new Runnable() {

                    @Override
                    public void run() {
                        tracker.close();
                    }
                });
            }
        }
    });
    walker.start();
    return future;
}
Also used : Collection(java.util.Collection) SnmpWalkCallback(org.opennms.netmgt.snmp.SnmpWalkCallback) CompletableFuture(java.util.concurrent.CompletableFuture) CollectionTracker(org.opennms.netmgt.snmp.CollectionTracker) SingleInstanceTracker(org.opennms.netmgt.snmp.SingleInstanceTracker) Collectors(java.util.stream.Collectors) Collectable(org.opennms.netmgt.snmp.Collectable) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) AbstractXmlRpcModule(org.opennms.core.rpc.xml.AbstractXmlRpcModule) List(java.util.List) SnmpUtils(org.opennms.netmgt.snmp.SnmpUtils) AggregateTracker(org.opennms.netmgt.snmp.AggregateTracker) Map(java.util.Map) SnmpResult(org.opennms.netmgt.snmp.SnmpResult) ColumnTracker(org.opennms.netmgt.snmp.ColumnTracker) ThreadFactory(java.util.concurrent.ThreadFactory) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpWalker(org.opennms.netmgt.snmp.SnmpWalker) ExecutorService(java.util.concurrent.ExecutorService) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) Collectable(org.opennms.netmgt.snmp.Collectable) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) CompletableFuture(java.util.concurrent.CompletableFuture) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) CollectionTracker(org.opennms.netmgt.snmp.CollectionTracker) SnmpResult(org.opennms.netmgt.snmp.SnmpResult) AggregateTracker(org.opennms.netmgt.snmp.AggregateTracker) SnmpWalker(org.opennms.netmgt.snmp.SnmpWalker) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) SnmpWalkCallback(org.opennms.netmgt.snmp.SnmpWalkCallback) SingleInstanceTracker(org.opennms.netmgt.snmp.SingleInstanceTracker) Collection(java.util.Collection) ColumnTracker(org.opennms.netmgt.snmp.ColumnTracker)

Example 2 with CollectionTracker

use of org.opennms.netmgt.snmp.CollectionTracker 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 3 with CollectionTracker

use of org.opennms.netmgt.snmp.CollectionTracker in project opennms by OpenNMS.

the class TcaProtocolCollector method collect.

@Override
public CollectionJob collect(final CollectionJob collectionJob) {
    logger.info("TcaProtocolCollector is collecting collectionJob '{}'", collectionJob);
    SnmpAgentConfig snmpAgentConfig = SnmpAgentConfig.parseProtocolConfigurationString(collectionJob.getProtocolConfiguration());
    List<Collectable> trackers = new ArrayList<Collectable>();
    for (final String metricObjId : collectionJob.getAllMetrics()) {
        final String keyword = metricObjId.substring(metricObjId.lastIndexOf("_") + 1);
        final SnmpObjId requestOid = SnmpObjId.get(metricObjId.substring(0, metricObjId.lastIndexOf("_")));
        SnmpObjId base = requestOid.getPrefix(requestOid.length() - 1);
        int lastId = requestOid.getLastSubId();
        SingleInstanceTracker instanceTracker = new SingleInstanceTracker(base, new SnmpInstId(lastId)) {

            @Override
            protected void storeResult(SnmpResult result) {
                logger.trace("Collected SnmpValue '{}'", result);
                SnmpValue value = result.getValue();
                String compositeResult = getCompositeValue(keyword, value.toDisplayString());
                collectionJob.setMetricValue(metricObjId, "int32", compositeResult);
            }

            @Override
            public void setFailed(boolean failed) {
                super.setFailed(failed);
                logger.trace("Collection Failed for metricObjId '{}'", metricObjId);
                collectionJob.setMetricValue(metricObjId, "unknown", null);
            }

            @Override
            public void setTimedOut(boolean timedOut) {
                super.setTimedOut(timedOut);
                logger.trace("Collection timedOut for metricObjId '{}'", metricObjId);
                collectionJob.setMetricValue(metricObjId, "unknown", null);
            }
        };
        trackers.add(instanceTracker);
    }
    CollectionTracker tracker = new AggregateTracker(trackers);
    try (SnmpWalker walker = m_snmpStrategy.createWalker(snmpAgentConfig, "SnmpProtocolCollector for " + snmpAgentConfig.getAddress(), tracker)) {
        walker.start();
        try {
            walker.waitFor();
        } catch (InterruptedException e) {
            logger.error("Interuppted while waiting for collector. Results may be incomplete.", e);
        }
    }
    return collectionJob;
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) Collectable(org.opennms.netmgt.snmp.Collectable) SnmpWalker(org.opennms.netmgt.snmp.SnmpWalker) ArrayList(java.util.ArrayList) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SingleInstanceTracker(org.opennms.netmgt.snmp.SingleInstanceTracker) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) CollectionTracker(org.opennms.netmgt.snmp.CollectionTracker) SnmpResult(org.opennms.netmgt.snmp.SnmpResult) AggregateTracker(org.opennms.netmgt.snmp.AggregateTracker)

Aggregations

CollectionTracker (org.opennms.netmgt.snmp.CollectionTracker)3 ArrayList (java.util.ArrayList)2 AggregateTracker (org.opennms.netmgt.snmp.AggregateTracker)2 Collectable (org.opennms.netmgt.snmp.Collectable)2 SingleInstanceTracker (org.opennms.netmgt.snmp.SingleInstanceTracker)2 SnmpInstId (org.opennms.netmgt.snmp.SnmpInstId)2 SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)2 SnmpResult (org.opennms.netmgt.snmp.SnmpResult)2 SnmpValue (org.opennms.netmgt.snmp.SnmpValue)2 SnmpWalker (org.opennms.netmgt.snmp.SnmpWalker)2 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 Collectors (java.util.stream.Collectors)1