Search in sources :

Example 6 with DefaultUdpTransportMapping

use of org.snmp4j.transport.DefaultUdpTransportMapping in project openhab1-addons by openhab.

the class SnmpBinding method listen.

/**
     * Configures a {@link DefaultUdpTransportMapping} and starts listening on
     * <code>SnmpBinding.port</code> for incoming SNMP Traps.
     */
private void listen() {
    UdpAddress address = new UdpAddress(SnmpBinding.port);
    try {
        if (transport != null) {
            transport.close();
            transport = null;
        }
        if (snmp != null) {
            snmp.close();
            snmp = null;
        }
        transport = new DefaultUdpTransportMapping(address);
        // add all security protocols
        SecurityProtocols.getInstance().addDefaultProtocols();
        SecurityProtocols.getInstance().addPrivacyProtocol(new Priv3DES());
        // Create Target
        if (SnmpBinding.community != null) {
            CommunityTarget target = new CommunityTarget();
            target.setCommunity(new OctetString(SnmpBinding.community));
        }
        snmp = new Snmp(transport);
        transport.listen();
        logger.debug("SNMP binding is listening on " + address);
    } catch (IOException ioe) {
        logger.error("SNMP binding couldn't listen to " + address, ioe);
    }
}
Also used : OctetString(org.snmp4j.smi.OctetString) UdpAddress(org.snmp4j.smi.UdpAddress) Priv3DES(org.snmp4j.security.Priv3DES) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) IOException(java.io.IOException) CommunityTarget(org.snmp4j.CommunityTarget)

Example 7 with DefaultUdpTransportMapping

use of org.snmp4j.transport.DefaultUdpTransportMapping in project camel by apache.

the class SnmpOIDPoller method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    this.targetAddress = GenericAddress.parse(this.endpoint.getAddress());
    // either tcp or udp
    if ("tcp".equals(endpoint.getProtocol())) {
        this.transport = new DefaultTcpTransportMapping();
    } else if ("udp".equals(endpoint.getProtocol())) {
        this.transport = new DefaultUdpTransportMapping();
    } else {
        throw new IllegalArgumentException("Unknown protocol: " + endpoint.getProtocol());
    }
    this.snmp = new Snmp(this.transport);
    if (SnmpConstants.version3 == endpoint.getSnmpVersion()) {
        UserTarget userTarget = new UserTarget();
        userTarget.setSecurityLevel(endpoint.getSecurityLevel());
        userTarget.setSecurityName(convertToOctetString(endpoint.getSecurityName()));
        userTarget.setAddress(targetAddress);
        userTarget.setRetries(endpoint.getRetries());
        userTarget.setTimeout(endpoint.getTimeout());
        userTarget.setVersion(endpoint.getSnmpVersion());
        this.target = userTarget;
        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
        SecurityModels.getInstance().addSecurityModel(usm);
        OID authProtocol = convertAuthenticationProtocol(endpoint.getAuthenticationProtocol());
        OctetString authPwd = convertToOctetString(endpoint.getAuthenticationPassphrase());
        OID privProtocol = convertPrivacyProtocol(endpoint.getPrivacyProtocol());
        OctetString privPwd = convertToOctetString(endpoint.getPrivacyPassphrase());
        UsmUser user = new UsmUser(convertToOctetString(endpoint.getSecurityName()), authProtocol, authPwd, privProtocol, privPwd);
        usm.addUser(convertToOctetString(endpoint.getSecurityName()), user);
        ScopedPDU scopedPDU = new ScopedPDU();
        if (endpoint.getSnmpContextEngineId() != null) {
            scopedPDU.setContextEngineID(new OctetString(endpoint.getSnmpContextEngineId()));
        }
        if (endpoint.getSnmpContextName() != null) {
            scopedPDU.setContextName(new OctetString(endpoint.getSnmpContextName()));
        }
        this.pdu = scopedPDU;
    } else {
        CommunityTarget communityTarget = new CommunityTarget();
        communityTarget.setCommunity(convertToOctetString(endpoint.getSnmpCommunity()));
        communityTarget.setAddress(targetAddress);
        communityTarget.setRetries(endpoint.getRetries());
        communityTarget.setTimeout(endpoint.getTimeout());
        communityTarget.setVersion(endpoint.getSnmpVersion());
        this.target = communityTarget;
        this.pdu = new PDU();
    }
    // listen to the transport
    if (LOG.isDebugEnabled()) {
        LOG.debug("Starting OID poller on {} using {} protocol", endpoint.getAddress(), endpoint.getProtocol());
    }
    this.transport.listen();
    if (LOG.isInfoEnabled()) {
        LOG.info("Started OID poller on {} using {} protocol", endpoint.getAddress(), endpoint.getProtocol());
    }
}
Also used : OctetString(org.snmp4j.smi.OctetString) ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) ScopedPDU(org.snmp4j.ScopedPDU) DefaultTcpTransportMapping(org.snmp4j.transport.DefaultTcpTransportMapping) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) OID(org.snmp4j.smi.OID) UserTarget(org.snmp4j.UserTarget) UsmUser(org.snmp4j.security.UsmUser) CommunityTarget(org.snmp4j.CommunityTarget) USM(org.snmp4j.security.USM)

Example 8 with DefaultUdpTransportMapping

use of org.snmp4j.transport.DefaultUdpTransportMapping 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 9 with DefaultUdpTransportMapping

use of org.snmp4j.transport.DefaultUdpTransportMapping 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 10 with DefaultUdpTransportMapping

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

Aggregations

Snmp (org.snmp4j.Snmp)17 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)17 OctetString (org.snmp4j.smi.OctetString)13 UdpAddress (org.snmp4j.smi.UdpAddress)11 PDU (org.snmp4j.PDU)9 ResponseEvent (org.snmp4j.event.ResponseEvent)8 CommunityTarget (org.snmp4j.CommunityTarget)7 ScopedPDU (org.snmp4j.ScopedPDU)7 UsmUser (org.snmp4j.security.UsmUser)7 USM (org.snmp4j.security.USM)6 IOException (java.io.IOException)4 UserTarget (org.snmp4j.UserTarget)4 DefaultTcpTransportMapping (org.snmp4j.transport.DefaultTcpTransportMapping)4 OID (org.snmp4j.smi.OID)3 InetAddress (java.net.InetAddress)2 Target (org.snmp4j.Target)2 Address (org.snmp4j.smi.Address)2 List (java.util.List)1 TimeoutException (java.util.concurrent.TimeoutException)1 Test (org.junit.Test)1