[SNMP4J] Matching Request and Response PDUs based on a target

Mark Cotner mark.cotner at gmail.com
Fri Mar 5 00:23:35 CET 2010


Disregard I guess.  I created this in Java and ran it about 50 times and it
worked every time.  It must be something in the JRuby Object -> Pojo mapping
that's messing things up.  I'll dig in a little deeper and bug them.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hellosnmp;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;

/**
 *
 * @author markcotner
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws InterruptedException {
        PDU pdu = new PDU();
        pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.1"))); //
sysDescr
        pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.3.1")));
        pdu.setType(PDU.GETNEXT);

        Address targetAddress = GenericAddress.parse("udp:localhost/161");
        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString("public"));
        target.setAddress(targetAddress);
        target.setVersion(SnmpConstants.version2c);

        Address targetAddress2 =
GenericAddress.parse("udp:rubydb.ove.local/161");
        CommunityTarget target2 = new CommunityTarget();
        target2.setCommunity(new OctetString("public"));
        target2.setAddress(targetAddress2);
        target2.setVersion(SnmpConstants.version2c);

        Snmp snmp;
        try {
            snmp = new Snmp(new DefaultUdpTransportMapping());
            snmp.listen();
            ResponseListener listener = new ResponseListener() {
                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();
                    PDU request = event.getRequest();
                    if (response == null) {
                        System.out.println(event.getUserObject() + " Request
"+request+" timed out");
                    } else {
                        System.out.println("Processing response from " +
event.getPeerAddress().toString());
                        System.out.println(event.getUserObject() + "
Received response "+response+" on request "+
                                                request);
                    }
                }
            };
            snmp.send(pdu, target2, "target2", listener);
            snmp.send(pdu, target, "target1", listener);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null,
ex);
        }
        Thread.sleep(2000);
    }
}

'njoy,
Mark

On Thu, Mar 4, 2010 at 5:57 PM, Mark Cotner <mark.cotner at gmail.com> wrote:

> Hi,
> I'm writing a bulk poller that does async requests against multiple hosts
> and need to line up the request with the response based on the host.  I've
> been passing in the unique target/host name with the request, but what comes
> back out with event.getUserObject is not lining up.
>
> For instance . . . I deliberately put in a host that times out and one that
> polls correctly.  95% of the time the PDU from the working host gets
> associated to the one that times out.
>
> I went back to June on this list and didn't see anyone else complaining
> about this.  Do you know of any issues with the object reference passing
> from send(3rd argument) and event.getUserObject?
>
> Please note that I'm doing this in JRuby and although unlikely it could be
> flubbing the object references.  I'm going to write a pure Java test example
> tonight and see if I can duplicate the issue.
>
> 'njoy,
> Mark
>
> --
> "Keynes observed that pragmatic businessmen often could not imagine that
> they were the slaves of defunct economists, but ironically, never is this
> more true than today of Keynes himself." -- John Train
>



-- 
"Keynes observed that pragmatic businessmen often could not imagine that
they were the slaves of defunct economists, but ironically, never is this
more true than today of Keynes himself." -- John Train



More information about the SNMP4J mailing list