Search in sources :

Example 1 with SnmpV3User

use of org.opennms.netmgt.snmp.SnmpV3User in project opennms by OpenNMS.

the class Snmp4jTrapReceiverIT method testTrapReceiverWithOpenNMSAuthPriv.

@Test
public void testTrapReceiverWithOpenNMSAuthPriv() {
    final Snmp4JStrategy strategy = new Snmp4JStrategy();
    assertEquals(0, m_trapCount);
    LOG.debug("ONMS: Register for Traps");
    final TestTrapListener trapListener = new TestTrapListener();
    SnmpV3User user = new SnmpV3User("opennmsUser", "MD5", "0p3nNMSv3", "DES", "0p3nNMSv3");
    try {
        strategy.registerForTraps(trapListener, getAgentAddress(), 9162, Collections.singletonList(user));
        sendTraps(strategy, SnmpConfiguration.AUTH_PRIV);
        await().atMost(5, SECONDS).until(() -> m_trapCount, equalTo(2));
    } catch (final IOException e) {
        LOG.debug("Failed to register for traps.", e);
    } catch (final Exception e) {
        LOG.debug("Failed to send traps.", e);
    } finally {
        LOG.debug("ONMS: Unregister for Traps");
        try {
            strategy.unregisterForTraps(trapListener, 9162);
        } catch (final IOException e) {
            LOG.debug("Failed to unregister for traps.", e);
        }
    }
    LOG.debug("ONMS: Checking Trap status");
    assertFalse(trapListener.hasError());
    assertEquals(2, trapListener.getReceivedTrapCount());
    strategy.clearUsers();
}
Also used : IOException(java.io.IOException) IOException(java.io.IOException) SnmpV3User(org.opennms.netmgt.snmp.SnmpV3User) Test(org.junit.Test)

Example 2 with SnmpV3User

use of org.opennms.netmgt.snmp.SnmpV3User in project opennms by OpenNMS.

the class TrapdConfigBean method toSnmpV3User.

private static SnmpV3User toSnmpV3User(Snmpv3User snmpv3User) {
    SnmpV3User snmpV3User = new SnmpV3User();
    snmpV3User.setAuthPassPhrase(snmpv3User.getAuthPassphrase());
    snmpV3User.setAuthProtocol(snmpv3User.getAuthProtocol());
    snmpV3User.setEngineId(snmpv3User.getEngineId());
    snmpV3User.setPrivPassPhrase(snmpv3User.getPrivacyPassphrase());
    snmpV3User.setPrivProtocol(snmpv3User.getPrivacyProtocol());
    snmpV3User.setSecurityName(snmpv3User.getSecurityName());
    return snmpV3User;
}
Also used : SnmpV3User(org.opennms.netmgt.snmp.SnmpV3User)

Example 3 with SnmpV3User

use of org.opennms.netmgt.snmp.SnmpV3User in project opennms by OpenNMS.

the class Snmp4JStrategy method registerForTraps.

@Override
public void registerForTraps(final TrapNotificationListener listener, InetAddress address, int snmpTrapPort, List<SnmpV3User> snmpUsers) throws IOException {
    final RegistrationInfo info = new RegistrationInfo(listener, address, snmpTrapPort);
    final Snmp4JTrapNotifier trapNotifier = new Snmp4JTrapNotifier(listener);
    info.setHandler(trapNotifier);
    final UdpAddress udpAddress;
    if (address == null) {
        udpAddress = new UdpAddress(snmpTrapPort);
    } else {
        udpAddress = new UdpAddress(address, snmpTrapPort);
    }
    // Set socket option SO_REUSEADDR so that we can bind to the port even if it
    // has recently been closed by passing 'true' as the second argument here.
    final DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping(udpAddress, true);
    // Increase the receive buffer for the socket
    LOG.debug("Attempting to set receive buffer size to {}", Integer.MAX_VALUE);
    transport.setReceiveBufferSize(Integer.MAX_VALUE);
    LOG.debug("Actual receive buffer size is {}", transport.getReceiveBufferSize());
    info.setTransportMapping(transport);
    Snmp snmp = new Snmp(transport);
    snmp.addCommandResponder(trapNotifier);
    if (snmpUsers != null) {
        for (SnmpV3User user : snmpUsers) {
            SnmpAgentConfig config = new SnmpAgentConfig();
            config.setVersion(SnmpConfiguration.VERSION3);
            config.setSecurityName(user.getSecurityName());
            config.setAuthProtocol(user.getAuthProtocol());
            config.setAuthPassPhrase(user.getAuthPassPhrase());
            config.setPrivProtocol(user.getPrivProtocol());
            config.setPrivPassPhrase(user.getPrivPassPhrase());
            Snmp4JAgentConfig agentConfig = new Snmp4JAgentConfig(config);
            UsmUser usmUser = new UsmUser(agentConfig.getSecurityName(), agentConfig.getAuthProtocol(), agentConfig.getAuthPassPhrase(), agentConfig.getPrivProtocol(), agentConfig.getPrivPassPhrase());
            /* This doesn't work as expected. Basically SNMP4J is ignoring the engineId
                if (user.getEngineId() == null) {
                    snmp.getUSM().addUser(agentConfig.getSecurityName(), usmUser);
                } else {
                    snmp.getUSM().addUser(agentConfig.getSecurityName(), new OctetString(user.getEngineId()), usmUser);
                }
                */
            snmp.getUSM().addUser(agentConfig.getSecurityName(), usmUser);
        }
    }
    info.setSession(snmp);
    s_registrations.put(listener, info);
    snmp.listen();
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) UdpAddress(org.snmp4j.smi.UdpAddress) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) UsmUser(org.snmp4j.security.UsmUser) SnmpV3User(org.opennms.netmgt.snmp.SnmpV3User)

Example 4 with SnmpV3User

use of org.opennms.netmgt.snmp.SnmpV3User in project opennms by OpenNMS.

the class Snmp4jTrapReceiverIT method testTrapReceiverWithOpenNMSNoAuthNoPriv.

@Test
public void testTrapReceiverWithOpenNMSNoAuthNoPriv() {
    final Snmp4JStrategy strategy = new Snmp4JStrategy();
    assertEquals(0, m_trapCount);
    LOG.debug("ONMS: Register for Traps");
    final TestTrapListener trapListener = new TestTrapListener();
    //SnmpV3User user = new SnmpV3User("opennmsUser", "MD5", "0p3nNMSv3", "DES", "0p3nNMSv3");
    SnmpV3User user = new SnmpV3User("noAuthUser", null, null, null, null);
    try {
        long start = System.currentTimeMillis();
        strategy.registerForTraps(trapListener, getAgentAddress(), 9162, Collections.singletonList(user));
        sendTraps(strategy, SnmpConfiguration.NOAUTH_NOPRIV);
        await().atMost(5, SECONDS).until(() -> m_trapCount, equalTo(2));
    } catch (final IOException e) {
        LOG.debug("Failed to register for traps.", e);
    } catch (final Exception e) {
        LOG.debug("Failed to send traps.", e);
    } finally {
        LOG.debug("ONMS: Unregister for Traps");
        try {
            strategy.unregisterForTraps(trapListener, 9162);
        } catch (final IOException e) {
            LOG.debug("Failed to unregister for traps.", e);
        }
    }
    LOG.debug("ONMS: Checking Trap status");
    assertFalse(trapListener.hasError());
    assertEquals(2, trapListener.getReceivedTrapCount());
    strategy.clearUsers();
}
Also used : IOException(java.io.IOException) IOException(java.io.IOException) SnmpV3User(org.opennms.netmgt.snmp.SnmpV3User) Test(org.junit.Test)

Example 5 with SnmpV3User

use of org.opennms.netmgt.snmp.SnmpV3User in project opennms by OpenNMS.

the class AbstractTrapReceiverTest method setUp.

/**
     * Sets up the test (initialize a trap listener).
     *
     * @throws Exception the exception
     */
@Before
public void setUp() throws Exception {
    MockLogAppender.setupLogging();
    resetTrapsReceived();
    System.setProperty("opennms.home", "src/test/resources");
    SnmpPeerFactory.init();
    Assert.assertEquals("Snmp4JStrategy", SnmpUtils.getStrategy().getClass().getSimpleName());
    SnmpAgentConfig config = SnmpPeerFactory.getInstance().getAgentConfig(TRAP_DESTINATION);
    SnmpV3User user = new SnmpV3User(config.getSecurityName(), config.getAuthProtocol(), config.getAuthPassPhrase(), config.getPrivProtocol(), config.getPrivPassPhrase());
    SnmpUtils.registerForTraps(this, TRAP_DESTINATION, TRAP_PORT, Collections.singletonList(user));
    LOG.info("Registered Trap Listener for {} on port {}", TRAP_DESTINATION, TRAP_PORT);
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) SnmpV3User(org.opennms.netmgt.snmp.SnmpV3User) Before(org.junit.Before)

Aggregations

SnmpV3User (org.opennms.netmgt.snmp.SnmpV3User)6 IOException (java.io.IOException)3 Test (org.junit.Test)2 SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)2 File (java.io.File)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Before (org.junit.Before)1 ConfigFileConstants (org.opennms.core.utils.ConfigFileConstants)1 JaxbUtils (org.opennms.core.xml.JaxbUtils)1 Snmpv3User (org.opennms.netmgt.config.trapd.Snmpv3User)1 TrapdConfiguration (org.opennms.netmgt.config.trapd.TrapdConfiguration)1 Snmp (org.snmp4j.Snmp)1 UsmUser (org.snmp4j.security.UsmUser)1 UdpAddress (org.snmp4j.smi.UdpAddress)1 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)1