跳到主要内容

4. 定义对象

互联网标准 SMI 对对象定义采用两级方法. 一个 MIB 定义由两部分组成: 文本部分, 其中对象被归入各个组; 以及 MIB 模块, 其中对象仅用 SMI 定义的 ASN.1 宏 OBJECT-TYPE 来描述.

前一种定义的示例如下:

          OBJECT:
-------
sysLocation { system 6 }

Syntax:
DisplayString (SIZE (0..255))

Definition:
The physical location of this node (e.g., "telephone
closet, 3rd floor").

Access:
read-only.

Status:
mandatory.

后一种定义的示例如下:

               sysLocation OBJECT-TYPE
SYNTAX DisplayString (SIZE (0..255))

ACCESS read-only
STATUS mandatory
::= { system 6 }

为了简洁并减少编辑出错的机会, 将这两个定义合并起来似乎是有用的. 这可以通过为 OBJECT-TYPE 宏定义一个扩展来实现:

          IMPORTS
              ObjectName
                  FROM RFC1155-SMI
              DisplayString
                  FROM RFC1158-MIB;

          OBJECT-TYPE MACRO ::=
          BEGIN
              TYPE NOTATION ::=
                                          -- must conform to
                                          -- RFC1155's ObjectSyntax
                                "SYNTAX" type(ObjectSyntax)
                                "ACCESS" Access
                                "STATUS" Status
                                DescrPart
                                ReferPart
                                IndexPart
                                DefValPart
              VALUE NOTATION ::= value (VALUE ObjectName)

              Access ::= "read-only"
                              | "read-write"
                              | "write-only"
                              | "not-accessible"
              Status ::= "mandatory"
                              | "optional"
                              | "obsolete"
                              | "deprecated"

              DescrPart ::=
                         "DESCRIPTION" value (description DisplayString)
                              | empty

              ReferPart ::=
                         "REFERENCE" value (reference DisplayString)
                              | empty

              IndexPart ::=
                         "INDEX" "{" IndexTypes "}"

                              | empty
              IndexTypes ::=
                         IndexType | IndexTypes "," IndexType
              IndexType ::=
                                  -- if indexobject, use the SYNTAX
                                  -- value of the correspondent
                                  -- OBJECT-TYPE invocation
                         value (indexobject ObjectName)
                                  -- otherwise use named SMI type
                                  -- must conform to IndexSyntax below
                              | type (indextype)

              DefValPart ::=
                         "DEFVAL" "{" value (defvalue ObjectSyntax) "}"
                              | empty

          END

          IndexSyntax ::=
              CHOICE {
                  number
                      INTEGER (0..MAX),
                  string
                      OCTET STRING,
                  object
                      OBJECT IDENTIFIER,
                  address
                      NetworkAddress,
                  ipAddress
                      IpAddress
              }

4.1. OBJECT-TYPE 宏的映射​

应当注意, OBJECT-TYPE 宏的展开在概念上发生在实现期间, 而不是在运行期间.

4.1.1. SYNTAX 子句的映射​

SYNTAX 子句必须存在, 它定义与该对象类型相对应的抽象数据结构. 为此使用 ASN.1 语言 [6]. 然而, SMI 有意限制了可以使用的 ASN.1 构造. 这些限制是明确为了简单性而作出的.

4.1.2. ACCESS 子句的映射​

ACCESS 子句必须存在, 它定义该对象类型所需的最低支持级别. 作为本地事项, 实现可以支持其他访问类型 (例如, 实现可以选择允许写入标记为 read-only 的变量). 此外, 协议特定的 "视图" (例如由 SNMP community 间接蕴含的那些) 可能对变量的访问施加进一步限制.

4.1.3. STATUS 子句的映射​

STATUS 子句必须存在, 它定义该对象类型所需的实现支持.

4.1.4. DESCRIPTION 子句的映射​

DESCRIPTION 子句不必存在, 它包含该对象类型的文本定义, 提供实现所需的所有语义定义, 并且应当包含原本会在与该对象相关的 ASN.1 注释中传达的任何信息. 请注意, 为了符合 ASN.1 语法, 该子句的整个值必须用双引号括起来, 尽管该值可以是多行的.

此外请注意, 如果 MIB 模块中其他地方不包含该对象类型的文本描述, 那么 DESCRIPTION 子句必须存在.

4.1.5. REFERENCE 子句的映射​

REFERENCE 子句不必存在, 它包含指向在其他某个 MIB 模块中定义的对象的文本交叉引用. 这在去 OSI 化由其他组织生成的 MIB 时很有用.

4.1.6. INDEX 子句的映射​

INDEX 子句仅当该对象类型对应于概念行时才可以存在, 它定义该对象类型的实例标识信息. (历史上, 每个 MIB 定义都包含一个题为 "Identification of OBJECT instances for use with the SNMP" 的小节. 通过使用 INDEX 子句, 该小节不再需要出现, 因为该子句简洁地捕获了实例标识所需的精确语义.)

如果 INDEX 子句不存在, 且该对象类型对应于非列对象, 那么该对象的实例通过在该对象名称后附加一个为零的子标识符来标识. 此外请注意, 如果 MIB 模块不包含关于如何为列对象导出实例标识信息的文本描述, 那么 INDEX 子句必须存在.

为了定义实例标识信息, 要确定哪些对象值能够无歧义地区分一个概念行. 这些对象的语法指示了如何构成实例标识符:

  1. 整数值: 一个取该整数值的单一子标识符 (这仅适用于非负整数);
  2. 字符串值, 定长字符串: 'n' 个子标识符, 其中 'n' 是字符串的长度 (字符串的每个八位组编码在一个单独的子标识符中);
  3. 字符串值, 变长字符串: 'n+1' 个子标识符, 其中 'n' 是字符串的长度 (第一个子标识符是 'n' 本身, 其后字符串的每个八位组编码在一个单独的子标识符中);
  4. 对象标识符值: 'n+1' 个子标识符, 其中 'n' 是该值中子标识符的数目 (第一个子标识符是 'n' 本身, 其后复制该值中的每个子标识符);
  5. NetworkAddress 值: 'n+1' 个子标识符, 其中 'n' 取决于所编码地址的种类 (第一个子标识符指示地址的种类, 值 1 表示 IpAddress); 或者,
  6. IpAddress 值: 4 个子标识符, 采用熟悉的 a.b.c.d 记法.

请注意, 如果存在 "indextype" 值 (例如用 INTEGER 而不是 ifIndex), 那么 DESCRIPTION 子句必须存在; 其中包含的文本指示 "indextype" 值的语义.

举例来说, 在 MIB-II [7] 的上下文中, 可能存在以下 INDEX 子句:

                 objects under         INDEX clause
----------------- ------------
ifEntry { ifIndex }
atEntry { atNetIfIndex,
atNetAddress }
ipAddrEntry { ipAdEntAddr }
ipRouteEntry { ipRouteDest }
ipNetToMediaEntry { ipNetToMediaIfIndex,
ipNetToMediaNetAddress }
tcpConnEntry { tcpConnLocalAddress,
tcpConnLocalPort,
tcpConnRemoteAddress,
tcpConnRemotePort }
udpEntry { udpLocalAddress,
udpLocalPort }
egpNeighEntry { egpNeighAddr }

4.1.7. DEFVAL 子句的映射​

DEFVAL 子句不必存在, 它定义一个可接受的默认值, 当代理按照上文第 4.2 节所述的第三种范式自行决定创建对象实例时, 可以使用该值.

在概念行创建期间, 如果某个列对象的实例没有作为相应 SNMP set 操作的操作数之一出现, 那么 DEFVAL 子句的值 (如果存在) 指示代理可以使用的可接受默认值.

当然, DEFVAL 子句的值必须与该对象的 SYNTAX 子句相对应. 请注意, 如果 SNMP set 操作的某个操作数是 read-only 对象的实例, 那么将返回错误 noSuchName. 因此, DEFVAL 子句可用于提供代理可以使用的可接受默认值.

对于允许创建新对象实例的概念行中的任何列对象, 有可能不存在可接受的默认值. 在这种情况下, INDEX 子句中指定的对象必须具有 read-write 的对应 ACCESS 子句值.

举例来说, 考虑以下可能的 DEFVAL 子句:

       ObjectSyntax            DEFVAL clause
----------------- ------------
INTEGER 1 -- same for Counter, Gauge, TimeTicks
OCTET STRING 'ffffffffffff'h
DisplayString "any NVT ASCII string"
OBJECT IDENTIFIER sysDescr
OBJECT IDENTIFIER { system 2 }
NULL NULL
NetworkAddress { internet 'c0210415'h }
IpAddress 'c0210415'h -- 192.33.4.21

4.1.8. OBJECT-TYPE 值的映射​

OBJECT-TYPE 宏调用的值就是该对象的名称, 它是一个对象标识符.

4.2. 用法示例​

考虑 MIB-II 中的 ipNetToMediaTable 可以如何完整地描述:

          -- the IP Address Translation tables

          -- The Address Translation tables contain IpAddress to
          -- "physical" address equivalences.  Some interfaces do not
          -- use translation tables for determining address equivalences
          -- (e.g., DDN-X.25 has an algorithmic method); if all
          -- interfaces are of this type, then the Address Translation
          -- table is empty, i.e., has zero entries.

          ipNetToMediaTable OBJECT-TYPE
              SYNTAX  SEQUENCE OF IpNetToMediaEntry
              ACCESS  not-accessible
              STATUS  mandatory
              DESCRIPTION
                      "The IP Address Translation table used for mapping
                      from IP addresses to physical addresses."
              ::= { ip 22 }

          ipNetToMediaEntry OBJECT-TYPE
              SYNTAX  IpNetToMediaEntry
              ACCESS  not-accessible
              STATUS  mandatory
              DESCRIPTION
                      "Each entry contains one IpAddress to 'physical'

                      address equivalence."
              INDEX   { ipNetToMediaIfIndex,
                        ipNetToMediaNetAddress }
              ::= { ipNetToMediaTable 1 }

          IpNetToMediaEntry ::=
              SEQUENCE {
                  ipNetToMediaIfIndex
                      INTEGER,
                  ipNetToMediaPhysAddress
                      OCTET STRING,
                  ipNetToMediaNetAddress
                      IpAddress,
                  ipNetoToMediaType
                      INTEGER
              }

          ipNetToMediaIfIndex OBJECT-TYPE
              SYNTAX  INTEGER
              ACCESS  read-write
              STATUS  mandatory
              DESCRIPTION
                      "The interface on which this entry's equivalence
                      is effective.  The interface identified by a
                      particular value of this index is the same
                      interface as identified by the same value of
                      ifIndex."
              ::= { ipNetToMediaEntry 1 }

          ipNetToMediaPhysAddress OBJECT-TYPE
              SYNTAX  OCTET STRING
              ACCESS  read-write
              STATUS  mandatory
              DESCRIPTION
                      "The media-dependent 'physical' address."
              ::= { ipNetToMediaEntry 2 }

          ipNetToMediaNetAddress OBJECT-TYPE
              SYNTAX  IpAddress
              ACCESS  read-write
              STATUS  mandatory
              DESCRIPTION
                      "The IpAddress corresponding to the media-
                      dependent 'physical' address."
              ::= { ipNetToMediaEntry 3 }

          ipNetToMediaType OBJECT-TYPE
              SYNTAX  INTEGER {

                          other(1),   -- none of the following
                          invalid(2), -- an invalidated mapping
                          dynamic(3),
                          static(4)
                      }
              ACCESS  read-write
              STATUS  mandatory
              DESCRIPTION
                      "The type of mapping.

                      Setting this object to the value invalid(2) has
                      the effect of invalidating the corresponding entry
                      in the ipNetToMediaTable.  That is, it effectively
                      disassociates the interface identified with said
                      entry from the mapping identified with said entry.
                      It is an implementation-specific matter as to
                      whether the agent removes an invalidated entry
                      from the table.  Accordingly, management stations
                      must be prepared to receive tabular information
                      from agents that corresponds to entries not
                      currently in use.  Proper interpretation of such
                      entries requires examination of the relevant
                      ipNetToMediaType object."
                  ::= { ipNetToMediaEntry 4 }