mib for dummies - net-snmp wiki

Upload: temporal11

Post on 14-Apr-2018

259 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 MIB for Dummies - Net-SNMP Wiki

    1/4

    15/08/13 MIB for Dummies - Net-SNMP Wiki

    www.net-snmp.org/wiki/index.php/MIB_for_Dummies 1/4

    MFD Tutorial

    MIB for Dummies

    MFD:IF-MIB

    MFD:ifTable

    MFD:ifTable:mib2c

    MFD:ifTable:Data Structures

    MFD:ifTable:Data Access

    MFD:ifTable:Get Data

    MFD:ifTable:Testing

    MFD:ifXTable

    MFD:ifXTable:mib2cMFD:ifXTable:Data Structures

    MFD:ifXTable:Data Access

    MFD:ifXTable:Get Data

    MFD:ifXTable:Testing Gets

    MFD:ifXTable:Set Data

    MFD:ifXTable:Testing Sets

    MIB for Dummies

    From Net-SNMP Wiki

    Contents1 MIBs For Dummies

    1.1 Introduction

    1.2 Data Structures

    1.2.1 User Context

    1.2.2 MIB context

    1.2.3 Data Context

    1.2.4 Row Request Context

    1.3 Data Lookup

    1.3.1 container-cached

    1.3.2 Iterator (unsorted-external)

    1.3.3 Direct

    1.4 Data manipulation

    1.5 Getting Started

    1.5.1 Basic Tables

    1.5.2 Intermediate Tables

    1.5.3 Advanced Tables

    MIBs For Dummies

    NOTE: this tutorial is based on the code for release 5.2. If you generate code from a release prior to 5.2

    or with 5.3.x, your results may vary.

    Power users or repeat readers, you may wish to jump to the list in the Getting Started node.

    Introduction

    One of the questions that comes up on our mailing lists a lot is:I just ran mib2c on my MIB. What do I do

    now?. This tutorial, and the MIB forDummies code approach, is designed to make your life as easy as

    possible. In this tutorial we'll walk you through the entire process of creating C code starting from only a MIB

    definition. This should answer this question for you, assuming you're willing to use the MIB for Dummies

    approach to coding.

    One of the output code styles that mib2c supports is a style called "MIB for Dummies". This configuration file

    is called "local/mib2c.mfd.conf (http://net-snmp.git.sourceforge.net/git/gitweb.cgi?p=net-snmp/net-

    nmp;a=blob_plain;f=local/mib2c.mfd.conf;hb=HEAD) "and is what you would pass to the -c argument o

    mib2c to get it to produce the style of code this tutorial is going to discuss. It will generate mib-module code

    which is designed to plug into an agent when developing Net-SNMP agent extensions.

    http://net-snmp.git.sourceforge.net/git/gitweb.cgi?p=net-snmp/net-snmp;a=blob_plain;f=local/mib2c.mfd.conf;hb=HEADhttp://www.net-snmp.org/wiki/index.php?title=MFD:ifXTable:Set_Data&action=edit&redlink=1http://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#Advanced_Tableshttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#Basic_Tableshttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#Getting_Startedhttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#Data_manipulationhttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#Row_Request_Contexthttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#MIB_contexthttp://www.net-snmp.org/wiki/index.php/MFD:ifTable:Data_Accesshttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#User_Contexthttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#Introductionhttp://www.net-snmp.org/wiki/index.php/MFD:ifTablehttp://net-snmp.git.sourceforge.net/git/gitweb.cgi?p=net-snmp/net-snmp;a=blob_plain;f=local/mib2c.mfd.conf;hb=HEADhttp://www.net-snmp.org/wiki/index.php/Mib2chttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#Getting_Startedhttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#Advanced_Tableshttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#Intermediate_Tableshttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#Basic_Tableshttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#Getting_Startedhttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#Data_manipulationhttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#Directhttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#Iterator_.28unsorted-external.29http://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#container-cachedhttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#Data_Lookuphttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#Row_Request_Contexthttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#Data_Contexthttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#MIB_contexthttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#User_Contexthttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#Data_Structureshttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#Introductionhttp://www.net-snmp.org/wiki/index.php/MIB_for_Dummies#MIBs_For_Dummieshttp://www.net-snmp.org/wiki/index.php?title=MFD:ifXTable:Testing_Sets&action=edit&redlink=1http://www.net-snmp.org/wiki/index.php?title=MFD:ifXTable:Set_Data&action=edit&redlink=1http://www.net-snmp.org/wiki/index.php?title=MFD:ifXTable:Testing_Gets&action=edit&redlink=1http://www.net-snmp.org/wiki/index.php/MFD:ifXTable:Get_Datahttp://www.net-snmp.org/wiki/index.php/MFD:ifXTable:Data_Accesshttp://www.net-snmp.org/wiki/index.php/MFD:ifXTable:Data_Structureshttp://www.net-snmp.org/wiki/index.php/MFD:ifXTable:mib2chttp://www.net-snmp.org/wiki/index.php/MFD:ifXTablehttp://www.net-snmp.org/wiki/index.php/MFD:ifTable:Testinghttp://www.net-snmp.org/wiki/index.php/MFD:ifTable:Get_Datahttp://www.net-snmp.org/wiki/index.php/MFD:ifTable:Data_Accesshttp://www.net-snmp.org/wiki/index.php/MFD:ifTable:Data_Structureshttp://www.net-snmp.org/wiki/index.php/MFD:ifTable:mib2chttp://www.net-snmp.org/wiki/index.php/MFD:ifTablehttp://www.net-snmp.org/wiki/index.php/MFD:IF-MIB
  • 7/30/2019 MIB for Dummies - Net-SNMP Wiki

    2/4

    15/08/13 MIB for Dummies - Net-SNMP Wiki

    www.net-snmp.org/wiki/index.php/MIB_for_Dummies 2/4

    One of the primary motivations for writing the MIBs for Dummies configuration file was to reduce the amount of

    SNMP knowledge needed to implement a MIB. When using the MFD configuration file (mib2c.mfd.conf),

    mib2c will generate template code that hides much of the SNMP (or Net-SNMP) specific details for

    implementing a module, and allows the rest of the template code to use simple C data structures that you are

    probably more familiar with. Most of the template functions are short and simple, with a clearly defined purpose.

    Another motivation was to separate the method used to locate the data for an incoming request to the SNMP

    agent from the methods that manipulate the data. The data lookup for MFD modules uses the

    netsnmp_container interface. Data ordering can be tricky in SNMP, since the lexicographical ordering required

    by SNMP specification isn't always intuitive, and the index specified in the MIB you have to implement might

    not match the order in which your data is currently stored. But the MFD configuration file can help you out with

    this problem too.

    The templates generated by the MFD configuration file fall into several categories:

    data structures

    contains the data used to answer a request

    data lookup

    finding the right data for a request

    data manipulation

    either returning existing values or setting new values.

    Data Structures

    There are 4 important data structures used in an MFD module. They are the user context, the mib context, the

    data contextand the the row request context.

    User Context

    The user context is a pointer provided by the user (ie, the programmer: You!) during module initialization. It is

    not used by the MFD code, other than to save it for the user. If your module needs access to some external

    data, you can use this pointer instead of a global variable.

    MIB context

    The MIB context is a generated structure which is used to store the MIB indexes for a row.

    Data Context

    The data context structure should contain all the data needed to get or set a value. The mib2c MFD

    configuration file will generate a data context structure with sufficient storage for all the objects defined in the

    MIB being implemented. (Later in the tutorial, we'll talk about using existing structures.)

    Row Request Context

    The row request context ties together all the other contexts. The table container will have a row request context

    for each row in the table.

    http://www.net-snmp.org/wiki/index.php/Containers
  • 7/30/2019 MIB for Dummies - Net-SNMP Wiki

    3/4

    15/08/13 MIB for Dummies - Net-SNMP Wiki

    www.net-snmp.org/wiki/index.php/MIB_for_Dummies 3/4

    Data Lookup

    There are lots of different ways to access data. Your data store may be a linked list, a text file, a database or an

    API to some device. Instead of trying to deal with all of the possiblities in the template code, the

    netsnmp_containerinterface is used.

    There are currently two different interfaces between the MFD template code and the netsnmp_containerusedto locate data for a request.

    container-cached

    The default method, which is generic enough to handle just about any situation, is the container-cachedmethod.

    This combines two features of net-snmp: the cache handler and Containers (netsnmp_container). Quite simply,

    the first time a request is received for a table, a cache_loadroutine is called with a pointer to a

    netsnmp_container. That function accesses any datastore(s) that contain data, and adds all the rows to the

    container. The container is then used to find the rows for the incoming request, and (optionally) kept around a

    configurable number of seconds for future requests. While the basic principle is simple, the flexiblity makes thismethod the preferred method for most situations. More details on this cache helpercan be found on the cache

    handlerpage.

    Iterator (unsorted-external)

    The unsorted-externalmethod is a wrapper around the netsnmp_continercontainer_iterator. This method is

    also very flexible, with a very simple interface. When a request is received for a table, a get_nextroutine is

    called repeatedly to loop over every item in the datastore(s). The agent will then select the appropriate row for

    the request. Due to the inefficency of this method, it is only recommended for small datastores, or when memorycontraints are very tight and response times are not important.

    Direct

    For advanced users, a custom netsnmp_containercan be used. This method lets you wrap a

    netsnmp_containeraround an existing datastore/access method. Be warned, however, that the implementation

    for the container'sfind-nextmethod must handle SNMP's lexicographical ordering rules.

    Data manipulation

    Once the appropriate data structure is retrieved from the data store, the data manipulation routines will be

    called. For each object defined in your mib module, a function will be generated to extract the data value from

    the data structure located during the data lookup phase.

    For read-write objects, things get more complicated. SET requests are processed in multiple steps, so there are

    multiple functions generated for each object. These include functions for syntax checks, value checks, undo

    setup, value set and undo value set. Additionlly, several functions are generated per table, for tasks which only

    need to be performed once, even if multiple objects are set for a given index. Two examples are consistency

    checks and commit changes. All of these functions will be discussed in more detail later in the tutorial.

    http://www.net-snmp.org/wiki/index.php/Cache_handlerhttp://www.net-snmp.org/wiki/index.php/Containershttp://www.net-snmp.org/wiki/index.php/Cache_handler
  • 7/30/2019 MIB for Dummies - Net-SNMP Wiki

    4/4

    15/08/13 MIB for Dummies - Net-SNMP Wiki

    www.net-snmp.org/wiki/index.php/MIB_for_Dummies 4/4

    Getting Started

    There are several example implementations. The process of implementing a module using the MFD configuration

    file can be broken down into several steps. You may wish to refer to this list in the future as it is a good high-

    level todo list for every module you implement.

    Generating the template code by running mib2c appropriately

    mib2c -c mib2c.mfd.confImplementing a netsnmp_containerfor data access

    edit theMYTABLE.h file, adding column storage items to theMYTABLE_rowreq_ctx_s structure

    edit theMYTABLE_data_access.c file, filling out theMYTABLE_container_loadfunction to load

    all the data into a container cache.

    (create multiple rowreq_ctx objects and insert them into the container)

    Implementing GET request functions

    In theMYTABLE_data_get.c file, fill out each COLUMN_get() to copy the data from your

    row_req cache object to the COLUMN_val_ptr objects.

    Implementing SET request functions

    Basic Tables

    These examples are fairly basic, and should suffice for most simple MIB tables and data stores.

    MFD:IF-MIB Tables

    ifTable: A simple table with a single integer index which gets it's data from a text file. (Partial, read-

    only implementation)

    ifXtable: A simple table with a single integer index which gets it's data from a text file, and caches

    it. (Partial, read-only implementation)

    Intermediate Tables

    These examples are sill fairly basic, but we are starting to add a few twists.

    TBD (set support; row creation?)

    Advanced Tables

    These examples show how to deal with complicated issues that sometimes come up while implementing MIB

    modules..

    TBD ...

    Retrieved from "http://www.net-snmp.org/wiki/index.php?title=MIB_for_Dummies&oldid=5279"

    This page was last modified on 28 July 2011, at 18:17.

    http://www.net-snmp.org/wiki/index.php?title=MIB_for_Dummies&oldid=5279http://www.net-snmp.org/wiki/index.php/MFD:ifXTablehttp://www.net-snmp.org/wiki/index.php/MFD:ifTablehttp://www.net-snmp.org/wiki/index.php/MFD:IF-MIBhttp://www.net-snmp.org/wiki/index.php/SEThttp://www.net-snmp.org/wiki/index.php/GEThttp://www.net-snmp.org/wiki/index.php?title=Container&action=edit&redlink=1