Re: mib2c.table_data.conf, cache timeout period does'nt seems to change.

Francois Bouchard <[email protected]>
Newsgroups gmane.network.net-snmp.user
Message-ID <CAAZiaZfehTuEPPhCT+tE5vV+s6TnjbESVVh35KLzxgz3ZamkhQ@mail.gmail.com>
> I'm sorry - I don't have the time to try and re-create the rest of the
> code that's missing here.   If you want assistance with tracking
> down the problem, can you please post the *complete* code files,
> so that we can try compiling them into a working module.
Here is the code for a cached snmp table module.




Francois

2012/2/13 Dave Shield <[email protected]>

> On 13 February 2012 15:46, Francois Bouchard <[email protected]>
> wrote:
> >> Perhaps you could post the code that you're working with
> >> so that we can test this
> > Yes, here is the code for the initialize function (generated using mib2c
> -c
> > mib2c.table_data.conf mpbcRMHAlarmTable  (when asked : option 1
> (cache))  )
>
>
> I'm sorry - I don't have the time to try and re-create the rest of the
> code that's missing here.   If you want assistance with tracking
> down the problem, can you please post the *complete* code files,
> so that we can try compiling them into a working module.
>
> Dave
>

------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d

_______________________________________________
Net-snmp-users mailing list
[email protected]
Please see the following page to unsubscribe or change other options:
https://lists.sourceforge.net/lists/listinfo/net-snmp-users
mpbcRMHAlarmTable.c (text/x-csrc, 13.2 KB)
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "mpbcRMHAlarmTable.h"

#define SD_HOME_NEM_CURALRM	"/mnt/sd/MPBC/Alarms/NEM_CurrentAlarm" 

#define RMH_NEID_MAX_LEN 	100
#define RMH_ALARM_MODULE_LEN 	100
#define RMH_ALARM_DESC_LEN	100
#define RMH_TIME_MAX_LEN	100	


// lenght of buffer to contain a whole alarm row from file 
#define STRMAX 			150


/* Typical data structure for a row entry */
struct mpbcRMHAlarmTable_entry {
    /* Index values */
    long mpbcRMHAlarmIndex;

    /* Column values */
    char mpbcRMHAlarmNEID[RMH_NEID_MAX_LEN];
    size_t mpbcRMHAlarmNEID_len;
    long mpbcRMHAlarmSlot;
    char mpbcRMHAlarmModule[RMH_ALARM_MODULE_LEN];
    size_t mpbcRMHAlarmModule_len;
    long mpbcRMHAlarmID;
    char mpbcRMHAlarmDescription[RMH_ALARM_DESC_LEN];
    size_t mpbcRMHAlarmDescription_len;
    long mpbcRMHAlarmSeverity;
    char mpbcRMHAlarmStartTime[RMH_TIME_MAX_LEN];
    size_t mpbcRMHAlarmStartTime_len;

    int   valid;
};




/** Initializes the mpbcRMHAlarmTable module */
void init_mpbcRMHAlarmTable(void)
{

  /* here we initialize all the tables we're planning on supporting */
    initialize_table_mpbcRMHAlarmTable();

}

/** Initialize the mpbcRMHAlarmTable table by defining its contents and how it's structured */
void initialize_table_mpbcRMHAlarmTable(void)
{
    const oid mpbcRMHAlarmTable_oid[] = {1,3,6,1,4,1,4464,2,1,1,1};
    const size_t mpbcRMHAlarmTable_oid_len   = OID_LENGTH(mpbcRMHAlarmTable_oid);
    netsnmp_handler_registration    *reg;
    netsnmp_tdata                   *table_data;
    netsnmp_table_registration_info *table_info;
    netsnmp_cache                   *cache;

    DEBUGMSGTL(("mpbcRMHAlarmTable:init", "initializing table mpbcRMHAlarmTable\n"));

    snmp_log(LOG_ERR, "initializing table mpbcRMHAlarmTable\n"); 

    reg = netsnmp_create_handler_registration(
              "mpbcRMHAlarmTable",     mpbcRMHAlarmTable_handler,
              mpbcRMHAlarmTable_oid, mpbcRMHAlarmTable_oid_len,
              HANDLER_CAN_RONLY
              );

    table_data = netsnmp_tdata_create_table( "mpbcRMHAlarmTable", 0 );
    if (NULL == table_data) {
        snmp_log(LOG_ERR,"error creating tdata table for mpbcRMHAlarmTable\n");
        return;
    }
    cache = netsnmp_cache_create(MPBCRMHALARMTABLE_TIMEOUT,
                                  mpbcRMHAlarmTable_load, mpbcRMHAlarmTable_free,
                                  mpbcRMHAlarmTable_oid, mpbcRMHAlarmTable_oid_len);
    if (NULL == cache) {
        snmp_log(LOG_ERR,"error creating cache for mpbcRMHAlarmTable\n");
    }
    else
        cache->magic = (void *)table_data;
    table_info = SNMP_MALLOC_TYPEDEF( netsnmp_table_registration_info );
    if (NULL == table_info) {
        snmp_log(LOG_ERR,"error creating table info for mpbcRMHAlarmTable\n");
        return;
    }
    netsnmp_table_helper_add_indexes(table_info,
                           ASN_INTEGER,  /* index: mpbcRMHAlarmIndex */
                           0);

    table_info->min_column = COLUMN_MPBCRMHALARMNEID;
    table_info->max_column = COLUMN_MPBCRMHALARMSTARTTIME;
    
    netsnmp_tdata_register( reg, table_data, table_info );
    if (cache) {
 	netsnmp_inject_handler( reg, netsnmp_cache_handler_get(cache));   
    }



    /* Initialise the contents of the table here */
    // N/A

}

/**************************************************************************
FUNCT:                                         
***************************************************************************/
/* create a new row in the table */
netsnmp_tdata_row * mpbcRMHAlarmTable_createEntry(netsnmp_tdata *table_data, long  mpbcRMHAlarmIndex)
{
    struct mpbcRMHAlarmTable_entry *entry;
    netsnmp_tdata_row *row;

    snmp_log(LOG_ERR, "mpbcRMHAlarmTable_createEntry\n"); 

    entry = SNMP_MALLOC_TYPEDEF(struct mpbcRMHAlarmTable_entry);
    if (!entry)
        return NULL;

    row = netsnmp_tdata_create_row();
    if (!row) {
        SNMP_FREE(entry);
        return NULL;
    }
    row->data = entry;

    DEBUGMSGT(("mpbcRMHAlarmTable:entry:create", "row 0x%x\n", (uintptr_t)row));
    entry->mpbcRMHAlarmIndex = mpbcRMHAlarmIndex;
    netsnmp_tdata_row_add_index( row, ASN_INTEGER,
                                 &(entry->mpbcRMHAlarmIndex),
                                 sizeof(entry->mpbcRMHAlarmIndex));
    if (table_data)
        netsnmp_tdata_add_row( table_data, row );
    return row;
}



/**************************************************************************
FUNCT:                                         
***************************************************************************/
/* remove a row from the table */
void mpbcRMHAlarmTable_removeEntry(netsnmp_tdata * table_data, netsnmp_tdata_row *row)
{
    struct mpbcRMHAlarmTable_entry *entry;
    snmp_log(LOG_ERR, "mpbcRMHAlarmTable_removeEntry\n"); 
    if (!row)
        return;    /* Nothing to remove */

    DEBUGMSGT(("mpbcRMHAlarmTable:entry:remove", "row 0x%x\n", (uintptr_t)row));

    entry = (struct mpbcRMHAlarmTable_entry *)row->data;
    SNMP_FREE( entry );   /* XXX - release any other internal resources */

    if (table_data)
        netsnmp_tdata_remove_and_delete_row( table_data, row );
    else
        netsnmp_tdata_delete_row( row );    
}



/**************************************************************************
FUNCT:                                         
***************************************************************************/
/* cache handling - set up table_data list from a suitable file */
int mpbcRMHAlarmTable_load( netsnmp_cache *cache, void *vmagic )
{
    long idx = 0;
    netsnmp_tdata     *table = (netsnmp_tdata *)vmagic;
    netsnmp_tdata_row *row;
    struct mpbcRMHAlarmTable_entry *this;
    FILE *fp;
    char buf[STRMAX];
    long  mpbcRMHAlarmIndex = 0;

    /* column items (dummy values for now */
    long lmpbcRMHAlarmSlot = 16;
    long lmpbcRMHAlarmID = 245;
    long lmpbcRMHAlarmSeverity = 1;

    char strRMHAlarmNEID[] = "Montreal";
    char strRMHAlarmModule[] = "OTM4";
    char strRMHAlarmDescription[] = "Loss of frame(s)";
    char strRMHAlarmStartTime[]  = "13 feb 2012 08:09:12.843";


    /* The basic load routine (the data to be reported is held in a file - with one row of the file
       for each row of the table.  */
    fp = fopen( SD_HOME_NEM_CURALRM, "r" );
    if ( !fp ) {
        return -1;
    }
    snmp_log(LOG_ERR, "mpbcRMHAlarmTable_load: loaded %s\n", SD_HOME_NEM_CURALRM); 
    while ( fgets( buf, STRMAX, fp )) {

        /* XXX - Unpick 'buf' to extract the individual field values
                 (or at least the index values for this row) ... */

        row = mpbcRMHAlarmTable_createEntry(table, mpbcRMHAlarmIndex++);
        if (row == NULL)
            continue;

	this = (struct mpbcRMHAlarmTable_entry *)row->data;


        /* Populate the 'this' data structure with column values extracted from 'buf' above */

        // Alarm entry index
	this->mpbcRMHAlarmIndex = mpbcRMHAlarmIndex;

        // RMH Alarm NEID
	memcpy(this->mpbcRMHAlarmNEID, strRMHAlarmNEID, sizeof(strRMHAlarmNEID));
	this->mpbcRMHAlarmNEID_len = sizeof(strRMHAlarmNEID);

	// RMH Alarm slot number
	this->mpbcRMHAlarmSlot = lmpbcRMHAlarmSlot;

	// RMH Alarm module name
	memcpy(this->mpbcRMHAlarmModule, strRMHAlarmModule, sizeof(strRMHAlarmModule));
	this->mpbcRMHAlarmModule_len = sizeof(strRMHAlarmModule);

	// RMH Alarm number
	this->mpbcRMHAlarmID = lmpbcRMHAlarmID;
	
	// RMH Alarm description
	memcpy(this->mpbcRMHAlarmDescription, strRMHAlarmDescription, sizeof(strRMHAlarmDescription));
	this->mpbcRMHAlarmDescription_len = sizeof(strRMHAlarmDescription);

	// RMH Alarm Severity
	this->mpbcRMHAlarmSeverity = lmpbcRMHAlarmSeverity;


	// RMH Alarm start time
	memcpy(this->mpbcRMHAlarmStartTime, strRMHAlarmStartTime, sizeof(strRMHAlarmStartTime));
	this->mpbcRMHAlarmStartTime_len =   sizeof(strRMHAlarmStartTime);

    }
    fclose(fp);
    return 0;  /* OK */
}



/**************************************************************************
FUNCT:                                         
***************************************************************************/
void mpbcRMHAlarmTable_free( netsnmp_cache *cache, void *vmagic )
{
    snmp_log(LOG_ERR, "mpbcRMHAlarmTable_free\n"); 
    netsnmp_tdata     *table = (netsnmp_tdata *)vmagic;
    netsnmp_tdata_row *this;

	while ((this = netsnmp_tdata_row_first(table))) {
        netsnmp_tdata_remove_and_delete_row(table, this);
    }
}



/**************************************************************************
FUNCT:                                         
***************************************************************************/
/** handles requests for the mpbcRMHAlarmTable table */
int mpbcRMHAlarmTable_handler(
    netsnmp_mib_handler               *handler,
    netsnmp_handler_registration      *reginfo,
    netsnmp_agent_request_info        *reqinfo,
    netsnmp_request_info              *requests)
{
    netsnmp_request_info       *request;
    netsnmp_table_request_info *table_info;
    netsnmp_tdata              *table_data;
    netsnmp_tdata_row          *table_row;
    struct mpbcRMHAlarmTable_entry          *table_entry;
    int                         ret;

    DEBUGMSGTL(("mpbcRMHAlarmTable:handler", "Processing request (%d)\n", reqinfo->mode));

    switch (reqinfo->mode) {
        /*
         * Read-support (also covers GetNext requests)
         */
    case MODE_GET:
        for (request=requests; request; request=request->next) {
            table_entry = (struct mpbcRMHAlarmTable_entry *)
                              netsnmp_tdata_extract_entry(request);
            table_info  =     netsnmp_extract_table_info( request);
    
            switch (table_info->colnum) {
            case COLUMN_MPBCRMHALARMNEID:
                if ( !table_entry ) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR,
                                          table_entry->mpbcRMHAlarmNEID,
                                          table_entry->mpbcRMHAlarmNEID_len);
                break;
            case COLUMN_MPBCRMHALARMSLOT:
                if ( !table_entry ) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                snmp_set_var_typed_integer( request->requestvb, ASN_INTEGER,
                                            table_entry->mpbcRMHAlarmSlot);
                break;
            case COLUMN_MPBCRMHALARMMODULE:
                if ( !table_entry ) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR,
                                          table_entry->mpbcRMHAlarmModule,
                                          table_entry->mpbcRMHAlarmModule_len);
                break;
            case COLUMN_MPBCRMHALARMID:
                if ( !table_entry ) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                snmp_set_var_typed_integer( request->requestvb, ASN_INTEGER,
                                            table_entry->mpbcRMHAlarmID);
                break;
            case COLUMN_MPBCRMHALARMDESCRIPTION:
                if ( !table_entry ) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR,
                                          table_entry->mpbcRMHAlarmDescription,
                                          table_entry->mpbcRMHAlarmDescription_len);
                break;
            case COLUMN_MPBCRMHALARMSEVERITY:
                if ( !table_entry ) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                snmp_set_var_typed_integer( request->requestvb, ASN_INTEGER,
                                            table_entry->mpbcRMHAlarmSeverity);
                break;
            case COLUMN_MPBCRMHALARMSTARTTIME:
                if ( !table_entry ) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR,
                                          table_entry->mpbcRMHAlarmStartTime,
                                          table_entry->mpbcRMHAlarmStartTime_len);
                break;
             default:
                netsnmp_set_request_error(reqinfo, request,
                                          SNMP_NOSUCHOBJECT);
                break;
            }
        }
        break;

    }
    return SNMP_ERR_NOERROR;
}
mpbcRMHAlarmTable.h (text/x-chdr, 647 B)
#ifndef MPBCRMHALARMTABLE_H
#define MPBCRMHALARMTABLE_H

#define COLUMN_MPBCRMHALARMINDEX		1
#define COLUMN_MPBCRMHALARMNEID			2
#define COLUMN_MPBCRMHALARMSLOT			3
#define COLUMN_MPBCRMHALARMMODULE		4
#define COLUMN_MPBCRMHALARMID			5
#define COLUMN_MPBCRMHALARMDESCRIPTION		6
#define COLUMN_MPBCRMHALARMSEVERITY		7
#define COLUMN_MPBCRMHALARMSTARTTIME		8
#define MPBCRMHALARMTABLE_TIMEOUT  10	 

/* function declarations */
void init_mpbcRMHAlarmTable(void);
void initialize_table_mpbcRMHAlarmTable(void);
Netsnmp_Node_Handler mpbcRMHAlarmTable_handler;
NetsnmpCacheLoad mpbcRMHAlarmTable_load;
NetsnmpCacheFree mpbcRMHAlarmTable_free;


#endif
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.