You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用net-snmp-5.7.3自定义MIB后,snmptable获取MIB对象过多求助

Fixing snmptable Retrieving Extraneous MIB Objects in net-snmp 5.7.3

I’ve run into this exact issue before when working with custom MIBs in net-snmp—snmptable tends to wander into unrelated OID branches if your table registration or MIB definition isn’t properly constrained. Let’s break down the most effective fixes:

1. Validate Your MIB Definition’s Table Boundaries

First, double-check that your table is enclosed in a dedicated OID branch with clear boundaries in your MIB file. For your table at .1.3.6.1.4.1.XXXXXX.3.2, make sure:

  • The table is defined as a SEQUENCE OF its entry type
  • All table columns are nested under the table’s entry node
  • No non-table OIDs share the same parent branch as your table

Example MIB structure for your table:

myEnterprise MODULE-IDENTITY
    ::= { enterprises XXXXXX }

myTablesBranch OBJECT IDENTIFIER
    ::= { myEnterprise 3 }

myCustomTable OBJECT-TYPE
    SYNTAX SEQUENCE OF MyCustomTableEntry
    MAX-ACCESS not-accessible
    STATUS current
    DESCRIPTION "My custom table"
    ::= { myTablesBranch 2 }

MyCustomTableEntry OBJECT-TYPE
    SYNTAX MyCustomTableEntry
    MAX-ACCESS not-accessible
    STATUS current
    DESCRIPTION "Entry in myCustomTable"
    INDEX { myTableIndex }
    ::= { myCustomTable 1 }

MyCustomTableEntry ::= SEQUENCE {
    myTableIndex INTEGER,
    myTableValue OCTET STRING
}

myTableIndex OBJECT-TYPE
    SYNTAX INTEGER
    MAX-ACCESS not-accessible
    STATUS current
    DESCRIPTION "Table index"
    ::= { MyCustomTableEntry 1 }

myTableValue OBJECT-TYPE
    SYNTAX OCTET STRING
    MAX-ACCESS read-only
    STATUS current
    DESCRIPTION "Table value column"
    ::= { MyCustomTableEntry 2 }

This ensures snmptable knows exactly which OIDs belong to the table and won’t stray into your scalar branches (like .1.1 or .6.x).

2. Properly Register Your Table with Column Constraints

When registering your table in code, don’t just use a basic handler registration—use net-snmp’s table-specific registration functions to define column ranges. This tells the SNMP agent exactly which OIDs are part of the table.

Example code snippet for your table:

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

// Define your table OID
oid myCustomTable_oid[] = { 1,3,6,1,4,1,XXXXXX,3,2 };
size_t myCustomTable_oid_len = OID_LENGTH(myCustomTable_oid);

// Table handler function (implement get/get-next logic here)
int handle_myCustomTable(netsnmp_mib_handler *handler,
                         netsnmp_handler_registration *reginfo,
                         netsnmp_agent_request_info *reqinfo,
                         netsnmp_request_info *requests) {
    // Your table handling logic goes here
    return SNMP_ERR_NOERROR;
}

// Register the table in your agent initialization
void init_myCustomTable(void) {
    // Create table registration info to define column bounds
    netsnmp_table_registration_info *table_reg = netsnmp_create_table_registration_info();
    if (!table_reg) {
        snmp_log(LOG_ERR, "Failed to create table registration info\n");
        return;
    }

    // Set min/max column numbers (match your MIB's column OIDs)
    table_reg->min_column = 1;
    table_reg->max_column = 2; // Adjust based on your table's column count

    // Create handler registration
    netsnmp_handler_registration *reginfo = netsnmp_create_handler_registration(
        "myCustomTable", handle_myCustomTable,
        myCustomTable_oid, myCustomTable_oid_len,
        HANDLER_CAN_RONLY
    );
    if (!reginfo) {
        snmp_log(LOG_ERR, "Failed to create handler registration\n");
        netsnmp_free_table_registration_info(table_reg);
        return;
    }

    // Link table registration to the handler and register with the agent
    netsnmp_register_table(reginfo, table_reg);
}

This explicit column constraint prevents the agent from returning non-table OIDs when responding to snmptable requests.

3. Ensure Scalar and Table Handlers Are Isolated

Make sure your scalar handlers (like handle_fwVersion) don’t overlap with table OID ranges. In each handler, add checks to verify that the requested OID falls within the expected range. If it doesn’t, return SNMP_ERR_NOSUCHOBJECT to tell the agent to stop traversing that branch.

Example scalar handler check:

int handle_fwVersion(netsnmp_mib_handler *handler,
                     netsnmp_handler_registration *reginfo,
                     netsnmp_agent_request_info *reqinfo,
                     netsnmp_request_info *requests) {
    // Check if the requested OID matches your scalar OID
    if (!oid_equal(requests->requestvb->name, fwVersion_oid, OID_LENGTH(fwVersion_oid))) {
        return SNMP_ERR_NOSUCHOBJECT;
    }

    // Your scalar handling logic here
    return SNMP_ERR_NOERROR;
}

4. Use snmptable’s Explicit OID Argument (Quick Fix)

If you need an immediate workaround without modifying code/MIBs, specify the exact table OID when running snmptable to restrict its scope:

snmptable -v2c -c public localhost .1.3.6.1.4.1.XXXXXX.3.2

This forces snmptable to only query that specific table branch, ignoring other scalars or tables.

5. Verify OID Branch Separation

Double-check that your table’s OID branch doesn’t share a prefix with scalars. For example, if you had a scalar at .1.3.6.1.4.1.XXXXXX.3.1 and your table at .3.2, that’s fine—but if your scalar was at .3.2.1, it would be nested under the table’s branch, causing snmptable to pick it up. Ensure all scalars are in completely separate branches from tables.


内容的提问来源于stack exchange,提问作者Fatih Aslan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:22:05