我正在编写自己的 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;
}