user3102314
user3102314

Reputation:

Asynchornous SNMP get message with SNMP4J

I have been working in a simple SNMP manager for java using the library SNMP4J. I am trying to send simple asynchronous get messages. I have greated a SNMPmanager to create the PDUs and start the listeners and a simple class named SNMPget that makes a simple get request.

As stated in the javadoc:

Asynchronous responses are returned by calling a callback method on an object instance that implements the ResponseListener interface.

I have followed the exact instructions to implement the callback but looks like the callback method onResponse is never executed when sending a get request. In fact, I have used wireshark to check if packets are being send and the request is correctly send and the correct response is received. Any clue on what I am doing wrong?

    public class SNMPManager {
private static SNMPManager instance = new SNMPManager();
private Snmp snmp_ = null;

//Common SNMP variables
private int requestPort = 161;
private int timeout = 10000;
private int retryCount = 1;
private int maxRepetitions = 20;
private int nonRepeaters = 0;

//Singleton pattern
public static SNMPManager getInstance() {
    return instance;
}

//Empty Constructor
private SNMPManager() { }

//Start the manager
public void start() throws IOException {
    //Create a UDP transport on all interfaces
    DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping();
    //Create a SNMP instance
    snmp_ = new Snmp(transport);
    //Put all the transport mappings into listen mode
    snmp_.listen();
}

//Methods for creating targets

// Create the target for version 2c
public Target createVersion2cTarget(String ipaddress, String communityName) {
    CommunityTarget target = new CommunityTarget();
    target.setAddress(new UdpAddress(String.format("%s/%d", ipaddress, requestPort)));
    target.setCommunity(new OctetString(communityName));
    target.setTimeout(timeout);
    target.setRetries(retryCount);
    target.setVersion(SnmpConstants.version2c);
    return target;
}

//Methods for creating PDUs
public PDU createGetRequestPdu(String requestOID) {
    PDU pdu = new PDU();
    //Set the request type
    pdu.setType(PDU.GET);
    //Set the OID
    OID rOID = new OID(requestOID);
    pdu.add(new VariableBinding(rOID));
    return pdu;
}

//Methods for the request (async mode)
public void getRequest(PDU pdu, Target target, Object userHandle, ResponseListener listener) throws IOException {
    snmp_.get(pdu, target, userHandle, listener);
}

}

public class SNMPget {

protected final SNMPManager snmp_;
private String requestOID;
private String ipAddress;
private String communityName;

public SNMPget(String newRequestOID, String hostIpAddress, String newCommunityName) {
    snmp_ = SNMPManager.getInstance();
    requestOID = newRequestOID;
    ipAddress = hostIpAddress;
    communityName = newCommunityName;
}
public void get(ResponseListener listener) throws IOException {
    Target targetHost = snmp_.createVersion2cTarget(ipAddress, communityName);
    PDU pduSNMPget = snmp_.createGetRequestPdu(requestOID);
    snmp_.getRequest(pduSNMPget, targetHost, null, listener);
}

}

public class mainTest {
public static void main(String[] args) throws IOException {

    SNMPManager snmp = SNMPManager.getInstance();
    snmp.start();
    ResponseListener listener = new ResponseListener() {
        @Override
        public void onResponse(ResponseEvent event) {
            // Always cancel async request when response has been received
            // otherwise a memory leak is created! Not canceling a request
            // immediately can be useful when sending a request to a broadcast
            // address.
            ((Snmp) event.getSource()).cancel(event.getRequest(), this);
            PDU response = event.getResponse();
            System.out.println("Received response "+response);
        }
    };

    SNMPget snmpGetCmd = new SNMPget("1.3.6.1.2.1.1.1.0", "192.168.137.104", "public");

    snmpGetCmd.get(listener);

}

}

Upvotes: 1

Views: 1544

Answers (1)

user24282760
user24282760

Reputation: 1

Since this is an asynchronous operation, the main program exits before the response is received by the ResponseListener. If you add code to keep the main program from exiting, then you will receive the response.

Upvotes: 0

Related Questions