Search in sources :

Example 6 with ResponseEvent

use of org.snmp4j.event.ResponseEvent in project opennms by OpenNMS.

the class BrocadeMibIT method sendRequestV1V2.

private PDU sendRequestV1V2(PDU pdu, int version) throws Exception {
    PDU response;
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString("public"));
    target.setAddress(new UdpAddress(m_agent.getInetAddress(), m_agent.getPort()));
    target.setVersion(version);
    TransportMapping<UdpAddress> transport = null;
    try {
        transport = new DefaultUdpTransportMapping();
        Snmp snmp = new Snmp(transport);
        transport.listen();
        ResponseEvent e = snmp.send(pdu, target);
        response = e.getResponse();
    } finally {
        if (transport != null) {
            transport.close();
        }
    }
    return response;
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) UdpAddress(org.snmp4j.smi.UdpAddress) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) ResponseEvent(org.snmp4j.event.ResponseEvent) CommunityTarget(org.snmp4j.CommunityTarget)

Example 7 with ResponseEvent

use of org.snmp4j.event.ResponseEvent in project camel by apache.

the class SnmpProducer method process.

@Override
public void process(final Exchange exchange) throws Exception {
    // load connection data only if the endpoint is enabled
    Snmp snmp = null;
    TransportMapping<? extends Address> transport = null;
    try {
        LOG.debug("Starting SNMP producer on {}", this.endpoint.getAddress());
        // either tcp or udp
        if ("tcp".equals(this.endpoint.getProtocol())) {
            transport = new DefaultTcpTransportMapping();
        } else if ("udp".equals(this.endpoint.getProtocol())) {
            transport = new DefaultUdpTransportMapping();
        } else {
            throw new IllegalArgumentException("Unknown protocol: {} " + this.endpoint.getProtocol());
        }
        snmp = new Snmp(transport);
        LOG.debug("Snmp: i am sending");
        snmp.listen();
        ResponseEvent responseEvent = snmp.send(this.pdu, this.target);
        LOG.debug("Snmp: sended");
        if (responseEvent.getResponse() != null) {
            exchange.getIn().setBody(new SnmpMessage(responseEvent.getResponse()));
        } else {
            throw new TimeoutException("SNMP Producer Timeout");
        }
    } finally {
        try {
            transport.close();
        } catch (Exception e) {
        }
        try {
            snmp.close();
        } catch (Exception e) {
        }
    }
}
Also used : DefaultTcpTransportMapping(org.snmp4j.transport.DefaultTcpTransportMapping) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) ResponseEvent(org.snmp4j.event.ResponseEvent) TimeoutException(java.util.concurrent.TimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Example 8 with ResponseEvent

use of org.snmp4j.event.ResponseEvent 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();
    } catch (IOException e) {
        LOG.error("send: Could not create SNMP session for agent {}", agentConfig, e);
        future.completeExceptionally(new Exception("Could not create SNMP session for agent"));
        return;
    }
    if (expectResponse) {
        try {
            session.listen();
        } catch (IOException e) {
            LOG.error("send: error setting up listener for SNMP responses", e);
            future.completeExceptionally(new Exception("error setting up listener for SNMP responses"));
            return;
        }
    }
    try {
        if (expectResponse) {
            session.send(pdu, agentConfig.getTarget(), null, new ResponseListener() {

                @Override
                public void onResponse(ResponseEvent responseEvent) {
                    if (expectResponse) {
                        try {
                            future.complete(processResponse(agentConfig, responseEvent));
                        } catch (IOException e) {
                            future.completeExceptionally(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(session);
                                }
                            });
                        }
                    }
                }
            });
        } else {
            session.send(pdu, agentConfig.getTarget());
            future.complete(null);
            closeQuietly(session);
        }
    } catch (final IOException e) {
        LOG.error("send: error during SNMP operation", e);
        future.completeExceptionally(e);
    } catch (final RuntimeException e) {
        LOG.error("send: unexpected error during SNMP operation", e);
        future.completeExceptionally(e);
    }
}
Also used : Snmp(org.snmp4j.Snmp) ResponseEvent(org.snmp4j.event.ResponseEvent) IOException(java.io.IOException) ResponseListener(org.snmp4j.event.ResponseListener) SocketException(java.net.SocketException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with ResponseEvent

use of org.snmp4j.event.ResponseEvent in project opennms by OpenNMS.

the class MockAgentTest method testGetSysName.

public void testGetSysName() throws IOException {
    Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
    snmp.listen();
    Address addr = new UdpAddress(InetAddress.getLocalHost(), 9161);
    //Address addr = new UdpAddress(InetAddressUtils.addr("192.168.0.100"), 161);
    Target target = new CommunityTarget(addr, new OctetString("public"));
    target.setVersion(SnmpConstants.version1);
    target.setTimeout(3000);
    target.setRetries(3);
    PDUv1 getRequest = new PDUv1();
    getRequest.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0")));
    ResponseEvent e = snmp.get(getRequest, target);
    PDU response = e.getResponse();
    assertEquals(new OctetString("mockhost"), response.get(0).getVariable());
}
Also used : OctetString(org.snmp4j.smi.OctetString) PDU(org.snmp4j.PDU) UdpAddress(org.snmp4j.smi.UdpAddress) Target(org.snmp4j.Target) CommunityTarget(org.snmp4j.CommunityTarget) Address(org.snmp4j.smi.Address) UdpAddress(org.snmp4j.smi.UdpAddress) InetAddress(java.net.InetAddress) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) ResponseEvent(org.snmp4j.event.ResponseEvent) PDUv1(org.snmp4j.PDUv1) OID(org.snmp4j.smi.OID) CommunityTarget(org.snmp4j.CommunityTarget) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 10 with ResponseEvent

use of org.snmp4j.event.ResponseEvent in project opennms by OpenNMS.

the class MockSnmpAgentIT method sendRequestV3.

private PDU sendRequestV3(PDU pdu) {
    PDU response = null;
    OctetString userId = new OctetString("opennmsUser");
    OctetString pw = new OctetString("0p3nNMSv3");
    UserTarget target = new UserTarget();
    target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
    target.setSecurityName(userId);
    target.setAddress(new UdpAddress(InetAddressUtils.addr("127.0.0.1"), m_agent.getPort()));
    target.setVersion(SnmpConstants.version3);
    target.setTimeout(DEFAULT_TIMEOUT);
    TransportMapping<UdpAddress> transport = null;
    Snmp snmp = null;
    try {
        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
        SecurityModels.getInstance().addSecurityModel(usm);
        transport = new DefaultUdpTransportMapping();
        snmp = new Snmp(transport);
        UsmUser user = new UsmUser(userId, AuthMD5.ID, pw, PrivDES.ID, pw);
        snmp.getUSM().addUser(userId, user);
        transport.listen();
        ResponseEvent e = snmp.send(pdu, target);
        response = e.getResponse();
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        if (snmp != null) {
            try {
                snmp.close();
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
        if (transport != null) {
            try {
                transport.close();
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
    }
    return response;
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) UdpAddress(org.snmp4j.smi.UdpAddress) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) ResponseEvent(org.snmp4j.event.ResponseEvent) IOException(java.io.IOException) UserTarget(org.snmp4j.UserTarget) UsmUser(org.snmp4j.security.UsmUser) USM(org.snmp4j.security.USM)

Aggregations

ResponseEvent (org.snmp4j.event.ResponseEvent)15 PDU (org.snmp4j.PDU)13 ScopedPDU (org.snmp4j.ScopedPDU)12 OctetString (org.snmp4j.smi.OctetString)12 Snmp (org.snmp4j.Snmp)9 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)8 OID (org.snmp4j.smi.OID)7 UdpAddress (org.snmp4j.smi.UdpAddress)7 VariableBinding (org.snmp4j.smi.VariableBinding)7 LinkedHashMap (java.util.LinkedHashMap)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 CommunityTarget (org.snmp4j.CommunityTarget)4 IOException (java.io.IOException)3 UserTarget (org.snmp4j.UserTarget)3 USM (org.snmp4j.security.USM)3 UsmUser (org.snmp4j.security.UsmUser)3 List (java.util.List)2 Map (java.util.Map)2 BigDecimal (java.math.BigDecimal)1