天天看點

snmp4j 擷取主機磁盤資訊,由于磁盤大小超過Integer32長度産生錯誤。報錯内容

snmp4j 擷取主機磁盤資訊,由于磁盤大小超過Integer32長度産生錯誤。

  • 報錯内容
    • 源碼分析
    • 解決辦法

報錯内容

java.io.IOException: Length greater than 32bit are not supported for integers: at position 1030

at org.snmp4j.asn1.BER.decodeInteger(BER.java:695)

at org.snmp4j.smi.Integer32.decodeBER(Integer32.java:60)

at org.snmp4j.smi.AbstractVariable.createFromBER(AbstractVariable.java:173)

at org.snmp4j.smi.VariableBinding.decodeBER(VariableBinding.java:191)

at org.snmp4j.PDU.decodeBER(PDU.java:584)

at org.snmp4j.mp.MPv2c.prepareDataElements(MPv2c.java:201)

查詢到這個是snmp得bug

https://bugzilla.redhat.com/show_bug.cgi?id=654384

源碼分析

AbstractVariable

内聯代碼片

int type = inputStream.read();
....
variable = createVariable(type);
private static Variable createVariable(int smiSyntax) {
    switch (smiSyntax) {
      case SMIConstants.SYNTAX_OBJECT_IDENTIFIER: {
        return new OID();
      }
      case SMIConstants.SYNTAX_INTEGER: {
        return new Integer32();
      }
      case SMIConstants.SYNTAX_OCTET_STRING: {
        return new OctetString();
      }
   }
   }
           

發現在擷取int type = inputStream.read(); type為integer32類型,那麼當傳回資料超過2 (31)長度時,無法處理,并報出異常。

解決辦法

使用Counter64()實體類解析integer32得内容,嘗試已經可以使用,但需要注釋掉類型判斷部分。

public void decodeBER(BERInputStream inputStream) throws java.io.IOException {
    BER.MutableByte type = new BER.MutableByte();
    long newValue = BER.decodeUnsignedInt64(inputStream, type);
    if (type.getValue() != BER.COUNTER64) {
      throw new IOException("Wrong type encountered when decoding Counter64: " +
                            type.getValue());
    }
    setValue(newValue);
  }

           

以上為個人建議,這是第一次寫文章,如有錯誤請指正。