Search in sources :

Example 6 with TransportMapping

use of org.snmp4j.TransportMapping 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 7 with TransportMapping

use of org.snmp4j.TransportMapping in project pancm_project by xuwujing.

the class SnmpHelper method snmpSet.

public static void snmpSet(String ip, String community, PDU pdu) {
    CommunityTarget target = createDefault(ip, community);
    TransportMapping transport = null;
    Snmp snmp = null;
    List<String> result = new LinkedList<>();
    try {
        transport = new DefaultUdpTransportMapping();
        snmp = new Snmp(transport);
        transport.listen();
        sendRequest(snmp, pdu, target);
    } catch (Exception e) {
        log.error("SNMP walk Exception: ", e);
    } finally {
        if (snmp != null) {
            try {
                snmp.close();
            } catch (IOException ex1) {
                snmp = null;
            }
        }
    }
}
Also used : TransportMapping(org.snmp4j.TransportMapping) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) IOException(java.io.IOException) CommunityTarget(org.snmp4j.CommunityTarget) LinkedList(java.util.LinkedList) IOException(java.io.IOException)

Example 8 with TransportMapping

use of org.snmp4j.TransportMapping in project pentaho-kettle by pentaho.

the class JobEntrySNMPTrap method execute.

public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    result.setNrErrors(1);
    result.setResult(false);
    String servername = environmentSubstitute(serverName);
    int nrPort = Const.toInt(environmentSubstitute("" + port), DEFAULT_PORT);
    String Oid = environmentSubstitute(oid);
    int timeOut = Const.toInt(environmentSubstitute("" + timeout), DEFAULT_TIME_OUT);
    int retry = Const.toInt(environmentSubstitute("" + nrretry), 1);
    String messageString = environmentSubstitute(message);
    Snmp snmp = null;
    try {
        TransportMapping transMap = new DefaultUdpTransportMapping();
        snmp = new Snmp(transMap);
        UdpAddress udpAddress = new UdpAddress(InetAddress.getByName(servername), nrPort);
        ResponseEvent response = null;
        if (targettype.equals(target_type_Code[0])) {
            // Community target
            String community = environmentSubstitute(comString);
            CommunityTarget target = new CommunityTarget();
            PDUv1 pdu1 = new PDUv1();
            transMap.listen();
            target.setCommunity(new OctetString(community));
            target.setVersion(SnmpConstants.version1);
            target.setAddress(udpAddress);
            if (target.getAddress().isValid()) {
                if (log.isDebug()) {
                    logDebug("Valid IP address");
                }
            } else {
                throw new KettleException("Invalid IP address");
            }
            target.setRetries(retry);
            target.setTimeout(timeOut);
            // create the PDU
            pdu1.setGenericTrap(6);
            pdu1.setSpecificTrap(PDUv1.ENTERPRISE_SPECIFIC);
            pdu1.setEnterprise(new OID(Oid));
            pdu1.add(new VariableBinding(new OID(Oid), new OctetString(messageString)));
            response = snmp.send(pdu1, target);
        } else {
            // User target
            String userName = environmentSubstitute(user);
            String passPhrase = environmentSubstitute(passphrase);
            String engineID = environmentSubstitute(engineid);
            UserTarget usertarget = new UserTarget();
            transMap.listen();
            usertarget.setAddress(udpAddress);
            if (usertarget.getAddress().isValid()) {
                if (log.isDebug()) {
                    logDebug("Valid IP address");
                }
            } else {
                throw new KettleException("Invalid IP address");
            }
            usertarget.setRetries(retry);
            usertarget.setTimeout(timeOut);
            usertarget.setVersion(SnmpConstants.version3);
            usertarget.setSecurityLevel(SecurityLevel.AUTH_PRIV);
            usertarget.setSecurityName(new OctetString("MD5DES"));
            // Since we are using SNMPv3 we use authenticated users
            // this is handled by the UsmUser and USM class
            UsmUser uu = new UsmUser(new OctetString(userName), AuthMD5.ID, new OctetString(passPhrase), PrivDES.ID, new OctetString(passPhrase));
            USM usm = snmp.getUSM();
            if (usm == null) {
                throw new KettleException("Null Usm");
            } else {
                usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
                usm.addUser(new OctetString(userName), uu);
                if (log.isDebug()) {
                    logDebug("Valid Usm");
                }
            }
            // create the PDU
            ScopedPDU pdu = new ScopedPDU();
            pdu.add(new VariableBinding(new OID(Oid), new OctetString(messageString)));
            pdu.setType(PDU.TRAP);
            if (!Utils.isEmpty(engineID)) {
                pdu.setContextEngineID(new OctetString(engineID));
            }
            // send the PDU
            response = snmp.send(pdu, usertarget);
        }
        if (response != null) {
            if (log.isDebug()) {
                logDebug("Received response from: " + response.getPeerAddress() + response.toString());
            }
        }
        result.setNrErrors(0);
        result.setResult(true);
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "JobEntrySNMPTrap.ErrorGetting", e.getMessage()));
    } finally {
        try {
            if (snmp != null) {
                snmp.close();
            }
        } catch (Exception e) {
        /* Ignore */
        }
    }
    return result;
}
Also used : OctetString(org.snmp4j.smi.OctetString) KettleException(org.pentaho.di.core.exception.KettleException) UdpAddress(org.snmp4j.smi.UdpAddress) ScopedPDU(org.snmp4j.ScopedPDU) TransportMapping(org.snmp4j.TransportMapping) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) OctetString(org.snmp4j.smi.OctetString) OID(org.snmp4j.smi.OID) UsmUser(org.snmp4j.security.UsmUser) USM(org.snmp4j.security.USM) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) Result(org.pentaho.di.core.Result) Snmp(org.snmp4j.Snmp) ResponseEvent(org.snmp4j.event.ResponseEvent) PDUv1(org.snmp4j.PDUv1) UserTarget(org.snmp4j.UserTarget) CommunityTarget(org.snmp4j.CommunityTarget) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 9 with TransportMapping

use of org.snmp4j.TransportMapping in project thingsboard by thingsboard.

the class SnmpDeviceSimulatorV3 method initTransportMappings.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected void initTransportMappings() throws IOException {
    transportMappings = new TransportMapping[2];
    Address addr = GenericAddress.parse(address);
    TransportMapping tm = TransportMappings.getInstance().createTransportMapping(addr);
    transportMappings[0] = tm;
    transportMappings[1] = new DefaultTcpTransportMapping(new TcpAddress(address));
}
Also used : TcpAddress(org.snmp4j.smi.TcpAddress) Address(org.snmp4j.smi.Address) UdpAddress(org.snmp4j.smi.UdpAddress) GenericAddress(org.snmp4j.smi.GenericAddress) TcpAddress(org.snmp4j.smi.TcpAddress) DefaultTcpTransportMapping(org.snmp4j.transport.DefaultTcpTransportMapping) TransportMapping(org.snmp4j.TransportMapping) DefaultTcpTransportMapping(org.snmp4j.transport.DefaultTcpTransportMapping)

Aggregations

TransportMapping (org.snmp4j.TransportMapping)9 Snmp (org.snmp4j.Snmp)7 CommunityTarget (org.snmp4j.CommunityTarget)6 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)6 IOException (java.io.IOException)5 OctetString (org.snmp4j.smi.OctetString)4 UdpAddress (org.snmp4j.smi.UdpAddress)3 InvalidSnmpVersion (fish.payara.notification.snmp.exception.InvalidSnmpVersion)2 LinkedList (java.util.LinkedList)2 PDUv1 (org.snmp4j.PDUv1)2 ResponseEvent (org.snmp4j.event.ResponseEvent)2 USM (org.snmp4j.security.USM)2 Address (org.snmp4j.smi.Address)2 Config (com.sun.enterprise.config.serverbeans.Config)1 BlockingQueueHandler (fish.payara.nucleus.notification.BlockingQueueHandler)1 InetAddress (java.net.InetAddress)1 ByteBuffer (java.nio.ByteBuffer)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Level (java.util.logging.Level)1