MFD:ifXTable:Data Access

From Net-SNMP Wiki
Jump to: navigation, search

MFD : ifXTable data access

The net-snmp access routine returns a container of netsnmp_interface_entry pointers that contain a snapshot of the current data. However, as I mentioned before, this structure does not contain all the data we need for ifXTable mib. For example, the Linux kernel doesn't report the last time an interface was changed. We have to periodically poll for the interfaces, and check to see if each entry has changed. This polling has the added benefit of always keeping our cache current.


Initialization

When the agent starts up and initializes, the ifXTable_init_data function will be called to allow us to do any initilization for our data access functions. We don't have any, so we'll just leave the function body empty.

The cached-container access method also has it's own initilization routine, ifXTable_container_init. This allow you to use a custom container or tweak the cache parameters. We'll cover both of these more advanced topic in a later tutorial. For now, we'll leave the function body alone, using the default container and default cache parameters.


Loading the cache

int
ifXTable_cache_load(netsnmp_container * container)
{
    netsnmp_container * ifcontainer;
    ifXTable_rowreq_ctx *rowreq_ctx;

    DEBUGMSGTL(("verbose:ifXTable_cache_load", "called\n"));

    /*
     * TODO: update container
     *
     * loop over your data, allocate, set index, insert into the container
     */
    /*
     * get a container of netsnmp_interface_entry pointers from
     * the data access routine.
     */
    ifcontainer =
        netsnmp_access_interface_container_load(NULL,
                                                NETSNMP_ACCESS_INTERFACE_INIT_NOFLAGS);

    /*
     * netsnmp_containers don't have efficient iterators yet, so use the
     * foreach macro to get a callback for each interface.
     */
    CONTAINER_FOR_EACH(ifcontainer,
                       (netsnmp_container_obj_func*)_check_interface_entry_for_updates,
                       container);

    /*
     * free the container. we've either claimed each ifentry, or released it,
     * so the dal function doesn't need to clear the container.
     */
    netsnmp_access_interface_container_free(ifcontainer,
                                            NETSNMP_ACCESS_INTERFACE_FREE_DONT_CLEAR );
              
    return MFD_SUCCESS;
}
    while (1) {
        /*
         * TODO: check for end of data
         *
         * bail out if there is no more data
         */
        if (ifXTable_OUT_OF_DATA())
            break;

        /*
         * allocate an row context and set the index(es)
         */
        rowreq_ctx = ifXTable_allocate_rowreq_ctx(NULL);
        if (MFD_SUCCESS != ifXTable_indexes_set(rowreq_ctx, ifIndex)) {
            snmp_log(LOG_ERR, "error setting index while loading "
                     "ifXTable cache.\n");
            ifXTable_release_rowreq_ctx(rowreq_ctx);
            continue;
        }

        /*
         * TODO: populate data context
         */
        /*
         * TRANSIENT or semi-TRANSIENT data:
         * can copy data in row_prep or here, depending on when you
         * want to take the hit. Either copy the data now, or save
         * any info needed and do it in row_prep.
         */

        /*
         * insert into table container
         */
        CONTAINER_INSERT(container, rowreq_ctx);
    }

    return MFD_SUCCESS;