use of org.snmp4j.smi.IpAddress in project opennms by OpenNMS.
the class Snmp4JUtils method convertPduToBytes.
/**
* @param address
* @param port
* @param community
* @param pdu
*
* @return Byte array representing the {@link PDU} in either SNMPv1 or SNMPv2
* format, depending on the type of the {@link PDU} object.
*/
public static byte[] convertPduToBytes(InetAddress address, int port, String community, PDU pdu) throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<byte[]> bytes = new AtomicReference<>();
// IP address is optional when using the DummyTransport because
// all requests are sent to the {@link DummyTransportResponder}
final DummyTransport<IpAddress> transport = new DummyTransport<IpAddress>(null);
final AbstractTransportMapping<IpAddress> responder = transport.getResponder(null);
// Add a DummyTransportResponder listener that will receive the raw bytes of the PDU
responder.addTransportListener(new TransportListener() {
@Override
public void processMessage(TransportMapping transport, Address address, ByteBuffer byteBuffer, TransportStateReference state) {
byteBuffer.rewind();
final byte[] byteArray = new byte[byteBuffer.remaining()];
byteBuffer.get(byteArray);
bytes.set(byteArray);
byteBuffer.rewind();
latch.countDown();
}
});
// Create our own MessageDispatcher since we don't need to do all
// of the crypto operations necessary to initialize SNMPv3 which is slow
MessageDispatcher dispatcher = new MessageDispatcherImpl();
dispatcher.addMessageProcessingModel(new MPv1());
dispatcher.addMessageProcessingModel(new MPv2c());
Snmp snmp = new Snmp(dispatcher, responder);
snmp.listen();
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
if (pdu instanceof PDUv1) {
target.setVersion(SnmpConstants.version1);
} else {
target.setVersion(SnmpConstants.version2c);
}
target.setAddress(Snmp4JAgentConfig.convertAddress(address, port));
snmp.send(pdu, target, transport);
latch.await();
return bytes.get();
}
use of org.snmp4j.smi.IpAddress in project opennms by OpenNMS.
the class TrapDTOMapperTest method object2dtoTest.
@Test
public void object2dtoTest() throws UnknownHostException {
long testStartTime = new Date().getTime();
PDU snmp4JV2cTrapPdu = new PDU();
snmp4JV2cTrapPdu.setType(PDU.TRAP);
OID oid = new OID(".1.3.6.1.2.1.1.3.0");
snmp4JV2cTrapPdu.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000)));
snmp4JV2cTrapPdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
snmp4JV2cTrapPdu.add(new VariableBinding(SnmpConstants.snmpTrapAddress, new IpAddress("127.0.0.1")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(oid), new OctetString("Trap Msg v2-1")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(oid), new OctetString("Trap Msg v2-2")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new OctetString("Trap v1 msg-1")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.3"), new OctetString("Trap v1 msg-2")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(".1.3.6.1.6.3.1.1.4.1.1"), new OctetString("Trap v1 msg-3")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.733.6.3.18.1.5.0"), new Integer32(1)));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new Null()));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.1"), new Null(128)));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.2"), new Null(129)));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.3"), new Null(130)));
TrapInformation snmp4JV2cTrap = new Snmp4JTrapNotifier.Snmp4JV2TrapInformation(InetAddressUtils.ONE_TWENTY_SEVEN, "public", snmp4JV2cTrapPdu);
TrapDTO trapDto = new TrapDTO(snmp4JV2cTrap);
System.out.println("trapDto is : " + trapDto);
System.out.println("trapDto.getBody() is : " + trapDto.getRawMessage());
System.out.println("trapDto.getCommunity() is : " + trapDto.getCommunity());
assertEquals(".1.3.6.1.2.1.1.3", trapDto.getTrapIdentity().getEnterpriseId());
assertEquals(6, trapDto.getTrapIdentity().getGeneric());
assertEquals(0, trapDto.getTrapIdentity().getSpecific());
assertEquals(InetAddressUtils.ONE_TWENTY_SEVEN, trapDto.getAgentAddress());
assertEquals("public", trapDto.getCommunity());
assertEquals(5000, trapDto.getTimestamp());
// Trap and agent address are identical with SNMPv2
assertEquals(InetAddressUtils.ONE_TWENTY_SEVEN, trapDto.getAgentAddress());
assertEquals("v2", trapDto.getVersion());
// Make sure that the message was created after the start of the test
assertTrue(trapDto.getCreationTime() >= testStartTime);
}
use of org.snmp4j.smi.IpAddress 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.smi.IpAddress in project opennms by OpenNMS.
the class Snmp4JTrapNotifier method processPdu.
@Override
public void processPdu(CommandResponderEvent e) {
PDU command = e.getPDU();
if (command != null) {
IpAddress addr = ((IpAddress) e.getPeerAddress());
if (command.getType() == PDU.INFORM) {
// Backing up original content
int errorIndex = command.getErrorIndex();
int errorStatus = command.getErrorStatus();
int type = command.getType();
// Prepare resopnse
command.setErrorIndex(0);
command.setErrorStatus(0);
command.setType(PDU.RESPONSE);
StatusInformation statusInformation = new StatusInformation();
StateReference ref = e.getStateReference();
// Send the response
try {
e.getMessageDispatcher().returnResponsePdu(e.getMessageProcessingModel(), e.getSecurityModel(), e.getSecurityName(), e.getSecurityLevel(), command, e.getMaxSizeResponsePDU(), ref, statusInformation);
LOG.debug("Sent RESPONSE PDU to peer {} acknowledging receipt of INFORM (reqId={})", addr, command.getRequestID());
} catch (MessageException ex) {
LOG.error("Error while sending RESPONSE PDU to peer {}: {} acknowledging receipt of INFORM (reqId={})", addr, ex.getMessage(), command.getRequestID());
} finally {
// Restoring original settings
command.setErrorIndex(errorIndex);
command.setErrorStatus(errorStatus);
command.setType(type);
}
}
if (command instanceof PDUv1) {
m_listener.trapReceived(new Snmp4JV1TrapInformation(addr.getInetAddress(), new String(e.getSecurityName()), (PDUv1) command));
} else {
m_listener.trapReceived(new Snmp4JV2TrapInformation(addr.getInetAddress(), new String(e.getSecurityName()), command));
}
}
}
use of org.snmp4j.smi.IpAddress in project opennms by OpenNMS.
the class Snmp4JDummyTransportTest method makePdu.
private static final PDU makePdu() {
PDU snmp4JV2cTrapPdu = new PDUv1();
OID oid = new OID(".1.3.6.1.2.1.1.3.0");
snmp4JV2cTrapPdu.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000)));
snmp4JV2cTrapPdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, new OID(oid)));
snmp4JV2cTrapPdu.add(new VariableBinding(SnmpConstants.snmpTrapAddress, new IpAddress("127.0.0.1")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(oid), new OctetString("Trap Msg v2-1")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(oid), new OctetString("Trap Msg v2-2")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new OctetString("Trap v1 msg-1")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.3"), new OctetString("Trap v1 msg-2")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(".1.3.6.1.6.3.1.1.4.1.1"), new OctetString("Trap v1 msg-3")));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID(".1.3.6.1.4.1.733.6.3.18.1.5.0"), new Integer32(1)));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new Null()));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.1"), new Null(128)));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.2"), new Null(129)));
snmp4JV2cTrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.3"), new Null(130)));
snmp4JV2cTrapPdu.setType(PDU.V1TRAP);
return snmp4JV2cTrapPdu;
}
Aggregations