SNMP介绍, OID及MIB库_oid 库,2024年最新简单聊聊2024年物联网嵌入式开发开发的现状和思考

134 阅读6分钟

IMPORTS
enterprises, IpAddress, Gauge, TimeTicks FROM RFC1155-SMI
DisplayString FROM RFC1213-MIB
OBJECT-TYPE FROM RFC-1212
TRAP-TYPE FROM RFC-1215;

apc OBJECT IDENTIFIER ::= { enterprises 318 }
products OBJECT IDENTIFIER ::= { apc 1 }
apcmgmt OBJECT IDENTIFIER ::= { apc 2 }

Comments can be inserted into a MIB by prepending them with two dashes. In the header the declaration BEGIN starts off the MIB. Imports can be used to pull information from other MIBs, typically those mandated by the MIB-II standard.
可以用行开头为 -- 的方法在MIB中加入注释。
在头部用 BEGIN声明来开始MIB的定义。  Imports 可用来从其他MIB中提取信息,通常用它来提取MIB-II规范要求的内容。
MIB放置从enterprise值开始的OID地址的结构。在此,enterprise值是318, 对应 "apc" (相对地址为 .1.318)。 通常之后会定义几个类别。注意在花括号间定义的两个值,其父地址后面跟一个它自己的地址。因此产品标识符有apc标识符表示,其父为enterprise标识符,以此类推。类别和自类别的类型通常跟在MIB头的后面,并且把有用的键值分割为子组。通过分段,各种值分别被列出,这样可用的值更容易浏览。

MIB的真正好处在于对象类型的描述。以下是一个整形键值的例子:
upsBasicOutputStatus OBJECT-TYPE
SYNTAX INTEGER {
unknown(1),
onLine(2),
onBattery(3),
onSmartBoost(4),
timedSleeping(5),
softwareBypass(6),
off(7),
rebooting(8),
switchedBypass(9),
hardwareFailureBypass(10),
sleepingUntilPowerReturn(11),
onSmartTrim(12)
}
ACCESS read-only
STATUS mandatory
DESCRIPTION
"The current state of the UPS. If the UPS is unable
to determine the state of the UPS this variable is set
to unknown(1)."
::= { upsBasicOutput 1 }

    因此,在这里真正需要注意的是,MIB其实只是提供给我们一张我们想从某个SNMP代理中获得的可用OID的各种值的地图。一个MIB描述了在哪里找某个值、以及返回结果是什么。我们可以不用MIB与设备进行交互,只不过在理获得'Up'的返回值,要比‘1’的返回值要简单的多。通过利用Net-SNMP命令行工具,你可以决定你希望返回结果的输出样式(这种方式下使用“Up"这样的格式更好),或者你用脚本调用工具时(这种方式下使用”1“的格式就更好)。

 

 

2.3 OID数据类型

    SMI定义的OID返回值的数据类型。

    SMI定义了一定数量的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地址。 

 

 

 

Net-SNMP tools will report the datatype when returning an OID unless you otherwise disregard it. As an example of that you’ll see:

Net-SNMP工具在返回一个OID时会包括其数据类型,除非你不想要他。以下是一个例子:
SNMPv2-MIB::sysContact.0 = STRING: Ben Rockwood
IF-MIB::ifPhysAddress.1 = STRING: 0:c0:b7:63:ca:4c
SNMPv2-MIB::sysUpTime.0 = Timeticks: (47372422) 5 days, 11:35:24.22
IF-MIB::ifAdminStatus.1 = INTEGER: up(1)
SNMPv2-MIB::sysObjectID.0 = OID: SNMPv2-SMI::enterprises.318.1.3.7
RFC1213-MIB::atPhysAddress.1.1.10.10.1.1 = Hex-STRING: 00 50 73 28 47 A0
RFC1213-MIB::atNetAddress.1.1.10.10.1.1 = Network Address: 0A:0A:01:01
IF-MIB::ifSpeed.1 = Gauge32: 10000000
SNMPv2-MIB::snmpInPkts.0 = Counter32: 316
SNMPv2-MIB::snmpOutPkts.0 = Counter32: 314

 

 

 

 

 

这是一个相当典型的由Net-SNMP工具返回的数据类型表单。注意有些值已经由Net-SNMP自动解释了,比如sysUpTime和ifAdminStatus。这些值返回时使用到MIB,Net-SNMP会在MIBzhong找到返回值,并且为我们提供该值的文本表示。

 

 

 

 

 

 

 

3 实例

 

(1) MIB 结构

TEST-TRAP-MIB DEFINITIONS ::= BEGIN  

  
IMPORTS  
MODULE-IDENTITY, OBJECT-TYPE, TimeTicks FROM SNMPv2-SMI  
DisplayString, FROM SNMPv2-TC  
enterprises  
FROM RFC1155-SMI;  
test OBJECT IDENTIFIER ::= { enterprises 1000 }  
TestTraps OBJECT IDENTIFIER ::= { test 1 }  


cpuRatioHigh NOTIFICATION-TYPE  
OBJECTS  {SystemTrapDescription}  
STATUS  current  
DESCRIPTION  
"."  
::= { TestTraps 1 }      


TestDescription OBJECT IDENTIFIER ::= { TestTraps 2 }  

TestTrapDescription  OBJECT-TYPE  
SYNTAX  DisplayString (SIZE (0..256))  
MAX-ACCESS read-only  
STATUS current  
DESCRIPTION "  "  
::= { TestDescription 1 }  

END  

(2) 对应OID:

 const oid cpuRatioHigh_oid[] = { 1,3,6,1,4,1,1000,1,1 };  
const oid TestTrapDescription_oid[] = { 1,3,6,1,4,1,1000,1,2,1, 0 };  

某些唯一键值,可用多个实例表示,这样所有的OID都以实例值结尾。这就是为什么你得注意到大多数OID都是以一个 .0 结尾的。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

img img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上物联网嵌入式知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取