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); } }
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; }
@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()); }