Search in sources :

Example 1 with MPv2c

use of org.snmp4j.mp.MPv2c 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;
}
Also used : OctetString(org.snmp4j.smi.OctetString) MessageDispatcher(org.snmp4j.MessageDispatcher) MPv3(org.snmp4j.mp.MPv3) MPv2c(org.snmp4j.mp.MPv2c) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) MessageDispatcherImpl(org.snmp4j.MessageDispatcherImpl) MPv1(org.snmp4j.mp.MPv1) UsmUser(org.snmp4j.security.UsmUser) USM(org.snmp4j.security.USM)

Example 2 with MPv2c

use of org.snmp4j.mp.MPv2c 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);
        }
    }
}
Also used : OctetString(org.snmp4j.smi.OctetString) Address(org.snmp4j.smi.Address) IpAddress(org.snmp4j.smi.IpAddress) InetAddress(java.net.InetAddress) TransportMapping(org.snmp4j.TransportMapping) AbstractTransportMapping(org.snmp4j.transport.AbstractTransportMapping) MPv2c(org.snmp4j.mp.MPv2c) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageDispatcherImpl(org.snmp4j.MessageDispatcherImpl) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) TransportListener(org.snmp4j.transport.TransportListener) MessageDispatcher(org.snmp4j.MessageDispatcher) TransportStateReference(org.snmp4j.TransportStateReference) Snmp(org.snmp4j.Snmp) IpAddress(org.snmp4j.smi.IpAddress) MPv1(org.snmp4j.mp.MPv1) DummyTransport(org.snmp4j.transport.DummyTransport) PDUv1(org.snmp4j.PDUv1) CommunityTarget(org.snmp4j.CommunityTarget)

Example 3 with MPv2c

use of org.snmp4j.mp.MPv2c 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()");
    }
}
Also used : MPv3(org.snmp4j.mp.MPv3) MPv2c(org.snmp4j.mp.MPv2c) MessageDispatcherImpl(org.snmp4j.MessageDispatcherImpl) MPv1(org.snmp4j.mp.MPv1) USM(org.snmp4j.security.USM)

Example 4 with MPv2c

use of org.snmp4j.mp.MPv2c 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);
}
Also used : UdpAddress(org.snmp4j.smi.UdpAddress) Address(org.snmp4j.smi.Address) IpAddress(org.snmp4j.smi.IpAddress) TransportIpAddress(org.snmp4j.smi.TransportIpAddress) UdpAddress(org.snmp4j.smi.UdpAddress) InetSocketAddress(java.net.InetSocketAddress) GenericAddress(org.snmp4j.smi.GenericAddress) MPv2c(org.snmp4j.mp.MPv2c) MessageDispatcherImpl(org.snmp4j.MessageDispatcherImpl) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) OctetString(org.snmp4j.smi.OctetString) IOException(java.io.IOException) Snmp(org.snmp4j.Snmp) MPv1(org.snmp4j.mp.MPv1) MultiThreadedMessageDispatcher(org.snmp4j.util.MultiThreadedMessageDispatcher)

Aggregations

MessageDispatcherImpl (org.snmp4j.MessageDispatcherImpl)4 MPv1 (org.snmp4j.mp.MPv1)4 MPv2c (org.snmp4j.mp.MPv2c)4 Snmp (org.snmp4j.Snmp)3 OctetString (org.snmp4j.smi.OctetString)3 IOException (java.io.IOException)2 MessageDispatcher (org.snmp4j.MessageDispatcher)2 MPv3 (org.snmp4j.mp.MPv3)2 USM (org.snmp4j.security.USM)2 Address (org.snmp4j.smi.Address)2 IpAddress (org.snmp4j.smi.IpAddress)2 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)2 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 CommunityTarget (org.snmp4j.CommunityTarget)1 PDUv1 (org.snmp4j.PDUv1)1 TransportMapping (org.snmp4j.TransportMapping)1