Search in sources :

Example 1 with SnmpException

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

the class Snmp4JStrategy method send.

private void send(Snmp4JAgentConfig agentConfig, PDU pdu, boolean expectResponse, CompletableFuture<SnmpValue[]> future) {
    Snmp session;
    try {
        session = agentConfig.createSnmpSession();
        Snmp4JStrategy.trackSession(session);
    } catch (final Exception e) {
        LOG.error("send: Could not create SNMP session for agent {}", agentConfig, e);
        future.completeExceptionally(new SnmpException("Could not create SNMP session for agent", e));
        return;
    }
    if (expectResponse) {
        try {
            session.listen();
        } catch (final Exception e) {
            closeQuietly(session);
            LOG.error("send: error setting up listener for SNMP responses", e);
            future.completeExceptionally(new SnmpException("error setting up listener for SNMP responses", e));
            return;
        }
        try {
            final Snmp mySession = session;
            mySession.send(pdu, agentConfig.getTarget(), null, new ResponseListener() {

                @Override
                public void onResponse(final ResponseEvent responseEvent) {
                    try {
                        future.complete(processResponse(agentConfig, responseEvent));
                    } catch (final Exception e) {
                        future.completeExceptionally(new SnmpException(e));
                    } 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() {
                                closeQuietly(mySession);
                            }
                        });
                    }
                }
            });
        } catch (final Exception e) {
            // The ResponseListener will not be called since an exception occurred in the send,
            // so we make sure to close the session here
            closeQuietly(session);
            LOG.error("send: error during SNMP operation", e);
            future.completeExceptionally(e);
        }
    } else {
        // we're not expecting a response
        try {
            session.send(pdu, agentConfig.getTarget());
            future.complete(null);
        } catch (final Exception e) {
            LOG.error("send: error during SNMP operation", e);
            future.completeExceptionally(new SnmpException(e));
        } finally {
            closeQuietly(session);
            Snmp4JStrategy.reapSession(session);
        }
    }
}
Also used : SnmpException(org.opennms.netmgt.snmp.SnmpException) Snmp(org.snmp4j.Snmp) ResponseEvent(org.snmp4j.event.ResponseEvent) ResponseListener(org.snmp4j.event.ResponseListener) SnmpException(org.opennms.netmgt.snmp.SnmpException) SocketException(java.net.SocketException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 2 with SnmpException

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

the class Snmp4JStrategy method buildPdu.

protected PDU buildPdu(Snmp4JAgentConfig agentConfig, int pduType, SnmpObjId[] oids, SnmpValue[] values) {
    PDU pdu = agentConfig.createPdu(pduType);
    if (values == null) {
        for (SnmpObjId oid : oids) {
            pdu.add(new VariableBinding(new OID(oid.toString())));
        }
    } else {
        // TODO should this throw an exception?  This situation is fairly bogus and probably signifies a coding error.
        if (oids.length != values.length) {
            Exception e = new SnmpException("PDU values do not match OIDs");
            LOG.error("PDU to prepare has object values but not the same number as there are OIDs.  There are {} OIDs and {} object values.", oids.length, values.length, e);
            return null;
        }
        for (int i = 0; i < oids.length; i++) {
            pdu.add(new VariableBinding(new OID(oids[i].toString()), new Snmp4JValue(values[i].getType(), values[i].getBytes()).getVariable()));
        }
    }
    // TODO should this throw an exception?  This situation is fairly bogus.
    if (pdu.getVariableBindings().size() != oids.length) {
        Exception e = new SnmpException("PDU bindings do not match OIDs");
        LOG.error("Prepared PDU does not have as many variable bindings as there are OIDs.  There are {} OIDs and {} variable bindings.", oids.length, pdu.getVariableBindings(), e);
        return null;
    }
    return pdu;
}
Also used : PDU(org.snmp4j.PDU) ScopedPDU(org.snmp4j.ScopedPDU) SnmpException(org.opennms.netmgt.snmp.SnmpException) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) OID(org.snmp4j.smi.OID) VariableBinding(org.snmp4j.smi.VariableBinding) SnmpException(org.opennms.netmgt.snmp.SnmpException) SocketException(java.net.SocketException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 3 with SnmpException

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

the class Snmp4JWalker method sendNextPdu.

@Override
protected void sendNextPdu(WalkerPduBuilder pduBuilder) throws SnmpException {
    Snmp4JPduBuilder snmp4JPduBuilder = (Snmp4JPduBuilder) pduBuilder;
    try {
        if (m_session == null) {
            m_session = m_agentConfig.createSnmpSession();
            Snmp4JStrategy.trackSession(m_session);
            m_session.listen();
        }
    } catch (final IOException e) {
        close();
        throw new SnmpException(e);
    }
    LOG.debug("Sending tracker pdu of size {}", snmp4JPduBuilder.getPdu().size());
    try {
        m_session.send(snmp4JPduBuilder.getPdu(), m_tgt, null, m_listener);
    } catch (final IOException e) {
        LOG.debug("Failed to send pdu of size {}", snmp4JPduBuilder.getPdu().size(), e);
        close();
        throw new SnmpException(e);
    }
}
Also used : SnmpException(org.opennms.netmgt.snmp.SnmpException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)3 SnmpException (org.opennms.netmgt.snmp.SnmpException)3 SocketException (java.net.SocketException)2 UnknownHostException (java.net.UnknownHostException)2 SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)1 PDU (org.snmp4j.PDU)1 ScopedPDU (org.snmp4j.ScopedPDU)1 Snmp (org.snmp4j.Snmp)1 ResponseEvent (org.snmp4j.event.ResponseEvent)1 ResponseListener (org.snmp4j.event.ResponseListener)1 OID (org.snmp4j.smi.OID)1 VariableBinding (org.snmp4j.smi.VariableBinding)1