错误 - (noSuchName) 此 MIB 中没有这样的变量名称 - netSnmp?

Error - (noSuchName) There is no such variable name in this MIB - netSnmp?

我已经创建了一个私有 MIB - RASP-MIB。

这是 mib

RASP-MIB DEFINITIONS ::= BEGIN

IMPORTS
    OBJECT-TYPE, MODULE-IDENTITY,enterprises      FROM SNMPv2-SMI
    TEXTUAL-CONVENTION              FROM SNMPv2-TC;

rasp MODULE-IDENTITY
     LAST-UPDATED "201804210000Z"            -- 21 April 2018, midnight
     ORGANIZATION "net-snmp"
     CONTACT-INFO "postal:   ABC HELLO
                             CHECK 512310

                   email:    net-snmp-coders@lists.sourceforge.net"
     DESCRIPTION  "A simple mib for Raspberry PI information gathering."

::={enterprises 9100}

RowStatus ::= TEXTUAL-CONVENTION
    STATUS       current
    DESCRIPTION
        "The RowStatus textual convention is used to manage the
        creation and deletion of conceptual rows, and is used as the
        value of the SYNTAX clause for the status column of a
        conceptual row."
    SYNTAX   INTEGER {
         -- the following two values are states:
         -- these values may be read or written
         active(1),          -- state:  read/write
         notInService(2),    -- state:  read/write
         notReady(3),        -- state:  read only
         createAndGo(4),     -- action: write only
         createAndWait(5),   -- action: write only
         destroy(6)      -- action: write only
    }


    raspScalar OBJECT IDENTIFIER ::= {rasp 1}
    raspTable OBJECT IDENTIFIER ::= {rasp 2}

    freeSpaceAvailable OBJECT-TYPE
        SYNTAX      Integer32
        MAX-ACCESS  read-write
        STATUS      current
        DESCRIPTION "The free space available in the system in MB"
    DEFVAL { 2 }
    ::= { raspScalar 1 }

    scalar2 OBJECT-TYPE
        SYNTAX      Integer32
        MAX-ACCESS  read-write
        STATUS      current
        DESCRIPTION "scalar2"
        DEFVAL { 4 }
    ::= { raspScalar 2 }

    raspTableOne OBJECT-TYPE
        SYNTAX      SEQUENCE OF raspTable1Entry
        MAX-ACCESS  not-accessible
        STATUS      current
        DESCRIPTION "Table 1"
    ::= { raspTable 1 }

    raspTable1Entry OBJECT-TYPE
        SYNTAX      raspTable1Entry
        MAX-ACCESS  not-accessible
        STATUS      current
        DESCRIPTION "Entry"
        INDEX { tIndex }
    ::= { raspTableOne 1 }

    raspTable1Entry ::= SEQUENCE {
        tIndex      Integer32,
        tName       DisplayString,
        tMarks      Integer32,
        tRowStatus  INTEGER
    }

    tIndex OBJECT-TYPE
        SYNTAX      Integer32 (1..100)
        MAX-ACCESS  read-only
        STATUS      current
        DESCRIPTION "Index for Table 1"
    ::= { raspTable1Entry 1 }

    tName OBJECT-TYPE
        SYNTAX      DisplayString
        MAX-ACCESS  read-create
        STATUS      current
        DESCRIPTION "Name for Table 1"
    ::= { raspTable1Entry 2 }

    tMarks OBJECT-TYPE
        SYNTAX      Integer32(1..100)
        MAX-ACCESS  read-create
        STATUS      current
        DESCRIPTION "Marks for Table 1"
    ::= { raspTable1Entry 3 } 


    tRowStatus OBJECT-TYPE
        SYNTAX     RowStatus
        MAX-ACCESS read-create
        STATUS     current
        DESCRIPTION "The status"
    ::= { raspTable1Entry 4 }      

END

接下来,这个 mib 有一个标量对象 - freeSpaceAvailable。我正在尝试使用这个。

1) 首先我使用 snmptranslate

翻译了这个

$snmptranslate -m +RASP-MIB -IR -On freeSpaceAvailable

.1.3.6.1.4.1.9100.1.1

2) 接下来,我生成了 mib2c 代码 -

 env MIBS="+RASP-MIB" mibc2c freeSpaceAvailable

3) 接下来,我将此文件修改为 return 硬编码值

4) 将其编译为 subAgent

$net-snmp-config --compile-subagent mysubagent freeSpaceAvailable.c

5) 已启动 snmp 守护进程

sudo service snmpd start

6) 测试是否有效

 snmpwalk localhost -c public -v1

7) 运行 我的副代理

$./mysubagent &

$pidof mysubagent
27709

现在,不确定我为什么会遇到这个问题?

$snmpget -v 1 -c public localhost .1.3.6.1.4.1.9100.1.1
Error in packet
Reason: (noSuchName) There is no such variable name in this MIB.
Failed object: iso.3.6.1.4.1.9100.1.1

这是屏幕截图 -

这里是freeSpaceAvailable.c代码

/*
 * Note: this file originally auto-generated by mib2c using
 *        $
 */

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

static unsigned long myVarUlong = 0;


/** Initializes the freeSpaceAvailable module */
void
init_freeSpaceAvailable(void)
{
    const oid       freeSpaceAvailable_oid[] =
        { 1, 3, 6, 1, 4, 1, 9100, 1, 1 };

    DEBUGMSGTL(("freeSpaceAvailable", "Initializing\n"));

    netsnmp_register_scalar(netsnmp_create_handler_registration
                            ("freeSpaceAvailable",
                             handle_freeSpaceAvailable,
                             freeSpaceAvailable_oid,
                             OID_LENGTH(freeSpaceAvailable_oid),
                             HANDLER_CAN_RWRITE));
}

int
handle_freeSpaceAvailable(netsnmp_mib_handler *handler,
                          netsnmp_handler_registration *reginfo,
                          netsnmp_agent_request_info *reqinfo,
                          netsnmp_request_info *requests)
{
    int             ret = -1;
    /*
     * We are never called for a GETNEXT if it's registered as a
     * "instance", as it's "magically" handled for us.  
     */

    /*
     * a instance handler also only hands us one request at a time, so
     * we don't need to loop over a list of requests; we'll only get one. 
     */

    switch (reqinfo->mode) {

    case MODE_GET:
        snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER, &myVarUlong,sizeof(myVarUlong) );
        break;

        /*
         * SET REQUEST
         *
         * multiple states in the transaction.  See:
         * http://www.net-snmp.org/tutorial-5/toolkit/mib_module/set-actions.jpg
         */
    case MODE_SET_RESERVE1:
        /*
         * or you could use netsnmp_check_vb_type_and_size instead 
         */
        ret = netsnmp_check_vb_type(requests->requestvb, ASN_INTEGER);
        if (ret != SNMP_ERR_NOERROR) {
            netsnmp_set_request_error(reqinfo, requests, ret);
        }
        break;

    case MODE_SET_RESERVE2:
        /*
         * XXX malloc "undo" storage buffer 
         */
        //if ( /* XXX if malloc, or whatever, failed: */ ) {
        //    netsnmp_set_request_error(reqinfo, requests,
          //                            SNMP_ERR_RESOURCEUNAVAILABLE);
       // }
        break;

    case MODE_SET_FREE:
        /*
         * XXX: free resources allocated in RESERVE1 and/or
         * RESERVE2.  Something failed somewhere, and the states
         * below won't be called. 
         */
        break;

    case MODE_SET_ACTION:
        /*
         * XXX: perform the value change here 
         */
        myVarUlong = (unsigned long)*requests->requestvb->val.integer;
        if ( myVarUlong == 0 ) {
            netsnmp_set_request_error(reqinfo, requests, ret
                                      );
        }
        break;

    case MODE_SET_COMMIT:
        /*
         * XXX: delete temporary storage 
         */
        //if ( /* XXX: error? */ ) {
            /*
             * try _really_really_ hard to never get to this point 
             */

          // netsnmp_set_request_error(reqinfo, requests,
            //                          SNMP_ERR_COMMITFAILED);
       // }
        break;

    case MODE_SET_UNDO:
        /*
         * XXX: UNDO and return to previous value for the object 
         */
        #if 0
        if ( /* XXX: error? */ ) {
            /*
             * try _really_really_ hard to never get to this point 
             */
            netsnmp_set_request_error(reqinfo, requests,
                                      SNMP_ERR_UNDOFAILED);
        }
        #endif
        break;

    default:
        /*
         * we should never get here, so this is a really bad error 
         */
        snmp_log(LOG_ERR,
                 "unknown mode (%d) in handle_freeSpaceAvailable\n",
                 reqinfo->mode);
        return SNMP_ERR_GENERR;
    }

    return SNMP_ERR_NOERROR;
}

似乎主 SNMP 守护进程根本没有收到通知。 我在

中添加了调试信息

/etc/default/snmpd

SNMPDOPTS='-LS 0-4 d -Lf /dev/null -u snmp -g snmp -I -smux,mteTrigger,mteTriggerConf -p /var/run/snmpd.pid'

在系统日志中 -

sudo tail -f /var/log/syslog
Apr 23 16:00:17 pc snmpd[11340]: /etc/snmp/snmpd.conf: line 143: Warning: Unknown token: defaultMonitors.
Apr 23 16:00:17 pc snmpd[11340]: /etc/snmp/snmpd.conf: line 145: Warning: Unknown token: linkUpDownNotifications.
Apr 23 16:00:17 pc snmpd[11340]: /var/lib/snmp/snmpd.conf: line 32: Warning: Unknown token: _mteTTable.
Apr 23 16:00:17 pc snmpd[11340]: /var/lib/snmp/snmpd.conf: line 33: Warning: Unknown token: _mteTTable.
Apr 23 16:00:17 pc snmpd[11340]: /var/lib/snmp/snmpd.conf: line 34: Warning: Unknown token: _mteTTable.
Apr 23 16:00:17 pc snmpd[11340]: /var/lib/snmp/snmpd.conf: line 35: Warning: Unknown token: _mteTTable.
Apr 23 16:00:17 pc snmpd[11340]: /var/lib/snmp/snmpd.conf: line 36: Warning: Unknown token: _mteTTable.
Apr 23 16:00:17 pc snmpd[11340]: /var/lib/snmp/snmpd.conf: line 37: Warning: Unknown token: _mteTTable.
Apr 23 16:00:17 pc snmpd[11340]: /var/lib/snmp/snmpd.conf: line 38: Warning: Unknown token: _mteTTable.

即使我杀死或生成 mysubagent,也没有任何区别。

节点定义中有一个小错误。应该是::= { raspScalar 1 }

freeSpaceAvailable OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DESCRIPTION "The free space available in the system in MB"
DEFVAL { 2 }
::= { raspScalar 1 }

我使用 tkmib (linux) 来解决问题。 关注了this tutorial,得到了很好的回复

c代码片段:

int fSpace;

// inside init_freeSpaceAvailable
    fSpace = (int) random();

// inside switch statement
        case MODE_GET:
            snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,
                                    (u_char *)&fSpace,
                                    sizeof(fSpace));
            break;

在 /etc/snmp/snmpd.conf 添加

master agentx

启动 snmpd 为

sudo snmpd -f -Lo -C --rwcommunity=public --master=agentx --agentXSocket=tcp:localhost:1705

启动代理为

./mysubagent -f -Lo -x tcp:localhost:1705

测试为

snmpget -v 1 -c public localhost RASP-MIB::freeSpaceAvailable.0<br> RASP-MIB::freeSpaceAvailable.0 = INTEGER: 924855091

或通过 OID

snmpget -v 1 -c public -m "+RASP-MIB" localhost 1.3.6.1.4.1.9100.1.1.0 RASP-MIB::freeSpaceAvailable.0 = INTEGER: 924855091