Search in sources :

Example 1 with UdpAddress

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

the class MockSnmpAgentIT method sendRequestV1V2.

private PDU sendRequestV1V2(PDU pdu, int version) {
    PDU response = null;
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString("public"));
    target.setAddress(new UdpAddress(InetAddressUtils.addr("127.0.0.1"), m_agent.getPort()));
    target.setVersion(version);
    TransportMapping<UdpAddress> transport = null;
    Snmp snmp = null;
    try {
        transport = new DefaultUdpTransportMapping();
        snmp = new Snmp(transport);
        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) CommunityTarget(org.snmp4j.CommunityTarget)

Example 2 with UdpAddress

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

the class BrocadeMibIT method sendRequestV1V2.

private PDU sendRequestV1V2(PDU pdu, int version) throws Exception {
    PDU response;
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString("public"));
    target.setAddress(new UdpAddress(m_agent.getInetAddress(), m_agent.getPort()));
    target.setVersion(version);
    TransportMapping<UdpAddress> transport = null;
    try {
        transport = new DefaultUdpTransportMapping();
        Snmp snmp = new Snmp(transport);
        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) CommunityTarget(org.snmp4j.CommunityTarget)

Example 3 with UdpAddress

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

the class MockSnmpAgent method initTransportMappings.

/** {@inheritDoc} */
@Override
protected void initTransportMappings() throws IOException {
    s_log.info("MockSnmpAgent: starting initTransportMappings()");
    try {
        final MockUdpTransportMapping mapping = new MockUdpTransportMapping(new UdpAddress(m_address.get()), true);
        mapping.setThreadName("MockSnmpAgent-UDP-Transport");
        transportMappings = new TransportMapping<?>[] { mapping };
    } catch (final IOException e) {
        s_log.info("MockSnmpAgent: initTransportMappings() caught an IoException: " + e.getMessage());
        m_failure.set(e);
        throw e;
    } finally {
        s_log.info("MockSnmpAgent: finished initTransportMappings()");
    }
}
Also used : UdpAddress(org.snmp4j.smi.UdpAddress) IOException(java.io.IOException)

Example 4 with UdpAddress

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

the class MockAgentTest method testWalkSystem.

public void testWalkSystem() throws IOException {
    Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
    TableUtils walker = new TableUtils(snmp, new DefaultPDUFactory());
    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);
    // Implements snmp4j API
    @SuppressWarnings("rawtypes") List results = walker.getTable(target, new OID[] { new OID("1.3.6.1.2.1.1") }, null, null);
    assertNotNull(results);
    assertFalse(results.isEmpty());
    assertTrue(results.get(results.size() - 1) instanceof TableEvent);
    TableEvent lastEvent = (TableEvent) results.get(results.size() - 1);
    MockUtil.println("Status of lastEvent is " + lastEvent.getStatus());
    assertEquals(TableEvent.STATUS_OK, lastEvent.getStatus());
}
Also used : OctetString(org.snmp4j.smi.OctetString) UdpAddress(org.snmp4j.smi.UdpAddress) Address(org.snmp4j.smi.Address) UdpAddress(org.snmp4j.smi.UdpAddress) InetAddress(java.net.InetAddress) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) OID(org.snmp4j.smi.OID) Target(org.snmp4j.Target) CommunityTarget(org.snmp4j.CommunityTarget) DefaultPDUFactory(org.snmp4j.util.DefaultPDUFactory) TableEvent(org.snmp4j.util.TableEvent) Snmp(org.snmp4j.Snmp) List(java.util.List) TableUtils(org.snmp4j.util.TableUtils) CommunityTarget(org.snmp4j.CommunityTarget)

Example 5 with UdpAddress

use of org.snmp4j.smi.UdpAddress in project openhab1-addons by openhab.

the class SnmpBinding method listen.

/**
     * Configures a {@link DefaultUdpTransportMapping} and starts listening on
     * <code>SnmpBinding.port</code> for incoming SNMP Traps.
     */
private void listen() {
    UdpAddress address = new UdpAddress(SnmpBinding.port);
    try {
        if (transport != null) {
            transport.close();
            transport = null;
        }
        if (snmp != null) {
            snmp.close();
            snmp = null;
        }
        transport = new DefaultUdpTransportMapping(address);
        // add all security protocols
        SecurityProtocols.getInstance().addDefaultProtocols();
        SecurityProtocols.getInstance().addPrivacyProtocol(new Priv3DES());
        // Create Target
        if (SnmpBinding.community != null) {
            CommunityTarget target = new CommunityTarget();
            target.setCommunity(new OctetString(SnmpBinding.community));
        }
        snmp = new Snmp(transport);
        transport.listen();
        logger.debug("SNMP binding is listening on " + address);
    } catch (IOException ioe) {
        logger.error("SNMP binding couldn't listen to " + address, ioe);
    }
}
Also used : OctetString(org.snmp4j.smi.OctetString) UdpAddress(org.snmp4j.smi.UdpAddress) Priv3DES(org.snmp4j.security.Priv3DES) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) IOException(java.io.IOException) CommunityTarget(org.snmp4j.CommunityTarget)

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