【问题标题】:How do I keep SNMP4J GETBULK from incrementing rightmost digit before OID?如何防止 SNMP4J GETBULK 在 OID 之前增加最右边的数字?
【发布时间】:2015-02-05 21:27:28
【问题描述】:

我正在使用这个 SNMP4J 代码来执行一些 SNMP walks。但是,当我在 1.3.6.1.2.1.31.1.1.1.1(即 ifName)上运行它时,它会获取由 1.3.6.1.2.1.31.1.1.1.1.x 表示的所有接口,但随后它还抓取 1.3.6.1.2.1.31.1.1.1.2,它们是 ifInMulticastPkts,有时是 1.3.6.1.2.1.31.1.1.1.3,它们是 ifInBroadcastPkts。我只对 ifName 感兴趣。

如何防止 GETBULK 在遍历 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;
}

【问题讨论】:

    标签: java snmp snmp4j


    【解决方案1】:

    您可能应该使用the getTable method of the TableUtils class 来获取您需要的列。这样一来,您就不必担心走桌子时协议行为的细节。

    也就是说,如果你想自己做......你发现的是 GetBulk 的正常行为。代理只负责返回您指定的行数 (200)(如果有的话)。如果您想要更多行,响应还将指示您应该请求的 OID。

    只有您作为经理才能知道您何时获得了所需的所有数据,并停止发送新的 GetBulk 请求。您应该简单地丢弃最后一个响应中不需要的数据。在您的情况下,检测 OID 是否不再是您请求的列的子项。

    您可以在RFC 1905 中阅读有关 SNMP 遍历和 GetBulk 工作原理的更多信息,尤其是第 4.2.3 和 4.2.3.1 部分。

    但请务必使用API​​提供的方法,它会为您节省一些白发,并且保证是正确的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-04
      • 1970-01-01
      相关资源
      最近更新 更多