简体   繁体   中英

How do I keep SNMP4J GETBULK from incrementing rightmost digit before OID?

I'm using this SNMP4J code to do some SNMP walks. But, when I run it on say, 1.3.6.1.2.1.31.1.1.1.1 which is ifName, it gets all the interfaces which are represented by 1.3.6.1.2.1.31.1.1.1.1.x, but then it also grabs 1.3.6.1.2.1.31.1.1.1.2 which are ifInMulticastPkts and then sometimes 1.3.6.1.2.1.31.1.1.1.3 which are ifInBroadcastPkts. I'm only interested in ifName.

How do keep the GETBULK from incrementing the last digit before traversing the MIB?

public ArrayList<String> walk(String oid) throws IOException, InterruptedException {
    Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
    snmp.listen();

    Address targetAddress = GenericAddress.parse(address);
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString(community));
    target.setVersion(SnmpConstants.version2c);
    target.setAddress(targetAddress);
    target.setTimeout(3000);    //3s
    target.setRetries(1);

    PDU pdu = new PDU();
    pdu.setType(PDU.GETBULK);
    pdu.setMaxRepetitions(200);
    pdu.setNonRepeaters(0);
    pdu.add(new VariableBinding(new OID(oid)));

    ResponseEvent responseEvent = snmp.send(pdu, target);
    PDU response = responseEvent.getResponse();

    ArrayList<String> responsePieces = new ArrayList<String>();
    if (response == null) {
        System.out.println("TimeOut...");
    }
    else
    {
        if (response.getErrorStatus() == PDU.noError)
        {
            Vector<? extends VariableBinding> vbs = response.getVariableBindings();
            for (VariableBinding vb : vbs) {
                responsePieces.add(vb.toString());
            }
        }
        else
        {
            System.out.println("Error:" + response.getErrorStatusText());
        }
    }
    return responsePieces;
}

You should probably be using the getTable method of the TableUtils class to fetch the column(s) you need. That way, you don't have to worry about the nitty-gritty details of how the protocol behaves when walking a table.

That said, if you want to do it yourself... What you've discovered is the normal behavior of GetBulk. The Agent is only responsible for returning as many rows as you specified (200), if it has them. The response will also indicate what OID you should request if you want more rows.

It is only you as a manager who can tell when you've got all the data you want, and stop sending new GetBulk requests. You should simply discard the unwanted data in the last response. In your case, detect whether an OID is no longer a child of the column you requested.

You can read more about how SNMP walking and GetBulk work, in RFC 1905 , particularly sections 4.2.3 and 4.2.3.1.

But do use the method provided by the API, it will save you some gray hairs, and is guaranteed to be correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM