Search in sources :

Example 11 with UdpAddress

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

the class MockSnmpAgent method addNotificationTargets.

/**
 * {@inheritDoc}
 */
@Override
protected void addNotificationTargets(SnmpTargetMIB targetMIB, SnmpNotificationMIB notificationMIB) {
    targetMIB.addDefaultTDomains();
    targetMIB.addTargetAddress(new OctetString("notification"), TransportDomains.transportDomainUdpIpv4, new OctetString(new UdpAddress("127.0.0.1/162").getValue()), 200, 1, new OctetString("notify"), new OctetString("v2c"), StorageType.permanent);
    targetMIB.addTargetParams(new OctetString("v2c"), MessageProcessingModel.MPv2c, SecurityModel.SECURITY_MODEL_SNMPv2c, new OctetString("public"), SecurityLevel.NOAUTH_NOPRIV, StorageType.permanent);
    notificationMIB.addNotifyEntry(new OctetString("default"), new OctetString("notify"), SnmpNotificationMIB.SnmpNotifyTypeEnum.trap, StorageType.permanent);
}
Also used : OctetString(org.snmp4j.smi.OctetString) UdpAddress(org.snmp4j.smi.UdpAddress)

Example 12 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 13 with UdpAddress

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

the class MockSnmpAgentIT method sendRequestV3.

private PDU sendRequestV3(PDU pdu) {
    PDU response = null;
    OctetString userId = new OctetString("opennmsUser");
    OctetString pw = new OctetString("0p3nNMSv3");
    UserTarget target = new UserTarget();
    target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
    target.setSecurityName(userId);
    target.setAddress(new UdpAddress(InetAddressUtils.addr("127.0.0.1"), m_agent.getPort()));
    target.setVersion(SnmpConstants.version3);
    target.setTimeout(DEFAULT_TIMEOUT);
    TransportMapping<UdpAddress> transport = null;
    Snmp snmp = null;
    try {
        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
        SecurityModels.getInstance().addSecurityModel(usm);
        transport = new DefaultUdpTransportMapping();
        snmp = new Snmp(transport);
        UsmUser user = new UsmUser(userId, AuthMD5.ID, pw, PrivDES.ID, pw);
        snmp.getUSM().addUser(userId, user);
        transport.listen();
        ResponseEvent e = snmp.send(pdu, target);
        response = e.getResponse();
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        if (snmp != null) {
            try {
                snmp.close();
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
        if (transport != null) {
            try {
                transport.close();
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
    }
    return response;
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) UdpAddress(org.snmp4j.smi.UdpAddress) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) ResponseEvent(org.snmp4j.event.ResponseEvent) IOException(java.io.IOException) UserTarget(org.snmp4j.UserTarget) UsmUser(org.snmp4j.security.UsmUser) USM(org.snmp4j.security.USM)

Example 14 with UdpAddress

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

the class BrocadeMibIT method sendRequestV3.

private PDU sendRequestV3(PDU pdu) throws IOException {
    PDU response;
    OctetString userId = new OctetString("opennmsUser");
    OctetString pw = new OctetString("0p3nNMSv3");
    UserTarget target = new UserTarget();
    target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
    target.setSecurityName(userId);
    target.setAddress(new UdpAddress(m_agent.getInetAddress(), m_agent.getPort()));
    target.setVersion(SnmpConstants.version3);
    target.setTimeout(5000);
    TransportMapping<UdpAddress> transport = null;
    try {
        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
        SecurityModels.getInstance().addSecurityModel(usm);
        transport = new DefaultUdpTransportMapping();
        Snmp snmp = new Snmp(transport);
        UsmUser user = new UsmUser(userId, AuthMD5.ID, pw, PrivDES.ID, pw);
        snmp.getUSM().addUser(userId, user);
        transport.listen();
        ResponseEvent e = snmp.send(pdu, target);
        response = e.getResponse();
    } finally {
        if (transport != null) {
            transport.close();
        }
    }
    return response;
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) UdpAddress(org.snmp4j.smi.UdpAddress) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) ResponseEvent(org.snmp4j.event.ResponseEvent) UserTarget(org.snmp4j.UserTarget) UsmUser(org.snmp4j.security.UsmUser) USM(org.snmp4j.security.USM)

Example 15 with UdpAddress

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

the class LLDPMibIT method sendRequestV3.

private PDU sendRequestV3(PDU pdu) throws IOException {
    PDU response;
    OctetString userId = new OctetString("opennmsUser");
    OctetString pw = new OctetString("0p3nNMSv3");
    UserTarget target = new UserTarget();
    target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
    target.setSecurityName(userId);
    target.setAddress(new UdpAddress(m_agent.getInetAddress(), m_agent.getPort()));
    target.setVersion(SnmpConstants.version3);
    if (m_timeout > 0) {
        target.setTimeout(m_timeout);
    } else {
        target.setTimeout(5000);
    }
    TransportMapping<UdpAddress> transport = null;
    try {
        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
        SecurityModels.getInstance().addSecurityModel(usm);
        transport = new DefaultUdpTransportMapping();
        Snmp snmp = new Snmp(transport);
        UsmUser user = new UsmUser(userId, AuthMD5.ID, pw, PrivDES.ID, pw);
        snmp.getUSM().addUser(userId, user);
        transport.listen();
        ResponseEvent e = snmp.send(pdu, target);
        response = e.getResponse();
    } finally {
        if (transport != null) {
            transport.close();
        }
    }
    return response;
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) UdpAddress(org.snmp4j.smi.UdpAddress) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) ResponseEvent(org.snmp4j.event.ResponseEvent) UserTarget(org.snmp4j.UserTarget) UsmUser(org.snmp4j.security.UsmUser) USM(org.snmp4j.security.USM)

Aggregations

UdpAddress (org.snmp4j.smi.UdpAddress)16 Snmp (org.snmp4j.Snmp)13 OctetString (org.snmp4j.smi.OctetString)13 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)12 CommunityTarget (org.snmp4j.CommunityTarget)7 PDU (org.snmp4j.PDU)7 ResponseEvent (org.snmp4j.event.ResponseEvent)7 IOException (java.io.IOException)6 ScopedPDU (org.snmp4j.ScopedPDU)6 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 Config (com.sun.enterprise.config.serverbeans.Config)1 InvalidSnmpVersion (fish.payara.notification.snmp.exception.InvalidSnmpVersion)1 BlockingQueueHandler (fish.payara.nucleus.notification.BlockingQueueHandler)1