Esempio n. 1
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();
   }
 }
Esempio n. 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();
   }
 }
Esempio n. 3
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());
   }
 }
Esempio n. 4
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());
   }
 }
 /**
  * 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();
 }
Esempio n. 6
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());
   }
 }
Esempio n. 7
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;
 }
Esempio n. 8
0
 private long convertStringToLong() {
   return Double.valueOf(m_value.toString()).longValue();
 }