Example #1
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);
    }
  }
Example #2
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);
 }
 /**
  * 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");
 }
Example #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);
  }
Example #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);
  }
Example #6
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());
    }
  }