Search in sources :

Example 1 with Counter64

use of org.snmp4j.smi.Counter64 in project opennms by OpenNMS.

the class PropertiesBackedManagedObject method getVariableFromValueString.

/**
 * <p>getVariableFromValueString</p>
 *
 * @param oidStr a {@link java.lang.String} object.
 * @param valStr a {@link java.lang.String} object.
 * @return a {@link org.snmp4j.smi.Variable} object.
 * @throws SnmpErrorStatusException
 */
private Variable getVariableFromValueString(String oidStr, String valStr) throws SnmpErrorStatusException {
    Variable newVar;
    if (valStr.startsWith("Wrong Type")) {
        String newVal = valStr.replaceFirst("Wrong Type \\(should be .*\\): ", "");
        s_log.error("Bad Mib walk has value: '" + valStr + "' using '" + newVal + "'");
        valStr = newVal;
    }
    if ("\"\"".equals(valStr)) {
        newVar = new Null();
    } else {
        String moTypeStr = valStr.substring(0, valStr.indexOf(':'));
        String moValStr = valStr.substring(valStr.indexOf(':') + 2);
        try {
            if (moTypeStr.equals("STRING")) {
                if (moValStr.startsWith("\"") && moValStr.endsWith("\"")) {
                    moValStr = moValStr.substring(1, moValStr.length() - 1);
                }
                newVar = new OctetString(moValStr);
            } else if (moTypeStr.equals("Hex-STRING")) {
                newVar = OctetString.fromHexString(moValStr.trim().replace(' ', ':'));
            } else if (moTypeStr.equals("INTEGER")) {
                newVar = new Integer32(Integer.parseInt(moValStr));
            } else if (moTypeStr.equals("Gauge32")) {
                newVar = new Gauge32(Long.parseLong(moValStr));
            } else if (moTypeStr.equals("Counter32")) {
                // a 32 bit counter can be > 2 ^ 31, which is > INTEGER_MAX
                newVar = new Counter32(Long.parseLong(moValStr));
            } else if (moTypeStr.equals("Counter64")) {
                newVar = new Counter64(Long.parseLong(moValStr));
            } else if (moTypeStr.equals("Timeticks")) {
                Integer ticksInt = Integer.parseInt(moValStr.substring(moValStr.indexOf('(') + 1, moValStr.indexOf(')')));
                newVar = new TimeTicks(ticksInt);
            } else if (moTypeStr.equals("OID")) {
                newVar = new OID(moValStr);
            } else if (moTypeStr.equals("IpAddress")) {
                newVar = new IpAddress(moValStr.trim());
            } else if (moTypeStr.equals("Network Address")) {
                newVar = OctetString.fromHexString(moValStr.trim());
            } else if (moTypeStr.equals("Responder")) {
                newVar = handleDynamicVariable(oidStr, moValStr);
            } else {
                // newVar = new OctetString(moValStr);
                throw new IllegalArgumentException("Unrecognized SNMP Type " + moTypeStr);
            }
        } catch (SnmpErrorStatusException e) {
            throw e;
        } catch (Throwable t) {
            throw new UndeclaredThrowableException(t, "Could not convert value '" + moValStr + "' of type '" + moTypeStr + "' to SNMP object for OID " + oidStr);
        }
    }
    return newVar;
}
Also used : OctetString(org.snmp4j.smi.OctetString) Null(org.snmp4j.smi.Null) DynamicVariable(org.opennms.mock.snmp.responder.DynamicVariable) Variable(org.snmp4j.smi.Variable) TimeTicks(org.snmp4j.smi.TimeTicks) OctetString(org.snmp4j.smi.OctetString) OID(org.snmp4j.smi.OID) Counter32(org.snmp4j.smi.Counter32) Integer32(org.snmp4j.smi.Integer32) Counter64(org.snmp4j.smi.Counter64) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) Gauge32(org.snmp4j.smi.Gauge32) SnmpErrorStatusException(org.opennms.mock.snmp.responder.SnmpErrorStatusException) IpAddress(org.snmp4j.smi.IpAddress)

Example 2 with Counter64

use of org.snmp4j.smi.Counter64 in project LogHub by fbacchella.

the class SnmpTrap method convertVar.

private Object convertVar(Variable var) {
    if (var == null) {
        return null;
    }
    if (var instanceof UnsignedInteger32) {
        return var.toLong();
    } else if (var instanceof Integer32) {
        return var.toInt();
    } else if (var instanceof Counter64) {
        return var.toLong();
    } else {
        switch(var.getSyntax()) {
            case BER.ASN_BOOLEAN:
                return var.toInt();
            case BER.INTEGER:
                return var.toInt();
            case BER.OCTETSTRING:
                // It might be a C string, try to remove the last 0;
                // But only if the new string is printable
                OctetString octetVar = (OctetString) var;
                int length = octetVar.length();
                if (length > 1 && octetVar.get(length - 1) == 0) {
                    OctetString newVar = octetVar.substring(0, length - 1);
                    if (newVar.isPrintable()) {
                        var = newVar;
                        logger.debug("Convertion an octet stream from {} to {}", octetVar, var);
                    }
                }
                return var.toString();
            case BER.NULL:
                return null;
            case BER.OID:
                {
                    OID oid = (OID) var;
                    Map<String, Object> parsed = formatter.store.parseIndexOID(oid.getValue());
                    // If an empty map was return or a single entry map, it's not an table entry, just format the OID
                    if (parsed.size() <= 1) {
                        return oid.format();
                    } else {
                        return parsed;
                    }
                }
            case BER.IPADDRESS:
                return ((IpAddress) var).getInetAddress();
            case BER.COUNTER32:
            case BER.GAUGE32:
                return var.toLong();
            case BER.TIMETICKS:
                return new Double(1.0 * ((TimeTicks) var).toMilliseconds() / 1000.0);
            case BER.OPAQUE:
                return resolvOpaque((Opaque) var);
            case BER.COUNTER64:
                return var.toLong();
            default:
                logger.warn("Unknown syntax: " + var.getSyntaxString());
                return null;
        }
    }
}
Also used : OctetString(org.snmp4j.smi.OctetString) Integer32(org.snmp4j.smi.Integer32) UnsignedInteger32(org.snmp4j.smi.UnsignedInteger32) Counter64(org.snmp4j.smi.Counter64) UnsignedInteger32(org.snmp4j.smi.UnsignedInteger32) IpAddress(org.snmp4j.smi.IpAddress) TransportIpAddress(org.snmp4j.smi.TransportIpAddress) OID(org.snmp4j.smi.OID) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Counter64 (org.snmp4j.smi.Counter64)2 Integer32 (org.snmp4j.smi.Integer32)2 IpAddress (org.snmp4j.smi.IpAddress)2 OID (org.snmp4j.smi.OID)2 OctetString (org.snmp4j.smi.OctetString)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DynamicVariable (org.opennms.mock.snmp.responder.DynamicVariable)1 SnmpErrorStatusException (org.opennms.mock.snmp.responder.SnmpErrorStatusException)1 Counter32 (org.snmp4j.smi.Counter32)1 Gauge32 (org.snmp4j.smi.Gauge32)1 Null (org.snmp4j.smi.Null)1 TimeTicks (org.snmp4j.smi.TimeTicks)1 TransportIpAddress (org.snmp4j.smi.TransportIpAddress)1 UnsignedInteger32 (org.snmp4j.smi.UnsignedInteger32)1 Variable (org.snmp4j.smi.Variable)1