asynchronous snmp requests with java, snmp4j

NOT DOCUMENTED WELL. I Will a sample code to the git hub

You need list of treelistener for each request. I used verctor, you may use array, stack, Linkedlist etc.

// Creating Treelistener array for requests

Vector<TreeResponseListener> treelisteners = new Vector<TreeResponseListener>(????, 10);

// Create treelistener for each request

TreeResponseListener treelistener=new TreeResponseListener();

// add it to the array

treelisteners.add(treelistener);

// create request and send the request

TreeUtils treeUtils = new TreeUtils(snmp, new MyDefaultPDUFactory(PDU.GETNEXT, contextEngineId));

treeUtils.getSubtree(targets[i], oid, i, treelistener);

later on check and wait for each treelistener for to finish

 

boolean finished = false;

while (!finished) {

booleantreelistenersFinished = true;

for (TreeResponseListener treelistener : treelisteners) {

logger.debug(“is treelistenersFinished?:”+treelistenersFinished);

logger.debug(“Checking snmp listener for dslam:”+treelistener.getDslam().getIp()+” oid:”+treelistener.getOidtable().getOid().getName()+

” finished?:”+treelistener.isFinished() +

” is treelistenersFinished?:”+treelistenersFinished);

// checking runtime for the listener

longnow = Instant.now().getEpochSecond();

longruntime = now-treelistener.getStartTime();

if (!treelistener.isFinished()) {

if (runtime > (treelistener.getOidtable().getOid().getAvgresponsetime()*2/1000)) {

treelistener.stopWalk();

logger.error(“Long runtime:”+ runtime + ” for treelistener of dslam:”+treelistener.getDslam().getIp()+” for oid:”+

treelistener.getOidtable().getOid().getName()+

” error:”+treelistener.getOidtable().getError() );

}

}

if (treelistenersFinished == true && treelistener.isFinished()) {

treelistenersFinished = true;

} else {

treelistenersFinished = treelistener.isFinished();

}

}

if (treelistenersFinished == true) {

logger.debug(“All snmp listners are finished, ending check”);

finished=treelistenersFinished;

} else {

logger.debug(“All snmp listners are not FINISHED, will retry check”);

Thread.sleep(10000);

}

}

}

 

 

— Custom defined TreeResponseListener class :

import java.time.Instant;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.util.TreeEvent;
import org.snmp4j.util.TreeListener;

 

public class TreeResponseListener implements TreeListener {
private static final Logger logger = LoggerFactory.getLogger(“TreeListener”);
private boolean finished;
private long StartTime;

/**
* Instantiates a new Tree response listener. You may include paramaters like the snmp target and the oid that used
*/

public TreeResponseListener() {
this.StartTime=Instant.now().getEpochSecond();
this.dslam=dslam;
this.oidtable=oidtable;
logger.trace(“Created new listener  StartTime:”+StartTime);
}
/**
* Stop walk. Needed for preventing long run time check in the main process.
*/
public void stopWalk() {
finished = true;
logger.debug(“Stopted snmp listener,finished:”+this.finished);
synchronized (this) {
this.notify();
}
}
public void finished(TreeEvent e) {
if ((e.getVariableBindings() != null) && (e.getVariableBindings().length > 0)) {
next(e);
long now=Instant.now().getEpochSecond();
long totalRunTime=now-this.getStartTime();
logger.debug(“completed snmp total run time:”+totalRunTime);
}
if (e.isError()) {
this.oidtable.setError(1);
long now=Instant.now().getEpochSecond();
long totalRunTime=now-this.getStartTime();
logger.error(“The following error occurred”
+ e.getErrorMessage() + ” total run time:”+totalRunTime);
}
finished = true;
synchronized (this) {
this.notify();
}
}

public boolean isFinished() {
return finished;
}

public boolean next(TreeEvent e) {
if (e.getVariableBindings() != null) {
VariableBinding[] varBindings = e.getVariableBindings();
for (VariableBinding varBinding : varBindings) {
if (varBinding == null) {
continue;
}
logger.trace(“Returned : “+
varBinding.getOid().toString()+
” : ” +
varBinding.getVariable().getSyntaxString() +
” : ” +
varBinding.getOid().last() +
” : ” +
varBinding.getVariable() +
” : Value as String ” +
varBinding.getVariable().toString());
}
}
return true;
}

/**
* @return the startTime
*/
public long getStartTime() {
return StartTime;
}

}