Search in sources :

Example 1 with SnmpWalker

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

the class MockSnmpStrategyTest method testTracker.

@Test
public void testTracker() throws Exception {
    final CountingColumnTracker ct = new CountingColumnTracker(SnmpObjId.get(".1.3.5.1.1"));
    try (SnmpWalker walker = walk(ct, 10, 3)) {
        walker.start();
        walker.waitFor();
    }
    assertEquals("number of columns returned must match test data", Long.valueOf(9).longValue(), ct.getCount());
}
Also used : SnmpWalker(org.opennms.netmgt.snmp.SnmpWalker) Test(org.junit.Test)

Example 2 with SnmpWalker

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

the class MockSnmpStrategyTest method toCompletableFuture.

private static CompletableFuture<Long> toCompletableFuture(CountingColumnTracker ct, SnmpWalker walker) {
    final CompletableFuture<Long> future = new CompletableFuture<>();
    walker.setCallback(new SnmpWalkCallback() {

        @Override
        public void complete(SnmpWalker tracker, Throwable t) {
            if (t != null) {
                future.completeExceptionally(t);
            } else {
                future.complete(ct.getCount());
            }
        }
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) SnmpWalker(org.opennms.netmgt.snmp.SnmpWalker) SnmpWalkCallback(org.opennms.netmgt.snmp.SnmpWalkCallback)

Example 3 with SnmpWalker

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

the class MockSnmpStrategyTest method testTrackerTimeout.

@Test
public void testTrackerTimeout() throws Exception {
    final CountingColumnTracker ct = new CountingColumnTracker(SnmpObjId.get(".1.3.5.1.1"));
    final SnmpAgentConfig sac = getAgentConfig();
    sac.setPort(12345);
    try (final SnmpWalker walker = SnmpUtils.createWalker(sac, "test", ct)) {
        assertNotNull(walker);
        walker.start();
        walker.waitFor();
    }
    assertEquals("it should match no columns (timeout)", Long.valueOf(0).longValue(), ct.getCount());
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) SnmpWalker(org.opennms.netmgt.snmp.SnmpWalker) Test(org.junit.Test)

Example 4 with SnmpWalker

use of org.opennms.netmgt.snmp.SnmpWalker 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(SnmpObjId::get).map(ColumnTracker::new).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 : Collectable(org.opennms.netmgt.snmp.Collectable) SnmpWalker(org.opennms.netmgt.snmp.SnmpWalker) ArrayList(java.util.ArrayList) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) SnmpWalkCallback(org.opennms.netmgt.snmp.SnmpWalkCallback) LinkedHashMap(java.util.LinkedHashMap) CompletableFuture(java.util.concurrent.CompletableFuture) SingleInstanceTracker(org.opennms.netmgt.snmp.SingleInstanceTracker) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) Collection(java.util.Collection) CollectionTracker(org.opennms.netmgt.snmp.CollectionTracker) SnmpResult(org.opennms.netmgt.snmp.SnmpResult) AggregateTracker(org.opennms.netmgt.snmp.AggregateTracker)

Example 5 with SnmpWalker

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

the class LocationAwareSnmpClientIT method canWalkIpAddressTableDirectly.

/**
 * Verifies that SNMP WALKs are successful when directly using SnmpUtils.
 *
 * Used a basis for comparison.
 */
@Test
public void canWalkIpAddressTableDirectly() throws InterruptedException {
    // Gather the list of IP addresses
    final IPAddressGatheringTracker tracker = new IPAddressGatheringTracker();
    try (SnmpWalker walker = SnmpUtils.createWalker(agentConfig, tracker.getDescription(), tracker)) {
        walker.start();
        walker.waitFor();
    }
    ExpectedResults.compareToKnownIpAddressList(tracker.getIpAddresses());
    // Now determine their interface indices using a different type of tracker
    final Set<SnmpInstId> ipAddrs = new TreeSet<>();
    for (final String ipAddr : tracker.getIpAddresses()) {
        ipAddrs.add(new SnmpInstId(InetAddressUtils.toOid(InetAddressUtils.addr(ipAddr))));
    }
    IpAddrTable ipAddrTable = new IpAddrTable(agentConfig.getAddress(), ipAddrs);
    try (SnmpWalker walker = SnmpUtils.createWalker(agentConfig, "ipAddrTable", ipAddrTable)) {
        walker.start();
        walker.waitFor();
    }
    ExpectedResults.compareToKnownIfIndices(ipAddrTable.getIfIndices());
}
Also used : SnmpWalker(org.opennms.netmgt.snmp.SnmpWalker) IpAddrTable(org.opennms.netmgt.provision.service.snmp.IpAddrTable) TreeSet(java.util.TreeSet) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) IPAddressGatheringTracker(org.opennms.netmgt.snmp.proxy.common.testutils.IPAddressGatheringTracker) Test(org.junit.Test) CamelBlueprintTest(org.opennms.core.test.camel.CamelBlueprintTest)

Aggregations

SnmpWalker (org.opennms.netmgt.snmp.SnmpWalker)13 SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)8 Test (org.junit.Test)6 SnmpInstId (org.opennms.netmgt.snmp.SnmpInstId)5 InetAddress (java.net.InetAddress)4 SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)4 SnmpValue (org.opennms.netmgt.snmp.SnmpValue)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 PollStatus (org.opennms.netmgt.poller.PollStatus)2 AggregateTracker (org.opennms.netmgt.snmp.AggregateTracker)2 Collectable (org.opennms.netmgt.snmp.Collectable)2 CollectionTracker (org.opennms.netmgt.snmp.CollectionTracker)2 RowCallback (org.opennms.netmgt.snmp.RowCallback)2 SingleInstanceTracker (org.opennms.netmgt.snmp.SingleInstanceTracker)2 SnmpResult (org.opennms.netmgt.snmp.SnmpResult)2 SnmpRowResult (org.opennms.netmgt.snmp.SnmpRowResult)2 SnmpWalkCallback (org.opennms.netmgt.snmp.SnmpWalkCallback)2 TableTracker (org.opennms.netmgt.snmp.TableTracker)2