use of org.snmp4j.mp.MPv1 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.mp.MPv1 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.mp.MPv1 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.mp.MPv1 in project LogHub by fbacchella.
the class SnmpTrap method configure.
@Override
public boolean configure(Properties properties) {
decoder = Receiver.NULLDECODER;
if (!reconfigured && properties.containsKey("mibdirs")) {
reconfigured = true;
String[] mibdirs = null;
try {
mibdirs = Arrays.stream((Object[]) properties.get("mibdirs")).map(i -> i.toString()).toArray(String[]::new);
formatter = OIDFormatter.register(mibdirs);
} catch (ClassCastException e) {
logger.error("mibdirs property is not a string array");
logger.catching(Level.DEBUG, e.getCause());
return false;
}
} else {
formatter = OIDFormatter.register();
}
threadPool = ThreadPool.create("Trap", 2);
MultiThreadedMessageDispatcher dispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());
dispatcher.addCommandResponder(this);
dispatcher.addMessageProcessingModel(new MPv1());
dispatcher.addMessageProcessingModel(new MPv2c());
Address listenAddress = GenericAddress.parse(protocol + ":" + listen + "/" + port);
TransportMapping<?> transport;
try {
transport = new DefaultUdpTransportMapping((UdpAddress) listenAddress);
} catch (IOException e) {
logger.error("can't bind to {}: {}", listenAddress, e.getMessage());
return false;
}
snmp = new Snmp(dispatcher, transport);
try {
snmp.listen();
} catch (IOException e) {
logger.error("can't listen: {}", e.getMessage());
}
return super.configure(properties);
}
Aggregations