Java Code Examples for org.snmp4j.event.ResponseEvent#getResponse()
The following examples show how to use
org.snmp4j.event.ResponseEvent#getResponse() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SnmpH3C.java From yuzhouwan with Apache License 2.0 | 7 votes |
/** * Send PDU to H3C, then waiting response event will be caught by listener. * * @param snmp * @param userTarget * @param pdu * @throws IOException * @throws InterruptedException */ private void sendGetPDU(Snmp snmp, UserTarget userTarget, ScopedPDU pdu) throws IOException, InterruptedException { final CountDownLatch latch = new CountDownLatch(1); ResponseListener listener = new ResponseListener() { @Override public void onResponse(ResponseEvent event) { ((Snmp) event.getSource()).cancel(event.getRequest(), this); PDU response = event.getResponse(); PDU request = event.getRequest(); LOG.debug("[request]: {}", request); if (response == null) { LOG.error("[ERROR]: response is null"); } else if (response.getErrorStatus() != 0) { LOG.error(String.format("[ERROR]: response status %s, Text: %s", response.getErrorStatus(), response.getErrorStatusText())); } else { LOG.debug("Received response Success!"); for (int i = 0; i < response.size(); i++) { VariableBinding vb = response.get(i); LOG.info("{} = {}", vb.getOid(), vb.getVariable()); } LOG.debug("SNMP Async GetList OID finished. "); latch.countDown(); } } }; snmp.send(pdu, userTarget, null, listener); LOG.debug("async send pdu wait for response..."); boolean wait = latch.await(3, TimeUnit.SECONDS); LOG.debug("latch.await =:" + wait); }
Example 2
Source File: SNMPClient.java From mysql_perf_analyzer with Apache License 2.0 | 6 votes |
/** * This method is capable of handling multiple OIDs * @param oids * @return * @throws IOException */ public Map<OID, String> get(OID oids[]) throws IOException { PDU pdu = createPDU(); for (OID oid : oids) { pdu.add(new VariableBinding(oid)); } pdu.setType(PDU.GET); ResponseEvent event = snmp.send(pdu, getTarget(), null); if(event != null) { PDU pdu2 = event.getResponse(); VariableBinding[] binds = pdu2!=null?event.getResponse().toArray():null; if(binds!=null) { Map<OID, String> res = new LinkedHashMap<OID, String>(binds.length); for(VariableBinding b: binds) res.put(b.getOid(), b.getVariable().toString()); return res; }else return null; } throw new RuntimeException("GET timed out"); }
Example 3
Source File: SnmpUtil.java From yuzhouwan with Apache License 2.0 | 6 votes |
private static PDU walk(PDU request, Target target) throws IOException { request.setNonRepeaters(0); OID rootOID = request.get(0).getOid(); PDU response; int objects = 0; int requests = 0; long startTime = System.currentTimeMillis(); do { requests++; ResponseEvent responseEvent = SnmpUtil.snmp.send(request, target); response = responseEvent.getResponse(); if (response != null) { objects += response.size(); } } while (!processWalk(response, request, rootOID)); System.out.println(); System.out.println("Total requests sent: " + requests); System.out.println("Total objects received: " + objects); System.out.println("Total walk time: " + (System.currentTimeMillis() - startTime) + " milliseconds"); return response; }
Example 4
Source File: SNMPHelper.java From SuitAgent with Apache License 2.0 | 5 votes |
/** * 获取指定OID的 get * * @param snmp * @param target * @param oid * @return * @throws IOException */ public static PDU snmpGet(Snmp snmp, Target target, String oid) throws IOException { ScopedPDU pdu = new ScopedPDU(); pdu.setType(PDU.GET); pdu.add(new VariableBinding(new OID(oid))); ResponseEvent responseEvent = snmp.send(pdu, target); PDU response = responseEvent.getResponse(); if(response == null){ log.warn("response null - error:{} peerAddress:{} source:{} request:{}", responseEvent.getError(), responseEvent.getPeerAddress(), responseEvent.getSource(), responseEvent.getRequest()); } return response; }
Example 5
Source File: SNMPHelper.java From OpenFalcon-SuitAgent with Apache License 2.0 | 5 votes |
/** * 获取指定OID的 get * * @param snmp * @param target * @param oid * @return * @throws IOException */ public static PDU snmpGet(Snmp snmp, Target target, String oid) throws IOException { ScopedPDU pdu = new ScopedPDU(); pdu.setType(PDU.GET); pdu.add(new VariableBinding(new OID(oid))); ResponseEvent responseEvent = snmp.send(pdu, target); PDU response = responseEvent.getResponse(); if(response == null){ log.warn("response null - error:{} peerAddress:{} source:{} request:{}", responseEvent.getError(), responseEvent.getPeerAddress(), responseEvent.getSource(), responseEvent.getRequest()); } return response; }
Example 6
Source File: SnmpUtil.java From yuzhouwan with Apache License 2.0 | 5 votes |
public PDU send() throws IOException { snmp = createSnmpSession(); this.target = createTarget(); target.setVersion(version); target.setAddress(address); target.setRetries(retries); target.setTimeout(timeout); snmp.listen(); PDU request = createPDU(target); for (Object vb : vbs) { request.add((VariableBinding) vb); } PDU response = null; if (_operation == WALK) { response = walk(request, target); } else { ResponseEvent responseEvent; long startTime = System.currentTimeMillis(); responseEvent = snmp.send(request, target); if (responseEvent != null) { response = responseEvent.getResponse(); System.out.println("Received response after " + (System.currentTimeMillis() - startTime) + " millis"); } } snmp.close(); return response; }
Example 7
Source File: SNMPHelper.java From SuitAgent with Apache License 2.0 | 4 votes |
/** * walk方式获取指定的oid value * * @param snmp * @param target * @param oid * @return * @throws IOException */ public static List<PDU> snmpWalk(Snmp snmp, Target target, String oid) throws IOException { List<PDU> pduList = new ArrayList<>(); ScopedPDU pdu = new ScopedPDU(); OID targetOID = new OID(oid); pdu.add(new VariableBinding(targetOID)); boolean finished = false; while (!finished) { VariableBinding vb = null; ResponseEvent respEvent = snmp.getNext(pdu, target); PDU response = respEvent.getResponse(); if (null == response) { break; } else { vb = response.get(0); } // check finish finished = checkWalkFinished(targetOID, pdu, vb); if (!finished) { pduList.add(response); // Set up the variable binding for the next entry. pdu.setRequestID(new Integer32(0)); pdu.set(0, vb); } } return pduList; }
Example 8
Source File: SNMPHelper.java From OpenFalcon-SuitAgent with Apache License 2.0 | 4 votes |
/** * walk方式获取指定的oid value * * @param snmp * @param target * @param oid * @return * @throws IOException */ public static List<PDU> snmpWalk(Snmp snmp, Target target, String oid) throws IOException { List<PDU> pduList = new ArrayList<>(); ScopedPDU pdu = new ScopedPDU(); OID targetOID = new OID(oid); pdu.add(new VariableBinding(targetOID)); boolean finished = false; while (!finished) { VariableBinding vb = null; ResponseEvent respEvent = snmp.getNext(pdu, target); PDU response = respEvent.getResponse(); if (null == response) { break; } else { vb = response.get(0); } // check finish finished = checkWalkFinished(targetOID, pdu, vb); if (!finished) { pduList.add(response); // Set up the variable binding for the next entry. pdu.setRequestID(new Integer32(0)); pdu.set(0, vb); } } return pduList; }
Example 9
Source File: SNMPHelper.java From SuitAgent with Apache License 2.0 | 2 votes |
/** * 获取指定OID的 getNext * * @param snmp * @param target * @param oid * @return * @throws IOException */ public static PDU snmpGetNext(Snmp snmp, Target target, String oid) throws IOException { ScopedPDU pdu = new ScopedPDU(); pdu.setType(PDU.GETNEXT); pdu.add(new VariableBinding(new OID(oid))); ResponseEvent responseEvent = snmp.send(pdu, target); return responseEvent.getResponse(); }
Example 10
Source File: SNMPHelper.java From OpenFalcon-SuitAgent with Apache License 2.0 | 2 votes |
/** * 获取指定OID的 getNext * * @param snmp * @param target * @param oid * @return * @throws IOException */ public static PDU snmpGetNext(Snmp snmp, Target target, String oid) throws IOException { ScopedPDU pdu = new ScopedPDU(); pdu.setType(PDU.GETNEXT); pdu.add(new VariableBinding(new OID(oid))); ResponseEvent responseEvent = snmp.send(pdu, target); return responseEvent.getResponse(); }