Search in sources :

Example 1 with DefaultTcpTransportMapping

use of org.snmp4j.transport.DefaultTcpTransportMapping 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 2 with DefaultTcpTransportMapping

use of org.snmp4j.transport.DefaultTcpTransportMapping 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 3 with DefaultTcpTransportMapping

use of org.snmp4j.transport.DefaultTcpTransportMapping 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 4 with DefaultTcpTransportMapping

use of org.snmp4j.transport.DefaultTcpTransportMapping 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)4 DefaultTcpTransportMapping (org.snmp4j.transport.DefaultTcpTransportMapping)4 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)4 PDU (org.snmp4j.PDU)2 TimeoutException (java.util.concurrent.TimeoutException)1 CommunityTarget (org.snmp4j.CommunityTarget)1 ScopedPDU (org.snmp4j.ScopedPDU)1 UserTarget (org.snmp4j.UserTarget)1 ResponseEvent (org.snmp4j.event.ResponseEvent)1 USM (org.snmp4j.security.USM)1 UsmUser (org.snmp4j.security.UsmUser)1 OID (org.snmp4j.smi.OID)1 OctetString (org.snmp4j.smi.OctetString)1 TcpAddress (org.snmp4j.smi.TcpAddress)1