Search in sources :

Example 51 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project mysql_perf_analyzer by yahoo.

the class SNMPClient method getDiskIndex.

private int getDiskIndex(String device) throws IOException {
    TableUtils tUtils = new TableUtils(snmp, new DefaultPDUFactory());
    logger.fine("Query " + this.address + " for disk data: " + device);
    @SuppressWarnings("unchecked") List<TableEvent> events = tUtils.getTable(getTarget(), new OID[] { new OID("." + DISK_TABLE_DEVICE_OID) }, null, null);
    for (TableEvent event : events) {
        if (event.isError()) {
            logger.warning(this.address + ": SNMP event error: " + event.getErrorMessage());
            continue;
        // throw new RuntimeException(event.getErrorMessage());
        }
        for (VariableBinding vb : event.getColumns()) {
            String key = vb.getOid().toString();
            String value = vb.getVariable().toString();
            if (value != null && value.equals(device)) {
                logger.fine("Find device OID entry: " + key);
                int index = -1;
                String[] strs = key.split("\\.");
                try {
                    index = Integer.parseInt(strs[strs.length - 1]);
                } catch (Exception ex) {
                }
                return index;
            }
        }
    }
    return -1;
}
Also used : DefaultPDUFactory(org.snmp4j.util.DefaultPDUFactory) TableEvent(org.snmp4j.util.TableEvent) TableUtils(org.snmp4j.util.TableUtils) OID(org.snmp4j.smi.OID) OctetString(org.snmp4j.smi.OctetString) VariableBinding(org.snmp4j.smi.VariableBinding) IOException(java.io.IOException)

Example 52 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project mysql_perf_analyzer by yahoo.

the class SNMPClient method getNetIfIndexes.

private Map<Integer, String> getNetIfIndexes(String device) throws IOException {
    Map<Integer, String> ifMaps = new HashMap<Integer, String>();
    TableUtils tUtils = new TableUtils(snmp, new DefaultPDUFactory());
    logger.fine("Query " + this.address + " for network interface, excluding lo");
    @SuppressWarnings("unchecked") List<TableEvent> events = tUtils.getTable(getTarget(), new OID[] { new OID("." + IF_TABLE_DEVICE_OID) }, null, null);
    for (TableEvent event : events) {
        if (event.isError()) {
            logger.warning(this.address + ": SNMP event error: " + event.getErrorMessage());
            continue;
        // throw new RuntimeException(event.getErrorMessage());
        }
        for (VariableBinding vb : event.getColumns()) {
            String key = vb.getOid().toString();
            String value = vb.getVariable().toString();
            if (device != null && !device.isEmpty() && !value.equalsIgnoreCase(device))
                continue;
            if (value != null && !value.equalsIgnoreCase("lo")) {
                logger.fine("Find device OID entry: " + key);
                int index = -1;
                String[] strs = key.split("\\.");
                try {
                    index = Integer.parseInt(strs[strs.length - 1]);
                    ifMaps.put(index, value);
                } catch (Exception ex) {
                }
            }
        }
    }
    return ifMaps;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) OctetString(org.snmp4j.smi.OctetString) OID(org.snmp4j.smi.OID) IOException(java.io.IOException) DefaultPDUFactory(org.snmp4j.util.DefaultPDUFactory) TableEvent(org.snmp4j.util.TableEvent) TableUtils(org.snmp4j.util.TableUtils) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 53 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project cloudstack by apache.

the class SnmpHelper method createPDU.

private PDU createPDU(SnmpTrapInfo snmpTrapInfo) {
    PDU trap = new PDU();
    trap.setType(PDU.TRAP);
    int alertType = snmpTrapInfo.getAlertType() + 1;
    if (alertType > 0) {
        long sysUpTimeTicks = ManagementFactory.getRuntimeMXBean().getUptime() / 10;
        trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(sysUpTimeTicks)));
        trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, getOID(CsSnmpConstants.TRAPS_PREFIX + alertType)));
        if (snmpTrapInfo.getDataCenterId() != 0) {
            trap.add(new VariableBinding(getOID(CsSnmpConstants.DATA_CENTER_ID), new UnsignedInteger32(snmpTrapInfo.getDataCenterId())));
        }
        if (snmpTrapInfo.getPodId() != 0) {
            trap.add(new VariableBinding(getOID(CsSnmpConstants.POD_ID), new UnsignedInteger32(snmpTrapInfo.getPodId())));
        }
        if (snmpTrapInfo.getClusterId() != 0) {
            trap.add(new VariableBinding(getOID(CsSnmpConstants.CLUSTER_ID), new UnsignedInteger32(snmpTrapInfo.getClusterId())));
        }
        if (snmpTrapInfo.getMessage() != null) {
            trap.add(new VariableBinding(getOID(CsSnmpConstants.MESSAGE), new OctetString(snmpTrapInfo.getMessage())));
        } else {
            throw new CloudRuntimeException(" What is the use of alert without message ");
        }
        if (snmpTrapInfo.getGenerationTime() != null) {
            trap.add(new VariableBinding(getOID(CsSnmpConstants.GENERATION_TIME), new OctetString(snmpTrapInfo.getGenerationTime().toString())));
        } else {
            trap.add(new VariableBinding(getOID(CsSnmpConstants.GENERATION_TIME)));
        }
    } else {
        throw new CloudRuntimeException(" Invalid alert Type ");
    }
    return trap;
}
Also used : PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) TimeTicks(org.snmp4j.smi.TimeTicks) UnsignedInteger32(org.snmp4j.smi.UnsignedInteger32) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 54 with VariableBinding

use of org.snmp4j.smi.VariableBinding 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 55 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project thingsboard by thingsboard.

the class PduService method createPdu.

public PDU createPdu(DeviceSessionContext sessionContext, SnmpCommunicationConfig communicationConfig, Map<String, String> values) {
    PDU pdu = setUpPdu(sessionContext);
    pdu.setType(communicationConfig.getMethod().getCode());
    pdu.addAll(communicationConfig.getAllMappings().stream().filter(mapping -> values.isEmpty() || values.containsKey(mapping.getKey())).map(mapping -> Optional.ofNullable(values.get(mapping.getKey())).map(value -> {
        Variable variable = toSnmpVariable(value, mapping.getDataType());
        return new VariableBinding(new OID(mapping.getOid()), variable);
    }).orElseGet(() -> new VariableBinding(new OID(mapping.getOid())))).collect(Collectors.toList()));
    return pdu;
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) IntStream(java.util.stream.IntStream) JsonObject(com.google.gson.JsonObject) ScopedPDU(org.snmp4j.ScopedPDU) SnmpDeviceTransportConfiguration(org.thingsboard.server.common.data.device.data.SnmpDeviceTransportConfiguration) HashMap(java.util.HashMap) SnmpCommunicationConfig(org.thingsboard.server.common.data.transport.snmp.config.SnmpCommunicationConfig) PDU(org.snmp4j.PDU) DataType(org.thingsboard.server.common.data.kv.DataType) Service(org.springframework.stereotype.Service) VariableBinding(org.snmp4j.smi.VariableBinding) Map(java.util.Map) Null(org.snmp4j.smi.Null) SnmpProtocolVersion(org.thingsboard.server.common.data.transport.snmp.SnmpProtocolVersion) SnmpMapping(org.thingsboard.server.common.data.transport.snmp.SnmpMapping) Integer32(org.snmp4j.smi.Integer32) OctetString(org.snmp4j.smi.OctetString) Collectors(java.util.stream.Collectors) SnmpMethod(org.thingsboard.server.common.data.transport.snmp.SnmpMethod) Objects(java.util.Objects) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) TbSnmpTransportComponent(org.thingsboard.server.queue.util.TbSnmpTransportComponent) Optional(java.util.Optional) Variable(org.snmp4j.smi.Variable) OID(org.snmp4j.smi.OID) DeviceSessionContext(org.thingsboard.server.transport.snmp.session.DeviceSessionContext) Variable(org.snmp4j.smi.Variable) OID(org.snmp4j.smi.OID) VariableBinding(org.snmp4j.smi.VariableBinding)

Aggregations

VariableBinding (org.snmp4j.smi.VariableBinding)55 OID (org.snmp4j.smi.OID)40 PDU (org.snmp4j.PDU)33 OctetString (org.snmp4j.smi.OctetString)33 ScopedPDU (org.snmp4j.ScopedPDU)19 Test (org.junit.Test)11 HashMap (java.util.HashMap)10 PDUv1 (org.snmp4j.PDUv1)10 Variable (org.snmp4j.smi.Variable)10 IOException (java.io.IOException)9 LinkedHashMap (java.util.LinkedHashMap)8 ResponseEvent (org.snmp4j.event.ResponseEvent)8 ArrayList (java.util.ArrayList)7 IpAddress (org.snmp4j.smi.IpAddress)7 CommunityTarget (org.snmp4j.CommunityTarget)6 Integer32 (org.snmp4j.smi.Integer32)6 TimeTicks (org.snmp4j.smi.TimeTicks)6 DefaultPDUFactory (org.snmp4j.util.DefaultPDUFactory)6 TableEvent (org.snmp4j.util.TableEvent)6 TableUtils (org.snmp4j.util.TableUtils)6