To understand this tutorial its important to have the following under hand:
NetSNMP 5.0.x installed. You can download the latest binary and source code from: The NET-SNMP Project Home Page.
Understanding of Management Information Base (MIB) syntax. I recommend Understanding SNMP MIBs by David T. Perkins and Evan McGinnis, published by Prentice Hall. ISBN 0-13-437708-7.
Knowledge of SNMP GET/SET operation. The above mentioned book covers it thoroughly, however these Requests for Comments (RFCs) are also a good read:
"The Party-based SNMPv2" circa 1993-1995 [RFC 1441 - RFC 1452].
MIB definition file. In this tutorial I refer to NET-SNMP-TUTORIAL-MIB.txt and implement a sub-agent based on this file.
In order to implement a new sub-agent, three files are necessary:
MIB definition file.
C header file
C implementation file
The MIB definition file we refer to in this tutorial is the NET-SNMP-TUTORIAL-MIB.txt.
The C header and implementation file can be generated automaticly from the MIB definition file using the mib2c tool.
It generates the corresponding the C and header file (or files) with the skeleton code. This chapter will explain in details how to use the mib2c tool. The next chapter will explain the generated C and header file (or files).
From the mib2c manpage. The mib2c tool is designed to take a portion of the MIB tree (as defined by a MIB file) and generate the template C code necessary to implement the corresponding MIB module.
The mib2c tool uses the MIB definition file to produce the two C code files. Thus, mib2c generates a template that you can edit to add logic necessary to obtain information from the operating system or application to complete the module.
MIBNODE is the top level mib node you want to generate code for. You must give mib2c a mib node (e.g., netSnmpIETFWGTable), not a mib file on the command line. (this is the most common mistake).
The mib2c tool accepts both SMIv1 and SMIv2 MIBs.
mib2c needs to be able to find and load a MIB file in order to generate C code for the MIB. To enable mib2c to find the MIB file, set the MIBS environment variable to include the MIB file you are using. An example of setting this environment variable is:
$ export MIBS=+NET-SNMP-TUTORIAL-MIB
$ export MIBS=ALL
The first example ensures that mib2c finds the NET-SNMP- TUTORIAL-MIB mib, in addition to the default MIB modules. The default list of MIB modules is set when the suite is first configured and built and basically corresponds to the list of modules that the agent supports. The second example ensures that mib2c finds all MIBs in the search location for MIB files. The default search location for MIB files is /usr/share/snmp/mibs. This search location can be modified by the MIBDIRS environment variable.
Both the MIB files to be loaded and the MIB file search location can also be configured in the snmp.conf file. Please see snmp.conf(5) for more information.
The generated *.c and *.h files will be created in the current working directory.
The second parameter to mib2c tool is the CONFIGFILE which is used when generating code. These files will be searched for first in the current directory and then in the /usr/share directory (which is where the default mib2c configuration files can be found). Running mib2c without the -c CONFIGFILE option will display a description of the valid val ues for CONFIGFILE, that is, the available config files, including new ones that you might author.
$ mib2c netSnmpIETFWGTable
will display a description of the currently avail able values for CONFIGFILE.
The following values are supported for CONFIGFILE:
mib2c.scalar.conf mib2c.int_watch.conf mib2c.iterate.conf mib2c.create-dataset.conf mib2c.array-user.conf mib2c.column_defines.conf mib2c.column_enums.conf
If you're writing code for some scalars, run:
$ mib2c -c mib2c.scalar.conf MIBNODE
If you want to magically "tie" integer variables to integer scalars, use:
$ mib2c -c mib2c.int_watch.conf MIBNODE
If your table data is kept somewhere else (e.g. it's in the kernel and not in the memory of the agent itself) and you need to "iterate" over it to find the right data for the SNMP row being accessed. See the agent/mibgroup/mibII/vacm_con text.c file for an example:
$ mib2c -c mib2c.iterate.conf MIBNODE
If your table data is kept in the agent (i.e. it's not located in an external source) and is purely data driven (i.e. you do not need to perform any work when a set occurs). See the agent/mib group/examples/data_set.c file for an example of such a table:
$ mib2c -c mib2c.create-dataset.conf MIBNODE
If your table data is kept in the agent (i.e. it's not located in an external source), and you can keep your data sorted by the table index but you do need to perform work when a set occurs:
$ mib2c -c mib2c.array-user.conf MIBNODE
To generate just a header with a define for each column number in your table:
$ mib2c -c mib2c.column_defines.conf MIBNODE
To generate just a header with a define for each enum for any column containing enums:
$ mib2c -c mib2c.column_enums.conf MIBNODE
$ mib2c -c mib2c.old-api.conf MIBNODE
If you get a warning message, such as:
you didn't give me a valid OID to start with at /usr/bin/mib2c line 115.do define the environment variable MIBS to be ALL or the name of your MIB.
For our implementation of the netSnmpIETFWGTable we picked the mib2c.array-user.conf configuration file.
$ mib2c -c mib2c.array-user.conf netSnmpIETFWGTable writing to netSnmpIETFWGTable.h writing to netSnmpIETFWGTable.c running indent on netSnmpIETFWGTable.c running indent on netSnmpIETFWGTable.h