Search in sources :

Example 21 with OctetString

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

use of org.snmp4j.smi.OctetString 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)

Example 23 with OctetString

use of org.snmp4j.smi.OctetString in project camel by apache.

the class SnmpOIDPoller method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    this.targetAddress = GenericAddress.parse(this.endpoint.getAddress());
    // either tcp or udp
    if ("tcp".equals(endpoint.getProtocol())) {
        this.transport = new DefaultTcpTransportMapping();
    } else if ("udp".equals(endpoint.getProtocol())) {
        this.transport = new DefaultUdpTransportMapping();
    } else {
        throw new IllegalArgumentException("Unknown protocol: " + endpoint.getProtocol());
    }
    this.snmp = new Snmp(this.transport);
    if (SnmpConstants.version3 == endpoint.getSnmpVersion()) {
        UserTarget userTarget = new UserTarget();
        userTarget.setSecurityLevel(endpoint.getSecurityLevel());
        userTarget.setSecurityName(convertToOctetString(endpoint.getSecurityName()));
        userTarget.setAddress(targetAddress);
        userTarget.setRetries(endpoint.getRetries());
        userTarget.setTimeout(endpoint.getTimeout());
        userTarget.setVersion(endpoint.getSnmpVersion());
        this.target = userTarget;
        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
        SecurityModels.getInstance().addSecurityModel(usm);
        OID authProtocol = convertAuthenticationProtocol(endpoint.getAuthenticationProtocol());
        OctetString authPwd = convertToOctetString(endpoint.getAuthenticationPassphrase());
        OID privProtocol = convertPrivacyProtocol(endpoint.getPrivacyProtocol());
        OctetString privPwd = convertToOctetString(endpoint.getPrivacyPassphrase());
        UsmUser user = new UsmUser(convertToOctetString(endpoint.getSecurityName()), authProtocol, authPwd, privProtocol, privPwd);
        usm.addUser(convertToOctetString(endpoint.getSecurityName()), user);
        ScopedPDU scopedPDU = new ScopedPDU();
        if (endpoint.getSnmpContextEngineId() != null) {
            scopedPDU.setContextEngineID(new OctetString(endpoint.getSnmpContextEngineId()));
        }
        if (endpoint.getSnmpContextName() != null) {
            scopedPDU.setContextName(new OctetString(endpoint.getSnmpContextName()));
        }
        this.pdu = scopedPDU;
    } else {
        CommunityTarget communityTarget = new CommunityTarget();
        communityTarget.setCommunity(convertToOctetString(endpoint.getSnmpCommunity()));
        communityTarget.setAddress(targetAddress);
        communityTarget.setRetries(endpoint.getRetries());
        communityTarget.setTimeout(endpoint.getTimeout());
        communityTarget.setVersion(endpoint.getSnmpVersion());
        this.target = communityTarget;
        this.pdu = new PDU();
    }
    // listen to the transport
    if (LOG.isDebugEnabled()) {
        LOG.debug("Starting OID poller on {} using {} protocol", endpoint.getAddress(), endpoint.getProtocol());
    }
    this.transport.listen();
    if (LOG.isInfoEnabled()) {
        LOG.info("Started OID poller on {} using {} protocol", endpoint.getAddress(), endpoint.getProtocol());
    }
}
Also used : OctetString(org.snmp4j.smi.OctetString) ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) ScopedPDU(org.snmp4j.ScopedPDU) DefaultTcpTransportMapping(org.snmp4j.transport.DefaultTcpTransportMapping) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) OID(org.snmp4j.smi.OID) UserTarget(org.snmp4j.UserTarget) UsmUser(org.snmp4j.security.UsmUser) CommunityTarget(org.snmp4j.CommunityTarget) USM(org.snmp4j.security.USM)

Example 24 with OctetString

use of org.snmp4j.smi.OctetString in project camel by apache.

the class SnmpProducer method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    this.targetAddress = GenericAddress.parse(this.endpoint.getAddress());
    LOG.debug("targetAddress: {}", targetAddress);
    this.usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
    SecurityModels.getInstance().addSecurityModel(this.usm);
    // setting up target
    this.target = new CommunityTarget();
    this.target.setCommunity(new OctetString(endpoint.getSnmpCommunity()));
    this.target.setAddress(this.targetAddress);
    this.target.setRetries(this.endpoint.getRetries());
    this.target.setTimeout(this.endpoint.getTimeout());
    this.target.setVersion(this.endpoint.getSnmpVersion());
    this.pdu = new PDU();
    for (OID oid : this.endpoint.getOids()) {
        this.pdu.add(new VariableBinding(oid));
    }
    this.pdu.setErrorIndex(0);
    this.pdu.setErrorStatus(0);
    this.pdu.setMaxRepetitions(0);
    this.pdu.setType(PDU.GET);
}
Also used : OctetString(org.snmp4j.smi.OctetString) PDU(org.snmp4j.PDU) OID(org.snmp4j.smi.OID) CommunityTarget(org.snmp4j.CommunityTarget) VariableBinding(org.snmp4j.smi.VariableBinding) USM(org.snmp4j.security.USM)

Example 25 with OctetString

use of org.snmp4j.smi.OctetString in project camel by apache.

the class SnmpTrapProducer method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    this.targetAddress = GenericAddress.parse(this.endpoint.getAddress());
    LOG.debug("targetAddress: {}", targetAddress);
    this.usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
    SecurityModels.getInstance().addSecurityModel(this.usm);
    // setting up target
    this.target = new CommunityTarget();
    this.target.setCommunity(new OctetString(endpoint.getSnmpCommunity()));
    this.target.setAddress(this.targetAddress);
    this.target.setRetries(this.endpoint.getRetries());
    this.target.setTimeout(this.endpoint.getTimeout());
    this.target.setVersion(this.endpoint.getSnmpVersion());
}
Also used : OctetString(org.snmp4j.smi.OctetString) CommunityTarget(org.snmp4j.CommunityTarget) USM(org.snmp4j.security.USM)

Aggregations

OctetString (org.snmp4j.smi.OctetString)40 PDU (org.snmp4j.PDU)17 OID (org.snmp4j.smi.OID)17 Snmp (org.snmp4j.Snmp)14 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)13 CommunityTarget (org.snmp4j.CommunityTarget)11 ScopedPDU (org.snmp4j.ScopedPDU)11 USM (org.snmp4j.security.USM)11 UdpAddress (org.snmp4j.smi.UdpAddress)11 VariableBinding (org.snmp4j.smi.VariableBinding)11 Test (org.junit.Test)10 UsmUser (org.snmp4j.security.UsmUser)8 Integer32 (org.snmp4j.smi.Integer32)8 ResponseEvent (org.snmp4j.event.ResponseEvent)7 IOException (java.io.IOException)5 TrapInformation (org.opennms.netmgt.snmp.TrapInformation)5 Address (org.snmp4j.smi.Address)5 TimeTicks (org.snmp4j.smi.TimeTicks)5 InetAddress (java.net.InetAddress)4 PDUv1 (org.snmp4j.PDUv1)4