[SNMP4J] SnmpWalk Termination Conditions

Tim Couger tcouger at esisoft.us
Wed Nov 17 01:17:35 CET 2004


I've been investigating the SNMP4j API ...

All I really need is simple SNMP Walk functionality (for now).

After perusing the website and searching the web for examples, I determined
that I was going to have to glean the information from the SnmpRequest.java
example and the various javadocs.

So ... I came up with a simple Proof of Concept (attached).  DISCLAIMER:
It's only a Proof of Concept ... so it ain't pretty (i.e., just a "main"
method, specific "import" statements, etc.).

Anyway ... my question:

>From what I could tell in the examples, there are several conditions that
will terminate the walk.  These are:

1)  responsePDU == null
2)  responsePDU.getErrorStatus() != 0
3)  responsePDU.get(0).getOid() == null
4)  responsePDU.get(0).getOid().size() < targetOID.size()
5)  targetOID.leftMostCompare(targetOID.size(),
                               responsePDU.get(0).getOid()) != 0
6)  Null.isExceptionSyntax(responsePDU.get(0).getVariable().getSyntax())
7)  responsePDU.get(0).getOid().compareTo(targetOID) <= 0

>From my tests, I only found situations that triggered conditions #1, #2, and
#5.  Can someone tell me when situations #3, #4, #6, and #7 might occur?
The reason I care is that I will need to explain them in the documentation.

Thanks ...

---------------------Begin Simple SNMP Walk
import java.io.IOException;

import org.apache.log4j.*;

import org.snmp4j.CommunityTarget;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.PDU;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.Null;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.Snmp;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.TransportMapping;

public class SNMPWalk
{
  //
  // Command line format:
  //   java SNMPWalk targetAddress targetOID
  // EX:
  //   java SNMPWalk 192.168.76.15/161 1.3.6.1.4.1.517
  //
  public static void main(String[] args)
  {
    Address targetAddress = new UdpAddress(args[0]);
    OID targetOID         = new OID(args[1]);

    Logger.getRootLogger().setLevel(Level.OFF);

    PDU requestPDU = new PDU();
    requestPDU.add(new VariableBinding(targetOID));
    requestPDU.setType(PDU.GETNEXT);

    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString("public"));
    target.setAddress(targetAddress);
    target.setVersion(SnmpConstants.version1);

    try
    {
      TransportMapping transport = new DefaultUdpTransportMapping();
      Snmp snmp = new Snmp(transport);
      transport.listen();

      boolean finished = false;

      while (!finished)
      {
        VariableBinding vb = null;

        PDU responsePDU = snmp.sendPDU(requestPDU, target);
        if (responsePDU != null)
        {
          vb = responsePDU.get(0);
        }

        if (responsePDU == null)
        {
          System.out.println("responsePDU == null");
          finished = true;
        }
        else if (responsePDU.getErrorStatus() != 0)
        {
          System.out.println("responsePDU.getErrorStatus() != 0");
          System.out.println(responsePDU.getErrorStatusText());
          finished = true;
        }
        else if (vb.getOid() == null)
        {
          System.out.println("vb.getOid() == null");
          finished = true;
        }
        else if (vb.getOid().size() < targetOID.size())
        {
          System.out.println("vb.getOid().size() < targetOID.size()");
          finished = true;
        }
        else if (targetOID.leftMostCompare(targetOID.size(),
                                           vb.getOid()) != 0)
        {
          System.out.println("targetOID.leftMostCompare() != 0)");
          finished = true;
        }
        else if (Null.isExceptionSyntax(vb.getVariable().getSyntax()))
        {
          System.out.println(
          "Null.isExceptionSyntax(vb.getVariable().getSyntax())");
          finished = true;
        }
        else if (vb.getOid().compareTo(targetOID) <= 0)
        {
          System.out.println("Variable received is not "+
                       "lexicographic successor of requested "+
                       "one:");
          System.out.println(vb.toString() + " <= "+targetOID);
          finished = true;

        }
        else
        {
          // Dump response.
          System.out.println(vb.toString());

          // Set up the variable binding for the next entry.
          requestPDU.setRequestID(new Integer32(0));
          requestPDU.set(0, vb);
        }
      }

      snmp.close();

    }
    catch (IOException e)
    {
      System.out.println("IOException: "+e);
    }

  }

}
---------------------End Simple SNMP Walk




More information about the SNMP4J mailing list