2015-02-05 126 views
3

我正在使用此SNMP4J代码来执行一些SNMP操作。但是,当我运行它,如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感兴趣。如何让SNMP4J GETBULK在OID之前递增最右边的数字?

如何防止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; 
} 

回答

4

您应该使用the getTable method of the TableUtils class来获取所需的列。这样,您不必担心行走表格时协议行为的细节。

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

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

您可以在RFC 1905,特别是4.2.3节和4.2.3.1节中详细了解SNMP步行和GetBulk的工作方式。

但是使用API​​提供的方法,它会为您节省一些灰色头发,并保证是正确的。