Search code examples
csnmpnet-snmp

using net-snmp:can not regularly read and update data from a file


/*
 * Note: this file originally auto-generated by mib2c using
 *        $ mib2c.create-dataset.conf
 */

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "poolTable.h"

/** Initialize the poolTable table by defining its contents and how it's structured */

int initialize_table_poolTable(void)
{
     const oid poolTable_oid[] = {1,3,6,1,4,1,21068,4,1};
     netsnmp_table_data_set *table_set;

     /* create the table structure itself */
     table_set = netsnmp_create_table_data_set("poolTable");
     table_set->allow_creation = 1;

     /********************* Adding indexes************************/   
     DEBUGMSGTL(("initialize_table_poolTable","adding indexes to table poolTable\n"));

     netsnmp_table_set_add_indexes(table_set,ASN_INTEGER,/* index:poolIndex*/0);

     DEBUGMSGTL(("initialize_table_poolTable","adding column types to table poolTable\n"));         


     netsnmp_table_set_multi_add_default_row(table_set,COLUMN_POOLINDEX, ASN_INTEGER, 0,
                                            NULL, 0,COLUMN_POOLINOCTETS, ASN_COUNTER, 0,
                                            NULL, 0,0);

     netsnmp_register_table_data_set(

                     netsnmp_create_handler_registration("poolTable",                                                  poolTable_handler,poolTable_oid,
                                           OID_LENGTH(poolTable_oid),
                                            HANDLER_CAN_RWRITE),
                                 table_set, NULL);

     int index=1; long data[2];

     FILE *fin;

     fin=fopen("/home/xx/data.txt","r");

     if(!fin)
     {
           snmp_log(LOG_ERR,"fopen failed in locating file\n");
           return -1;
     }

     while(!feof(fin))
     {
           while((fscanf(fin,"%ld %ld",&data[0],&data[1])) == 1)
           {
           }

           if(feof(fin) == 0)
           {          
                fillTableWithValues(table_set,index,data[0],data[1]);
           }
           index++;        
     }
     return;
}

void init_poolTable(void)
{
     initialize_table_poolTable();
}

int poolTable_handler(netsnmp_mib_handler               *handler,
                netsnmp_handler_registration      *reginfo,
                netsnmp_agent_request_info        *reqinfo,
                netsnmp_request_info              *requests) 
{
   return SNMP_ERR_NOERROR;
} 

void fillTableWithValues(netsnmp_table_data_set *table_set,int i,int Value1,int Value2)
{

     netsnmp_table_row *row;
     row = netsnmp_create_table_data_row();
     int index;
     index=i;
     netsnmp_table_row_add_index(row, ASN_INTEGER, &index,  sizeof(index));

     //setting a value to an integer column of my table
     int AValue = Value1;
     netsnmp_set_row_column(row, COLUMN_POOLINDEX, ASN_INTEGER, &AValue, sizeof(AValue));

     int BValue = Value2;
     netsnmp_set_row_column(row,COLUMN_POOLINOCTETS, ASN_COUNTER, &BValue, sizeof(BValue));

     netsnmp_table_dataset_add_row(table_set, row);
     netsnmp_register_auto_data_table(table_set, NULL);

}//end FillDaPathTableUp()

/********************************************************
Data.txt
*************************************************************
1 567
2 456
3 578
4 688
5 790
6 378
*************************************************************/

Above is my code for ‘table’.

I am compiling it as a “subagent” which is getting data from a file.

But my issue is it is taking the data from first time only.

The file “data.txt” is changing its values frequently but with snmpwalk I am only getting the same data every time. How can I get the updated data ? Where am I making a mistake? If it is possible with a for(;;)loop, then where should I add it? Please help me.


Solution

  • It's returning static values because you're only reading the file during initialization of the whole system. You need to move the file-reading code into a separate function and call it on a regular basis, like during the actual request handling, using the cache handler routines to call it as needed, or by using snmp_alarm to trigger a timer to be called every once in a while to read and load the data (and delete the old data, of course).