jtor
jtor

Reputation: 133

SNMP - C - Implement subtree from MIB

I've been working on my own SNMP agent using the example found here : http://www.net-snmp.org/dev/agent/example_8c_source.html

I am wanting to better organize my tree structure to make more sense which in turn makes using client commands easier.

I am using the traditional old C API to achieve this and is what is used in the example link.

I have a tree I want to implement

Sample OID Tree

My MIB :

    MIB-NAME-HERE DEFINITIONS ::= BEGIN

    IMPORTS
        MODULE-IDENTITY, OBJECT-TYPE, Integer32, enterprises,
        NOTIFICATION-TYPE                       FROM SNMPv2-SMI
        OBJECT-GROUP, NOTIFICATION-GROUP        FROM SNMPv2-CONF
    ;

    myProduct MODULE-IDENTITY
        LAST-UPDATED "201505200000Z"
        ORGANIZATION "www.example.com"
        CONTACT-INFO
             "email: [email protected]"
        DESCRIPTION
            "MIB Example."
        REVISION     "201505200000Z"
        DESCRIPTION
            "version 1.0"
        ::= { enterprises 54321 }

    --
    -- top level structure
    --
       IPConfig OBJECT IDENTIFIER ::= { myProduct 1 }
       Services OBJECT IDENTIFIER ::= { myProduct 3 }

    IPConfigValuesGroup OBJECT-GROUP
        OBJECTS { ObjectA,
                  ObjectB,
                  ObjectC
                }

        STATUS current
        DESCRIPTION
               "Group of all blahblah variables."

        ::= { myProduct 4 } <----**How would this affect a client request?**

    --
    -- Values
    --

    ObjectA OBJECT-TYPE
        SYNTAX      OCTET STRING (SIZE(1..4096))
        MAX-ACCESS  read-write
        STATUS      current
        DESCRIPTION
            "Test Example"
        ::= { IPConfig 1 }

    ObjectB OBJECT-TYPE
        SYNTAX      OCTET STRING (SIZE(1..4096))
        MAX-ACCESS  read-write
        STATUS      current
        DESCRIPTION
            "Test Example"
        ::= { IPConfig 2 } 

--MORE STUFF...
    .... END 

I want to be able to group relative objects together so the user could do an

snmpset -v 2c -c communityNameHere -m MIB-NAME-HERE.txt 10.20.30.40 1.3.6.1.4.1.54321.x.1.3 s "I am a string"

to access ObjectC under the IPConfig group.

Question : How do I implement subtrees into my 'subagent'?

This is an excerpt from the link shown above.
    /*
     * This array defines the OID of the top of the mib tree that we're
     *  registering underneath.
     * Note that this needs to be the correct size for the OID being 
     *  registered, so that the length of the OID can be calculated.
     *  The format given here is the simplest way to achieve this.
     */
oid             example_variables_oid[] = { 1, 3, 6, 1, 4, 1, 54321, x};

Do I have to declare another array to include, per say, the Services OID?

oid             example_variables_oid[] = { 1, 3, 6, 1, 4, 1, 54321, x, 3};

Or for each subtree do they need to have an example.c*-type* file?

Question : Would this MIB achieve what I want? What would need to be done different? I've read up on OBJECT-GROUPS, SEQUENCE, O'Reily's book as well as the RFCs. I'm still trying to grasp everything.

Upvotes: 3

Views: 4115

Answers (1)

Wes Hardaker
Wes Hardaker

Reputation: 22262

There is a lot of questions here, and honestly it would take a super long post to answer them all. So, I'll answer them at a high level and then provide you a bunch of links to go read longer and more in depth articles on.

First, writing mibs is not exactly straight forward. The most referenced book on the subject is probably Understand SNMP MIBs and is quite good (I have a copy). All your mib writing questions are well answered in there, but a couple of quick points about what you have above:

1) Almost all MIB objects should start with a lower case letter (except table sequences which you're not to yet).

2) There is no 'x' in your oids above. You've created a tree structure in the definitions that derive straight to 1.3.6.1.4.1.54321.1.3, for example.

3) Just throw out the object group clause for now. It'll only confuse you at first and isn't needed. It's only there to really write a standards-definition for listing objects you must implement in order to conform to the mib (using a conformance statement). For you right now, this is not at all needed. Just kill the whole thing.

As for writing code to support the object you're trying to define, you'll need to do that in C code within the agent or subagent that you're writing. There is already a lot of documentation on the Net-SNMP project site about that, so you should really go look there. The links that will help you get started are:

Generally how to write mib code for a net-smnp based agent:

http://www.net-snmp.org/wiki/index.php/TUT:Writing_a_MIB_Module

How to use the mib2c translator to produce some template code to start with:

http://www.net-snmp.org/wiki/index.php/TUT:mib2c_General_Overview

And more generically, all the coding tutorials for Net-SNMP can be found here:

http://www.net-snmp.org/wiki/index.php/Tutorials#Coding_Tutorials

And one final comment: the objects you're defining above are called "scalars". IE, there is only a single instance of them in the tree. So when you're reading the tutorials or the mib2c questions it asks you, the above are "scalars". Tables will likely come next in your project, as everyone seems to end up with tables! Good luck!

Upvotes: 2

Related Questions