Search in sources :

Example 6 with UdpAddress

use of org.snmp4j.smi.UdpAddress 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 7 with UdpAddress

use of org.snmp4j.smi.UdpAddress 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);
        }
    }
}
Also used : PduHandle(org.snmp4j.mp.PduHandle) MessageDispatcher(org.snmp4j.MessageDispatcher) UdpAddress(org.snmp4j.smi.UdpAddress) CommandResponderEvent(org.snmp4j.CommandResponderEvent) Snmp(org.snmp4j.Snmp) IpAddress(org.snmp4j.smi.IpAddress) PDUv1(org.snmp4j.PDUv1)

Example 8 with UdpAddress

use of org.snmp4j.smi.UdpAddress in project opennms by OpenNMS.

the class Snmp4JAgentConfig method convertAddress.

/**
     * This method converts an InetAddress to an implementation of an SNMP4J Address
     * (UdpAddress or TcpAddress)
     * 
     * TODO: This needs to be updated when the protocol flag is added to the SNMP Config
     * so that UDP or TCP can be used in v3 operations.
     */
public static Address convertAddress(InetAddress address, int port) {
    String transportAddress = address.getHostAddress();
    transportAddress += "/" + port;
    return new UdpAddress(transportAddress);
}
Also used : UdpAddress(org.snmp4j.smi.UdpAddress) OctetString(org.snmp4j.smi.OctetString)

Example 9 with UdpAddress

use of org.snmp4j.smi.UdpAddress in project opennms by OpenNMS.

the class Snmp4jTrapReceiverIT method testTrapReceiverWithoutOpenNMS.

/*
     * IMPORTANT:
     *
     * The sentence <code>snmp.getUSM().addUser(...)</code>, is the only requirement
     * in order to properly process SNMPv3 traps.
     * 
     * This is related with the credentials that should be created for Trapd in order
     * to properly authenticate and/or decode SNMPv3 traps in OpenNMS.
     * 
     * This is a user that should be configured (or should be used) by the external
     * devices to send SNMPv3 Traps to OpenNMS.
     * 
     * The SNMPv3 users should be configured in trapd-configuration.xml
     */
@Test
public void testTrapReceiverWithoutOpenNMS() throws Exception {
    final Snmp4JStrategy strategy = new Snmp4JStrategy();
    assertEquals(0, m_trapCount);
    LOG.debug("SNMP4J: Register for Traps");
    DefaultUdpTransportMapping transportMapping = null;
    Snmp snmp = null;
    try {
        // 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.
        transportMapping = new DefaultUdpTransportMapping(new UdpAddress(9162), true);
        snmp = new Snmp(transportMapping);
        snmp.addCommandResponder(this);
        snmp.getUSM().addUser(new OctetString("opennmsUser"), new UsmUser(new OctetString("opennmsUser"), AuthMD5.ID, new OctetString("0p3nNMSv3"), PrivDES.ID, new OctetString("0p3nNMSv3")));
        snmp.listen();
        sendTraps(strategy, SnmpConfiguration.AUTH_PRIV);
        await().atMost(5, SECONDS).until(() -> m_trapCount, equalTo(2));
    } finally {
        LOG.debug("SNMP4J: Unregister for Traps");
        if (snmp != null) {
            try {
                snmp.close();
            } catch (final IOException e) {
                LOG.debug("Failed to close Snmp object: {}", snmp, e);
            }
        }
        if (transportMapping != null) {
            try {
                transportMapping.close();
            } catch (final IOException e) {
                LOG.debug("Failed to close transport mapping: {}", transportMapping, e);
            }
        }
    }
    LOG.debug("SNMP4J: Checking Trap status");
    assertEquals(2, m_trapCount);
    snmp.getUSM().removeAllUsers();
}
Also used : OctetString(org.snmp4j.smi.OctetString) UdpAddress(org.snmp4j.smi.UdpAddress) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) IOException(java.io.IOException) UsmUser(org.snmp4j.security.UsmUser) Test(org.junit.Test)

Example 10 with UdpAddress

use of org.snmp4j.smi.UdpAddress in project opennms by OpenNMS.

the class MockAgentTest method testGetSysName.

public void testGetSysName() throws IOException {
    Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
    snmp.listen();
    Address addr = new UdpAddress(InetAddress.getLocalHost(), 9161);
    //Address addr = new UdpAddress(InetAddressUtils.addr("192.168.0.100"), 161);
    Target target = new CommunityTarget(addr, new OctetString("public"));
    target.setVersion(SnmpConstants.version1);
    target.setTimeout(3000);
    target.setRetries(3);
    PDUv1 getRequest = new PDUv1();
    getRequest.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0")));
    ResponseEvent e = snmp.get(getRequest, target);
    PDU response = e.getResponse();
    assertEquals(new OctetString("mockhost"), response.get(0).getVariable());
}
Also used : OctetString(org.snmp4j.smi.OctetString) PDU(org.snmp4j.PDU) UdpAddress(org.snmp4j.smi.UdpAddress) Target(org.snmp4j.Target) CommunityTarget(org.snmp4j.CommunityTarget) Address(org.snmp4j.smi.Address) UdpAddress(org.snmp4j.smi.UdpAddress) InetAddress(java.net.InetAddress) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) ResponseEvent(org.snmp4j.event.ResponseEvent) PDUv1(org.snmp4j.PDUv1) OID(org.snmp4j.smi.OID) CommunityTarget(org.snmp4j.CommunityTarget) VariableBinding(org.snmp4j.smi.VariableBinding)

Aggregations

UdpAddress (org.snmp4j.smi.UdpAddress)15 Snmp (org.snmp4j.Snmp)12 OctetString (org.snmp4j.smi.OctetString)12 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)11 PDU (org.snmp4j.PDU)7 ResponseEvent (org.snmp4j.event.ResponseEvent)7 CommunityTarget (org.snmp4j.CommunityTarget)6 ScopedPDU (org.snmp4j.ScopedPDU)6 IOException (java.io.IOException)5 UsmUser (org.snmp4j.security.UsmUser)5 UserTarget (org.snmp4j.UserTarget)3 USM (org.snmp4j.security.USM)3 InetAddress (java.net.InetAddress)2 PDUv1 (org.snmp4j.PDUv1)2 Target (org.snmp4j.Target)2 Address (org.snmp4j.smi.Address)2 OID (org.snmp4j.smi.OID)2 List (java.util.List)1 Test (org.junit.Test)1 SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)1