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