Example #1
0
  VariableBinding getVarBindForSetRequest(String oid, int type, String value) {
    VariableBinding vb = new VariableBinding(new OID(oid));

    if (value != null) {
      Variable variable;
      switch (type) {
        case DSnmpMibRecord.VALUE_TYPE_INTEGER32:
          variable = new Integer32(Integer.parseInt(value));
          break;
        case DSnmpMibRecord.VALUE_TYPE_UNSIGNED_INTEGER32:
          variable = new UnsignedInteger32(Long.parseLong(value));
          break;
        case DSnmpMibRecord.VALUE_TYPE_OCTET_STRING:
          variable = new OctetString(value);
          break;
        case DSnmpMibRecord.VALUE_TYPE_NULL:
          variable = new Null();
          break;
        case DSnmpMibRecord.VALUE_TYPE_OID:
          variable = new OID(value);
          break;
        case DSnmpMibRecord.VALUE_TYPE_TIMETICKS:
          variable = new TimeTicks(Long.parseLong(value));
          break;
        case DSnmpMibRecord.VALUE_TYPE_IP_ADDRESS:
          variable = new IpAddress(value);
          break;
        default:
          throw new IllegalArgumentException("Variable type " + type + " not supported");
      }
      vb.setVariable(variable);
    }
    return vb;
  }
Example #2
0
  /**
   * Encodes a <code>Variable</code> to an <code>OutputStream</code>.
   *
   * @param outputStream an <code>OutputStream</code>.
   * @throws IOException if an error occurs while writing to the stream.
   */
  @Override
  public void encodeBER(OutputStream outputStream) throws IOException {
    BER.encodeHeader(outputStream, type, getBERPayloadLength());

    if (type == PDU.V1TRAP) {
      enterprise.encodeBER(outputStream);
      agentAddress.encodeBER(outputStream);
      genericTrap.encodeBER(outputStream);
      specificTrap.encodeBER(outputStream);
      timestamp.encodeBER(outputStream);
    } else {
      requestID.encodeBER(outputStream);
      errorStatus.encodeBER(outputStream);
      errorIndex.encodeBER(outputStream);
    }
    int vbLength = 0;
    for (VariableBinding variableBinding : variableBindings) {
      vbLength += variableBinding.getBERLength();
    }
    BER.encodeHeader(outputStream, BER.SEQUENCE, vbLength);
    for (VariableBinding vb : variableBindings) {
      if (!isVariableV1(vb.getVariable())) {
        throw new IOException("Cannot encode Counter64 into a SNMPv1 PDU");
      }
      vb.encodeBER(outputStream);
    }
  }
Example #3
0
  /**
   * Decodes a <code>Variable</code> from an <code>InputStream</code>.
   *
   * @param inputStream an <code>InputStream</code> containing a BER encoded byte stream.
   * @throws IOException
   */
  @Override
  public void decodeBER(BERInputStream inputStream) throws IOException {
    MutableByte pduType = new MutableByte();
    int length = BER.decodeHeader(inputStream, pduType);
    int pduStartPos = (int) inputStream.getPosition();

    switch (pduType.getValue()) {
      case PDU.SET:
      case PDU.GET:
      case PDU.GETNEXT:
      case PDU.V1TRAP:
      case PDU.RESPONSE:
        break;
        // The following PDU types are not supported by the SNMPv1 standard!
      case PDU.NOTIFICATION:
      case PDU.INFORM:
        if (SNMP4JSettings.isAllowSNMPv2InV1()) {
          break;
        }
        // fall through
      default:
        throw new IOException("Unsupported PDU type: " + pduType.getValue());
    }
    this.setType(pduType.getValue());
    if (getType() == PDU.V1TRAP) {
      enterprise.decodeBER(inputStream);
      agentAddress.decodeBER(inputStream);
      genericTrap.decodeBER(inputStream);
      specificTrap.decodeBER(inputStream);
      timestamp.decodeBER(inputStream);
    } else {
      requestID.decodeBER(inputStream);
      errorStatus.decodeBER(inputStream);
      errorIndex.decodeBER(inputStream);
    }
    // reusing pduType here to save memory ;-)
    pduType = new BER.MutableByte();
    int vbLength = BER.decodeHeader(inputStream, pduType);
    if (pduType.getValue() != BER.SEQUENCE) {
      throw new IOException("Encountered invalid tag, SEQUENCE expected: " + pduType.getValue());
    }
    // rest read count
    int startPos = (int) inputStream.getPosition();
    variableBindings = new Vector<>();
    while (inputStream.getPosition() - startPos < vbLength) {
      VariableBinding vb = new VariableBinding();
      vb.decodeBER(inputStream);
      if (!isVariableV1(vb.getVariable())) {
        throw new MessageException("Counter64 encountered in SNMPv1 PDU " + "(RFC 2576 ยง4.1.2.1)");
      }
      variableBindings.add(vb);
    }
    if (BER.isCheckSequenceLength()) {
      BER.checkSequenceLength(vbLength, (int) inputStream.getPosition() - startPos, this);
      BER.checkSequenceLength(length, (int) inputStream.getPosition() - pduStartPos, this);
    }
  }
Example #4
0
 private SnmpOidValuePair outputResponse(VariableBinding vb) {
   SnmpOidValuePair oidval = new SnmpOidValuePair();
   oidval.oid = vb.getOid().toString();
   oidval.value_str = vb.getVariable().toString();
   if (m_snmpResponseHandler != null) {
     m_snmpResponseHandler.responseReceived(oidval);
   }
   return oidval;
 }
Example #5
0
 @Override
 protected int getBERPayloadLengthPDU() {
   if (getType() != PDU.V1TRAP) {
     return super.getBERPayloadLengthPDU();
   } else {
     int length = 0;
     // length for all vbs
     for (VariableBinding variableBinding : variableBindings) {
       length += variableBinding.getBERLength();
     }
     length += BER.getBERLengthOfLength(length) + 1;
     length += agentAddress.getBERLength();
     length += enterprise.getBERLength();
     length += genericTrap.getBERLength();
     length += specificTrap.getBERLength();
     length += timestamp.getBERLength();
     return length;
   }
 }
 /**
  * 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();
 }
Example #7
0
 public void browse(URI url, SnmpUriCallback callback, Object userObject)
     throws UnknownHostException {
   Request request = createSnmpRequest(url);
   PDU pdu = request.getPdu();
   switch (request.getType()) {
     case GET:
       pdu.setType(PDU.GET);
       pdu.addAll(VariableBinding.createFromOIDs(request.getOIDs()));
       sendSnmpRequest(request, pdu, url, callback, userObject);
       break;
     case NEXT:
       pdu.setType(PDU.GETNEXT);
       pdu.addAll(VariableBinding.createFromOIDs(request.getOIDs()));
       sendSnmpRequest(request, pdu, url, callback, userObject);
       break;
     case SUBTREE:
       TreeUtils treeUtils = new TreeUtils(snmp, pduFactory);
       TreeListener treeListener = new AsyncTreeListener(url, callback);
       treeUtils.walk(request.getTarget(), request.getOIDs(), userObject, treeListener);
       break;
   }
 }
Example #8
0
 private boolean processWalk(PDU response, PDU request, OID rootOID) throws SnmpException {
   if ((response == null)
       || (response.getErrorStatus() != 0)
       || (response.getType() == PDU.REPORT)) {
     return true;
   }
   boolean finished = false;
   OID lastOID = request.get(0).getOid();
   for (int i = 0; (!finished) && (i < response.size()); i++) {
     VariableBinding vb = response.get(i);
     if ((vb.getOid() == null)
         || (vb.getOid().size() < rootOID.size())
         || (rootOID.leftMostCompare(rootOID.size(), vb.getOid()) != 0)) {
       finished = true;
     } else if (Null.isExceptionSyntax(vb.getVariable().getSyntax())) {
       outputResponse(vb);
       finished = true;
     } else if (vb.getOid().compareTo(lastOID) <= 0) {
       throw new SnmpException(
           "Variable received is not lexicographic successor of requested one:"
               + vb.toString()
               + " <= "
               + lastOID);
     } else {
       outputResponse(vb);
       lastOID = vb.getOid();
     }
   }
   if (response.size() == 0) {
     finished = true;
   }
   if (!finished) {
     VariableBinding next = response.get(response.size() - 1);
     next.setVariable(new Null());
     request.set(0, next);
     request.setRequestID(new Integer32(0));
   }
   return finished;
 }
Example #9
0
 /**
  * Creates a static managed object group for the sub-tree with the specified root OID.
  *
  * @param root the root OID of the sub-tree to be registered by this managed object.
  * @param vbs the variable bindings to be returned in this sub-tree.
  */
 public StaticMOGroup(OID root, VariableBinding[] vbs) {
   this.root = root;
   this.scope = new DefaultMOScope(root, true, root.nextPeer(), false);
   for (VariableBinding vb : vbs) {
     if ((vb.getOid() != null) && (vb.getVariable() != null)) {
       if ((vb.getOid().size() >= root.size())
           && (vb.getOid().leftMostCompare(root.size(), root) == 0)) {
         this.vbs.put(vb.getOid(), vb.getVariable());
       }
     }
   }
 }
Example #10
0
  /**
   * Read a variable saved into @DriverConfiguration object
   *
   * @param propertyConfiguration driver configuration object
   * @throws GenericException if configuration fatal error
   */
  @Override
  public void readValue(DriverConfiguration propertyConfiguration) throws GenericException {
    try {
      ResponseEvent response = snmpGet(String.valueOf(propertyConfiguration.getValue()));
      if (response != null) {
        if (log.isDebugEnabled()) {
          log.debug("Got Snmp Get Response from Agent");
        }
        PDU responsePDU = response.getResponse();

        if (responsePDU != null) {
          int errorStatus = responsePDU.getErrorStatus();

          if (errorStatus == PDU.noError) {
            Vector<? extends VariableBinding> variableBindings = responsePDU.getVariableBindings();
            if (log.isDebugEnabled()) {
              log.debug(String.format("Snmp Get Response = %s", variableBindings));
            }
            VariableBinding variableBinding = variableBindings.get(0);
            Variable variable = variableBinding.getVariable();
            Object value = convert(variable, getValueType(variable));
            setValue(propertyConfiguration, value);
          } else {
            notifyResponse(response, propertyConfiguration.getName());
          }
        } else {
          if (log.isDebugEnabled()) {
            log.debug("Error: Response PDU is null");
          }
        }
      } else {
        notifyResponse(response, propertyConfiguration.getName());
      }
    } catch (Exception e) {
      throw new GenericException(e);
    }
  }