Search in sources :

Example 1 with DefaultUdpTransportMapping

use of org.snmp4j.transport.DefaultUdpTransportMapping in project mysql_perf_analyzer by yahoo.

the class SNMPClient method start.

/**
	 * Start the Snmp session. If you forget the listen() method you will not
	 * get any answers because the communication is asynchronous
	 * and the listen() method listens for answers.
	 * @throws IOException
	 */
public void start() throws IOException {
    TransportMapping transport = new DefaultUdpTransportMapping();
    snmp = new Snmp(transport);
    if (//add v3 support
    "3".equals(this.version)) {
        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
        SecurityModels.getInstance().addSecurityModel(usm);
    }
    // Do not forget this line!
    transport.listen();
}
Also used : OctetString(org.snmp4j.smi.OctetString) TransportMapping(org.snmp4j.TransportMapping) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) USM(org.snmp4j.security.USM)

Example 2 with DefaultUdpTransportMapping

use of org.snmp4j.transport.DefaultUdpTransportMapping 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 3 with DefaultUdpTransportMapping

use of org.snmp4j.transport.DefaultUdpTransportMapping 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 4 with DefaultUdpTransportMapping

use of org.snmp4j.transport.DefaultUdpTransportMapping 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 5 with DefaultUdpTransportMapping

use of org.snmp4j.transport.DefaultUdpTransportMapping 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)

Aggregations

Snmp (org.snmp4j.Snmp)17 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)17 OctetString (org.snmp4j.smi.OctetString)13 UdpAddress (org.snmp4j.smi.UdpAddress)11 PDU (org.snmp4j.PDU)9 ResponseEvent (org.snmp4j.event.ResponseEvent)8 CommunityTarget (org.snmp4j.CommunityTarget)7 ScopedPDU (org.snmp4j.ScopedPDU)7 UsmUser (org.snmp4j.security.UsmUser)7 USM (org.snmp4j.security.USM)6 IOException (java.io.IOException)4 UserTarget (org.snmp4j.UserTarget)4 DefaultTcpTransportMapping (org.snmp4j.transport.DefaultTcpTransportMapping)4 OID (org.snmp4j.smi.OID)3 InetAddress (java.net.InetAddress)2 Target (org.snmp4j.Target)2 Address (org.snmp4j.smi.Address)2 List (java.util.List)1 TimeoutException (java.util.concurrent.TimeoutException)1 Test (org.junit.Test)1