use of org.snmp4j.MessageDispatcherImpl in project opennms by OpenNMS.
the class Snmp4JAgentConfig method createSnmpSession.
public Snmp createSnmpSession() throws IOException {
final TransportMapping<?> transport = new DefaultUdpTransportMapping();
final MessageDispatcher disp = new MessageDispatcherImpl();
final Snmp session;
// models we need for the specific agent
if (!isSnmpV3()) {
disp.addMessageProcessingModel(new MPv1());
disp.addMessageProcessingModel(new MPv2c());
session = new Snmp(disp, transport);
} else {
// Make a new USM
final USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
// Add the specified user to the USM
usm.addUser(getSecurityName(), new UsmUser(getSecurityName(), getAuthProtocol(), getAuthPassPhrase(), getPrivProtocol(), getPrivPassPhrase()));
disp.addMessageProcessingModel(new MPv3(usm));
session = new Snmp(disp, transport);
}
return session;
}
use of org.snmp4j.MessageDispatcherImpl 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());
final Snmp snmp = new Snmp(dispatcher, responder);
Snmp4JStrategy.trackSession(snmp);
try {
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();
} finally {
try {
snmp.close();
} catch (final IOException e) {
LOG.error("failed to close SNMP session", e);
} finally {
Snmp4JStrategy.reapSession(snmp);
}
}
}
use of org.snmp4j.MessageDispatcherImpl in project opennms by OpenNMS.
the class MockSnmpAgent method initMessageDispatcher.
/**
* <p>
* Note that this method can hang if your system entropy is not high enough.
* Here is a stack trace from a test running on JDK 1.8u40:</p>
*
* <pre>
* Thread [MockSnmpAgent-1657048932] (Suspended)
* owns: SecureRandom (id=26)
* owns: SecureRandom (id=27)
* owns: SecurityProtocols (id=28)
* FileInputStream.readBytes(byte[], int, int) line: not available [native method]
* FileInputStream.read(byte[], int, int) line: 255
* NativeSeedGenerator(SeedGenerator$URLSeedGenerator).getSeedBytes(byte[]) line: 539
* SeedGenerator.generateSeed(byte[]) line: 144
* SecureRandom$SeederHolder.<clinit>() line: 203 [local variables unavailable]
* SecureRandom.engineNextBytes(byte[]) line: 221
* SecureRandom.nextBytes(byte[]) line: 468
* Salt.<init>() line: 54
* Salt.getInstance() line: 79
* PrivDES.<init>() line: 57
* SecurityProtocols.addDefaultProtocols() line: 155
* MockSnmpAgent.initMessageDispatcher() line: 306
* MockSnmpAgent(BaseAgent).init() line: 145
* MockSnmpAgent.run() line: 380
* Thread.run() line: 745
* </pre>
*/
@Override
protected void initMessageDispatcher() {
s_log.info("MockSnmpAgent: starting initMessageDispatcher()");
try {
dispatcher = new MessageDispatcherImpl();
usm = new USM(SecurityProtocols.getInstance(), agent.getContextEngineID(), updateEngineBoots());
mpv3 = new MPv3(usm);
SecurityProtocols.getInstance().addDefaultProtocols();
dispatcher.addMessageProcessingModel(new MPv1());
dispatcher.addMessageProcessingModel(new MPv2c());
dispatcher.addMessageProcessingModel(mpv3);
initSnmpSession();
} finally {
s_log.info("MockSnmpAgent: finished initMessageDispatcher()");
}
}
use of org.snmp4j.MessageDispatcherImpl in project LogHub by fbacchella.
the class TestTrap method testtrapv1Generic.
@Ignore
@Test
public void testtrapv1Generic() throws InterruptedException, IOException {
BlockingQueue<Event> receiver = new ArrayBlockingQueue<>(2);
SnmpTrap r = new SnmpTrap(receiver, new Pipeline(Collections.emptyList(), "testbig", null));
r.setPort(0);
Map<String, Object> props = new HashMap<>();
props.put("mibdirs", new String[] { "/usr/share/snmp/mibs", "/tmp/mibs" });
Assert.assertTrue(r.configure(new Properties(props)));
r.start();
CommandResponderEvent trapEvent = new CommandResponderEvent(new MessageDispatcherImpl(), new DefaultUdpTransportMapping(), TransportIpAddress.parse("127.0.0.1/162"), 0, 0, null, 0, null, null, 0, null);
PDUv1 pdu = new PDUv1();
pdu.setEnterprise(new OID("1.3.6.1.4.1.232"));
pdu.setAgentAddress(new IpAddress());
pdu.setGenericTrap(1);
pdu.setTimestamp(10);
pdu.add(new VariableBinding(new OID("1.3.6.1.6.3.1.1.4.1"), new OctetString("lldpRemTablesChange")));
trapEvent.setPDU(pdu);
r.processPdu(trapEvent);
Event e = receiver.poll();
Assert.assertEquals(0.1, (Double) e.get("time_stamp"), 1e-10);
Assert.assertEquals("warmStart", e.get("generic_trap"));
Assert.assertEquals("compaq", e.get("enterprise"));
Assert.assertEquals(null, e.get("specific_trap"));
Assert.assertEquals("lldpRemTablesChange", e.get("snmpTrapOID"));
Assert.assertEquals(InetAddress.getByName("0.0.0.0"), e.get("agent_addr"));
r.interrupt();
}
use of org.snmp4j.MessageDispatcherImpl in project LogHub by fbacchella.
the class TestTrap method testbig.
// @Test
// public void testone() throws InterruptedException, IOException {
// BlockingQueue<Event> receiver = new ArrayBlockingQueue<>(1);
// SnmpTrap r = new SnmpTrap(receiver, new Pipeline(Collections.emptyList(), "testone", null));
// r.setPort(0);
// Assert.assertTrue("Failed to configure trap receiver", r.configure(new Properties(Collections.emptyMap())));;
// List<String> content = r.smartPrint(new OID("1.0.8802.1.1.2.1.1.2.5"));
// Assert.assertEquals(1, content.size());
// Assert.assertEquals("lldpMessageTxHoldMultiplier", content.get(0));
// r.close();
// }
@Ignore
@Test
public void testbig() throws InterruptedException, IOException {
BlockingQueue<Event> receiver = new ArrayBlockingQueue<>(2);
SnmpTrap r = new SnmpTrap(receiver, new Pipeline(Collections.emptyList(), "testbig", null));
r.setPort(0);
Map<String, Object> props = new HashMap<>();
props.put("mibdirs", new String[] { "/usr/share/snmp/mibs", "/tmp/mibs" });
Assert.assertTrue(r.configure(new Properties(props)));
r.start();
CommandResponderEvent trapEvent = new CommandResponderEvent(new MessageDispatcherImpl(), new DefaultUdpTransportMapping(), TransportIpAddress.parse("127.0.0.1/162"), 0, 0, null, 0, null, null, 0, null);
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID("1.0.8802.1.1.2.1.4.1.1.8.207185300.2.15079"), new OctetString("vnet7")));
pdu.add(new VariableBinding(new OID("1.0.8802.1.1.2.1.3.7.1.4.2"), new OctetString("eth0")));
pdu.add(new VariableBinding(new OID("1.0.8802.1.1.2.1.4.1.1.9.207185300.2.15079"), new OctetString("localhost")));
pdu.add(new VariableBinding(new OID("1.3.6.1.6.3.1.1.4.1"), new OctetString("lldpRemTablesChange")));
trapEvent.setPDU(pdu);
r.processPdu(trapEvent);
Event e = receiver.poll();
logger.debug(e.getClass());
@SuppressWarnings("unchecked") Map<String, ?> details = (Map<String, ?>) e.get("lldpRemSysName");
Assert.assertEquals(3, Array.getLength(details.get("index")));
Assert.assertEquals("localhost", details.get("value"));
r.interrupt();
}
Aggregations