使用网络snmp:无法定期读取和更新文件中的数据 /* *注:此文件最初由mib2c使用自动生成 *$mib2c.create-dataset.conf */ #包括 #包括 #包括 #包括“poolTable.h” /**通过定义poolTable表的内容及其结构来初始化该表*/ int初始化\u表\u池表(无效) { const-oid poolTable_-oid[]={1,3,6,1,4,121068,4,1}; netsnmp_table_data_set*table_set; /*创建表结构本身*/ table_set=netsnmp_create_table_data_set(“poolTable”); 表设置->允许创建=1; /*********************添加索引************************/ DEBUGMSGTL((“初始化_table_poolTable”,“向表poolTable\n添加索引”); netsnmp_table_set_add_索引(table_set,ASN_INTEGER,/*索引:poolidex*/0); DEBUGMSGTL((“初始化_table_poolTable”,“将列类型添加到表poolTable\n”); netsnmp\u table\u set\u multi\u add\u default\u行(table\u set,COLUMN\u pooldex,ASN\u INTEGER,0, NULL,0,八位字节列,ASN计数器,0, 空,0,0); netsnmp\u寄存器\u表\u数据\u集( netsnmp\u创建\u处理程序\u注册(“poolTable”、poolTable\u处理程序、poolTable\u oid、, OID_长度(池表_OID), 处理器(可以写入), 表_集,空); int index=1;长数据[2]; 文件*fin; fin=fopen(“/home/xx/data.txt”,“r”); 如果(!fin) { snmp_日志(log_ERR,“fopen无法定位文件”); 返回-1; } 而(!feof(fin)) { 而((fscanf(fin,“%ld%ld”,&data[0],&data[1]))==1) { } 如果(feof(fin)==0) { 用值填充表格(表格集合,索引,数据[0],数据[1]); } 索引++; } 返回; } void init_poolTable(void) { 初始化_table_poolTable(); } int poolTable_handler(netsnmp_mib_handler*handler, netsnmp\u handler\u注册*reginfo, netsnmp\u代理\u请求\u信息*reqinfo, netsnmp_请求_信息*请求) { 返回SNMP\u ERR\u NOERROR; } void fillTableWithValues(netsnmp_table_data_set*table_set,int i,int Value1,int Value2) { netsnmp_table_row*行; 行=netsnmp_创建_表_数据_行(); 整数指数; 指数=i; netsnmp_table_row_add_index(row,ASN_INTEGER,&index,sizeof(index)); //将值设置为“我的表”的整数列 int AValue=Value1; netsnmp_set_row_column(row,column_pooldex,ASN_INTEGER,&AValue,sizeof(AValue)); int BValue=Value2; netsnmp_set_row_column(行、列、八位字节、ASN_计数器和BValue、sizeof(BValue)); netsnmp_table_dataset_add_row(table_set,row); netsnmp\ u寄存器\自动\数据\表(表\集,空); }//结束FillDaPathTableUp() /******************************************************** Data.txt ************************************************************* 1 567 2 456 3 578 4 688 5 790 6 378 *************************************************************/

使用网络snmp:无法定期读取和更新文件中的数据 /* *注:此文件最初由mib2c使用自动生成 *$mib2c.create-dataset.conf */ #包括 #包括 #包括 #包括“poolTable.h” /**通过定义poolTable表的内容及其结构来初始化该表*/ int初始化\u表\u池表(无效) { const-oid poolTable_-oid[]={1,3,6,1,4,121068,4,1}; netsnmp_table_data_set*table_set; /*创建表结构本身*/ table_set=netsnmp_create_table_data_set(“poolTable”); 表设置->允许创建=1; /*********************添加索引************************/ DEBUGMSGTL((“初始化_table_poolTable”,“向表poolTable\n添加索引”); netsnmp_table_set_add_索引(table_set,ASN_INTEGER,/*索引:poolidex*/0); DEBUGMSGTL((“初始化_table_poolTable”,“将列类型添加到表poolTable\n”); netsnmp\u table\u set\u multi\u add\u default\u行(table\u set,COLUMN\u pooldex,ASN\u INTEGER,0, NULL,0,八位字节列,ASN计数器,0, 空,0,0); netsnmp\u寄存器\u表\u数据\u集( netsnmp\u创建\u处理程序\u注册(“poolTable”、poolTable\u处理程序、poolTable\u oid、, OID_长度(池表_OID), 处理器(可以写入), 表_集,空); int index=1;长数据[2]; 文件*fin; fin=fopen(“/home/xx/data.txt”,“r”); 如果(!fin) { snmp_日志(log_ERR,“fopen无法定位文件”); 返回-1; } 而(!feof(fin)) { 而((fscanf(fin,“%ld%ld”,&data[0],&data[1]))==1) { } 如果(feof(fin)==0) { 用值填充表格(表格集合,索引,数据[0],数据[1]); } 索引++; } 返回; } void init_poolTable(void) { 初始化_table_poolTable(); } int poolTable_handler(netsnmp_mib_handler*handler, netsnmp\u handler\u注册*reginfo, netsnmp\u代理\u请求\u信息*reqinfo, netsnmp_请求_信息*请求) { 返回SNMP\u ERR\u NOERROR; } void fillTableWithValues(netsnmp_table_data_set*table_set,int i,int Value1,int Value2) { netsnmp_table_row*行; 行=netsnmp_创建_表_数据_行(); 整数指数; 指数=i; netsnmp_table_row_add_index(row,ASN_INTEGER,&index,sizeof(index)); //将值设置为“我的表”的整数列 int AValue=Value1; netsnmp_set_row_column(row,column_pooldex,ASN_INTEGER,&AValue,sizeof(AValue)); int BValue=Value2; netsnmp_set_row_column(行、列、八位字节、ASN_计数器和BValue、sizeof(BValue)); netsnmp_table_dataset_add_row(table_set,row); netsnmp\ u寄存器\自动\数据\表(表\集,空); }//结束FillDaPathTableUp() /******************************************************** Data.txt ************************************************************* 1 567 2 456 3 578 4 688 5 790 6 378 *************************************************************/,c,snmp,net-snmp,C,Snmp,Net Snmp,上面是我的“表”代码 我将它编译为一个“子代理”,它从文件中获取数据 但我的问题是,它只是从第一次获取数据 文件“data.txt”经常更改其值,但使用snmpwalk,每次只能获得相同的数据。 如何获取更新的数据?我哪里出错了? 如果可以使用(;)循环的,那么应该在哪里添加它? 请帮助我。它返回静态值,因为您只在整个系统初始化期间读取文件。您需要将文件读取代码移动到一个单独的函数中,并定期调用它,就像在实际的请求处理过程中一样,根据需要使用缓存处理程序例程调用它,或者通过使用snmp\u al

上面是我的“表”代码

我将它编译为一个“子代理”,它从文件中获取数据

但我的问题是,它只是从第一次获取数据

文件“data.txt”经常更改其值,但使用snmpwalk,每次只能获得相同的数据。 如何获取更新的数据?我哪里出错了? 如果可以使用(;)循环的
,那么应该在哪里添加它?

请帮助我。

它返回静态值,因为您只在整个系统初始化期间读取文件。您需要将文件读取代码移动到一个单独的函数中,并定期调用它,就像在实际的请求处理过程中一样,根据需要使用缓存处理程序例程调用它,或者通过使用
snmp\u alarm
触发一个计时器,每隔一段时间调用一次以读取和加载数据(当然,还可以删除旧数据).

它返回静态值,因为您只在整个系统初始化期间读取文件。您需要将文件读取代码移动到一个单独的函数中,并定期调用它,就像在实际的请求处理过程中一样,根据需要使用缓存处理程序例程调用它,或者通过使用
snmp\u alarm
触发一个计时器,每隔一段时间调用一次以读取和加载数据(当然,还可以删除旧数据).

我更喜欢snmp\u报警函数,但不喜欢unsigned int snmp\u报警寄存器(unsigned int when,unsigned int flags,SNMPAlarmCallback*thecallback,void*clientarg),这里它不接受返回类型为“int”的callback函数。(显示它必须是void!!)是否有任何解决方案??使用缓存方法完成..但是((fscanf(fin,“%ld,&dta))==1)正在工作。。。。。(文件中只有一列)但是((fscanf(fin,“%ld%ld”、&data[0]、&data[1])==1)不起作用,只给出两列的最后一个值。如果不想将返回值类型更改为void,我相信您可以强制将函数强制转换为
SNMPAlarmCallback
。我更喜欢snmp\u报警函数,而不是未签名的int snmp\u报警寄存器(unsigned int when,unsigned int flags,SNMPAlarmCallback*thecallback,void*clientarg)这里它不接受带有“int”返回类型的callback函数。(显示它必须是void!!)有什么解决方案吗?使用缓存方法完成..但是((fscanf(fin),%ld,&dta))=1)正在工作。。。。。(文件中只有一列)但((fscanf(fin,%ld%ld),&data[0],&data[1])==1)不起作用,并且只给出了两列的最后一个值。我确信您可以强制强制转换
/*
 * 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
*************************************************************/