use of org.snmp4j.event.ResponseEvent in project opennms by OpenNMS.
the class BrocadeMibIT method sendRequestV1V2.
private PDU sendRequestV1V2(PDU pdu, int version) throws Exception {
PDU response;
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(new UdpAddress(m_agent.getInetAddress(), m_agent.getPort()));
target.setVersion(version);
TransportMapping<UdpAddress> transport = null;
try {
transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
transport.listen();
ResponseEvent e = snmp.send(pdu, target);
response = e.getResponse();
} finally {
if (transport != null) {
transport.close();
}
}
return response;
}
use of org.snmp4j.event.ResponseEvent 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.event.ResponseEvent 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);
}
}
use of org.snmp4j.event.ResponseEvent in project opennms by OpenNMS.
the class MockAgentTest method testGetSysName.
public void testGetSysName() throws IOException {
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
Address addr = new UdpAddress(InetAddress.getLocalHost(), 9161);
//Address addr = new UdpAddress(InetAddressUtils.addr("192.168.0.100"), 161);
Target target = new CommunityTarget(addr, new OctetString("public"));
target.setVersion(SnmpConstants.version1);
target.setTimeout(3000);
target.setRetries(3);
PDUv1 getRequest = new PDUv1();
getRequest.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0")));
ResponseEvent e = snmp.get(getRequest, target);
PDU response = e.getResponse();
assertEquals(new OctetString("mockhost"), response.get(0).getVariable());
}
use of org.snmp4j.event.ResponseEvent in project opennms by OpenNMS.
the class MockSnmpAgentIT method sendRequestV3.
private PDU sendRequestV3(PDU pdu) {
PDU response = null;
OctetString userId = new OctetString("opennmsUser");
OctetString pw = new OctetString("0p3nNMSv3");
UserTarget target = new UserTarget();
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
target.setSecurityName(userId);
target.setAddress(new UdpAddress(InetAddressUtils.addr("127.0.0.1"), m_agent.getPort()));
target.setVersion(SnmpConstants.version3);
target.setTimeout(DEFAULT_TIMEOUT);
TransportMapping<UdpAddress> transport = null;
Snmp snmp = null;
try {
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
UsmUser user = new UsmUser(userId, AuthMD5.ID, pw, PrivDES.ID, pw);
snmp.getUSM().addUser(userId, user);
transport.listen();
ResponseEvent e = snmp.send(pdu, target);
response = e.getResponse();
} catch (final IOException e) {
e.printStackTrace();
} finally {
if (snmp != null) {
try {
snmp.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
if (transport != null) {
try {
transport.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
}
return response;
}
Aggregations