Beispiel #1
0
 @Override
 public int toInt() {
   switch (m_value.getSyntax()) {
     case SMIConstants.SYNTAX_COUNTER64:
       return (int) ((Counter64) m_value).getValue();
     case SMIConstants.SYNTAX_INTEGER:
       return ((Integer32) m_value).getValue();
     case SMIConstants.SYNTAX_COUNTER32:
     case SMIConstants.SYNTAX_TIMETICKS:
     case SMIConstants.SYNTAX_UNSIGNED_INTEGER32:
       return (int) ((UnsignedInteger32) m_value).getValue();
     default:
       return Integer.parseInt(m_value.toString());
   }
 }
Beispiel #2
0
 @Override
 public String toDisplayString() {
   switch (m_value.getSyntax()) {
     case SMIConstants.SYNTAX_OBJECT_IDENTIFIER:
       return SnmpObjId.get(((OID) m_value).getValue()).toString();
     case SMIConstants.SYNTAX_TIMETICKS:
       return Long.toString(toLong());
     case SMIConstants.SYNTAX_OCTET_STRING:
       return toStringDottingCntrlChars(((OctetString) m_value).getValue());
     case SMIConstants.SYNTAX_NULL:
       return "";
     default:
       return m_value.toString();
   }
 }
Beispiel #3
0
 private Class<?> getValueType(Variable variable) {
   switch (variable.getSyntax()) {
     case SMIConstants.EXCEPTION_END_OF_MIB_VIEW:
       return Exception.class;
     case SMIConstants.EXCEPTION_NO_SUCH_INSTANCE:
       return Exception.class;
     case SMIConstants.EXCEPTION_NO_SUCH_OBJECT:
       return Exception.class;
       // case SMIConstants.SYNTAX_BITS:
     case SMIConstants.SYNTAX_OCTET_STRING:
       return String.class;
     case SMIConstants.SYNTAX_COUNTER32:
       return Integer.class;
     case SMIConstants.SYNTAX_COUNTER64:
       return Long.class;
       // case SMIConstants.SYNTAX_GAUGE32:
     case SMIConstants.SYNTAX_UNSIGNED_INTEGER32:
       return Long.class;
       // case SMIConstants.SYNTAX_INTEGER:
     case SMIConstants.SYNTAX_INTEGER32:
       return Integer.class;
     case SMIConstants.SYNTAX_IPADDRESS:
       return InetAddress.class;
     case SMIConstants.SYNTAX_NULL:
       return String.class;
     case SMIConstants.SYNTAX_OBJECT_IDENTIFIER:
       return String.class;
     case SMIConstants.SYNTAX_OPAQUE:
       return String.class;
     case SMIConstants.SYNTAX_TIMETICKS:
       return Long.class;
     default:
       return Object.class;
   }
 }
Beispiel #4
0
 @Override
 public byte[] getBytes() {
   switch (m_value.getSyntax()) {
     case SMIConstants.SYNTAX_INTEGER:
     case SMIConstants.SYNTAX_COUNTER32:
     case SMIConstants.SYNTAX_COUNTER64:
     case SMIConstants.SYNTAX_TIMETICKS:
     case SMIConstants.SYNTAX_UNSIGNED_INTEGER32:
       return toBigInteger().toByteArray();
     case SMIConstants.SYNTAX_IPADDRESS:
       return toInetAddress().getAddress();
     case SMIConstants.SYNTAX_OBJECT_IDENTIFIER:
       return toSnmpObjId().toString().getBytes();
     case SMIConstants.SYNTAX_OCTET_STRING:
       return ((OctetString) m_value).getValue();
     case SMIConstants.SYNTAX_OPAQUE:
       return ((Opaque) m_value).getValue();
     case SMIConstants.EXCEPTION_END_OF_MIB_VIEW:
     case SMIConstants.EXCEPTION_NO_SUCH_INSTANCE:
     case SMIConstants.EXCEPTION_NO_SUCH_OBJECT:
     case SMIConstants.SYNTAX_NULL:
       return new byte[0];
     default:
       throw new IllegalArgumentException("cannot convert " + m_value + " to a byte array");
   }
 }
Beispiel #5
0
 @Override
 public long toLong() {
   switch (m_value.getSyntax()) {
     case SMIConstants.SYNTAX_COUNTER64:
       return ((Counter64) m_value).getValue();
     case SMIConstants.SYNTAX_INTEGER:
       return ((Integer32) m_value).getValue();
     case SMIConstants.SYNTAX_COUNTER32:
     case SMIConstants.SYNTAX_TIMETICKS:
     case SMIConstants.SYNTAX_UNSIGNED_INTEGER32:
       return ((UnsignedInteger32) m_value).getValue();
     case SMIConstants.SYNTAX_OCTET_STRING:
       return (convertStringToLong());
     default:
       return Long.parseLong(m_value.toString());
   }
 }
Beispiel #6
0
 @Override
 public InetAddress toInetAddress() {
   switch (m_value.getSyntax()) {
     case SMIConstants.SYNTAX_IPADDRESS:
       return ((IpAddress) m_value).getInetAddress();
     default:
       throw new IllegalArgumentException("cannot convert " + m_value + " to an InetAddress");
   }
 }
Beispiel #7
0
 @Override
 public String toHexString() {
   switch (m_value.getSyntax()) {
     case SMIConstants.SYNTAX_OCTET_STRING:
       return ((OctetString) m_value).toHexString().replaceAll(":", "");
     default:
       throw new IllegalArgumentException("cannot convert " + m_value + " to a HexString");
   }
 }
Beispiel #8
0
 @Override
 public SnmpObjId toSnmpObjId() {
   switch (m_value.getSyntax()) {
     case SMIConstants.SYNTAX_OBJECT_IDENTIFIER:
       return SnmpObjId.get(((OID) m_value).getValue());
     default:
       throw new IllegalArgumentException("cannot convert " + m_value + " to an SnmpObjId");
   }
 }
 /**
  * Method which takes a single OID and returns the response from the agent as a String.
  *
  * @param oid
  * @return
  * @throws IOException
  */
 public String getAsString(OID... oid) throws IOException {
   //        ResponseEvent event = get(new OID[]{oid});
   //        return event.getResponse().get(0).getVariable().toString();
   ResponseEvent event = get(oid);
   PDU response = event.getResponse();
   int size = oid.length;
   //        VariableBinding variableBinding=response.get(0);
   //        Variable variable=variableBinding.getVariable();
   //        String varStirng=variable.toString();
   //        return varStirng;
   StringBuilder builder = new StringBuilder();
   for (int i = 0; i < size; i++) {
     VariableBinding variableBinding = response.get(i);
     Variable variable = variableBinding.getVariable();
     String varStirng = variable.toString();
     String oi = variableBinding.getOid().toString();
     builder.append("oid: ").append(oi).append(" var: ").append(varStirng).append("\n");
   }
   return builder.toString();
 }
Beispiel #10
0
 @Override
 public BigInteger toBigInteger() {
   switch (m_value.getSyntax()) {
     case SMIConstants.SYNTAX_COUNTER64:
       final Counter64 cnt = (Counter64) m_value;
       if (cnt.getValue() > 0) {
         return BigInteger.valueOf(cnt.getValue());
       } else {
         return new BigInteger(cnt.toString());
       }
     case SMIConstants.SYNTAX_INTEGER:
       return BigInteger.valueOf(((Integer32) m_value).getValue());
     case SMIConstants.SYNTAX_COUNTER32:
     case SMIConstants.SYNTAX_TIMETICKS:
     case SMIConstants.SYNTAX_UNSIGNED_INTEGER32:
       return BigInteger.valueOf(((UnsignedInteger32) m_value).getValue());
     default:
       return new BigInteger(m_value.toString());
   }
 }
Beispiel #11
0
 @Override
 public boolean isNumeric() {
   switch (m_value.getSyntax()) {
     case SMIConstants.SYNTAX_INTEGER:
     case SMIConstants.SYNTAX_COUNTER32:
     case SMIConstants.SYNTAX_COUNTER64:
     case SMIConstants.SYNTAX_TIMETICKS:
     case SMIConstants.SYNTAX_UNSIGNED_INTEGER32:
       return true;
     default:
       return false;
   }
 }
Beispiel #12
0
 private Object convertVar(Variable valueAsVar) {
   Object retvalue = null;
   if (valueAsVar != null) {
     int type = valueAsVar.getSyntax();
     if (valueAsVar instanceof OID) {
       retvalue = valueAsVar;
     } else if (valueAsVar instanceof UnsignedInteger32) {
       if (valueAsVar instanceof TimeTicks) {
         long epochcentisecond = valueAsVar.toLong();
         retvalue = new Double(epochcentisecond / 100.0);
       } else retvalue = valueAsVar.toLong();
     } else if (valueAsVar instanceof Integer32) retvalue = valueAsVar.toInt();
     else if (valueAsVar instanceof Counter64) retvalue = valueAsVar.toLong();
     else if (valueAsVar instanceof OctetString) {
       if (valueAsVar instanceof Opaque) {
         retvalue = resolvOpaque((Opaque) valueAsVar);
       } else {
         // It might be a C string, try to remove the last 0;
         // But only if the new string is printable
         OctetString octetVar = (OctetString) valueAsVar;
         int length = octetVar.length();
         if (length > 1 && octetVar.get(length - 1) == 0) {
           OctetString newVar = octetVar.substring(0, length - 1);
           if (newVar.isPrintable()) {
             valueAsVar = newVar;
             logger.debug("Convertion an octet stream from " + octetVar + " to " + valueAsVar);
           }
         }
         retvalue = valueAsVar.toString();
       }
     } else if (valueAsVar instanceof Null) {
       retvalue = null;
     } else if (valueAsVar instanceof IpAddress) {
       retvalue = ((IpAddress) valueAsVar).getInetAddress();
     } else {
       logger.warn("Unknown syntax " + AbstractVariable.getSyntaxString(type));
     }
   }
   return retvalue;
 }
Beispiel #13
0
 /**
  * Converts variable to right object value
  *
  * @param variable snmp var
  * @param clazz class type to narrow to
  * @return object value
  * @throws ClassCastException if error
  * @throws java.net.UnknownHostException if error
  */
 @SuppressWarnings("UnnecessaryBoxing")
 private <T> T convert(Variable variable, Class<T> clazz)
     throws ClassCastException, UnknownHostException {
   switch (variable.getSyntax()) {
     case org.snmp4j.smi.SMIConstants.EXCEPTION_END_OF_MIB_VIEW:
     case org.snmp4j.smi.SMIConstants.EXCEPTION_NO_SUCH_INSTANCE:
     case org.snmp4j.smi.SMIConstants.EXCEPTION_NO_SUCH_OBJECT:
       return clazz.cast(new Exception(variable.toString()));
       // case org.snmp4j.smi.SMIConstants.SYNTAX_BITS:
     case org.snmp4j.smi.SMIConstants.SYNTAX_OCTET_STRING:
       if (variable instanceof BitString) {
         BitString bitString = (BitString) variable;
         return clazz.cast(bitString.toString());
       }
       return clazz.cast(variable.toString());
     case org.snmp4j.smi.SMIConstants.SYNTAX_COUNTER32:
       return clazz.cast(new Integer(variable.toInt()));
     case org.snmp4j.smi.SMIConstants.SYNTAX_COUNTER64:
       return clazz.cast(new Long(variable.toLong()));
       // case org.snmp4j.smi.SMIConstants.SYNTAX_GAUGE32:
     case org.snmp4j.smi.SMIConstants.SYNTAX_UNSIGNED_INTEGER32:
       if (variable instanceof Gauge32) {
         Gauge32 bitString = (Gauge32) variable;
         return clazz.cast(new Long(bitString.toLong()));
       }
       return clazz.cast(new Long(variable.toLong()));
       // case org.snmp4j.smi.SMIConstants.SYNTAX_INTEGER:
     case org.snmp4j.smi.SMIConstants.SYNTAX_INTEGER32:
       return clazz.cast(new Integer(variable.toInt()));
     case org.snmp4j.smi.SMIConstants.SYNTAX_IPADDRESS:
       IpAddress ipAddress = (IpAddress) variable;
       return clazz.cast(InetAddress.getByAddress(ipAddress.toByteArray()));
     case org.snmp4j.smi.SMIConstants.SYNTAX_NULL:
       return clazz.cast(variable.toString());
     case org.snmp4j.smi.SMIConstants.SYNTAX_OBJECT_IDENTIFIER:
       return clazz.cast(variable.toString());
     case org.snmp4j.smi.SMIConstants.SYNTAX_OPAQUE:
       return clazz.cast(variable.toString());
     case org.snmp4j.smi.SMIConstants.SYNTAX_TIMETICKS:
       return clazz.cast(new Long(variable.toLong()));
     default:
       throw new IllegalStateException();
   }
 }
Beispiel #14
0
 @Override
 public int getType() {
   return m_value.getSyntax();
 }
Beispiel #15
0
 @Override
 public int hashCode() {
   if (m_value == null) return 5231;
   return m_value.hashCode();
 }
Beispiel #16
0
 @Override
 public boolean isEndOfMib() {
   return m_value.getSyntax() == SMIConstants.EXCEPTION_END_OF_MIB_VIEW;
 }
Beispiel #17
0
 private long convertStringToLong() {
   return Double.valueOf(m_value.toString()).longValue();
 }