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;
}
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;
}
}
}
Aggregations