Search in sources :

Example 1 with ScopedPDU

use of org.snmp4j.ScopedPDU in project opennms by OpenNMS.

the class Snmp4JAgentConfig method createPdu.

/**
     * Creates an SNMP4J PDU based on the SNMP4J version constants.
     * A v3 request requires a ScopedPDU.
     * 
     * @param type
     * @return
     */
public PDU createPdu(int type) {
    PDU pdu = null;
    if (isSnmpV3()) {
        pdu = new ScopedPDU();
        ScopedPDU scopedPDU = (ScopedPDU) pdu;
        OctetString contextName = getContextName();
        if (contextName != null)
            scopedPDU.setContextName(contextName);
        OctetString contextEngineID = getContextEngineID();
        if (contextEngineID != null)
            scopedPDU.setContextEngineID(contextEngineID);
    } else {
        pdu = new PDU();
    }
    pdu.setType(type);
    return pdu;
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) ScopedPDU(org.snmp4j.ScopedPDU)

Example 2 with ScopedPDU

use of org.snmp4j.ScopedPDU in project opennms by OpenNMS.

the class TrapNotificationSerializationTest method testsnmp4JV3Serialization.

@Test
public void testsnmp4JV3Serialization() throws UnknownHostException {
    // create instance of snmp4JV3Trap
    PDU snmp4JV3TrapPdu = new ScopedPDU();
    snmp4JV3TrapPdu.setType(PDU.TRAP);
    snmp4JV3TrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new OctetString("mockhost")));
    snmp4JV3TrapPdu.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.3"), new OctetString("mockhost")));
    snmp4JV3TrapPdu.add(new VariableBinding(new OID(".1.3.6.1.6.3.1.1.4.1.0"), new OctetString("mockhost")));
    TrapInformation snmp4JV3Trap = new Snmp4JTrapNotifier.Snmp4JV2TrapInformation(inetAddress, new String("public"), snmp4JV3TrapPdu);
    assertTrue(writeTrapNotificationObject(snmp4JV3Trap));
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) ScopedPDU(org.snmp4j.ScopedPDU) OID(org.snmp4j.smi.OID) OctetString(org.snmp4j.smi.OctetString) VariableBinding(org.snmp4j.smi.VariableBinding) TrapInformation(org.opennms.netmgt.snmp.TrapInformation) Test(org.junit.Test)

Example 3 with ScopedPDU

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

Aggregations

PDU (org.snmp4j.PDU)3 ScopedPDU (org.snmp4j.ScopedPDU)3 OctetString (org.snmp4j.smi.OctetString)3 OID (org.snmp4j.smi.OID)2 Test (org.junit.Test)1 TrapInformation (org.opennms.netmgt.snmp.TrapInformation)1 CommunityTarget (org.snmp4j.CommunityTarget)1 Snmp (org.snmp4j.Snmp)1 UserTarget (org.snmp4j.UserTarget)1 USM (org.snmp4j.security.USM)1 UsmUser (org.snmp4j.security.UsmUser)1 VariableBinding (org.snmp4j.smi.VariableBinding)1 DefaultTcpTransportMapping (org.snmp4j.transport.DefaultTcpTransportMapping)1 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)1