Difference between revisions of "SNMP MIB Implementation"

From OpenCircuits
Jump to navigation Jump to search
m (Reverted edits by LLykins51 (Talk) to last revision by 1.55.122.57)
 
(43 intermediate revisions by 7 users not shown)
Line 4: Line 4:
 
# Create a ASN.1 MIB script '''foo.mib''' (an ASCII text file) for the tree structure.
 
# Create a ASN.1 MIB script '''foo.mib''' (an ASCII text file) for the tree structure.
 
# Convert foo.mib to binary file using '''mib2bin'''
 
# Convert foo.mib to binary file using '''mib2bin'''
# Build [http://www.rane.com/note161.html snmp's PDU use BER (Base encoding rules)] encoder and decoder library to process data that's transfer between NMS and agents.
+
# Build [http://www.rane.com/note161.html SNMP's PDU using BER (Base encoding rules)] encoder and decoder library to process data that's transfer between NMS and agents.
# Build snmp API use [http://www.sics.se/~adam/uip/index.php/Main_Page uIP-stack] to communicate between NMS and Agents (open two ports: The manager speak to agents on one port, the agent responds manager on the other port).
+
# Build SNMP API use [http://www.sics.se/~adam/uip/index.php/Main_Page uIP-stack] to communicate between NMS and Agents (open two ports: The manager speak to agents on one port, the agent responds manager on the other port).
 
# Build binary MIB file reader library.
 
# Build binary MIB file reader library.
 
# Build functions service oid tree.
 
# Build functions service oid tree.
Line 14: Line 14:
 
* Build MIB file's written in [http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf ANS.1 notation].
 
* Build MIB file's written in [http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf ANS.1 notation].
 
* Tutorial: [http://www.scribd.com/doc/276412/Understanding-SNMP-Stack Understanding SNMP Stack] to create ASN.1 MIB Script.
 
* Tutorial: [http://www.scribd.com/doc/276412/Understanding-SNMP-Stack Understanding SNMP Stack] to create ASN.1 MIB Script.
* Tools to create ASN.1 MIB Script <font color=red>Which tool are you using now?</font>:
+
* Tools to create ASN.1 MIB Script
** [http://www.asnlab.com/asndt/installing.html ASN.1 Editor plugin for Eclipse].
+
** [http://www.asnlab.com/asndt/installing.html ASN.1 Editor plugin for Eclipse] (Now using).
 
** [http://packages.ubuntu.com/karmic/asn1-mode Emacs mode for editing ASN.1 files].
 
** [http://packages.ubuntu.com/karmic/asn1-mode Emacs mode for editing ASN.1 files].
  
Line 24: Line 24:
  
 
==Convert MIB to Binary File==
 
==Convert MIB to Binary File==
* mib2bin tool is modified from [http://net-snmp.sourceforge.net/ net-snmp] to convert ASN.1 format file to three files, because the microchip '''mib2bib''' converter only supports upto 255 OIDs.
+
* mib2bin tool is modified from [http://net-snmp.sourceforge.net/ net-snmp] to convert ASN.1 format file to four files, because the microchip '''mib2bib''' converter only supports upto 255 OIDs.
 
                                 MIB compiler tools: '''mib2bin'''
 
                                 MIB compiler tools: '''mib2bin'''
   [http://www.modtronix.com/products/sbc44ec/00870a.pdf foo.mib] (ANS.1 format)  -----------------------------------> foo.bin + foo.h + foo_data.h
+
   [http://www.modtronix.com/products/sbc44ec/00870a.pdf foo.mib] (ANS.1 format)  -----------------------------------> foo.bin + foo_trap.bin + foo.h + foo_data.h
  
 
* Syntax to use mib2bin tool:
 
* Syntax to use mib2bin tool:
 
   '''mib2bin <MIBfile>...
 
   '''mib2bin <MIBfile>...
 
* where MIBfile file is ASN.1 format file. MIBfile = <name>.<type>
 
* where MIBfile file is ASN.1 format file. MIBfile = <name>.<type>
** '''<name>.bin''' is the binary file storing information of OID tree. This file can be placed on an SD media card to be read by the FAT16 file system.
+
** '''<name>.bin''' is the binary file storing information of OID tree. This file can be placed on an SD media card to be read by the FAT32 file system.
 +
** '''<name>_trap.bin''' is the binary file storing information of TRAP of MIB. This file can be placed on an SD media card to be read by the FAT32 file system.
 
** '''<name>_data.h''' is C header file storing information of OID tree.  This file is generated by converting mchip.bin file to the C header file. It's only used when a system don't have system file and place on program memory.
 
** '''<name>_data.h''' is C header file storing information of OID tree.  This file is generated by converting mchip.bin file to the C header file. It's only used when a system don't have system file and place on program memory.
 
** '''<name>.h''' is C header file storing ID that's reference to function service of OID.
 
** '''<name>.h''' is C header file storing ID that's reference to function service of OID.
 
* Note:  
 
* Note:  
 
** Subfolder '''mibs''' containing the basics MIB files (e.g.: RFC1155-SMI, RFC1213-MIB, RFC-1215, SNMPv2-MIB ... for us MIB file), must be present under the directory of execution.
 
** Subfolder '''mibs''' containing the basics MIB files (e.g.: RFC1155-SMI, RFC1213-MIB, RFC-1215, SNMPv2-MIB ... for us MIB file), must be present under the directory of execution.
** If the three files exist, mib2bin tool will overwrite the files.
+
** If the four files exist, mib2bin tool will overwrite the files.
  
===Binary File Format===
+
===Binary File Format <name>.bin===
 
* The binary file is an image of MIB file. It is generated by mib2bin tool. Agents will read binary file to respond NMS request.
 
* The binary file is an image of MIB file. It is generated by mib2bin tool. Agents will read binary file to respond NMS request.
* In the binary file, A parent is stored first, followed by its last-child to first-child. Next, the structure of next this parent is stored. This structure is repeated until the entire tree is stored.
+
* In the binary file, A parent is stored first, followed by its first-child to last-child. Next, the structure of next this parent is stored. This structure is repeated until the entire tree is stored.
 
* A parent or child is a record. Single record of binary file have format:
 
* A parent or child is a record. Single record of binary file have format:
  <oid>, <nodeInfo>,
 
  [id], [siblingOffset], [distantSiblingOffset], [dataType], [dataLen], [data],
 
  [{<IndexNumber>},{<IndexCount>, <IndexNodeInfo>, <IndexDataType>} ...]
 
 
<font color=red>Since [siblingOffset] and [distantSiblingOffset] can be combined to 1 field, can I represent the above as:
 
 
   <oid>, <nodeInfo>,  
 
   <oid>, <nodeInfo>,  
 
   [id], [siblingOffset]/[distantSiblingOffset], [dataType], [dataLen], [data],  
 
   [id], [siblingOffset]/[distantSiblingOffset], [dataType], [dataLen], [data],  
   [{<IndexNumber>},{<IndexCount>, <IndexNodeInfo>, <IndexDataType>} ...]</font>
+
   [{<IndexNumber>}, {<IndexCount>, <IndexNodeInfo>, <IndexDataType>} ...]
  where:
+
* where:
  fields indicated by angle brackets (< >) are always present
+
** fields indicated by angle brackets (< >) are always present
  fields in square brackets ([ ]) are optional depending on characteristics of the current node.
+
** fields in square brackets ([ ]) are optional depending on characteristics of the current node.
  fields in braces ({}) are optional but always occur together.
+
** fields in braces ({ }) are optional but always occur together.
  
 
====<oid> field====
 
====<oid> field====
 +
* The microchip format only supports OIDs upto 255. The following is an workaround to store OID greater than 255.
 
* Format of OID:
 
* Format of OID:
   oid1 char(16)
+
   An OID is a series of (one or more) octets (e.g. 04 01 82 99 5D). Bit 8 of each octet indicates whether it is the last octet in the series:  
  oid2 char(16)
+
  If bit 8 of is zero, it is the last octet (e.g. 04, 01, 5D), otherwise, bit 8 is set to one (e.g. 82, 99).
  ....
+
 
* if oid_i < 255: oid_i = char (oid_i)
+
  Bits 7 to 1 of the octets are used to encode the OID. Conceptually, these bits are concatenated to form an unsigned binary number whose most
* else oid_i = char(256) + char(length of oid_i base 256) + oid_i (base 256) + char(16)
+
  significant bit (bit 7) of the first octet and the least significant bit (bit 1) of the last octet. The OID shall be encoded in the fewest
* where
+
  possible octets, that is, the leading octet of the OID shall not have the value 0x80.
** char(x) that's mean get ascii of x.
 
** char(16) is used to separate two oids.
 
** char(255) is determined that oid geater than 254.
 
** "length of oid_i" is length string of oid_i that's converted base 256.
 
* The microchip format only supports OIDs upto 255. The above is an workaround to store OID greater than 255
 
 
* Example:
 
* Example:
     The OID for a node is '''.4.1.17095.''' After compiling to binary, the OID is represented as:
+
     The OID               encode of OID in binary file (hex)
       The OID:      <font color=blue>4</font>  .    <font color=blue>1</font>  .              <font color=blue>17095</font>    .
+
       4                       BYTE(0x04)
      The result: <font color=blue>0x04</font> 0x10 <font color=blue>0x10</font> 0x10 <font color=green>0xFF</font> <font color=red>0x02</font> <font color=blue>0xC7 0x42</font> 0x10
+
       1                        BYTE(0x01)
       where:
+
    36061                      BYTE(0x80 + 0x02) BYTE(0x80 + 0x19) BYTE(0x5D)
          <font color=blue>oid_1 = 4 </font> => <font color=blue> char(oid_1) = 0x04</font>
+
     The OID = 36061 is decoded by 0x02*0x80*0x80 + 0x19*0x80 + 0x5D
          <font color=blue>oid_2 = 2 </font> => <font color=blue> char(oid_2) = 0x01</font>
 
 
 
<font color=grey>In the example, i use the hexadecimal to easier to read. When i write to a file oid_1 = 4 will be fourth character in ASCII table (isn't the hexadecimal representation of 4). So i use char(oid_i), where: oid_i is number 4, isn't character '4'. Therefore, char(oid_i) = oid_i.</font>
 
 
 
<font color=red>In my concept, people usually refer to ASCII code when they have some human readable characters (i.e. printable characters) to represent. It is kind of weird to me the the OID will be in the format oid1.oid2.oid3.... where oid_i may take some non-printable characters such as the 4th ASCII character (end of transmission). Also, a number such as 17095th of ASCII is out of my comprehension. Can you give me some reference material discussing this issue?</font>
 
 
 
          <font color=blue>oid_3 = 17095 </font> => <font color=green>char(256) = 0xFF</font> <font color=red>char(length of oid_3 base 256) = 0x02</font> <font color=blue> char(oid_3) = 0xC742 (In little-endian format)</font>
 
          char(16) = 0x10
 
     Note: When a node have no sibling and only has one child, the node's OID will be merged with its child's OID (reduce data's stored).
 
    For example, in case of '''private (4) -> enterprises (1) -> microchip (17095)''', where '''private''' has no sibling and only has one child,
 
    and '''enterprises''' also has no sibling and only has one child, they can combined to a record has oid field is 4.1.17095. <font color=red>Am I right?</font>
 
  
 
====<nodeInfo> field====
 
====<nodeInfo> field====
 
* information of node
 
* information of node
 
   bit        when (set = 1)
 
   bit        when (set = 1)
   0          Node is a parent (0: node is a leaf)
+
   0          Node has sibling node
   1          Node is sequence
+
   1          Node has default data
   2          Node is readable
+
   2          Node is sequence
   3          Node is writable
+
   3          Node is readable
   4          Node is able to create
+
   4          Node is a parent
   5          Node has default data
+
   5          Node is writable
   6          (if node is sequence, this is mean implied index node)
+
   6          Node is able to modify
   7          always set 1
+
   7          Node has sibling field (in IndexNodeInfo this bit indicate that Indexes is imply)
  
 
====[id] field====
 
====[id] field====
Line 114: Line 95:
 
   so it's same value.
 
   so it's same value.
  
====[dataType], [dataLen], [data] fields====
+
====[dataType] field====
* If this record is a leaf and has default data
+
*If this record is a leaf
 
** [dataType] is type of leaf's data.
 
** [dataType] is type of leaf's data.
** [dataLen] is length of data.
 
** [data] is data on string.
 
 
* The tool supports the following base data types defined in SNMPv1:
 
* The tool supports the following base data types defined in SNMPv1:
 
** '''INTEGER''': The integer data type is a signed integer in the range of -2,147,483,648 to 2,147,483,647.
 
** '''INTEGER''': The integer data type is a signed integer in the range of -2,147,483,648 to 2,147,483,647.
Line 130: Line 109:
 
** '''Opaque''': An arbitrary encoding that is used to pass arbitrary information strings that do not conform to the strict data typing used by the mib.
 
** '''Opaque''': An arbitrary encoding that is used to pass arbitrary information strings that do not conform to the strict data typing used by the mib.
 
** '''SEQUENCE''': An ordered list of objects, somewhat like a struct in the C language. Type of objects in sequence is same type of node.
 
** '''SEQUENCE''': An ordered list of objects, somewhat like a struct in the C language. Type of objects in sequence is same type of node.
 +
 +
====[dataLen], [data] fields====
 +
* If this record is a leaf and has default data
 +
** [dataLen] is length of data.
 +
** [data] is data on string.
  
 
====[{<IndexNumber>}] and [{<IndexCount>, <IndexNodeInfo>, <IndexDataType>}] fields====
 
====[{<IndexNumber>}] and [{<IndexCount>, <IndexNodeInfo>, <IndexDataType>}] fields====
 
* If this record is sequence (an order list of objects),  
 
* If this record is sequence (an order list of objects),  
 
** <IndexNumber> is the number of INDEXes in sequence.
 
** <IndexNumber> is the number of INDEXes in sequence.
** <IndexCount>: is oid of index node in table
+
** <IndexCount>: is id of index node in table
 
** <IndexNodeInfo>: is info of index node
 
** <IndexNodeInfo>: is info of index node
 
** <IndexDataType>: is data type of index node
 
** <IndexDataType>: is data type of index node
 +
* See [http://www.simpleweb.org/w/images/9/91/Tutorial_Slides_Smi.pdf example] of accessing data in a table
 
* Example:  
 
* Example:  
 
** '''trap''' node is a sequence to inform the NMS of a significant event (an extraordinary event has occurred at an agent) asynchronously. This sequence has two INDEXes, so we have:
 
** '''trap''' node is a sequence to inform the NMS of a significant event (an extraordinary event has occurred at an agent) asynchronously. This sequence has two INDEXes, so we have:
 
       <IndexNumber> = 0x02
 
       <IndexNumber> = 0x02
 
       with the 1st INDEX:
 
       with the 1st INDEX:
           <IndexCount> = 0x04
+
           <IndexCount> = 0x05
           <IndexNodeInfo> = 0xCC
+
           <IndexNodeInfo> = 0x28
 
           <IndexDataType> = 0x02
 
           <IndexDataType> = 0x02
 
       with the 2nd INDEX:
 
       with the 2nd INDEX:
           <IndexCount> = 0x01
+
           <IndexCount> = 0x08
           <IndexNodeInfo> = 0x80
+
           <IndexNodeInfo> = 0xA4
           <IndexDataType> = 0x03
+
           <IndexDataType> = 0x04
   In this example, '''trap''' is a table which has 4 columns <font color=red>(4 leaf nodes?)</font>:  
+
   In this example, '''trap''' is a table which has 4 columns:  
 
       <font color=blue>trapReceiverNumber (1)</font>, trapEnabled (2), trapReceiverIPAddress (3), <font color=blue>trapCommunity(4)</font>.  
 
       <font color=blue>trapReceiverNumber (1)</font>, trapEnabled (2), trapReceiverIPAddress (3), <font color=blue>trapCommunity(4)</font>.  
 
       <font color=blue>trapReceiverNumber (1)</font>, trapEnabled (2), trapReceiverIPAddress (3), <font color=blue>trapCommunity(4)</font>.  
 
       <font color=blue>trapReceiverNumber (1)</font>, trapEnabled (2), trapReceiverIPAddress (3), <font color=blue>trapCommunity(4)</font>.  
 
       <font color=blue>trapReceiverNumber (1)</font>, trapEnabled (2), trapReceiverIPAddress (3), <font color=blue>trapCommunity(4)</font>.  
 
       <font color=blue>trapReceiverNumber (1)</font>, trapEnabled (2), trapReceiverIPAddress (3), <font color=blue>trapCommunity(4)</font>.  
   Each significant event will be a row in the table <font color=red>(which table?)</font>.
+
   Each significant event will be a row defined in the '''trap''' table.  
  So, we need an index node to determine the row number. A table needs at least one index.  
 
 
    
 
    
 
   This example has two INDEXes: the 1st INDEX node is trapCommunity (4) and the 2nd INDEX node trapReceiverNumber (1)  
 
   This example has two INDEXes: the 1st INDEX node is trapCommunity (4) and the 2nd INDEX node trapReceiverNumber (1)  
  <font color=red>Why these two are INDEXes, and not trapReceiverIPAddress or trapEnabled? Is it because they takes unique values?</font>.
 
  In fact, we need only trapReceiverNumber (1) INDEX to determine a row in this example (two INDEXes case is used to test complier only).
 
 
 
 
   Each INDEX is a node, so it has OID, info, data type.
 
   Each INDEX is a node, so it has OID, info, data type.
   The 1st INDEX node is trapCommunity, which has OID = 4, info is 0xCC and data type is INTEGER (0x02). so
+
   The 1st INDEX node is trapCommunity, which has id = 5, info is 0x28 and data type is INTEGER (0x02). so
 
           IndexCount = 0x04
 
           IndexCount = 0x04
           IndexNodeInfo = 0xCC
+
           IndexNodeInfo = 0x28
 
           IndexDataType = 0x02
 
           IndexDataType = 0x02
   The 2nd INDEX node is trapReceiverNumber, which has OID = 1, info is 0x80 and data type is DisplayString (0x03). so
+
   The 2nd INDEX node is trapReceiverNumber, which has id = 8, info is 0xA4 and data type is DisplayString (0x04). so
 
           IndexCount = 0x01
 
           IndexCount = 0x01
           IndexNodeInfo = 0x80
+
           IndexNodeInfo = 0xA4
           IndexDataType = 0x03
+
           IndexDataType = 0x04
 +
 
 +
===Binary File Format <name>_trap.bin===
 +
* The binary file store TRAP information. It is generated by mib2bin tool. Agents will read binary file to get information of TRAP when something bad occurs.
 +
* In the binary file, An enterprise OID is stored first, followed by its first specific trap number and IDs of first specific trap (those IDs matche with IDs of leaf in <name>.bin; IDs are reference to OIDs of data bindings that be included in this trap) to last specific trap number and IDs of last specific trap. Next, the structure of next this enterprise OID is stored. This structure is repeated until all enterprise OID in MIB file is stored.
 +
* The format of enterprise in <name>_trap.bin
 +
  <enterprise_oid><sibling_enterprise><enterprise_index>[specific_trap_number][sibling_specific_trap][number_varbinds][id_varbind]...
 +
* where:
 +
** fields indicated by angle brackets (< >) are always present.
 +
** fields in square brackets ([ ]) are optional depending on characteristics of the trap.
 +
 
 +
====<enterprise_oid> field====
 +
* Enterprise oid is full oid of enterprise trap that want to send in MIB file.
 +
* The format of enterprise oid.
 +
  <sub_oid> <info_sub_oid> ...
 +
* where:
 +
**  <font color=red><sub_oid>:</font> is same format of <oid> field in MIB file.
 +
**  <font color=red><info_sub_oid>:</font> Information of sub_oid.
 +
  <info_sub_oid> format:
 +
      bit        when (set = 1)
 +
      0          the first sub_oid in enterprise
 +
      1          no use
 +
      2          no use
 +
      3          no use
 +
      4          sub_oid is a parent
 +
      5          no use
 +
      6          no use
 +
      7          the last sub_oid in enterprise
 +
* If <sub_oid> == BYTE (0x00) and <info_sub_oid> == BYTE(0xFF), this is the end of infomation traps.
 +
 
 +
====<sibling_enterprise> field====
 +
* Point to next enterprise OID.
 +
* In little-endian format.
 +
====<enterprise_index> field====
 +
* Index of enterprise OID traps in <name>_trap.bin file.
 +
* Size of <enterprise_index> is 1 byte.
 +
====[specific_trap_number] field====
 +
* If the trap is sent is specific trap, specific trap number is a number indicting specific trap.
 +
* specific trap number is integer in little-endian format.
 +
====[sibling_specific_trap] field====
 +
* Point to next specific trap.
 +
* In little-endian format.
 +
====[number_varbinds] field====
 +
* A number of data bindings to be included in the specific trap.
 +
* Size of [number_varbinds] is 1 byte.
 +
====[id_varbind] field====
 +
* [id_varbind] define data bindings (OID and value of OID leaf) that is included specific trap.
 +
* [id_varbind] is reference to id of the leaf oid in <name>.bin.
 +
* In little-endian format.
  
 
===Example===
 
===Example===
Line 175: Line 204:
 
[[File:Snmp mib oid tree example.jpg | thumb | center | 900px]]
 
[[File:Snmp mib oid tree example.jpg | thumb | center | 900px]]
  
====Binary File====
+
====Binary File <name>.bin====
* The corresponding binary file is mchip.bin and it has data describe in  table below:
+
* The corresponding binary file is <name>.bin and it has data describe in  table below:
 
** '''oid''' is <oid> fields.
 
** '''oid''' is <oid> fields.
 
** '''info''' is <nodeInfo> fields.
 
** '''info''' is <nodeInfo> fields.
 
** '''dist''' is [distantSiblingOffset]/[siblingOffset] fields.
 
** '''dist''' is [distantSiblingOffset]/[siblingOffset] fields.
 
** '''id''' is [id] fields.
 
** '''id''' is [id] fields.
** '''defval''' is data fields, include [dataType], [dataLen], [data] fields.
+
** '''type''' is [dataType] fields.
 
** '''index''' is index fields of sequence, include [{index_number} , {<IndexCount>, <IndexNodeInfo>, <indexDataType>, ...}].
 
** '''index''' is index fields of sequence, include [{index_number} , {<IndexCount>, <IndexNodeInfo>, <indexDataType>, ...}].
  
 
[[File:Snmp mib binary example.jpg | thumb | center | 900px]]
 
[[File:Snmp mib binary example.jpg | thumb | center | 900px]]
  
* The detail description of mchip.bin is illustrated in the diagram below:
+
* The detail description of <name>.bin is illustrated in the diagram below:
** The arrow lines represent the OID tree structure.
+
** The black arrow lines represent the OID tree structure.
** The red and green lines represents pointers of data.
+
** The red and green lines represent pointers to data.
 
** The dist field points to next sibling record. After parent record is it's children.
 
** The dist field points to next sibling record. After parent record is it's children.
<font color=red>I can understand the relationship given by the green arrows, but how about the red arrows? For example, why do '''product''' and '''name''' point to '''experimental''', and etc.?</font>
+
[[File:Snmp mib binary detail example.jpg | thumb | center | 900px]]
 +
 
 +
====Binary File <name>_trap.bin====
 +
* The corresponding binary file is <name>_trap.bin and it has data describe in  table below:
 +
** '''enterp''' is <enterprise_oid> fields.
 +
** '''sibl_ep''' is <sibling_enterprise> fields.
 +
** '''index_ep''' is <enterprise_index> fields.
 +
** '''spec_id''' is [specific_trap_number] fields.
 +
** '''sibl_sid''' is [sibling_specific_trap] fields.
 +
** '''num_var''' is [number_varbinds] fields.
 +
** '''id_var''' is [id_varbind] fields.
 +
[[File:Snmp mib binary trap example.jpg | thumb | center | 900px]]
  
[[File:Snmp mib binary detail example.jpg | thumb | center | 900px]]
+
==[http://www.rane.com/note161.html=Build SNMP's PDU using BER]==
 +
* SNMP is the protocol that allows communicate between NMS and agents by exchanging SNMP messages. the SNMP message is a single field, of the Sequence type. SNMP message use data types specified by ASN.1 and use Basic Encoding Rules (BER) to encode data. The entire SNMP message is a Sequence of three smaller fields: the SNMP Version (Integer), the SNMP Community String (Octet String), and the SNMP PDU.
 +
* The SNMP's PDU is reference to SNMP version 1 (SNMPv1) PDU.
 +
* SNMPv1 PDU have five different PDU types:GetRequest,  GetNextRequest, GetResponse, SetRequest and Trap.
 +
* Get Request, GetNext Request, Get Response, Set Request are same format PDU. Trap use other format PDU.
 +
===ASN.1 data types===
 +
* ASN.1 data types fall into two categories: primitive and complex.
 +
* ASN.1 data types is used to build SNMP messages.
 +
** ASN.1 primitive data types include Integer, Octet (byte, character) String, Null, Boolean and Object Identifier.
 +
** ASN.1 complex data types are used to build SNMP message are Sequence type, Sequence-of type.
 +
 
 +
===[http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBgQFjAA&url=http%3A%2F%2Fwww.itu.int%2FITU-T%2Fstudygroups%2Fcom17%2Flanguages%2FX.690-0207.pdf&rct=j&q=x.690-0207&ei=cde_TcrvJouMvQOm5-jABA&usg=AFQjCNGYmD4USBwcWoeHoRFZ3zdP39kosw&cad=rja=Basic Encoding Rules (BER)]===
 +
* BER has three parts: Type, Length and Data field.
 +
  BER format:
 +
      +------+--------+------+
 +
      | Type | Length | Data |
 +
      +------+--------+------+
 +
 
 +
* Type field is single byte identifier.
 +
  '''Constructing byte Data type.'''
 +
  Format of byte Data type
 +
      +--+-+-----+
 +
      |  | |    +
 +
      +--+-+-----+
 +
      2  1  5
 +
  Bits 8 and 7 shall be encoded to represent the class of Data type follow table:
 +
      +------------------+-------+-------+
 +
      |      Class      | Bit 8 | Bit 7 |
 +
      +------------------+-------+-------+
 +
      | Universal        |  0  |  0  |
 +
      | Application      |  0  |  1  |
 +
      | Context-specific |  1  |  0  |
 +
      | Private          |  1  |  1  |
 +
      +------------------+-------+-------+
 +
  Bit 6 is encoded data is Primitive or Constructed follow table:
 +
      +-------+-------------+
 +
      | Bit 6 |  Type      |
 +
      +-------+-------------+
 +
      |  0  | Primitive  |
 +
      |  1  | Constructed |
 +
      +-------+-------------+
 +
  Bits 5 to 1 encode the number of Data type as a integer number.   
 +
 
 +
  '''Data type identifier in SNMP'''
 +
      '''Data type'''                '''Identifier'''      '''Note'''
 +
      Integer                      0x02        Primitive ASN.1 types
 +
      Octet String                0x04        Primitive ASN.1 types 
 +
      Null                        0x05        Primitive ASN.1 types
 +
      Object identifier            0x06        Primitive ASN.1 types
 +
      Sequence                    0x30        Constructed ASN.1 types
 +
      IpAddress                    0x40        Primitive SNMP application types
 +
      Counter                      0x41        Primitive SNMP application types
 +
      Gauge                        0x42        Primitive SNMP application types
 +
      TimeTicks                    0x43        Primitive SNMP application types
 +
      Opaque                      0x44        Primitive SNMP application types
 +
      NsapAddress                  0x45        Primitive SNMP application types
 +
      GetRequest PDU              0xA0        Context-specific Constructed SNMP types
 +
      GetNextRequest PDU          0xA1        Context-specific Constructed SNMP types
 +
      GetResponse PDU              0xA2        Context-specific Constructed SNMP types
 +
      SetRequest PDU              0xA3        Context-specific Constructed SNMP types
 +
      Trap PDU                    0xA4        Context-specific Constructed SNMP types
 +
* Length field is the number of bytes in Data field.
 +
** Length field is used either the short form or the long form as a option depend on Data field.
 +
*** The short form, Length field is a single octet in which bit 8 is zero and bits 7 to 1 encode the number of bytes in Data field, as an unsigned binary integer with bit 7 as the most significant bit.
 +
*** The long form, Length field shall consists of an initial octet and  one or more subsequent octets.
 +
**** The initial octet is encoded as follows:
 +
***** Bit 8 shall be one.
 +
***** Bits 7 to 1 shall encode the number of subsequent octets in the length field, as an unsigned binary integer with bit 7 as the most significant bit.
 +
***** The value 0xFF shall not be used.
 +
**** Subsequent octets:
 +
***** From the first subsequent octet to the last subsequent octet, shall be the encoding of an unsigned binary integer equal to the number bytes in Data field, with bit 8 of the first subsequent octet as the most significant bit.
 +
* Data field is actual data content.
 +
 
 +
* Example:
 +
  Actual Data is an integer, the value 100 can be encode as:
 +
  The short form:
 +
      +------+--------+-------+
 +
      | Type | Length | Data  |
 +
      +------+--------+-------+
 +
      | 0x02 |  0x01  | 0x64  |
 +
      +------+--------+-------+
 +
  The long form:
 +
      +------+-----------+-------+
 +
      | Type |  Length  | Data  |
 +
      +------+-----------+-------+
 +
      | 0x02 | 0x81 0x01 | 0x64  |
 +
      +------+-----------+-------+
 +
===SNMP Message Format===
 +
* SNMP Message is a Sequence of three smaller fields: the SNMP Version (Integer), the SNMP Community String (Octet String), and the SNMP PDU.
 +
  SNMP Message Format use BER.
 +
      +------------------------------------------------------------------------+
 +
      |                      SNMP Message (Sequence type)                      |
 +
      +------+----------------+------------------------------------------------+
 +
      | Type | Length of Data |                      Data                      |
 +
      +------+----------------+-------------+-----------------------+----------+
 +
      | 0x30 |    Length    |SNMP Version | SNMP Community String | SNMP PDU |                     
 +
      |      |                |  (Integer)  |    (Octet String)    |          |
 +
      +------+----------------+-------------+-----------------------+----------+
 +
                              |<--------------------Length-------------------->|
 +
* Length is bytes of Data field (SNMP Version, SNMP Community String and SNMP PDU).
 +
* SNMP Version is an integer that identifies the version of SNMP, SNMP version 1 = 0.
 +
* SNMP Community String is an Octet String to add security to Agents.
 +
* SNMP PDU is a SNMP verion 1 (SNMPv1) PDU.
 +
 
 +
===SNMP PDU Format===
 +
* SNMP PDU is is reference to SNMP version 1 (SNMPv1) PDU.
 +
====GetRequest PDU, GetNextRequest PDU, GetResponse PDU, SetRequest PDU Format====
 +
* GetRequest PDU, GetNextRequest PDU, GetResponse PDU, SetRequest PDU Format is shown here.
 +
  +-------------------------------------------------------------------------------------------------------------------------------------+
 +
  |                              SNMPv1 PDU (GetRequest, GetNextRequest, GetResponse, SetRequest                                      |
 +
  +------+--------+---------------------------------------------------------------------------------------------------------------------+
 +
  | PDU  | Length |                                            Data of SNMP PDU                                                        |
 +
  | type |  Data  |                                                                                                                    |
 +
  +------+--------+---------+---------+---------+---------------------------------------------------------------------------------------+
 +
  | PDU  | Length | Request | Error  | Error  |                            VarBind List (Sequence)                                  |
 +
  | type |  PDU  |  ID    | Status  | Index  +------+--------+--------------------------------+--------------------------------+-----+
 +
  |      |        |        |        |        | 0x30 | Length |          Varbind 1            |          Varbind 2            | ... |
 +
  |      |        |(Integer)|(Integer)|(Integer)|      |        |          (Sequence)            |          (Sequence)            | ... |
 +
  |      |        |        |        |        |      |        +------+-------+-------+---------+------+-------+-------+---------+-----+
 +
  |      |        |        |        |        |      |        | 0x30 | Len 1 | OID 1 | Value 1 | 0x30 | Len 2 | OID 1 | Value 2 | ... |
 +
  +------+--------+---------+---------+---------+------+--------+------+-------+-------+---------+------+-------+-------+---------+-----+
 +
                  |                                            |              |<-----Len 1----->|              |<-----Len 2----->|    |
 +
                  |                                            |<-------------------------------Length-------------------------------->|
 +
                  |<----------------------------------------------Lenght PDU----------------------------------------------------------->|
 +
* PDU Type is specific type of PDU, PDU Type is a single byte identifier.
 +
* Length Data is a number bytes of Data of SNMP PDU field.
 +
* Data of SNMP PDU is data content in SNMP PDU.
 +
* Request ID is an Integer that identifies a particular SNMP request. This index is echoed back in the response from the SNMP agent, allowing the SNMP manager to match an incoming response to the appropriate request.
 +
* Error Status is an Integer set to 0x00 in the request sent by the NMS. The SNMP agent places an error code in this field in the response message if an error occurred processing the request.
 +
  Some error codes include:
 +
  +-------------------+---------------------------------------------------------------------------+
 +
  |    Error Status  |                                                                          |
 +
  +------------+------+                                Description                                |
 +
  |    Name    | Code |                                                                          |
 +
  +------------+------+---------------------------------------------------------------------------+
 +
  | noError    | 0x00 | No error occurred.                                                        |
 +
  | tooBig    | 0x01 | The response to your request was too big to fit into one response.        |
 +
  | noSuchName | 0x02 | The OID in the request was not found.the OID doesn't exist.              |
 +
  | badValue  | 0x03 | A data type in the request did not match the data type in the SNMP agent. |
 +
  | readOnly  | 0x04 | The SNMP manager attempted to set a read-only parameter.                  |
 +
  | genErr    | 0x05 | General Error (some error other than the ones listed above).              |
 +
  +------------+------+---------------------------------------------------------------------------+
 +
* Error Index is an Integer. Only the response operation sets this field, if an Error occurs, the Error Index holds a pointer to the Object that caused the error. Other operations set this field to zero.
 +
* Varbind List is a Sequence of Varbinds.
 +
** Varbind is a Sequence of two fields, an OID and the value of OID.
 +
*** OID is an Object Identifier that points to a particular parameter in the SNMP agent. The first two object identifier components in the object identifier value being encoded, using the formula: <font color=red>(X*40) + Y</font>; where X is the value of the first object identifier component and Y is the value of the second object identifier component. The first two object identifier components recognizes that only three values are allocated from the root node, and at most 39 subsequent values from nodes reached by X = 0 and X = 1.
 +
*** Value is the value of OID.
 +
  +--------------------+-----------------------------------------------------------------+
 +
  |    SNMP PDU type  |                            Value                                |
 +
  +--------------------+-----------------------------------------------------------------+
 +
  | SetRequest PDU    | Value is applied to the specified OID of the SNMP agent.        |
 +
  | GetRequest PDU    | Value is a Null that acts as a placeholder for the return data. |
 +
  | GetNextRequest PDU | Value is a Null that acts as a placeholder for the return data. |
 +
  | GetResponse PDU    | The returned Value from the specified OID of the SNMP agent.    |
 +
  +--------------------+-----------------------------------------------------------------+
 +
====[http://docstore.mik.ua/orelly/networking_2ndEd/snmp/ch10_03.htm= Trap PDU Format]====
 +
* The format of the Trap PDU is shown below:
 +
  +------------------------------------------------------------------------------------------------------------------------------------------+
 +
  |                                                        TRAP SNMPv1 PDU                                                                  |
 +
  +----+------+------------------------------------------------------------------------------------------------------------------------------+
 +
  |PDU |Length|                                            Data of SNMP PDU                                                                |
 +
  |type| Data |                                                                                                                              |
 +
  +----+------+----------+-------+-------+--------+------+---------------------------------------------------- ------------------------------+
 +
  |0xA4|Length|Enterprise| Agent |Generic|Specific|Time  |                                VarBind List (Sequence)                            |
 +
  |    | PDU  |  OID    |Address| Trap  |  Trap  |Stamp +------+--------+------------------------------+------------------------------+-----+
 +
  |    |      |          |      | Type  | Number |      | 0x30 | Length |          Varbind 1          |        Varbind 22          | ... |
 +
  |    |      |  (OID)  |      |      |        |      |      |        |          (Sequence)          |        (Sequence)          | ... |
 +
  |    |      |          |      |      |        |      |      |        +----+-------+-------+---------+----+-------+-------+---------+-----+
 +
  |    |      |          |      |      |        |      |      |        |0x30| Len 1 | OID 1 | Value 1 |0x30| Len 2 | OID 1 | Value 2 | ... |
 +
  +----+------+----------+-------+-------+--------+------+------+--------+----+-------+-------+---------+----+-------+-------+---------+-----+
 +
                          |                                              |            |<-----Len 1----->|            |<-----Len 2----->|    |
 +
                          |                                              |<-----------------------------Length------------------------------>|
 +
                          |<--------------------------------------------Lenght PDU----------------------------------------------------------->|
 +
* PDU type is TRAP PDU = 0xA4.
 +
* Length Data is a number bytes of Data of SNMP PDU field.
 +
* Enterprise OID identifies the type of managed object generating the trap.
 +
* Agent Address is the IP address of the agent that is sending the trap.
 +
* [http://docstore.mik.ua/orelly/networking_2ndEd/snmp/ch02_06.htm#enettdg-CHP-2-TABLE-8= Generic Trap Type] indicates one of a number of generic trap types.
 +
  Generic Trap Type has seven values are defined:
 +
  +------------------------------+---------------------------------------------------------------------------+
 +
  |      Generic Trap Type      |                                                                          |
 +
  +-----------------------+------+                                Description                                |
 +
  |        Name          | Code |                                                                          |
 +
  +-----------------------+------+---------------------------------------------------------------------------+
 +
  | coldStart            | 0x00 | Indicates that the agent has rebooted. All management variables will be  |
 +
  |                      |      | reset; specifically, Counters and Gauges will be reset to zero (0).      |
 +
  +-----------------------+------+---------------------------------------------------------------------------+
 +
  | warmStart            | 0x01 | Indicates that the agent has reinitialized itself. None of the management |
 +
  |                      |      | variables will be reset.                                                  |
 +
  +-----------------------+------+---------------------------------------------------------------------------+
 +
  | linkDown              | 0x02 | Sent when an interface on a device goes down. The first variable binding  |
 +
  |                      |      | identifies which interface went down.                                    |
 +
  +-----------------------+------+---------------------------------------------------------------------------+
 +
  | linkUp                | 0x03 | Sent when an interface on a device comes back up. The first variable      |
 +
  |                      |      | binding identifies which interface came back up.                          |
 +
  +-----------------------+------+---------------------------------------------------------------------------+
 +
  | authenticationFailure | 0x04 | Indicates that someone has tried to query your agent with an incorrect    |
 +
  |                      |      | community string; useful in determining if someone is trying to gain      |
 +
  |                      |      | unauthorized access to one of your devices.                              |
 +
  +-----------------------+------+---------------------------------------------------------------------------+
 +
  | egpNeighborLoss      | 0x05 | Indicates that an Exterior Gateway Protocol (EGP) neighbor has gone down. |
 +
  +-----------------------+------+---------------------------------------------------------------------------+
 +
  | enterpriseSpecific    | 0x06 | Indicates that the trap is enterprise-specific. SNMP vendors and users    |
 +
  |                      |      | define their own traps under the private-enterprise branch of the SMI    |
 +
  |                      |      | object tree. To process this trap properly, the NMS has to decode the    |
 +
  |                      |      | specific trap number that is part of the SNMP message.                    |
 +
  +-----------------------+------+---------------------------------------------------------------------------+
 +
* Specific trap number is a number indicating the specific trap you want to send. If you're sending a generic trap, this parameter is ignored -- you're probably better off setting it to zero.
 +
* Time stamp is the time elapsed between the last initialization of the network entity and the generation of the trap.
 +
* Varbind List is a Sequence of Varbinds to be included in the trap.
 +
** Varbind is a Sequence of two fields, an OID and the value of OID. The OIDs for these variable bindings are often specific to the trap and therefore "underneath" the specific OID for the trap. But this isn't a requirement, and it's often useful to send bindings that aren't defined as part of the trap.
 +
*** OID is an Object Identifier that points to a particular parameter in the SNMP agent.
 +
*** Value is the value of OID
 +
 
 +
===Example SNMP Message===
 +
====GetRequest PDU====
 +
* The example gets the value of name agent with above MIB file.
 +
* Community String is "public"
 +
  +---------+-----------+--------------------------------------------------------------------------------------------+-------------------+
 +
  | SNMP    | Type      |                                              0x30                                        | Sequence          |
 +
  | message +-----------+--------------------------------------------------------------------------------------------+-------------------+
 +
  |        |Length    |                                              0x2B                                        | Length: 43        |
 +
  |        +-----------+--------------------------------------------------------------------------------------------+-------------------+
 +
  |        | Version  |                                              0x02                                        | Integer          |
 +
  |        |          |                                              0x01                                        | Length: 1        |
 +
  |        |          |                                              0x00                                        | Value: 0          |
 +
  |         +-----------+--------------------------------------------------------------------------------------------+-------------------+
 +
  |        | Community |                                              0x04                                        | Octet String      |
 +
  |        |          |                                              0x06                                        | Length: 6        |
 +
  |        |          |                                              0x70 0x75 0x62 0x6C 0x69 0x63                | Value: public    |
 +
  |        +-----------+ -------+--------------+--------------------------------------------------------------------+-------------------+
 +
  |        | Data      | SNMPv1 | PDU type    |                      0xA0                                        | GetRequest PDU    |
 +
  |        |          | PDU    +--------------+--------------------------------------------------------------------+-------------------+
 +
  |        |          |        | PDU length  |                      0x1E                                        | Length: 30        |
 +
  |        |          |        + -------------+--------------------------------------------------------------------+-------------------+
 +
  |        |          |        | Request ID  |                      0x02                                        | Integer          |
 +
  |        |          |        |              |                      0x04                                        | Length: 4        |
 +
  |        |          |        |              |                      0x37 0x9C 0x57 0x89                          | Value:            |
 +
  |        |          |        + -------------+--------------------------------------------------------------------+-------------------+
 +
  |        |          |        | Error Status |                      0x02                                        | Integer          |
 +
  |        |          |        |              |                      0x01                                        | Length: 1        |
 +
  |        |          |        |              |                      0x00                                        | Value: 0          |
 +
  |        |          |        + -------------+--------------------------------------------------------------------+-------------------+
 +
  |        |          |        | Error Index  |                      0x02                                        | Integer          |
 +
  |        |          |        |              |                      0x01                                        | Length: 1        |
 +
  |        |          |        |              |                      0x00                                        | Value: 0          |
 +
  |        |          |        + -------------+--------------------------------------------------------------------+-------------------+
 +
  |        |          |        | VarBind List |                      0x30                                        | Sequence          |
 +
  |        |          |        |              +-----------+--------------------------------------------------------+-------------------+
 +
  |        |          |        |              | Length    |          0x10                                        | Length: 16        |
 +
  |        |          |        |              +-----------+--------------------------------------------------------+ ------------------+
 +
  |        |          |        |              | VarBind 1 |          0x30                                        | Sequence          |
 +
  |        |          |        |              |          +---------+----------------------------------------------+-------------------+
 +
  |        |          |        |              |          | Len 1  | 0x0E                                        | Length: 14        |
 +
  |        |          |        |              |          +---------+----------------------------------------------+-------------------+
 +
  |        |          |        |              |          | OID 1  | 0x06                                        | Object identifier |
 +
  |        |          |        |              |          |        | 0x0A                                        | Length: 10        |
 +
  |        |          |        |              |          |        | 0x2B 0x06 0x01 0x04 0x01 0x82 0x99 0x5D 0x02 | Value:            |
 +
  |        |          |        |              |          +---------+----------------------------------------------+-------------------+
 +
  |        |          |        |              |          | Value 1 | 0x05                                        | NULL              |
 +
  |        |          |        |              |          |        | 0x00                                        | Length: 0        |
 +
  +---------+-----------+--------+--------------+-----------+---------+----------------------------------------------+-------------------+
 +
====TRAP PDU====
 +
* The example TRAP above MIB file.
 +
* Community String is "public"
 +
* Agent's IPAddress is 192.168.1.128
 +
* Timestamp is 123456
 +
* The operMasterControl OID is in TRAP that has value is 1
 +
  +---------+-----------+-------------------------------------------------------------------------------------------------------+-------------------+
 +
  | SNMP    | Type      |                                          0x30                                                        |Sequence          |
 +
  | message +-----------+-------------------------------------------------------------------------------------------------------+-------------------+
 +
  |        | Length    |                                          0x82 0x00 0x44                                              |Length: 68        |
 +
  |        +-----------+-------------------------------------------------------------------------------------------------------+-------------------+
 +
  |        | Version  |                                          0x02                                                        |Integer            |
 +
  |        |          |                                          0x01                                                        |Length: 1          |
 +
  |        |          |                                          0x00                                                        |Value: 0          |
 +
  |        +-----------+-------------------------------------------------------------------------------------------------------+-------------------+
 +
  |        | Community |                                          0x04                                                        |Octet String      |
 +
  |        |          |                                          0x06                                                        |Length: 6          |
 +
  |        |          |                                          0x70 0x75 0x62 0x6C 0x69 0x63                              |Value: public      |
 +
  |        +-----------+ -------+------------+---------------------------------------------------------------------------------+-------------------+
 +
  |        | Data      | SNMPv1 | PDU type  |                    0xA4                                                        |TRAP PDU          |
 +
  |        |          | PDU    +------------+---------------------------------------------------------------------------------+-------------------+
 +
  |        |          |        | PDU length |                    0x82 0x00 0x35                                              |Length: 53        |
 +
  |        |          |        + -----------+---------------------------------------------------------------------------------+-------------------+
 +
  |        |          |        | Enterprise |                    0x06                                                        |Object identifier  |
 +
  |        |          |        | OID        |                    0x09                                                        |Length: 9          |
 +
  |        |          |        |            |                    0x2B 0x06 0x01 0x04 0x01 0x82 0x99 0x5D 0x00                |Value:            |
 +
  |        |          |        + -----------+---------------------------------------------------------------------------------+-------------------+
 +
  |        |          |        | Agent      |                    0x40                                                        |IpAddress          |
 +
  |        |          |        | Address    |                    0x04                                                        |Length: 4          |
 +
  |        |          |        |            |                    0xC0 0xA8 0x01 0x80                                        |Value:192.168.1.128|
 +
  |        |          |        + -----------+---------------------------------------------------------------------------------+-------------------+
 +
  |        |          |        | Generic    |                    0x02                                                        |Integer            |
 +
  |        |          |        | Trap Type  |                    0x01                                                        |Length: 1          |
 +
  |        |          |        |            |                    0x06                                                        |Value: 6          |
 +
  |        |          |        + -----------+---------------------------------------------------------------------------------+-------------------+
 +
  |        |          |        | Specific  |                    0x02                                                        |Integer            |
 +
  |        |          |        | Trap      |                    0x01                                                        |Length: 1          |
 +
  |        |          |        | Number    |                    0x01                                                        |Value: 1          |
 +
  |        |          |        + -----------+---------------------------------------------------------------------------------+-------------------+
 +
  |        |          |        | Time      |                    0x43                                                        |TimeTicks          |
 +
  |        |          |        | Stamp      |                    0x03                                                        |Length: 3          |
 +
  |        |          |        |            |                    0x01 0xE2 0x40                                              |Value: 123456      |
 +
  |        |          |        + -----------+---------------------------------------------------------------------------------+-------------------+
 +
  |        |          |        | VarBind    |                    0x30                                                        |Sequence          |
 +
  |        |          |        | List      +-----------+---------------------------------------------------------------------+-------------------+
 +
  |        |          |        |            | Length    |        0x82 0x00 0x15                                              |Length: 21        |
 +
  |        |          |        |            +-----------+---------------------------------------------------------------------+ ------------------+
 +
  |        |          |        |            | VarBind 1 |        0x30                                                        |Sequence          |
 +
  |        |          |        |            |          +-------+-------------------------------------------------------------+-------------------+
 +
  |        |          |        |            |          | Len 1 | 0x82 0x00 0x11                                              |Length: 17        |
 +
  |        |          |        |            |          +-------+-------------------------------------------------------------+-------------------+
 +
  |        |          |        |            |          | OID 1 | 0x06                                                        |Object identifier  |
 +
  |        |          |        |            |          |      | 0x0C                                                        |Length: 12        |
 +
  |        |          |        |            |          |      | 0x2B 0x06 0x01 0x04 0x01 0x82 0x99 0x5D 0x03 0x01 0x01 0x01 |Value:            |
 +
  |        |          |        |            |          +-------+-------------------------------------------------------------+-------------------+
 +
  |        |          |        |            |          | Value | 0x02                                                        |Integer            |
 +
  |        |          |        |            |          | 1    | 0x01                                                        |Length: 1          |
 +
  |        |          |        |            |          |      | 0x01                                                        |Length: 1          |
 +
  +---------+-----------+--------+------------+-----------+-------+-------------------------------------------------------------+-------------------+
 +
==[http://docstore.mik.ua/orelly/networking_2ndEd/snmp/ch02_06.htm= SNMP operations]==
 +
* NMS uses GetRequest command to retrieve the value of objects from agents. Objects that is defined by object identifier in MIB file. It is possible for an NMS to get more than one object at a time. The agent receives the request and processes it. After that, it sends GetResponse back to NMS with current value of objects.
 +
* NMS uses GetNextRequest command to discover available objects and the value of these objects from agents. The GetNextRequest command traverses a subtree in lexicographic order. Based on the OIDs in GetNextRequest PDU, it's easy for an agent to start at the root of its SMI object tree and work its way down until it finds the OID it is looking for. After the agent found avaible objects, it sends GetResponse back to NMS with current value of objects.
 +
* NMS uses SetRequest command to change the value of objects from agents. After the agent processes SetRequest, it send GetResponse back to NMS with new value of objects (value of objects that get from SetRequest).
 +
* The agent sends Trap to NMS when agent's something bad operation conditions. No repspone is sent from the NMS to the agent when NMS receives Trap.
 +
* Example SNMP operations with net-snmp:
 +
  '''GetRequest command:'''
 +
  >> snmpget -v 1 -c public 192.168.1.128 1.3.6.1.4.1.36061.1.1
 +
  This command will send GetRequest to the agent (IP address of agent is 192.186.1.128). The SNMP is SNMPv1 (the option ''-v 1'') with community
 +
  string is public (the option ''-c public''). In above demo MIB file, 1.3.6.1.4.1.36061.1.1 is OID reference to devID object.
 +
  The effect of this command will get the value of agent's devID .
 +
  '''GetNextRequest command:'''
 +
  >> snmpgetnext -v 1 -c public 192.168.1.128 1.3.6.1.4.1.36061.1
 +
  This command will send GetNextRequest to the agent (IP address of agent is 192.186.1.128). The SNMP is SNMPv1 (the option ''-v 1'') with community
 +
  string is public (the option ''-c public''). In above demo MIB file, 1.3.6.1.4.1.36061.1 is OID.
 +
  The effect of this command will get the value of agent's devID.
 +
  '''SetRequest command:'''
 +
  >> snmpset -v 1 -c public 192.168.1.128 1.3.6.1.4.1.36061.1.1 s "Hello"
 +
  This command will send SetRequest to the agent (IP address of agent is 192.186.1.128). The SNMP is SNMPv1 (the option ''-v 1'') with community
 +
  string is public (the option ''-c public''). In above demo MIB file, 1.3.6.1.4.1.36061.1.1 is OID reference to devID object.
 +
  The effect of this command will set the value of agent's devID to "Hello".
 +
  '''Receiving Traps'''
 +
  To receive Traps, snmptrapd.conf file must be configured and snmptrapd must be running.
 +
  The easiest way to configure this is by adding this line to snmptrapd.conf file:
 +
      disableAuthorization yes
 +
  The following command will receive and view trap
 +
  >>snmptrapd -f -Le

Latest revision as of 06:28, 27 September 2011

This wiki describe how to generate a MIB (Management Information Base) for SNMP agent.

Steps[edit]

  1. Create a ASN.1 MIB script foo.mib (an ASCII text file) for the tree structure.
  2. Convert foo.mib to binary file using mib2bin
  3. Build SNMP's PDU using BER (Base encoding rules) encoder and decoder library to process data that's transfer between NMS and agents.
  4. Build SNMP API use uIP-stack to communicate between NMS and Agents (open two ports: The manager speak to agents on one port, the agent responds manager on the other port).
  5. Build binary MIB file reader library.
  6. Build functions service oid tree.
  7. Merge MIB ANS.1 file to NMS.


Create ASN.1 MIB Script[edit]

Abstract Syntax Notation[edit]

  • Each MIB variable contains several attributes, such as data type, access type and object identifier.
  • Abstract Syntax Notation version 1 (ASN.1) is a language to define these attributes in SNMP.


Convert MIB to Binary File[edit]

  • mib2bin tool is modified from net-snmp to convert ASN.1 format file to four files, because the microchip mib2bib converter only supports upto 255 OIDs.
                                MIB compiler tools: mib2bin
 foo.mib (ANS.1 format)   -----------------------------------> foo.bin + foo_trap.bin + foo.h + foo_data.h
  • Syntax to use mib2bin tool:
  mib2bin <MIBfile>...
  • where MIBfile file is ASN.1 format file. MIBfile = <name>.<type>
    • <name>.bin is the binary file storing information of OID tree. This file can be placed on an SD media card to be read by the FAT32 file system.
    • <name>_trap.bin is the binary file storing information of TRAP of MIB. This file can be placed on an SD media card to be read by the FAT32 file system.
    • <name>_data.h is C header file storing information of OID tree. This file is generated by converting mchip.bin file to the C header file. It's only used when a system don't have system file and place on program memory.
    • <name>.h is C header file storing ID that's reference to function service of OID.
  • Note:
    • Subfolder mibs containing the basics MIB files (e.g.: RFC1155-SMI, RFC1213-MIB, RFC-1215, SNMPv2-MIB ... for us MIB file), must be present under the directory of execution.
    • If the four files exist, mib2bin tool will overwrite the files.

Binary File Format <name>.bin[edit]

  • The binary file is an image of MIB file. It is generated by mib2bin tool. Agents will read binary file to respond NMS request.
  • In the binary file, A parent is stored first, followed by its first-child to last-child. Next, the structure of next this parent is stored. This structure is repeated until the entire tree is stored.
  • A parent or child is a record. Single record of binary file have format:
 <oid>, <nodeInfo>, 
 [id], [siblingOffset]/[distantSiblingOffset], [dataType], [dataLen], [data], 
 [{<IndexNumber>}, {<IndexCount>, <IndexNodeInfo>, <IndexDataType>} ...]
  • where:
    • fields indicated by angle brackets (< >) are always present
    • fields in square brackets ([ ]) are optional depending on characteristics of the current node.
    • fields in braces ({ }) are optional but always occur together.

<oid> field[edit]

  • The microchip format only supports OIDs upto 255. The following is an workaround to store OID greater than 255.
  • Format of OID:
  An OID is a series of (one or more) octets (e.g. 04 01 82 99 5D). Bit 8 of each octet indicates whether it is the last octet in the series: 
  If bit 8 of is zero, it is the last octet (e.g. 04, 01, 5D), otherwise, bit 8 is set to one (e.g. 82, 99).
  
  Bits 7 to 1 of the octets are used to encode the OID. Conceptually, these bits are concatenated to form an unsigned binary number whose most 
  significant bit (bit 7) of the first octet and the least significant bit (bit 1) of the last octet. The OID shall be encoded in the fewest 
  possible octets, that is, the leading octet of the OID shall not have the value 0x80.
  • Example:
   The OID               encode of OID in binary file (hex)
      4                        BYTE(0x04)
      1                        BYTE(0x01)
    36061                      BYTE(0x80 + 0x02) BYTE(0x80 + 0x19) BYTE(0x5D)
   The OID = 36061 is decoded by 0x02*0x80*0x80 + 0x19*0x80 + 0x5D

<nodeInfo> field[edit]

  • information of node
  bit        when (set = 1)
  0          Node has sibling node
  1          Node has default data
  2          Node is sequence 
  3          Node is readable
  4          Node is a parent
  5          Node is writable 
  6          Node is able to modify
  7          Node has sibling field (in IndexNodeInfo this bit indicate that Indexes is imply)

[id] field[edit]

  • If this record is leaf, id that's reference to function services the record.

[distantSiblingOffset] field[edit]

  • If this record is a node [distantSiblingOffset] is enabled. Point to next node sibling.
  • In little-endian format.
  • The last node's distant offset is set to 0x00000000.

[siblingOffset] field[edit]

  • If this record is a leaf [siblingOffset] is enabled. Point to next leaf sibling.
  siblingOffset and distantSiblingOffset in this format is same function (point to next node sibling). 
  They're only differences:
     siblingOffset use with leaf node.
     distantSiblingOffset use with node.
  so it's same value.

[dataType] field[edit]

  • If this record is a leaf
    • [dataType] is type of leaf's data.
  • The tool supports the following base data types defined in SNMPv1:
    • INTEGER: The integer data type is a signed integer in the range of -2,147,483,648 to 2,147,483,647.
    • OCTETSTRING: Octet strings are ordered sequences of 0 to 65,535 octets.
    • Gauge: Nonnegative integers that can increase or decrease but retain the maximum value reached. The limit of 2^32 -1.
    • TimeTicks: A hundredth of a second since some event. The limit of 2^32 -1.
    • Counter: Nonnegative integers that increase until they reach a maximum value (2^32 -1); then, the integers return to zero.
    • DisplayString: a special case of the octet string type where all the bytes are printable ASCII characters, include formatting characters such as CR and LF, and the C programming language string terminator character zero.
    • IpAddress: A four byte octet string in network order.
    • NetworkAddress: Used to indicate an address choice from one of the possible protocol families. Currently, only IP addresses are supported.
    • Opaque: An arbitrary encoding that is used to pass arbitrary information strings that do not conform to the strict data typing used by the mib.
    • SEQUENCE: An ordered list of objects, somewhat like a struct in the C language. Type of objects in sequence is same type of node.

[dataLen], [data] fields[edit]

  • If this record is a leaf and has default data
    • [dataLen] is length of data.
    • [data] is data on string.

[{<IndexNumber>}] and [{<IndexCount>, <IndexNodeInfo>, <IndexDataType>}] fields[edit]

  • If this record is sequence (an order list of objects),
    • <IndexNumber> is the number of INDEXes in sequence.
    • <IndexCount>: is id of index node in table
    • <IndexNodeInfo>: is info of index node
    • <IndexDataType>: is data type of index node
  • See example of accessing data in a table
  • Example:
    • trap node is a sequence to inform the NMS of a significant event (an extraordinary event has occurred at an agent) asynchronously. This sequence has two INDEXes, so we have:
     <IndexNumber> = 0x02
     with the 1st INDEX:
         <IndexCount> = 0x05
         <IndexNodeInfo> = 0x28
         <IndexDataType> = 0x02
     with the 2nd INDEX:
         <IndexCount> = 0x08
         <IndexNodeInfo> = 0xA4
         <IndexDataType> = 0x04
  In this example, trap is a table which has 4 columns: 
     trapReceiverNumber (1), trapEnabled (2), trapReceiverIPAddress (3), trapCommunity(4). 
     trapReceiverNumber (1), trapEnabled (2), trapReceiverIPAddress (3), trapCommunity(4). 
     trapReceiverNumber (1), trapEnabled (2), trapReceiverIPAddress (3), trapCommunity(4). 
  Each significant event will be a row defined in the trap table. 
  
  This example has two INDEXes: the 1st INDEX node is trapCommunity (4) and the 2nd INDEX node trapReceiverNumber (1) 
  Each INDEX is a node, so it has OID, info, data type.
  The 1st INDEX node is trapCommunity, which has id = 5, info is 0x28 and data type is INTEGER (0x02). so
         IndexCount = 0x04
         IndexNodeInfo = 0x28
         IndexDataType = 0x02
  The 2nd INDEX node is trapReceiverNumber, which has id = 8, info is 0xA4 and data type is DisplayString (0x04). so
         IndexCount = 0x01
         IndexNodeInfo = 0xA4
         IndexDataType = 0x04

Binary File Format <name>_trap.bin[edit]

  • The binary file store TRAP information. It is generated by mib2bin tool. Agents will read binary file to get information of TRAP when something bad occurs.
  • In the binary file, An enterprise OID is stored first, followed by its first specific trap number and IDs of first specific trap (those IDs matche with IDs of leaf in <name>.bin; IDs are reference to OIDs of data bindings that be included in this trap) to last specific trap number and IDs of last specific trap. Next, the structure of next this enterprise OID is stored. This structure is repeated until all enterprise OID in MIB file is stored.
  • The format of enterprise in <name>_trap.bin
  <enterprise_oid><sibling_enterprise><enterprise_index>[specific_trap_number][sibling_specific_trap][number_varbinds][id_varbind]...
  • where:
    • fields indicated by angle brackets (< >) are always present.
    • fields in square brackets ([ ]) are optional depending on characteristics of the trap.

<enterprise_oid> field[edit]

  • Enterprise oid is full oid of enterprise trap that want to send in MIB file.
  • The format of enterprise oid.
  <sub_oid> <info_sub_oid> ... 
  • where:
    • <sub_oid>: is same format of <oid> field in MIB file.
    • <info_sub_oid>: Information of sub_oid.
  <info_sub_oid> format:
     bit        when (set = 1)
     0          the first sub_oid in enterprise
     1          no use
     2          no use
     3          no use
     4          sub_oid is a parent
     5          no use
     6          no use
     7          the last sub_oid in enterprise
  • If <sub_oid> == BYTE (0x00) and <info_sub_oid> == BYTE(0xFF), this is the end of infomation traps.

<sibling_enterprise> field[edit]

  • Point to next enterprise OID.
  • In little-endian format.

<enterprise_index> field[edit]

  • Index of enterprise OID traps in <name>_trap.bin file.
  • Size of <enterprise_index> is 1 byte.

[specific_trap_number] field[edit]

  • If the trap is sent is specific trap, specific trap number is a number indicting specific trap.
  • specific trap number is integer in little-endian format.

[sibling_specific_trap] field[edit]

  • Point to next specific trap.
  • In little-endian format.

[number_varbinds] field[edit]

  • A number of data bindings to be included in the specific trap.
  • Size of [number_varbinds] is 1 byte.

[id_varbind] field[edit]

  • [id_varbind] define data bindings (OID and value of OID leaf) that is included specific trap.
  • [id_varbind] is reference to id of the leaf oid in <name>.bin.
  • In little-endian format.

Example[edit]

OID Tree[edit]

  • An example OID tree is given below:
Snmp mib oid tree example.jpg

Binary File <name>.bin[edit]

  • The corresponding binary file is <name>.bin and it has data describe in table below:
    • oid is <oid> fields.
    • info is <nodeInfo> fields.
    • dist is [distantSiblingOffset]/[siblingOffset] fields.
    • id is [id] fields.
    • type is [dataType] fields.
    • index is index fields of sequence, include [{index_number} , {<IndexCount>, <IndexNodeInfo>, <indexDataType>, ...}].
Snmp mib binary example.jpg
  • The detail description of <name>.bin is illustrated in the diagram below:
    • The black arrow lines represent the OID tree structure.
    • The red and green lines represent pointers to data.
    • The dist field points to next sibling record. After parent record is it's children.
Snmp mib binary detail example.jpg

Binary File <name>_trap.bin[edit]

  • The corresponding binary file is <name>_trap.bin and it has data describe in table below:
    • enterp is <enterprise_oid> fields.
    • sibl_ep is <sibling_enterprise> fields.
    • index_ep is <enterprise_index> fields.
    • spec_id is [specific_trap_number] fields.
    • sibl_sid is [sibling_specific_trap] fields.
    • num_var is [number_varbinds] fields.
    • id_var is [id_varbind] fields.
Snmp mib binary trap example.jpg

SNMP's PDU using BER[edit]

  • SNMP is the protocol that allows communicate between NMS and agents by exchanging SNMP messages. the SNMP message is a single field, of the Sequence type. SNMP message use data types specified by ASN.1 and use Basic Encoding Rules (BER) to encode data. The entire SNMP message is a Sequence of three smaller fields: the SNMP Version (Integer), the SNMP Community String (Octet String), and the SNMP PDU.
  • The SNMP's PDU is reference to SNMP version 1 (SNMPv1) PDU.
  • SNMPv1 PDU have five different PDU types:GetRequest, GetNextRequest, GetResponse, SetRequest and Trap.
  • Get Request, GetNext Request, Get Response, Set Request are same format PDU. Trap use other format PDU.

ASN.1 data types[edit]

  • ASN.1 data types fall into two categories: primitive and complex.
  • ASN.1 data types is used to build SNMP messages.
    • ASN.1 primitive data types include Integer, Octet (byte, character) String, Null, Boolean and Object Identifier.
    • ASN.1 complex data types are used to build SNMP message are Sequence type, Sequence-of type.

Encoding Rules (BER)[edit]

  • BER has three parts: Type, Length and Data field.
  BER format: 
     +------+--------+------+
     | Type | Length | Data |
     +------+--------+------+
  • Type field is single byte identifier.
  Constructing byte Data type.
  Format of byte Data type
     +--+-+-----+
     |  | |     +
     +--+-+-----+
      2  1   5
  Bits 8 and 7 shall be encoded to represent the class of Data type follow table:
     +------------------+-------+-------+
     |      Class       | Bit 8 | Bit 7 |
     +------------------+-------+-------+
     | Universal        |   0   |   0   |
     | Application      |   0   |   1   |
     | Context-specific |   1   |   0   |
     | Private          |   1   |   1   |
     +------------------+-------+-------+
  Bit 6 is encoded data is Primitive or Constructed follow table:
     +-------+-------------+
     | Bit 6 |   Type      |
     +-------+-------------+
     |   0   | Primitive   |
     |   1   | Constructed |
     +-------+-------------+ 
  Bits 5 to 1 encode the number of Data type as a integer number.     
  Data type identifier in SNMP
     Data type                 Identifier      Note
     Integer                      0x02         Primitive ASN.1 types
     Octet String                 0x04         Primitive ASN.1 types  
     Null                         0x05         Primitive ASN.1 types
     Object identifier            0x06         Primitive ASN.1 types
     Sequence                     0x30         Constructed ASN.1 types
     IpAddress                    0x40         Primitive SNMP application types
     Counter                      0x41         Primitive SNMP application types
     Gauge                        0x42         Primitive SNMP application types
     TimeTicks                    0x43         Primitive SNMP application types 
     Opaque                       0x44         Primitive SNMP application types
     NsapAddress                  0x45         Primitive SNMP application types
     GetRequest PDU               0xA0         Context-specific Constructed SNMP types
     GetNextRequest PDU           0xA1         Context-specific Constructed SNMP types
     GetResponse PDU              0xA2         Context-specific Constructed SNMP types
     SetRequest PDU               0xA3         Context-specific Constructed SNMP types
     Trap PDU                     0xA4         Context-specific Constructed SNMP types
  • Length field is the number of bytes in Data field.
    • Length field is used either the short form or the long form as a option depend on Data field.
      • The short form, Length field is a single octet in which bit 8 is zero and bits 7 to 1 encode the number of bytes in Data field, as an unsigned binary integer with bit 7 as the most significant bit.
      • The long form, Length field shall consists of an initial octet and one or more subsequent octets.
        • The initial octet is encoded as follows:
          • Bit 8 shall be one.
          • Bits 7 to 1 shall encode the number of subsequent octets in the length field, as an unsigned binary integer with bit 7 as the most significant bit.
          • The value 0xFF shall not be used.
        • Subsequent octets:
          • From the first subsequent octet to the last subsequent octet, shall be the encoding of an unsigned binary integer equal to the number bytes in Data field, with bit 8 of the first subsequent octet as the most significant bit.
  • Data field is actual data content.
  • Example:
  Actual Data is an integer, the value 100 can be encode as:
  The short form:
     +------+--------+-------+
     | Type | Length | Data  |
     +------+--------+-------+
     | 0x02 |  0x01  | 0x64  |
     +------+--------+-------+
  The long form:
     +------+-----------+-------+
     | Type |  Length   | Data  |
     +------+-----------+-------+
     | 0x02 | 0x81 0x01 | 0x64  |
     +------+-----------+-------+

SNMP Message Format[edit]

  • SNMP Message is a Sequence of three smaller fields: the SNMP Version (Integer), the SNMP Community String (Octet String), and the SNMP PDU.
  SNMP Message Format use BER.
     +------------------------------------------------------------------------+
     |                      SNMP Message (Sequence type)                      |
     +------+----------------+------------------------------------------------+
     | Type | Length of Data |                      Data                      |
     +------+----------------+-------------+-----------------------+----------+
     | 0x30 |     Length     |SNMP Version | SNMP Community String | SNMP PDU |                      
     |      |                |  (Integer)  |    (Octet String)     |          |
     +------+----------------+-------------+-----------------------+----------+
                             |<--------------------Length-------------------->|
  • Length is bytes of Data field (SNMP Version, SNMP Community String and SNMP PDU).
  • SNMP Version is an integer that identifies the version of SNMP, SNMP version 1 = 0.
  • SNMP Community String is an Octet String to add security to Agents.
  • SNMP PDU is a SNMP verion 1 (SNMPv1) PDU.

SNMP PDU Format[edit]

  • SNMP PDU is is reference to SNMP version 1 (SNMPv1) PDU.

GetRequest PDU, GetNextRequest PDU, GetResponse PDU, SetRequest PDU Format[edit]

  • GetRequest PDU, GetNextRequest PDU, GetResponse PDU, SetRequest PDU Format is shown here.
  +-------------------------------------------------------------------------------------------------------------------------------------+
  |                               SNMPv1 PDU (GetRequest, GetNextRequest, GetResponse, SetRequest                                       |
  +------+--------+---------------------------------------------------------------------------------------------------------------------+
  | PDU  | Length |                                             Data of SNMP PDU                                                        |
  | type |  Data  |                                                                                                                     |
  +------+--------+---------+---------+---------+---------------------------------------------------------------------------------------+
  | PDU  | Length | Request | Error   | Error   |                             VarBind List (Sequence)                                   |
  | type |  PDU   |   ID    | Status  | Index   +------+--------+--------------------------------+--------------------------------+-----+
  |      |        |         |         |         | 0x30 | Length |           Varbind 1            |           Varbind 2            | ... |
  |      |        |(Integer)|(Integer)|(Integer)|      |        |          (Sequence)            |          (Sequence)            | ... | 
  |      |        |         |         |         |      |        +------+-------+-------+---------+------+-------+-------+---------+-----+
  |      |        |         |         |         |      |        | 0x30 | Len 1 | OID 1 | Value 1 | 0x30 | Len 2 | OID 1 | Value 2 | ... |
  +------+--------+---------+---------+---------+------+--------+------+-------+-------+---------+------+-------+-------+---------+-----+
                  |                                             |              |<-----Len 1----->|              |<-----Len 2----->|     |
                  |                                             |<-------------------------------Length-------------------------------->|
                  |<----------------------------------------------Lenght PDU----------------------------------------------------------->| 
  • PDU Type is specific type of PDU, PDU Type is a single byte identifier.
  • Length Data is a number bytes of Data of SNMP PDU field.
  • Data of SNMP PDU is data content in SNMP PDU.
  • Request ID is an Integer that identifies a particular SNMP request. This index is echoed back in the response from the SNMP agent, allowing the SNMP manager to match an incoming response to the appropriate request.
  • Error Status is an Integer set to 0x00 in the request sent by the NMS. The SNMP agent places an error code in this field in the response message if an error occurred processing the request.
  Some error codes include:
  +-------------------+---------------------------------------------------------------------------+
  |    Error Status   |                                                                           |
  +------------+------+                                Description                                |
  |    Name    | Code |                                                                           |
  +------------+------+---------------------------------------------------------------------------+
  | noError    | 0x00 | No error occurred.                                                        |
  | tooBig     | 0x01 | The response to your request was too big to fit into one response.        |
  | noSuchName | 0x02 | The OID in the request was not found.the OID doesn't exist.               |
  | badValue   | 0x03 | A data type in the request did not match the data type in the SNMP agent. |
  | readOnly   | 0x04 | The SNMP manager attempted to set a read-only parameter.                  |
  | genErr     | 0x05 | General Error (some error other than the ones listed above).              |
  +------------+------+---------------------------------------------------------------------------+
  • Error Index is an Integer. Only the response operation sets this field, if an Error occurs, the Error Index holds a pointer to the Object that caused the error. Other operations set this field to zero.
  • Varbind List is a Sequence of Varbinds.
    • Varbind is a Sequence of two fields, an OID and the value of OID.
      • OID is an Object Identifier that points to a particular parameter in the SNMP agent. The first two object identifier components in the object identifier value being encoded, using the formula: (X*40) + Y; where X is the value of the first object identifier component and Y is the value of the second object identifier component. The first two object identifier components recognizes that only three values are allocated from the root node, and at most 39 subsequent values from nodes reached by X = 0 and X = 1.
      • Value is the value of OID.
  +--------------------+-----------------------------------------------------------------+
  |    SNMP PDU type   |                            Value                                |
  +--------------------+-----------------------------------------------------------------+
  | SetRequest PDU     | Value is applied to the specified OID of the SNMP agent.        |
  | GetRequest PDU     | Value is a Null that acts as a placeholder for the return data. |
  | GetNextRequest PDU | Value is a Null that acts as a placeholder for the return data. |
  | GetResponse PDU    | The returned Value from the specified OID of the SNMP agent.    |
  +--------------------+-----------------------------------------------------------------+

Trap PDU Format[edit]

  • The format of the Trap PDU is shown below:
  +------------------------------------------------------------------------------------------------------------------------------------------+
  |                                                         TRAP SNMPv1 PDU                                                                  |
  +----+------+------------------------------------------------------------------------------------------------------------------------------+
  |PDU |Length|                                             Data of SNMP PDU                                                                 |
  |type| Data |                                                                                                                              |
  +----+------+----------+-------+-------+--------+------+---------------------------------------------------- ------------------------------+
  |0xA4|Length|Enterprise| Agent |Generic|Specific|Time  |                                VarBind List (Sequence)                            |
  |    | PDU  |   OID    |Address| Trap  |  Trap  |Stamp +------+--------+------------------------------+------------------------------+-----+
  |    |      |          |       | Type  | Number |      | 0x30 | Length |           Varbind 1          |         Varbind 22           | ... |
  |    |      |  (OID)   |       |       |        |      |      |        |          (Sequence)          |         (Sequence)           | ... |
  |    |      |          |       |       |        |      |      |        +----+-------+-------+---------+----+-------+-------+---------+-----+
  |    |      |          |       |       |        |      |      |        |0x30| Len 1 | OID 1 | Value 1 |0x30| Len 2 | OID 1 | Value 2 | ... |
  +----+------+----------+-------+-------+--------+------+------+--------+----+-------+-------+---------+----+-------+-------+---------+-----+
                         |                                               |            |<-----Len 1----->|            |<-----Len 2----->|     |
                         |                                               |<-----------------------------Length------------------------------>|
                         |<--------------------------------------------Lenght PDU----------------------------------------------------------->|
  • PDU type is TRAP PDU = 0xA4.
  • Length Data is a number bytes of Data of SNMP PDU field.
  • Enterprise OID identifies the type of managed object generating the trap.
  • Agent Address is the IP address of the agent that is sending the trap.
  • Generic Trap Type indicates one of a number of generic trap types.
  Generic Trap Type has seven values are defined: 
  +------------------------------+---------------------------------------------------------------------------+
  |       Generic Trap Type      |                                                                           |
  +-----------------------+------+                                Description                                |
  |         Name          | Code |                                                                           |
  +-----------------------+------+---------------------------------------------------------------------------+
  | coldStart             | 0x00 | Indicates that the agent has rebooted. All management variables will be   |
  |                       |      | reset; specifically, Counters and Gauges will be reset to zero (0).       |
  +-----------------------+------+---------------------------------------------------------------------------+
  | warmStart             | 0x01 | Indicates that the agent has reinitialized itself. None of the management |
  |                       |      | variables will be reset.                                                  |
  +-----------------------+------+---------------------------------------------------------------------------+
  | linkDown              | 0x02 | Sent when an interface on a device goes down. The first variable binding  |
  |                       |      | identifies which interface went down.                                     |
  +-----------------------+------+---------------------------------------------------------------------------+
  | linkUp                | 0x03 | Sent when an interface on a device comes back up. The first variable      | 
  |                       |      | binding identifies which interface came back up.                          |
  +-----------------------+------+---------------------------------------------------------------------------+
  | authenticationFailure | 0x04 | Indicates that someone has tried to query your agent with an incorrect    |
  |                       |      | community string; useful in determining if someone is trying to gain      |
  |                       |      | unauthorized access to one of your devices.                               |
  +-----------------------+------+---------------------------------------------------------------------------+
  | egpNeighborLoss       | 0x05 | Indicates that an Exterior Gateway Protocol (EGP) neighbor has gone down. |
  +-----------------------+------+---------------------------------------------------------------------------+
  | enterpriseSpecific    | 0x06 | Indicates that the trap is enterprise-specific. SNMP vendors and users    |
  |                       |      | define their own traps under the private-enterprise branch of the SMI     |
  |                       |      | object tree. To process this trap properly, the NMS has to decode the     |
  |                       |      | specific trap number that is part of the SNMP message.                    |
  +-----------------------+------+---------------------------------------------------------------------------+
  • Specific trap number is a number indicating the specific trap you want to send. If you're sending a generic trap, this parameter is ignored -- you're probably better off setting it to zero.
  • Time stamp is the time elapsed between the last initialization of the network entity and the generation of the trap.
  • Varbind List is a Sequence of Varbinds to be included in the trap.
    • Varbind is a Sequence of two fields, an OID and the value of OID. The OIDs for these variable bindings are often specific to the trap and therefore "underneath" the specific OID for the trap. But this isn't a requirement, and it's often useful to send bindings that aren't defined as part of the trap.
      • OID is an Object Identifier that points to a particular parameter in the SNMP agent.
      • Value is the value of OID

Example SNMP Message[edit]

GetRequest PDU[edit]

  • The example gets the value of name agent with above MIB file.
  • Community String is "public"
  +---------+-----------+--------------------------------------------------------------------------------------------+-------------------+
  | SNMP    | Type      |                                               0x30                                         | Sequence          |
  | message +-----------+--------------------------------------------------------------------------------------------+-------------------+
  |         |Length     |                                               0x2B                                         | Length: 43        |
  |         +-----------+--------------------------------------------------------------------------------------------+-------------------+
  |         | Version   |                                               0x02                                         | Integer           |
  |         |           |                                               0x01                                         | Length: 1         |
  |         |           |                                               0x00                                         | Value: 0          |
  |         +-----------+--------------------------------------------------------------------------------------------+-------------------+
  |         | Community |                                               0x04                                         | Octet String      |
  |         |           |                                               0x06                                         | Length: 6         |
  |         |           |                                               0x70 0x75 0x62 0x6C 0x69 0x63                | Value: public     |
  |         +-----------+ -------+--------------+--------------------------------------------------------------------+-------------------+
  |         | Data      | SNMPv1 | PDU type     |                       0xA0                                         | GetRequest PDU    |
  |         |           | PDU    +--------------+--------------------------------------------------------------------+-------------------+
  |         |           |        | PDU length   |                       0x1E                                         | Length: 30        |
  |         |           |        + -------------+--------------------------------------------------------------------+-------------------+
  |         |           |        | Request ID   |                       0x02                                         | Integer           |
  |         |           |        |              |                       0x04                                         | Length: 4         |
  |         |           |        |              |                       0x37 0x9C 0x57 0x89                          | Value:            |
  |         |           |        + -------------+--------------------------------------------------------------------+-------------------+
  |         |           |        | Error Status |                       0x02                                         | Integer           |
  |         |           |        |              |                       0x01                                         | Length: 1         |
  |         |           |        |              |                       0x00                                         | Value: 0          |
  |         |           |        + -------------+--------------------------------------------------------------------+-------------------+
  |         |           |        | Error Index  |                       0x02                                         | Integer           |
  |         |           |        |              |                       0x01                                         | Length: 1         |
  |         |           |        |              |                       0x00                                         | Value: 0          |
  |         |           |        + -------------+--------------------------------------------------------------------+-------------------+
  |         |           |        | VarBind List |                       0x30                                         | Sequence          |
  |         |           |        |              +-----------+--------------------------------------------------------+-------------------+
  |         |           |        |              | Length    |           0x10                                         | Length: 16        |
  |         |           |        |              +-----------+--------------------------------------------------------+ ------------------+
  |         |           |        |              | VarBind 1 |           0x30                                         | Sequence          |
  |         |           |        |              |           +---------+----------------------------------------------+-------------------+
  |         |           |        |              |           | Len 1   | 0x0E                                         | Length: 14        |
  |         |           |        |              |           +---------+----------------------------------------------+-------------------+
  |         |           |        |              |           | OID 1   | 0x06                                         | Object identifier |
  |         |           |        |              |           |         | 0x0A                                         | Length: 10        |
  |         |           |        |              |           |         | 0x2B 0x06 0x01 0x04 0x01 0x82 0x99 0x5D 0x02 | Value:            |
  |         |           |        |              |           +---------+----------------------------------------------+-------------------+
  |         |           |        |              |           | Value 1 | 0x05                                         | NULL              |
  |         |           |        |              |           |         | 0x00                                         | Length: 0         |
  +---------+-----------+--------+--------------+-----------+---------+----------------------------------------------+-------------------+

TRAP PDU[edit]

  • The example TRAP above MIB file.
  • Community String is "public"
  • Agent's IPAddress is 192.168.1.128
  • Timestamp is 123456
  • The operMasterControl OID is in TRAP that has value is 1
  +---------+-----------+-------------------------------------------------------------------------------------------------------+-------------------+
  | SNMP    | Type      |                                           0x30                                                        |Sequence           |
  | message +-----------+-------------------------------------------------------------------------------------------------------+-------------------+
  |         | Length    |                                           0x82 0x00 0x44                                              |Length: 68         |
  |         +-----------+-------------------------------------------------------------------------------------------------------+-------------------+
  |         | Version   |                                           0x02                                                        |Integer            |
  |         |           |                                           0x01                                                        |Length: 1          |
  |         |           |                                           0x00                                                        |Value: 0           |
  |         +-----------+-------------------------------------------------------------------------------------------------------+-------------------+
  |         | Community |                                           0x04                                                        |Octet String       |
  |         |           |                                           0x06                                                        |Length: 6          |
  |         |           |                                           0x70 0x75 0x62 0x6C 0x69 0x63                               |Value: public      |
  |         +-----------+ -------+------------+---------------------------------------------------------------------------------+-------------------+
  |         | Data      | SNMPv1 | PDU type   |                     0xA4                                                        |TRAP PDU           |
  |         |           | PDU    +------------+---------------------------------------------------------------------------------+-------------------+
  |         |           |        | PDU length |                     0x82 0x00 0x35                                              |Length: 53         |
  |         |           |        + -----------+---------------------------------------------------------------------------------+-------------------+
  |         |           |        | Enterprise |                     0x06                                                        |Object identifier  |
  |         |           |        | OID        |                     0x09                                                        |Length: 9          |
  |         |           |        |            |                     0x2B 0x06 0x01 0x04 0x01 0x82 0x99 0x5D 0x00                |Value:             |
  |         |           |        + -----------+---------------------------------------------------------------------------------+-------------------+
  |         |           |        | Agent      |                     0x40                                                        |IpAddress          |
  |         |           |        | Address    |                     0x04                                                        |Length: 4          |
  |         |           |        |            |                     0xC0 0xA8 0x01 0x80                                         |Value:192.168.1.128|
  |         |           |        + -----------+---------------------------------------------------------------------------------+-------------------+
  |         |           |        | Generic    |                     0x02                                                        |Integer            |
  |         |           |        | Trap Type  |                     0x01                                                        |Length: 1          |
  |         |           |        |            |                     0x06                                                        |Value: 6           |
  |         |           |        + -----------+---------------------------------------------------------------------------------+-------------------+
  |         |           |        | Specific   |                     0x02                                                        |Integer            |
  |         |           |        | Trap       |                     0x01                                                        |Length: 1          |
  |         |           |        | Number     |                     0x01                                                        |Value: 1           |
  |         |           |        + -----------+---------------------------------------------------------------------------------+-------------------+
  |         |           |        | Time       |                     0x43                                                        |TimeTicks          |
  |         |           |        | Stamp      |                     0x03                                                        |Length: 3          |
  |         |           |        |            |                     0x01 0xE2 0x40                                              |Value: 123456      |
  |         |           |        + -----------+---------------------------------------------------------------------------------+-------------------+
  |         |           |        | VarBind    |                     0x30                                                        |Sequence           |
  |         |           |        | List       +-----------+---------------------------------------------------------------------+-------------------+
  |         |           |        |            | Length    |         0x82 0x00 0x15                                              |Length: 21         |
  |         |           |        |            +-----------+---------------------------------------------------------------------+ ------------------+
  |         |           |        |            | VarBind 1 |         0x30                                                        |Sequence           |
  |         |           |        |            |           +-------+-------------------------------------------------------------+-------------------+
  |         |           |        |            |           | Len 1 | 0x82 0x00 0x11                                              |Length: 17         |
  |         |           |        |            |           +-------+-------------------------------------------------------------+-------------------+
  |         |           |        |            |           | OID 1 | 0x06                                                        |Object identifier  |
  |         |           |        |            |           |       | 0x0C                                                        |Length: 12         |
  |         |           |        |            |           |       | 0x2B 0x06 0x01 0x04 0x01 0x82 0x99 0x5D 0x03 0x01 0x01 0x01 |Value:             |
  |         |           |        |            |           +-------+-------------------------------------------------------------+-------------------+
  |         |           |        |            |           | Value | 0x02                                                        |Integer            |
  |         |           |        |            |           | 1     | 0x01                                                        |Length: 1          |
  |         |           |        |            |           |       | 0x01                                                        |Length: 1          |
  +---------+-----------+--------+------------+-----------+-------+-------------------------------------------------------------+-------------------+

SNMP operations[edit]

  • NMS uses GetRequest command to retrieve the value of objects from agents. Objects that is defined by object identifier in MIB file. It is possible for an NMS to get more than one object at a time. The agent receives the request and processes it. After that, it sends GetResponse back to NMS with current value of objects.
  • NMS uses GetNextRequest command to discover available objects and the value of these objects from agents. The GetNextRequest command traverses a subtree in lexicographic order. Based on the OIDs in GetNextRequest PDU, it's easy for an agent to start at the root of its SMI object tree and work its way down until it finds the OID it is looking for. After the agent found avaible objects, it sends GetResponse back to NMS with current value of objects.
  • NMS uses SetRequest command to change the value of objects from agents. After the agent processes SetRequest, it send GetResponse back to NMS with new value of objects (value of objects that get from SetRequest).
  • The agent sends Trap to NMS when agent's something bad operation conditions. No repspone is sent from the NMS to the agent when NMS receives Trap.
  • Example SNMP operations with net-snmp:
  GetRequest command:
  >> snmpget -v 1 -c public 192.168.1.128 1.3.6.1.4.1.36061.1.1
  This command will send GetRequest to the agent (IP address of agent is 192.186.1.128). The SNMP is SNMPv1 (the option -v 1) with community 
  string is public (the option -c public). In above demo MIB file, 1.3.6.1.4.1.36061.1.1 is OID reference to devID object. 
  The effect of this command will get the value of agent's devID .
  GetNextRequest command:
  >> snmpgetnext -v 1 -c public 192.168.1.128 1.3.6.1.4.1.36061.1
  This command will send GetNextRequest to the agent (IP address of agent is 192.186.1.128). The SNMP is SNMPv1 (the option -v 1) with community 
  string is public (the option -c public). In above demo MIB file, 1.3.6.1.4.1.36061.1 is OID.
  The effect of this command will get the value of agent's devID.
  SetRequest command:
  >> snmpset -v 1 -c public 192.168.1.128 1.3.6.1.4.1.36061.1.1 s "Hello"
  This command will send SetRequest to the agent (IP address of agent is 192.186.1.128). The SNMP is SNMPv1 (the option -v 1) with community 
  string is public (the option -c public). In above demo MIB file, 1.3.6.1.4.1.36061.1.1 is OID reference to devID object. 
  The effect of this command will set the value of agent's devID to "Hello".
  Receiving Traps
  To receive Traps, snmptrapd.conf file must be configured and snmptrapd must be running.
  The easiest way to configure this is by adding this line to snmptrapd.conf file:
     disableAuthorization yes
  The following command will receive and view trap
  >>snmptrapd -f -Le