Problem
网上搜索到的 Input Bandwidth 计算方法普遍采用的思路为取单位时间内的平均值,即 (第 [n+m] 秒的 ifHCInOctets 存量值 - 第 [n] 秒的 ifHCInOctets 存量值) / m,这个在网络设备更新 ifHCInOctets 值的频率和 m 秒(单位时间)存在差距的时候,就会出现异常的数值抖动和不准确。
Solution
每秒带宽 MIB
可用 Juniper 设备的 MIB 库查询到直接的每秒出入带宽,如下:
- ifIn1SecRate
- oid: 1.3.6.1.4.1.2636.3.3.1.1.1
- The number of bits per second (bps), delivered by this (sub-)layer to its next higher (sub-)layer.
- ifIn1SecOctets
- oid: 1.3.6.1.4.1.2636.3.3.1.1.2
- The number of octets per second (Bps, Bytes per second), delivered by this (sub-)layer to its next higher (sub-)layer.
- ifHCIn1SecRate
- oid: 1.3.6.1.4.1.2636.3.3.1.1.7
- The number of bits per second (bps), delivered by this (sub-)layer to its next higher (sub-)layer. This object is a 64 bit version of ifIn1SecRate.
- ifHCIn1SecOctets
- oid: 1.3.6.1.4.1.2636.3.3.1.1.34
- The number of bytes per second (Bps), delivered by this (sub-)layer to its next higher (sub-)layer. This object is a 64 bit version of ifIn1SecOctets.
Oid Sample with netsnmp Python Library
import netsnmp
#---------------------------------------------------------------------------------------------
# deviceip 设备 ip
# interface 端口 index 号
# * 形如以下:
# * Interface index: 166, SNMP ifIndex: 612
# * Logical interface et-2/3/0.0 (Index 353) (SNMP ifIndex 523)
# * 则,取 612 为参数 interface 的值
#---------------------------------------------------------------------------------------------
def getinfo(deviceip, interface):
"""get oid info"""
session = netsnmp.Session(Version = 3, DestHost = deviceip, SecLevel = authPriv, SecName = securityname, PrivProto = privacyprotocol, PrivPass = privacyphrase, AuthProto = authenticationprotocol, AuthPass = authenticationphrase)
varList = netsnmp.VarList(netsnmp.Varbind("ifDescr." + interface), netsnmp.Varbind(".1.3.6.1.4.1.2636.3.3.1.1.7." + interface), netsnmp.Varbind(".1.3.6.1.4.1.2636.3.3.1.1.8." + interface))
values = session.get(varList)
return values
Extends
Oid 数据类型
- Integer 整型
Signed 32bit Integer (values between -2147483648 and 2147483647). 有符号32位整数(值范围: -2147483648 - +2147483648) - Integer32
Same as Integer. 与Integer相同。 - UInteger32
Unsigned 32bit Integer (values between 0 and 4294967295). 无符号32位整数(值范围:0-4294967295). - Octet String
Arbitrary binary or textual data, typically limited to 255 characters in length. 任意二进制或文本数据,通常长度限制在255个字符内。 - Object Identifier
An OID. 一个OID。 - Bit String
Represents an enumeration of named bits. This is an unsigned datatype. 表示取名的位的枚举。这是一个无符号的数据类型。 - IpAddress
An IP address. 一个IP地址。 - Counter32
Represents a non-negative integer which monotonically increases until it reaches a maximum value of 32bits-1 (4294967295 dec), when it wraps around and starts increasing again from zero. 表示一个非负的整数(可递增到32位最大值-1),然后恢复并从0开始递增。 - Counter64
Same as Counter32 but has a maximum value of 64bits-1. 与Counter32相同,最大值为64位的最大值-1。 - Gauge32
Represents an unsigned integer, which may increase or decrease, but shall never exceed a maximum value. 表示无符号整数,可增加或减少,但是不超过最大值。 - TimeTicks
Represents an unsigned integer which represents the time, modulo 2ˆ32 (4294967296 dec), in hundredths of a second between two epochs. 表示代表数据的一个无符号整数,2^32取模(4294967296),两个值之间为百分之一秒。 - Opaque
Provided solely for backward-compatibility, its no longer used. 提供向下兼容,不再使用的数据类型 - NsapAddress
Represents an OSI address as a variable-length OCTET STRING. 表示一个用变长八进制字符窗表示的OSI地址。
不同 Oid 数据类型 Sample
SNMPv2-SMI::enterprises.2636.3.3.1.1.2.111 = Gauge32: 7571581
SNMPv2-SMI::enterprises.2636.3.3.1.1.7.111 = Counter64: 60572648
这里的 Gauge32, Counter64 其实就是说明对应着不同的数据类型,和编程语言中理解的没啥大概念上的不同。(便于理解所谓的 64 bit 是啥意思,其实就是简单的一种数据类型罢了)