在表上使用snmpgetnext

时间:2015-05-12 07:26:21

标签: snmp net-snmp snmpd

我正在编写自己的MIB模块,其中包含一个包含2列的表。使用snmptable可以正常工作并检索包含所有行的表的所有值。 但是使用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;
}

1 个答案:

答案 0 :(得分:0)

关于:

  

那么为什么我不能用简单的snmpgetnext请求检索单个值?

引用man snmpgetnext

  

说明          snmpget是一个SNMP应用程序,它使用SNMP GETNEXT请求来查询网络实体的信息。一个或多个   对象标识符(OID)可以          在命令行中作为参数给出。每个变量名称都以变量(5)中指定的格式给出。对于每一个,   将返回远程实体的MIB中按字典顺序“next”的变量。例如:

     

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

     

将检索变量   interfaces.ifTable.ifEntry.ifType.2

换句话说,它的工作就是让一件事回来。

如果要使用它来获取完整的表,则需要多次调用它。引用snmptable的手册页:

  

snmptable是一个SNMP应用程序,它重复使用SNMP GETNEXT或GETBULK请求来查询网络实体的信息。