use of org.opennms.netmgt.snmp.SnmpException 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();
Snmp4JStrategy.trackSession(session);
} catch (final Exception e) {
LOG.error("send: Could not create SNMP session for agent {}", agentConfig, e);
future.completeExceptionally(new SnmpException("Could not create SNMP session for agent", e));
return;
}
if (expectResponse) {
try {
session.listen();
} catch (final Exception e) {
closeQuietly(session);
LOG.error("send: error setting up listener for SNMP responses", e);
future.completeExceptionally(new SnmpException("error setting up listener for SNMP responses", e));
return;
}
try {
final Snmp mySession = session;
mySession.send(pdu, agentConfig.getTarget(), null, new ResponseListener() {
@Override
public void onResponse(final ResponseEvent responseEvent) {
try {
future.complete(processResponse(agentConfig, responseEvent));
} catch (final Exception e) {
future.completeExceptionally(new SnmpException(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(mySession);
}
});
}
}
});
} catch (final Exception e) {
// The ResponseListener will not be called since an exception occurred in the send,
// so we make sure to close the session here
closeQuietly(session);
LOG.error("send: error during SNMP operation", e);
future.completeExceptionally(e);
}
} else {
// we're not expecting a response
try {
session.send(pdu, agentConfig.getTarget());
future.complete(null);
} catch (final Exception e) {
LOG.error("send: error during SNMP operation", e);
future.completeExceptionally(new SnmpException(e));
} finally {
closeQuietly(session);
Snmp4JStrategy.reapSession(session);
}
}
}
use of org.opennms.netmgt.snmp.SnmpException in project opennms by OpenNMS.
the class Snmp4JStrategy method buildPdu.
protected PDU buildPdu(Snmp4JAgentConfig agentConfig, int pduType, SnmpObjId[] oids, SnmpValue[] values) {
PDU pdu = agentConfig.createPdu(pduType);
if (values == null) {
for (SnmpObjId oid : oids) {
pdu.add(new VariableBinding(new OID(oid.toString())));
}
} else {
// TODO should this throw an exception? This situation is fairly bogus and probably signifies a coding error.
if (oids.length != values.length) {
Exception e = new SnmpException("PDU values do not match OIDs");
LOG.error("PDU to prepare has object values but not the same number as there are OIDs. There are {} OIDs and {} object values.", oids.length, values.length, e);
return null;
}
for (int i = 0; i < oids.length; i++) {
pdu.add(new VariableBinding(new OID(oids[i].toString()), new Snmp4JValue(values[i].getType(), values[i].getBytes()).getVariable()));
}
}
// TODO should this throw an exception? This situation is fairly bogus.
if (pdu.getVariableBindings().size() != oids.length) {
Exception e = new SnmpException("PDU bindings do not match OIDs");
LOG.error("Prepared PDU does not have as many variable bindings as there are OIDs. There are {} OIDs and {} variable bindings.", oids.length, pdu.getVariableBindings(), e);
return null;
}
return pdu;
}
use of org.opennms.netmgt.snmp.SnmpException in project opennms by OpenNMS.
the class Snmp4JWalker method sendNextPdu.
@Override
protected void sendNextPdu(WalkerPduBuilder pduBuilder) throws SnmpException {
Snmp4JPduBuilder snmp4JPduBuilder = (Snmp4JPduBuilder) pduBuilder;
try {
if (m_session == null) {
m_session = m_agentConfig.createSnmpSession();
Snmp4JStrategy.trackSession(m_session);
m_session.listen();
}
} catch (final IOException e) {
close();
throw new SnmpException(e);
}
LOG.debug("Sending tracker pdu of size {}", snmp4JPduBuilder.getPdu().size());
try {
m_session.send(snmp4JPduBuilder.getPdu(), m_tgt, null, m_listener);
} catch (final IOException e) {
LOG.debug("Failed to send pdu of size {}", snmp4JPduBuilder.getPdu().size(), e);
close();
throw new SnmpException(e);
}
}
Aggregations