Search code examples
ubuntu-14.04snmpnet-snmp

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


I have made a private MIB - RASP-MIB.

Here is the 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:    [email protected]"
     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

Next,This mib has a scalar object - freeSpaceAvailable. I am trying to use this one.

1) First I translated this using snmptranslate

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

.1.3.6.1.4.1.9100.1.1

2) Next, I have generated mib2c code -

 env MIBS="+RASP-MIB" mibc2c freeSpaceAvailable

3) Next, I have modified this file to return a hardcoded value

4) Compiled it as subAgent

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

5) Started snmp daemon

sudo service snmpd start

6) tested whether it is working or not

 snmpwalk localhost -c public -v1

7) Running my sub-agent

$./mysubagent &

$pidof mysubagent
27709

Now, not sure why do I get this problem?

$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

Here is the screen shot -

enter image description here

Here is the freeSpaceAvailable.c code

/*
 * 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;
}

It seems like main SNMP daemon doesn't get notified at all. I have added a debug information in

/etc/default/snmpd

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

in the syslog -

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.

Even if I kill or spawn the mysubagent, it doesn't make any difference.


Solution

  • There's an small bug in the node definition. It should be ::= { 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 }
    

    I used tkmib (linux) to troubleshoot the issue. Following this tutorial, I was able to get good responses

    c code fragment:

    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;
    

    on /etc/snmp/snmpd.conf add

    master agentx

    start snmpd as

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

    start agent as

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

    Test as

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

    or by 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