2015-05-12 76 views
0

我正在写我自己的MIB模块,包含一个2列的表。使用snmptable工作得很好,并检索所有行的表的所有值。 但snmpgetnext我只能检索表的第一行。在表上使用snmpgetnext

snmpgetnext -v2c -c public localhost sensorTable 
MY-PERSONAL-MIB::sensorVoltage."1" = STRING: "2.3V" 

要检索下一个值我必须运行:

snmpgetnext -v2c -c public localhost sensorVoltage."2" 
MY-PERSONAL-MIB::sensorTemperature."1" = "3.2C" 

运行snmpgetnext -v2c -c public localhost sensorVoltage."1"会导致sensorVoltage."1"再次,同为sensorTemperature."1"

snmpgetnext -v2c -c public localhost sensorTemperature."2" 
SNMPv2-MIB::snmpSetSerialNo.0 = INTEGER: 1664041205 

而且,我跑了snmptable -CB,因此经理只使用GETNEXT检索表中的值。这工作也很好。 那么为什么我不能用简单的snmpgetnext请求检索单个值? 最后,snmpget根本不起作用。我得到以下错误:

snmpget -v2c -c publicl localhost sensorTemperature."1" 
MY-PERSONAL-MIB::sensorTemperature.1 = No such Instance currently exists at this OID 

最后,我的代码我用于我的MIB模块。我使用处理程序从文件读取数据并通过它创建表结构。我试图使用snmpgetnext和通过初始化例程创建的表导致相同的问题,所以处理程序例程不应该是这里的问题,但我仍然追加它只是为了完成和好,谁知道!

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

#define firstEntryRow 2 

netsnmp_table_data_set *table_set; 

void 
initialize_table_sensorTable(void) 
{ 

netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_X_SOCKET, "tcp:localhost:705");  

const oid sensorTable_oid[] = { 1 ,3 ,6 ,1 ,4 ,1 ,8072 ,1259 ,1 ,1 }; 

table_set = netsnmp_create_table_data_set("sensorTable"); 
netsnmp_table_row *row; 

table_set->allow_creation = 1; 

DEBUGMSGTL(("initialize_table_sensorTable", 
      "adding indexes to table sensorTable\n")); 
netsnmp_table_dataset_add_index(table_set, 
         ASN_OCTET_STR); 

DEBUGMSGTL(("initialize_table_sensorTable", 
      "adding column types to table sensorTable\n"));  
netsnmp_table_set_multi_add_default_row(table_set, 
             COLUMN_SENSORVOLTAGE, ASN_OCTET_STR, 1, 
             NULL, 0, 
             COLUMN_SENSORTEMPERATURE, ASN_OCTET_STR, 1, 
             NULL, 0, 
          0); 

netsnmp_register_table_data_set(netsnmp_create_handler_registration("sensorTable", sensorTable_handler, 
                sensorTable_oid, 
                OID_LENGTH(sensorTable_oid), 
                HANDLER_CAN_RWRITE), 
         table_set, NULL); 

row = netsnmp_create_table_data_row(); 
netsnmp_table_row_add_index(row, ASN_OCTET_STR, "1", 
          strlen("1")); 
netsnmp_set_row_column(row, 2, ASN_OCTET_STR, 
         "5.9V", strlen("5.9V")); 
netsnmp_set_row_column(row, 3, ASN_OCTET_STR, 
         "21.5C", strlen("21.5C")); 
netsnmp_table_dataset_add_row(table_set, row); 


} 


void 
init_sensorTable(void) 
{ 
    initialize_table_sensorTable(); 
} 

int 
sensorTable_handler(
netsnmp_mib_handler    *handler, 
netsnmp_handler_registration  *reginfo, 
netsnmp_agent_request_info  *reqinfo, 
netsnmp_request_info    *requests) { 

netsnmp_table_row *row, *tempRow; 

if(table_set != NULL) 
{ 

    for (tempRow = table_set->table->first_row; 
      tempRow; tempRow = tempRow->next) 
    { 
     row = netsnmp_table_data_set_get_first_row(table_set); 
     netsnmp_table_dataset_remove_row(table_set, row); 
    } 
} 

/* Start reading "input.txt */ 
char sensorValues[32]; 
char *singleValue; 
FILE *f = fopen("/home/supra/Desktop/input.txt", "r"); 

/* check if the file was found */ 
if(f == NULL) { 
    printf("Error opening file!\n"); 
    exit(EXIT_FAILURE); 
} 

/* check if the file is empty */ 
if (fgets(sensorValues, sizeof(sensorValues), f) == NULL) 
{ 
    printf("Warning: File is empty!\n"); 
    exit(EXIT_FAILURE); 
} 

/* if the file is not empty, create a row and start 
* breaking the string into words. Fill the row with the words 
* and add it to the table. */ 
do 
{ 
    int rowEntries = firstEntryRow; 
    singleValue = strtok(sensorValues, " "); 
    row = netsnmp_create_table_data_row(); 
    netsnmp_table_row_add_index(row, ASN_OCTET_STR, singleValue, strlen(singleValue));  
    singleValue = strtok(NULL, " "); 

    /* Fill the row with values */ 
    while(singleValue != NULL) 
    { 
     netsnmp_set_row_column(row, rowEntries, ASN_OCTET_STR, singleValue, strlen(singleValue)); 
     rowEntries++; 
     singleValue = strtok(NULL, " "); 
    } 

    netsnmp_table_dataset_add_row(table_set, row); 

} while(fgets(sensorValues, sizeof(sensorValues), f) != NULL); 
fclose(f); 

return SNMP_ERR_NOERROR; 
} 

回答

0

关于:

So why can't I retrieve the single values with a simple snmpgetnext request?

报价man snmpgetnext

DESCRIPTION snmpget is an SNMP application that uses the SNMP GETNEXT request to query for information on a network entity. One or more object identifiers (OIDs) may be given as arguments on the command line. Each variable name is given in the format specified in variables(5). For each one, the variable that is lexicographically "next" in the remote entity's MIB will be returned. For example:

snmpgetnext -c public zeus interfaces.ifTable.ifEntry.ifType.1

will retrieve the variable interfaces.ifTable.ifEntry.ifType.2

换句话说它的工作是送一件事情。

如果你想用它来获得一个完整的表格,你需要多次调用它。引用snmptable的手册页:

snmptable is an SNMP application that repeatedly uses the SNMP GETNEXT or GETBULK requests to query for information on a network entity.

+0

我明白这一点。问题是,如果我手动链接'snmpgetnext',我不会像使用'snmptable'那样获得entrys。第一个以外的所有条目都会跳过,然后跳到下一列。我可能有一个错误,虽然在我刚刚意识到的代码中,所以也许修复它会有帮助! – Christian