Sunday, December 20, 2009

Compiling your own SNMP subagent

I had some trouble remembering how to do this so I thought I would write down how to decipher the net-snmp tutorial instructions on how to build a subagent for your custom MIB.

Now why would you want a subagent? A subagent is just a demon that runs on its own and connects to the snmpd demon, so adding its functionality to the whole. The advantage of this approach is that you don't have to recompile the main agent or (almost as bad) compile it into a 'dynamically loadable object' which boils down to much the same thing as compiling it into the code. With a subagent you can bring your custom agent and its custom MIB up or down without disturbing the main agent.

Before you start you must have installed the libsnmp-dev package, which contains all the development headers. And also all the snmp and snmpd stuff as described in my earlier blog entry. But you knew that, right?

So, OK, how do you do it?

  1. First write a MIB. I used the NET-SNMP-TUTORIAL-MIB as a basis for my own. After examining it carefully for a few minutes changing it to do what I wanted was not hard. You have to change the names of the parts of the MIB tree to suit your variables.
  2. Next, turn the MIB into C. I used mib2c on the commandline:
    mib2c dosTFMIBObjects
    You are supposed to modify this but for a demo you can just leave it as is. You might want to modify the default values in the .c file though. 'dosTFMIBObjects' is the name of the main node of my custom MIB subtree. For this to work you must already have put your MIB file into .snmp/mibs.
  3. Modify the example-demon.c file (I renamed it dostf-demon.c) by changing the name of the init function to match your own.
  4. Now execute the manual compile steps, using the generated C file:
     gcc -I. `net-snmp-config --cflags` -c -o dostf-demon.o dostf-demon.c
     gcc -I. `net-snmp-config --cflags` -c -o dosTFMIB.o dosTFMIB.c
     gcc -o dostf-demon dostf-demon.o dosTFMIB.o `net-snmp-config --agent-libs`
    
  5. Finally, run your subagent:
    sudo ./dostf-demon
    And type in the password or it will fail to run correctly. You may also need to enable agentx. If so, add the line:
    master agentx
    to your /etc/snmp/snmpd.conf file and restart the snmpd service.

To access the values just use snmpget:

snmpget -v2c -c public localhost DOSTF-MIB::dosTFSubagentObject.0
This is the leaf-node of the MIB tree you created in your MIB. Now all you have to do is to modify the .c file you generated with mib2c so that it does something useful, i.e. actually computes the values you are retrieving.

1 comment:

  1. hi desmond,
    After generating C and H files using mib2c,in which diectoru i need to keep the generated files?..I am new to linux a step by step example will be more helpful..Please help me

    ReplyDelete