ifInOctets and ifOutOctets 的类型是counter是一个32位的无符号数,

包含了框架位,目前还不清楚框架位是如何计算的。

流量比较大时采集几分钟流量就会出现溢出,SNMPv2版本中使用了64位数大概率避免了溢出之后取数T2比T1低,算出的流量使用情况成负值,但仍然会出现这种情况,加一个判断如果T2小,就用2^32-T1+T1即为该时间段中流量(其中32位中框架位仍按数据计算,影响不大)

 

from snmp_cmds import snmpwalk
import time

def Get_IntPercentage(IP,OID):
res1 = snmpwalk(ipaddress = IP,oid = OID, community = 'ssss')
T1 = int(res1[0][1])
time.sleep(10)
res2 = snmpwalk(ipaddress = IP,oid = OID, community = 'ssss')
T2 = int(res2[0][1])

If T2>T1:
return('percent: {:.0f}%'.format((T2-T1)*8/10/1000000000*100))

if T2<T1:

return('percent: {:.0f}%'.format((2^32-T1+T2)*8/10/1000000000*100))

print(Get_IntPercentage('10.1.110.28','IF-MIB:ifInOctets.8'))

 

print 即可,

然而Counter是32位的总会出现问题, 本以为Counter计数超过2的32次方之后以上面方法就可以了,但问题是时间长了可能会出现计数超过2的32次方两次三次就会出现问题,以上方法仍然存在问题,最终还是使用了SNMP V2版本的Counter64的计数器节约问题。

OID名称改为IF-MIB:ifHCInOctets.8即为SNMP v2版本Counter64的,基本不会溢出

----------------------------------------------2022----------------------

RFC1213定义了SNMP V1的字段:

IF-MIB:ifInOctets,解释为The total number of octets received on the interface, including framing characters.

RFC2863中SNMP v2版本定义的64位字段:

IF-MIB:ifHCInOctetsThe total number of octets received on the interface, including framing characters. This object is a 64-bit version of ifInOctets.Discontinuities in the value of this counter can occur at re-initialization of the management system, and at other times as indicated by the value of ifCounterDiscontinuityTime.

所以建议最终还是直接使用SNMP v2版本,就目前来看还没遇到过不支持v2的,上面之前使用v1的T2-T1的折中方法有点愚蠢的不堪过往了。。。。

以上

posted on 2020-07-07 09:51  千手白狐  阅读(1230)  评论(0编辑  收藏  举报