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 是啥意思,其實就是簡單的一種數據類型罷了)