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);
}
}
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());
}
}
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) {
}
}
}
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());
}
}
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) {
}
}
}
Aggregations