Beispiel #1
0
 private ResponseEvent snmpGet(String targetOid) throws IOException {
   PDU pdu = new PDU();
   pdu.add(new VariableBinding(new OID(targetOid)));
   pdu.setType(PDU.GET);
   // send the PDU
   return snmp.send(pdu, target);
 }
Beispiel #2
0
 private void notifyResponse(ResponseEvent response, String targetOid) {
   if (response == null) {
     for (ResponseHandler responseHandler : responseHandlers) {
       responseHandler.onTimeout(targetOid);
     }
   } else {
     PDU responsePDU = response.getResponse();
     int errorStatus = responsePDU.getErrorStatus();
     int errorIndex = responsePDU.getErrorIndex();
     String errorStatusText = responsePDU.getErrorStatusText();
     switch (errorStatus) {
       case SnmpConstants.SNMP_ERROR_SUCCESS:
         for (ResponseHandler responseHandler : responseHandlers) {
           responseHandler.onSuccess(targetOid);
         }
         break;
       default:
         if (log.isDebugEnabled()) {
           log.debug("Error: Request Failed");
           log.debug("Error Status = " + errorStatus);
           log.debug("Error Index = " + errorIndex);
           log.debug("Error Status Text = " + errorStatusText);
         }
         for (ResponseHandler responseHandler : responseHandlers) {
           responseHandler.onError(targetOid, errorStatus, errorStatusText);
         }
         break;
     }
   }
 }
 /**
  * This method is capable of handling multiple OIDs
  *
  * @param oids
  * @return
  * @throws IOException
  */
 public ResponseEvent get(OID... oids) throws IOException {
   PDU pdu = new PDU();
   for (OID oid : oids) {
     pdu.add(new VariableBinding(oid));
   }
   pdu.setType(PDU.GET);
   ResponseEvent event = snmp.send(pdu, getTarget(), null);
   if (event != null) {
     return event;
   }
   throw new RuntimeException("GET timed out");
 }
Beispiel #4
0
  public void updateByValue(
      URI url, List<Variable> values, SnmpUriCallback callback, Object userObject)
      throws UnknownHostException {
    Request request = createSnmpRequest(url);
    PDU pdu = request.getPdu();
    pdu.setType(PDU.SET);
    OID[] oids = request.getOIDs();
    for (int i = 0; i < oids.length && i < values.size(); i++) {
      pdu.add(new VariableBinding(oids[i], values.get(i)));
    }

    sendSnmpRequest(request, pdu, url, callback, userObject);
  }
Beispiel #5
0
  public void sendByBinding(
      URI url,
      List<VariableBinding> values,
      int pduType,
      SnmpUriCallback callback,
      Object userObject)
      throws UnknownHostException {
    Request request = createSnmpRequest(url);
    PDU pdu = request.getPdu();
    pdu.setType(pduType);
    for (VariableBinding vb : values) {
      pdu.add(vb);
    }

    sendSnmpRequest(request, pdu, url, callback, userObject);
  }
Beispiel #6
0
  public void snmpWalk(String oidFrom) throws SnmpException {
    PDU request = new PDU();
    request.setType(PDU.GETNEXT);
    request.add(new VariableBinding(new OID(oidFrom)));
    request.setNonRepeaters(0);
    OID rootOID = request.get(0).getOid();
    PDU response = null;

    int objects = 0;
    int requests = 0;
    long startTime = System.currentTimeMillis();

    try {
      Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
      snmp.listen();
      m_sessionDestroyed = false;
      do {
        requests++;
        ResponseEvent responseEvent = snmp.send(request, getTarget(getReadCommunity()));
        response = responseEvent.getResponse();
        if (response != null) {
          objects += response.size();
        }
      } while (!processWalk(response, request, rootOID) && !m_sessionDestroyed);

    } catch (SnmpException e) {
      throw e;
    } catch (Exception e) {
      e.printStackTrace();
      throw new SnmpException(e.getMessage());
    }
    if (m_snmpResponseHandler != null) {
      m_snmpResponseHandler.requestStats(requests, objects, System.currentTimeMillis() - startTime);
    }
  }
Beispiel #7
0
 @Override
 public void onResponse(ResponseEvent event) {
   SnmpUriResponse response = new SnmpUriResponse(SnmpUriResponse.Type.TIMEOUT);
   PDU responsePDU = event.getResponse();
   if (responsePDU != null) {
     if (responsePDU.getErrorStatus() != PDU.noError) {
       response = new SnmpUriResponse(responsePDU.getErrorStatus());
     } else {
       response =
           new SnmpUriResponse(
               Collections.singletonList(
                   responsePDU
                       .getVariableBindings()
                       .toArray(new VariableBinding[responsePDU.size()])));
     }
   }
   callback.onResponse(response, url, event.getUserObject());
 }
 /**
  * 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 #9
0
  void snmpSetValue(String oid, int syntax, String value) throws SnmpException {
    VariableBinding varbind = getVarBindForSetRequest(oid, syntax, value);

    PDU request = new PDU();
    request.setType(PDU.SET);
    request.add(varbind);
    PDU response = null;

    long startTime = System.currentTimeMillis();

    try {
      Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
      snmp.listen();
      ResponseEvent responseEvent = snmp.send(request, getTarget(getWriteCommunity()));
      response = responseEvent.getResponse();
      // System.out.println(response);
    } catch (Exception e) {
      e.printStackTrace();
      throw new SnmpException(e.getMessage());
    }
  }
Beispiel #10
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;
   }
 }
Beispiel #11
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;
 }
Beispiel #12
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);
    }
  }