________________________________
From: Wes Hardaker <[email protected]>
To: sujata patra <[email protected]>
Cc: Wes Hardaker <[email protected]>; 
[email protected]
Sent: Wed, December 22, 2010 10:05:34 AM
Subject: Re: doubt in nested table implementation

>>>>> On Tue, 21 Dec 2010 20:15:12 -0800 (PST), sujata patra 
>>>>><[email protected]> said:

sp> Thanks Wes. That's right . I have got it now and able to progress . I was 
sp> confused whether to make tree structure .

Tree structures are not supported by SMIv2 (the MIB language).  If
you're getting the point of needing to do that it's probably time to
pick up a book on good mib design ("Understanding SNMP MIBs" is a good one).

sp> This is basically making the tree to linear array by storing the
sp> indexes of each parent/grand parent node. right ?

If you're familiar with relational databases, MIBs follow similar
concepts.  The topic is much longer than I can explain quickly over
mail, so I suggest you either read a good book or read the RFC itself
(RFC2578).

sp> I have added  all the columns.
sp> netsnmp_table_helper_add_indexes(table_info,

The only columns you should add are the indexes.  The rest of them
shouldn't be there.
But how to tell about the columns? I do update the values of
 table_info->min_column =;
 table_info->max_column = ;

stack trace of crash point. This is  for a single table.

#0  snmp_set_var_value (vars=0x0, value=0xbfdcea12, len=10) at snmp_client.c:765
#1  0x08048f43 in messageTable_get_next_data_point (my_loop_context=0xbfdcecd8, 
my_data_context=0xbfdcecd4, put_index_data=0x91c17a8, 

    mydata=0x9161718) at abcModule.c:158
#2  0x0015f572 in netsnmp_table_iterator_helper_handler (handler=0x9161748, 
reginfo=0x9161438, reqinfo=0x91a9d48, requests=0x91aa728)
    at helpers/table_iterator.c:587
#3  0x001206c2 in netsnmp_call_handler (next_handler=0x9161748, 
reginfo=0x9161438, reqinfo=0x91a9d48, requests=0x91aa728)
    at agent_handler.c:522

     netsnmp_table_helper_add_indexes(table_info, ASN_INTEGER,
                                                  /* ASN_OCTET_STR,
                                                  ASN_INTEGER,
                                                  ASN_INTEGER, 
                                                  ASN_INTEGER,*/ 0);


It works fine if I uncomment the commented part from the above . any missing 
piece? attaching the file for your reference


-- 
Wes Hardaker
Please mail all replies to [email protected]



      
/*
 * Note: this file originally auto-generated by mib2c using
 *  : mib2c.iterate.conf 19302 2010-08-13 12:19:42Z dts12 $
 */

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "abcModule.h"
//#define SA_REPEAT 5
#define STRMAX 256
static void update_stats( unsigned int, void* );



/** Initializes the abcModule module */
void
init_abcModule(void)
{
  /* here we initialize all the tables we're planning on supporting */
    initialize_table_messageTable();
}

 

/** Initialize the messageTable table by defining its contents and how it's 
structured */
void
initialize_table_messageTable(void)
{
    const oid messageTable_oid[] = {1,3,6,1,4,1,11456,3,3};
    const size_t messageTable_oid_len   = OID_LENGTH(messageTable_oid);
    netsnmp_handler_registration    *reg;
    netsnmp_iterator_info           *iinfo;
    netsnmp_table_registration_info *table_info;

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

    reg = netsnmp_create_handler_registration(
              "messageTable",     messageTable_handler,
              messageTable_oid, messageTable_oid_len,
              HANDLER_CAN_RWRITE
              );

    table_info = SNMP_MALLOC_TYPEDEF( netsnmp_table_registration_info );
     netsnmp_table_helper_add_indexes(table_info, ASN_INTEGER, 
                                                /* ASN_OCTET_STR,
                                                 ASN_INTEGER,
                                                 ASN_INTEGER, 
                                                 ASN_INTEGER,*/ 0);

    table_info->min_column = COLUMN_MESSAGETYPE;
    table_info->max_column = COLUMN_CURRENTMSGID;
    
    iinfo = SNMP_MALLOC_TYPEDEF( netsnmp_iterator_info );
    iinfo->get_first_data_point = messageTable_get_first_data_point;
    iinfo->get_next_data_point  = messageTable_get_next_data_point;
    iinfo->table_reginfo        = table_info;
    
    netsnmp_register_table_iterator( reg, iinfo );
    snmp_alarm_register( 5, SA_REPEAT , update_stats, NULL );

    /* Initialise the contents of the table here */
}

    /* Typical data structure for a row entry */
struct messageTable_entry {
    /* Index values */
    long messageIndex;

    /* Column values */
    char messageType[10];
    long sentMsgCount;

    long errorMsgCount;

    long currentMsgID;


    /* Illustrate using a simple linked list */
    int   valid;
    struct messageTable_entry *next;
};

struct messageTable_entry  *messageTable_head;

/* create a new row in the (unsorted) table */
struct messageTable_entry *
messageTable_createEntry(
                 long  messageIndex
                ) {
    struct messageTable_entry *entry;

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

    entry->messageIndex = messageIndex;
    entry->next = messageTable_head;
    messageTable_head = entry;
    return entry;
}

/* remove a row from the table */
void
messageTable_removeEntry( struct messageTable_entry *entry ) {
    struct messageTable_entry *ptr, *prev;

    if (!entry)
        return;    /* Nothing to remove */

    for ( ptr  = messageTable_head, prev = NULL;
          ptr != NULL;
          prev = ptr, ptr = ptr->next ) {
        if ( ptr == entry )
            break;
    }
    if ( !ptr )
        return;    /* Can't find it */

    if ( prev == NULL )
        messageTable_head = ptr->next;
    else
        prev->next = ptr->next;

    SNMP_FREE( entry );   /* XXX - release any other internal resources */
}


/* Example iterator hook routines - using 'get_next' to do most of the work */
netsnmp_variable_list *
messageTable_get_first_data_point(void **my_loop_context,
                          void **my_data_context,
                          netsnmp_variable_list *put_index_data,
                          netsnmp_iterator_info *mydata)
{
    *my_loop_context = messageTable_head;
    return messageTable_get_next_data_point(my_loop_context, my_data_context,
                                    put_index_data,  mydata );
}

netsnmp_variable_list *
messageTable_get_next_data_point(void **my_loop_context,
                          void **my_data_context,
                          netsnmp_variable_list *put_index_data,
                          netsnmp_iterator_info *mydata)
{
    struct messageTable_entry *entry = (struct messageTable_entry 
*)*my_loop_context;
    netsnmp_variable_list *idx = put_index_data;
    int msg_id, msg_sent, msg_err = 0;
    char type[10];
    if ( entry ) {
        snmp_set_var_typed_integer( idx, ASN_INTEGER, entry->messageIndex );
        
        idx = idx->next_variable;
        //type = (char*) malloc(11);
        //char type1 ='j';
        strcpy(type, entry->messageType);
        snmp_set_var_value(idx, (u_char *)&type, sizeof(type));
 
        idx = idx->next_variable;
        msg_id = entry->currentMsgID;
        snmp_set_var_value(idx, (u_char *)&msg_id, sizeof(msg_id));

        idx = idx->next_variable;
        msg_sent = entry->sentMsgCount;
        snmp_set_var_value(idx, (u_char *)&msg_sent, sizeof(msg_sent));
        
        idx = idx->next_variable;
        msg_err = entry->errorMsgCount;
        snmp_set_var_value(idx, (u_char *)&msg_err, sizeof(msg_err));

        *my_data_context = (void *)entry;
        *my_loop_context = (void *)entry->next;
        return put_index_data;
    } else {
        return NULL;
    }

}


/** handles requests for the messageTable table */
int
messageTable_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;
    struct messageTable_entry          *table_entry;

    DEBUGMSGTL(("abcModule: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 messageTable_entry *)
                              netsnmp_extract_iterator_context(request);
            table_info  =     netsnmp_extract_table_info(      request);
    
            switch (table_info->colnum) {
            case COLUMN_MESSAGETYPE:
                if ( !table_entry ) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR,
                                          table_entry->messageType,
                                          sizeof(table_entry->messageType));   
                break;
            case COLUMN_SENTMSGCOUNT:
                if ( !table_entry ) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                snmp_set_var_typed_integer( request->requestvb, ASN_INTEGER,
                                            table_entry->sentMsgCount);
                break;
            case COLUMN_ERRORMSGCOUNT:
                if ( !table_entry ) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                snmp_set_var_typed_integer( request->requestvb, ASN_INTEGER,
                                            table_entry->errorMsgCount);
                break;
            case COLUMN_CURRENTMSGID:
                if ( !table_entry ) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                snmp_set_var_typed_integer( request->requestvb, ASN_INTEGER,
                                            table_entry->currentMsgID);
                break;
            default:
                netsnmp_set_request_error(reqinfo, request,
                                          SNMP_NOSUCHOBJECT);
                break;
            }
        }
        break;

        /*
         * Write-support
         */
   case MODE_SET_RESERVE1:
        break;

    case MODE_SET_RESERVE2:
        break;

    case MODE_SET_FREE:
        break;

    case MODE_SET_ACTION:
        break;

    case MODE_SET_UNDO:
       break;

    case MODE_SET_COMMIT:
        break;
    }
    return SNMP_ERR_NOERROR;
}
static void
update_stats(unsigned int reg, void * vargs ) {
    FILE *fp;
    struct messageTable_entry *this;
    char buf[STRMAX];
    char type[10];
    int index = 0;
    int msg_id, msg_sent, msg_err = 0;
    memset(type,0x00,10);
    fp = fopen( "data.txt", "r" );

    if ( !fp ) {
        return ;
    }
    while ( fgets( buf, STRMAX, fp )) {

        this = SNMP_MALLOC_TYPEDEF( struct messageTable_entry );
        /* Unpick 'buf' and populate 'this' */
        this->messageIndex = index++;
 
            sprintf(type, "%s", strtok(buf, ","));
        strcpy(this->messageType, type);
        
        msg_id = atoi(strtok(NULL, ","));
        this->currentMsgID = msg_id;

        msg_sent = atoi(strtok(NULL, ","));
        this->sentMsgCount = msg_sent;
        
        msg_err = atoi(strtok(NULL, ","));
        this->errorMsgCount = msg_err;

        this->next = messageTable_head;
        messageTable_head = this;    /* Iterate helper is fine with unordered 
lists! */
    }
    fclose(fp);
    return ;  /* OK */
}

------------------------------------------------------------------------------
Forrester recently released a report on the Return on Investment (ROI) of
Google Apps. They found a 300% ROI, 38%-56% cost savings, and break-even
within 7 months.  Over 3 million businesses have gone Google with Google Apps:
an online email calendar, and document program that's accessible from your 
browser. Read the Forrester report: http://p.sf.net/sfu/googleapps-sfnew
_______________________________________________
Net-snmp-coders mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to