Search in sources :

Example 11 with Snmp

use of org.snmp4j.Snmp in project camel by apache.

the class SnmpTrapConsumer method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    // load connection data only if the endpoint is enabled
    if (LOG.isInfoEnabled()) {
        LOG.info("Starting trap consumer on {}", this.endpoint.getAddress());
    }
    this.listenGenericAddress = GenericAddress.parse(this.endpoint.getAddress());
    // either tcp or udp
    if ("tcp".equals(endpoint.getProtocol())) {
        this.transport = new DefaultTcpTransportMapping((TcpAddress) this.listenGenericAddress);
    } else if ("udp".equals(endpoint.getProtocol())) {
        this.transport = new DefaultUdpTransportMapping((UdpAddress) this.listenGenericAddress);
    } else {
        throw new IllegalArgumentException("Unknown protocol: " + endpoint.getProtocol());
    }
    this.snmp = new Snmp(transport);
    this.snmp.addCommandResponder(this);
    // listen to the transport
    if (LOG.isDebugEnabled()) {
        LOG.debug("Starting trap consumer on {} using {} protocol", endpoint.getAddress(), endpoint.getProtocol());
    }
    this.transport.listen();
    if (LOG.isInfoEnabled()) {
        LOG.info("Started trap consumer on {} using {} protocol", endpoint.getAddress(), endpoint.getProtocol());
    }
}
Also used : TcpAddress(org.snmp4j.smi.TcpAddress) DefaultTcpTransportMapping(org.snmp4j.transport.DefaultTcpTransportMapping) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping)

Example 12 with Snmp

use of org.snmp4j.Snmp in project camel by apache.

the class SnmpTrapProducer 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 Trap 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("SnmpTrap: getting pdu from body");
        PDU trap = exchange.getIn().getBody(PDU.class);
        trap.setErrorIndex(0);
        trap.setErrorStatus(0);
        trap.setMaxRepetitions(0);
        if (this.endpoint.getSnmpVersion() == SnmpConstants.version1) {
            trap.setType(PDU.V1TRAP);
        } else {
            trap.setType(PDU.TRAP);
        }
        LOG.debug("SnmpTrap: sending");
        snmp.send(trap, this.target);
        LOG.debug("SnmpTrap: sent");
    } finally {
        try {
            transport.close();
        } catch (Exception e) {
        }
        try {
            snmp.close();
        } catch (Exception e) {
        }
    }
}
Also used : PDU(org.snmp4j.PDU) DefaultTcpTransportMapping(org.snmp4j.transport.DefaultTcpTransportMapping) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping)

Example 13 with Snmp

use of org.snmp4j.Snmp in project opennms by OpenNMS.

the class Snmp4JStrategy method registerForTraps.

@Override
public void registerForTraps(final TrapNotificationListener listener, InetAddress address, int snmpTrapPort, List<SnmpV3User> snmpUsers) throws IOException {
    final RegistrationInfo info = new RegistrationInfo(listener, address, snmpTrapPort);
    final Snmp4JTrapNotifier trapNotifier = new Snmp4JTrapNotifier(listener);
    info.setHandler(trapNotifier);
    final UdpAddress udpAddress;
    if (address == null) {
        udpAddress = new UdpAddress(snmpTrapPort);
    } else {
        udpAddress = new UdpAddress(address, snmpTrapPort);
    }
    // Set socket option SO_REUSEADDR so that we can bind to the port even if it
    // has recently been closed by passing 'true' as the second argument here.
    final DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping(udpAddress, true);
    // Increase the receive buffer for the socket
    LOG.debug("Attempting to set receive buffer size to {}", Integer.MAX_VALUE);
    transport.setReceiveBufferSize(Integer.MAX_VALUE);
    LOG.debug("Actual receive buffer size is {}", transport.getReceiveBufferSize());
    info.setTransportMapping(transport);
    Snmp snmp = new Snmp(transport);
    snmp.addCommandResponder(trapNotifier);
    if (snmpUsers != null) {
        for (SnmpV3User user : snmpUsers) {
            SnmpAgentConfig config = new SnmpAgentConfig();
            config.setVersion(SnmpConfiguration.VERSION3);
            config.setSecurityName(user.getSecurityName());
            config.setAuthProtocol(user.getAuthProtocol());
            config.setAuthPassPhrase(user.getAuthPassPhrase());
            config.setPrivProtocol(user.getPrivProtocol());
            config.setPrivPassPhrase(user.getPrivPassPhrase());
            Snmp4JAgentConfig agentConfig = new Snmp4JAgentConfig(config);
            UsmUser usmUser = new UsmUser(agentConfig.getSecurityName(), agentConfig.getAuthProtocol(), agentConfig.getAuthPassPhrase(), agentConfig.getPrivProtocol(), agentConfig.getPrivPassPhrase());
            /* This doesn't work as expected. Basically SNMP4J is ignoring the engineId
                if (user.getEngineId() == null) {
                    snmp.getUSM().addUser(agentConfig.getSecurityName(), usmUser);
                } else {
                    snmp.getUSM().addUser(agentConfig.getSecurityName(), new OctetString(user.getEngineId()), usmUser);
                }
                */
            snmp.getUSM().addUser(agentConfig.getSecurityName(), usmUser);
        }
    }
    info.setSession(snmp);
    s_registrations.put(listener, info);
    snmp.listen();
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) UdpAddress(org.snmp4j.smi.UdpAddress) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) UsmUser(org.snmp4j.security.UsmUser) SnmpV3User(org.opennms.netmgt.snmp.SnmpV3User)

Example 14 with Snmp

use of org.snmp4j.Snmp in project opennms by OpenNMS.

the class Snmp4JStrategy method sendTest.

public void sendTest(String agentAddress, int port, String community, PDU pdu) {
    for (RegistrationInfo info : s_registrations.values()) {
        if (port == info.getPort()) {
            Snmp snmp = info.getSession();
            MessageDispatcher dispatcher = snmp.getMessageDispatcher();
            TransportMapping<UdpAddress> transport = info.getTransportMapping();
            int securityModel = (pdu instanceof PDUv1 ? SecurityModel.SECURITY_MODEL_SNMPv1 : SecurityModel.SECURITY_MODEL_SNMPv2c);
            int messageModel = (pdu instanceof PDUv1 ? MessageProcessingModel.MPv1 : MessageProcessingModel.MPv2c);
            CommandResponderEvent e = new CommandResponderEvent(dispatcher, transport, new IpAddress(agentAddress), messageModel, securityModel, community.getBytes(), SecurityLevel.NOAUTH_NOPRIV, new PduHandle(), pdu, 1000, null);
            info.getHandler().processPdu(e);
        }
    }
}
Also used : PduHandle(org.snmp4j.mp.PduHandle) MessageDispatcher(org.snmp4j.MessageDispatcher) UdpAddress(org.snmp4j.smi.UdpAddress) CommandResponderEvent(org.snmp4j.CommandResponderEvent) Snmp(org.snmp4j.Snmp) IpAddress(org.snmp4j.smi.IpAddress) PDUv1(org.snmp4j.PDUv1)

Example 15 with Snmp

use of org.snmp4j.Snmp 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)

Aggregations

Snmp (org.snmp4j.Snmp)21 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)17 OctetString (org.snmp4j.smi.OctetString)14 UdpAddress (org.snmp4j.smi.UdpAddress)12 PDU (org.snmp4j.PDU)10 ResponseEvent (org.snmp4j.event.ResponseEvent)9 CommunityTarget (org.snmp4j.CommunityTarget)8 ScopedPDU (org.snmp4j.ScopedPDU)8 UsmUser (org.snmp4j.security.UsmUser)7 USM (org.snmp4j.security.USM)6 IOException (java.io.IOException)5 UserTarget (org.snmp4j.UserTarget)4 DefaultTcpTransportMapping (org.snmp4j.transport.DefaultTcpTransportMapping)4 InetAddress (java.net.InetAddress)3 MessageDispatcher (org.snmp4j.MessageDispatcher)3 PDUv1 (org.snmp4j.PDUv1)3 Address (org.snmp4j.smi.Address)3 OID (org.snmp4j.smi.OID)3 MessageDispatcherImpl (org.snmp4j.MessageDispatcherImpl)2 Target (org.snmp4j.Target)2