Search in sources :

Example 1 with SnmpObjId

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

the class V1TrapInformation method getSnmpVarBindDTO.

@Override
public SnmpVarBindDTO getSnmpVarBindDTO(int i) {
    SnmpObjId name = SnmpObjId.get(getVarBindAt(i).getName().getIdentifiers());
    SnmpValue value = new JoeSnmpValue(getVarBindAt(i).getValue());
    return new SnmpVarBindDTO(name, value);
}
Also used : SnmpVarBindDTO(org.opennms.netmgt.snmp.SnmpVarBindDTO) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId)

Example 2 with SnmpObjId

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

the class V2TrapInformation method getSnmpVarBindDTO.

@Override
public SnmpVarBindDTO getSnmpVarBindDTO(int i) {
    if (i < 2) {
        if (i == 0) {
            LOG.debug("Skipping processing of varbind it is the sysuptime and the first varbind, it is not processed as a parm per RFC2089");
        } else {
            LOG.debug("Skipping processing of varbind it is the trap OID and the second varbind, it is not processed as a parm per RFC2089");
        }
        return null;
    } else {
        SnmpObjId name = SnmpObjId.get(getVarBindAt(i).getName().getIdentifiers());
        SnmpValue value = new JoeSnmpValue(getVarBindAt(i).getValue());
        return new SnmpVarBindDTO(name, value);
    }
}
Also used : SnmpVarBindDTO(org.opennms.netmgt.snmp.SnmpVarBindDTO) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId)

Example 3 with SnmpObjId

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

the class RequestPdu method send.

/*
     * This simulates send a packet and waiting for a response. 
     * 
     * This is a template method based on te getBulk algorithm. We use the getBulk
     * algorithm for get and nexts as well.  nonRepeaters for gets and nexts is always
     * equals to pdu size so there are no repeaters. maxRepitions is also always zero
     * for gets and nexts.
     * 
     * The method getRespObjIdFromReqObjId which by default goes 'next' is overridden
     * and does 'get' in the GetPdu.
     */
public ResponsePdu send(TestAgent agent) {
    ResponsePdu resp = TestPdu.getResponse();
    try {
        // first do non repeaters
        int nonRepeaters = Math.min(size(), getNonRepeaters());
        for (int i = 0; i < nonRepeaters; i++) {
            int errIndex = i + 1;
            TestVarBind varBind = (TestVarBind) getVarBindAt(i);
            SnmpObjId lastOid = varBind.getObjId();
            TestVarBind newVarBind = getResponseVarBind(agent, lastOid, errIndex);
            resp.addVarBind(newVarBind);
            // make sure we haven't exceeded response size
            validateResponseSize(resp, agent);
        }
        // make a list to track the repititions
        int repeaters = size() - nonRepeaters;
        List<SnmpObjId> repeaterList = new ArrayList<SnmpObjId>(repeaters);
        for (int i = nonRepeaters; i < size(); i++) {
            repeaterList.add(getVarBindAt(i).getObjId());
        }
        // now generate varbinds for the repeaters
        for (int count = 0; count < getMaxRepititions(); count++) {
            for (int i = 0; i < repeaterList.size(); i++) {
                int errIndex = nonRepeaters + i + 1;
                SnmpObjId lastOid = (SnmpObjId) repeaterList.get(i);
                TestVarBind newVarBind = getResponseVarBind(agent, lastOid, errIndex);
                resp.addVarBind(newVarBind);
                repeaterList.set(i, newVarBind.getObjId());
                // make sure we haven't exceeded response size
                validateResponseSize(resp, agent);
            }
        }
        return resp;
    } catch (AgentIndexException e) {
        // this happens for GenErr and NoSuchName errs
        resp.setVarBinds(getVarBinds());
        resp.setErrorStatus(e.getErrorStatus());
        // errorIndex uses indices starting at 1
        resp.setErrorIndex(e.getErrorIndex());
        return resp;
    } catch (AgentTooBigException e) {
        // when we exceed response size we'll get here
        return handleTooBig(agent, resp);
    }
}
Also used : ArrayList(java.util.ArrayList) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId)

Example 4 with SnmpObjId

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

the class MockSnmpStrategyTest method testSetMultipleBadAgent.

@Test
public void testSetMultipleBadAgent() throws Exception {
    final SnmpAgentConfig sac = getAgentConfig();
    sac.setAddress(InetAddressUtils.addr("1.2.3.4"));
    final SnmpObjId[] oids = new SnmpObjId[] { SnmpObjId.get(".1.3.5.1.1.3.0"), SnmpObjId.get(".1.3.5.1.1.4.0") };
    final SnmpValue[] values = new SnmpValue[] { m_strategy.getValueFactory().getInt32(4), m_strategy.getValueFactory().getGauge32(5) };
    m_strategy.set(sac, oids, values);
    final SnmpValue[] results = m_strategy.get(sac, oids);
    assertNotNull(results);
    assertEquals(2, results.length);
    assertNull(results[0]);
    assertNull(results[1]);
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) Test(org.junit.Test)

Example 5 with SnmpObjId

use of org.opennms.netmgt.snmp.SnmpObjId 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)

Aggregations

SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)71 SnmpValue (org.opennms.netmgt.snmp.SnmpValue)48 SnmpInstId (org.opennms.netmgt.snmp.SnmpInstId)22 SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)20 InetAddress (java.net.InetAddress)18 ArrayList (java.util.ArrayList)15 Map (java.util.Map)14 Test (org.junit.Test)13 PollStatus (org.opennms.netmgt.poller.PollStatus)12 PDU (org.snmp4j.PDU)10 ParameterMap (org.opennms.core.utils.ParameterMap)9 SnmpResult (org.opennms.netmgt.snmp.SnmpResult)7 List (java.util.List)5 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)4 ExecutionException (java.util.concurrent.ExecutionException)4 SnmpTrapBuilder (org.opennms.netmgt.snmp.SnmpTrapBuilder)4 SnmpWalker (org.opennms.netmgt.snmp.SnmpWalker)4 IOException (java.io.IOException)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3