3

First thing first. I want to create my own MIB.

Please pardon me if I'm asking a simple question as I'm a newbie to SNMP and I'm struggling on this from couple of weeks. I have already been through TUT:mib2c.

UPDATE: I've the following MIB named SAMPLE-MIB, which compiles good without any error (This compilation is done only on the agent system, not on the server).

SAMPLE-MIB DEFINITIONS ::= BEGIN

IMPORTS
    MODULE-IDENTITY, OBJECT-TYPE, Integer32,
        NOTIFICATION-TYPE                       FROM SNMPv2-SMI
        SnmpAdminString                         FROM SNMP-FRAMEWORK-MIB
        netSnmp                                 FROM NET-SNMP-MIB
;



sample    MODULE-IDENTITY
    LAST-UPDATED "201709120200Z"
    ORGANIZATION "NCS"
    CONTACT-INFO
        "@ncs-in.com"        
    DESCRIPTION
        "A test MIB"
    REVISION    "201709120200Z"
    DESCRIPTION
        "initial Draft"
    ::= { iso org(3) dod(6) internet(1) private(4) enterprises(1) 39106 }

sampleResearch               OBJECT IDENTIFIER ::= { sample 2 }

sampleVar OBJECT-TYPE
    SYNTAX Integer32
    MAX-ACCESS  read-write
    STATUS  current
    DESCRIPTION
        "testing"
    ::= { sampleResearch 1}

END

I have already done this:

root@snmp-agent:# MIBS=+SAMPLE-MIB  
root@snmp-agent:# export MIBS    
root@snmp-agent:# MIBS=ALL

My MIB files are in this path: /usr/share/snmp/mibs which is the default search path. I've already compiled it and generated .c and .h files successfully, like this:

root@snmp-agent:# snmptranslate -On SAMPLE-MIB::sampleVar
root@snmp-agent:# snmptranslate -IR SAMPLE-MIB
root@snmp-agent:# mib2c -c mib2c.scalar.conf sampleVar

NOTE: These sampleVar.c and sampleVar.h files are in the path: /home/snmp-agent/Desktop/project/net-snmp-5.7.3/agent/mibgroup/.

To keep it simple I din't modify anything in the sampleVar.h and this is how my sampleVar.c file looks:

/*

 * 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 "sampleVar.h"







static int myvar=10;

/** Initializes the sampleVar module */

void

init_sampleVar(void)

{

    const oid sampleVar_oid[] = { 1,3,6,1,4,1,39106,2,1 };

    const size_t sampleVar_oid_len   = OID_LENGTH(sampleVar_oid);

    netsnmp_handler_registration    *reg;

    netsnmp_iterator_info           *iinfo;

    netsnmp_table_registration_info *table_info;



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



   /* netsnmp_register_scalar(

        netsnmp_create_handler_registration("sampleVar", handle_sampleVar,

                               sampleVar_oid, OID_LENGTH(sampleVar_oid),

                               &,HANDLER_CAN_RWRITE   ));*/



 netsnmp_register_int_instance("sampleVar",

                                  sampleVar_oid,

                                  OID_LENGTH(sampleVar_oid),

                                  &myvar, NULL);





}



int

handle_sampleVar(netsnmp_mib_handler *handler,

                          netsnmp_handler_registration *reginfo,

                          netsnmp_agent_request_info   *reqinfo,

                          netsnmp_request_info         *requests)

{

    int ret = 120892;

    netsnmp_request_info       *request;

    netsnmp_table_request_info *table_info;

    struct poolTable_entry          *table_entry;

    /* We are never called for a GETNEXT if it's registered as a

       "instance", as it's "magically" handled for us.  */



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



    /* 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. */







    return ret;

}

Now I reconfigure net-snmp with the MIB's like this:

./configure --with-mib-modules="sampleVar"
make
make install

After this when I do (on the agent) snmptranslate -IR sampleVar.0 I get the output as:

root@snmp-agent:# snmptranslate -IR sampleVar.0
SAMPLE-MIB::sampleVar.0 (expected output of the command)

And with the command snmptranslate -On objectName.0 I get output as:

root@snmp-agent:# snmptranslate -On SAMPLE-MIB::sampleVar.0
.1.3.6.1.4.1.39106.2.1 (expected output of the command)

When I do snmpwalk on server:

snmp-server:# snmpwalk -v 2c -c ncs -m SAMPLE-MIB 192.168.5.22 .1.3.6.1.4.1.39106.2.1
SAMPLE-MIB::sampleVar.2.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = Wrong Type (should be INTEGER): STRING: "/bin/echo"
SAMPLE-MIB::sampleVar.3.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = Wrong Type (should be INTEGER): ""
SAMPLE-MIB::sampleVar.4.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = Wrong Type (should be INTEGER): ""
SAMPLE-MIB::sampleVar.5.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = INTEGER: 5
SAMPLE-MIB::sampleVar.6.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = INTEGER: 1
SAMPLE-MIB::sampleVar.7.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = INTEGER: 1
SAMPLE-MIB::sampleVar.20.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = INTEGER: 4
SAMPLE-MIB::sampleVar.21.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = INTEGER: 1

But, When I do snmpget from my server, I get:

root@snmp-server:# snmpget 192.168.5.22 sampleVar.0
SAMPLE-MIB::sampleVar = No Such Object available on this agent at this OID
root@snmp-server:# snmpget -v 2c -c ncs 192.168.5.22 -m SAMPLE-MIB .1.3.6.1.4.1.39106.2.1
SAMPLE-MIB::sampleResearch.3.1.2 = No Such Object available on this agent at this OID

I do get expected output for sysUpTime.0 from my server:

root@snmp-server:# snmpget 192.168.5.22 sysUpTime.0
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (721781) 2:00:17.81 (OUTPUT)

I have googled it and still searching but no luck. So anyone can tell me what to do next? How should I use snmpget from my server on my own MIBs?

Any ideas or suggestions are highly appreciated. Thank you.

UPDATE 2: Output from snmpgetnext:

root@snmp-server:~# snmpgetnext -v 2c -c ncs 192.168.5.22 -m SAMPLE-MIB .1.3.6.1.4.1.39106.2.1
SAMPLE-MIB::sampleVar.2.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = Wrong Type (should be INTEGER): STRING: "/bin/echo"
root@snmp-server:~# snmpgetnext -v 2c -c ncs 192.168.5.22 -m SAMPLE-MIB .1.3.6.1.4.1.39106.2.2
SAMPLE-MIB::sample.3.1.1.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = ""

As you said: snmpget is used to target a specific OID in SNMP, So accordingly when I specify the complete OID, I get correct output:

root@snmp-server:~# snmpget -v 2c -c ncs -m SAMPLE-MIB 192.168.5.22 sampleVar.2.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54
SAMPLE-MIB::sampleVar.2.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54 = Wrong Type (should be INTEGER): STRING: "/bin/echo"

But what is this: sampleVar.2.18.46.49.46.51.46.54.46.49.46.52.46.49.46.51.57.49.48.54? What's wrong here? I don't understand that! I din't specify this stuff!!!!

7
  • Why do you want to create your own MIB? This is an extremely non-trivial endeavor, and as you've said you're a "newbie" to SNMP - what problem are you trying to solve? Sep 15 '17 at 19:23
  • @JohnJensen Its the client requirement, I'm sorry I can't tell you about the requirement. Sep 18 '17 at 5:44
  • 1
    then you should delegate this to someone that knows what they're doing. Sep 21 '17 at 19:46
  • Did any answer help you? If so, you should accept the answer so that the question doesn't keep popping up forever, looking for an answer. Alternatively, you can provide and accept your own answer.
    – Ron Maupin
    Feb 19 '18 at 18:44
  • @RonMaupin Actually none helped me Feb 20 '18 at 6:27
9

I think your issue is coming from a simple misconception of how SNMP data (OIDs) is structured, and how snmpwalk, snmpget, and snmpgetnext interact with it.

SNMP organizes the OIDs into related nested trees (or tables), the MIBs define those trees and the OIDs in them.

It's important to understand how snmpget and snmpgetnext works before getting into snmpwalk

  1. snmpget

snmpget is used to target a specific OID in SNMP. This assumes I know the exact OID I want data from, for example, I want the first entry in the IF-MIB::ifName table (1.3.6.1.2.1.31.1.1.1.1.1.1). I know that the entry is there for a fact, so it will return a value.

[jhead@sonder ~]$ snmpget -On -v2c -c public 1.2.3.4 1.3.6.1.2.1.31.1.1.1.1.1
.1.3.6.1.2.1.31.1.1.1.1.1 = STRING: fxp0
  1. snmpgetnext

snmpgetnext is for when you don't know exactly how things are organized on your target system. Meaning, you know that system has the IF-MIB::ifName table, but you don't know exactly what the first entry is. snmpgetnext's job is to literally find, the next entry to whatever OID you point it at.

I know for a fact that 1.3.6.1.2.1.31.1.1.1.1.2 does not exist on my target system, lets verify that with an snmpget:

[jhead@sonder ~]$ snmpget -On -v2c -c public 1.2.3.4 1.3.6.1.2.1.31.1.1.1.1.2
.1.3.6.1.2.1.31.1.1.1.1.2 = No Such Instance currently exists at this OID

Now let's try an snmpgetnext:

[jhead@sonder ~]$ snmpgetnext -On -v2c -c public 1.2.3.4 1.3.6.1.2.1.31.1.1.1.1.2
.1.3.6.1.2.1.31.1.1.1.1.4 = STRING: lsi

It looks like my next entry is .4, great!

  1. snmpwalk

snmpwalk is effectively a recursive search on whatever OID value you target. snmpwalk actually executes multiple snmpgetnext operations to determine the contents of the table, because that table might vary in size, or change over time.

For example, if I wanted to retrieve all OID entries for IF-MIB::ifName (my particular MIB defines that as 1.3.6.1.2.1.31.1.1.1.1)

[jhead@sonder ~]$ snmpwalk -On -v2c -c public 1.2.3.4 1.3.6.1.2.1.31.1.1.1.1
.1.3.6.1.2.1.31.1.1.1.1.1 = STRING: fxp0
.1.3.6.1.2.1.31.1.1.1.1.4 = STRING: lsi
.1.3.6.1.2.1.31.1.1.1.1.5 = STRING: dsc
.1.3.6.1.2.1.31.1.1.1.1.6 = STRING: lo0
.1.3.6.1.2.1.31.1.1.1.1.7 = STRING: tap
.1.3.6.1.2.1.31.1.1.1.1.8 = STRING: gre
.1.3.6.1.2.1.31.1.1.1.1.9 = STRING: ipip

As you can see, I retrieved all entries in that table of OIDs.

snmpwalk will also recurse through a table A, if table A contains tables B, C, and D. Such as the entire DISMAN-PING-MIB in your example, it will recurse those as well.

  1. Your original question.

Now, lets take a look at why you can't use snmpget to target a table like you did in your example:

[jhead@sonder ~]$ snmpget -On -v2c -c public 1.2.3.4 1.3.6.1.2.1.31.1.1.1.1
.1.3.6.1.2.1.31.1.1.1.1 = No Such Instance currently exists at this OID

It fails because that OID is a table, no specific information tied to .1.3.6.1.2.1.31.1.1.1.1, it is just a container for the rest of the information.

There are more than the 3 methods I mentioned, like snmpbulkwalk and others. Which method you use could vary depending upon what you're trying to accomplish, but I suspect snmpwalk will work fine. You can see examples here.

6
  • yes you're right, I get the values when I do snmpgetnext but not with snmpget. I also get expected output from snmpwalk. So what to do next so that I can also get the values through snmpget? Sep 14 '17 at 10:55
  • The short answer is, you can't. There either isn't any information in the OID you are trying to get or the OID is a table, snmpget won't work for a table. Is there any reason you have to use snmpget? snmpwalk should be fine. Sep 14 '17 at 12:44
  • ya as I said I've a custom MIB that I want to implement(add) in snmp. Ok I will update my question with a simple MIB file that I'm testing with. Sep 15 '17 at 5:11
  • I updated the post. Please have a look and tell me if I'm wrong? Sep 15 '17 at 5:35
  • Thanks for the update. I don't know enough about the library you are using to say exactly what is going on, but where it says "Wrong Type" that has to do with the type of data that is stored in each OID, examples are INTEGER, STRING, etc. The values need to correspond to the type of data that are stored there, correctly. Beyond that, given how the scope of your question has changed I suspect it is now off topic for this SE site, I would try asking a new question in Stackoverflow or Super User regarding your most recent update, and how to properly define MIBs. Sep 15 '17 at 14:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.