AgentX Tutorial using Net-SNMP

Since no good tutorials are available I put together what I learnt. My approach was step by step learning and that is what I describe here. Explorer should clone this code and checkout step by step. Or an advanced explorer could skip some steps. Your choice. To help understand what is going on, I checkin the code more often so explorer could compare what was auto-generated and what was modification to make things work.

Notes

  1. I used net-snmp 5.7.3 for this tutorial.
  2. All code is in flat directory structure in start. Only during the last stage I will move things around to src and include directories to make it cleaner.
  3. We need a MIB first. I created a sample mib AGENTX-TUTORIAL-MIB under experimental branch (.1.3.6.1.3) so my MIB branch is .1.3.6.1.3.9999.
  4. I also created CMakeLists.txt for anyone wanting to use cmake for compiling.

Step 1 - Simple MIB with two Read-Only Scalars

MIB

In step 1 I implement only two read-only scalars (one integer and one string) since they are easiest to follow. MIB structure is as below.

vishalj@dreams:~/workspace/agentx-tutorial⟫ snmptranslate -Tp -IR agentxTutorial
+--agentxTutorial(9999)
   |
   +--myScalars(1)
   |  |
   |  +-- -R-- String    myROString(1)
   |  |        Textual Convention: DisplayString
   |  |        Size: 0..255
   |  +-- -R-- Integer32 myROInteger(2)
   |           Range: 0..2147483647
   |
   +--myConformenceGroups(99)
      |
      +--myScalarGroup(1)

I used smilint to ensure that mib structure is correct. You can verify it as below and it should not give any errors.

vishalj@dreams:~/workspace/agentx-tutorial⟫ smilint -l 4 -s ./AGENTX-TUTORIAL-MIB
Compiling the MIB / Generating Code

Using mib2c we can compile this MIB into 'C' code. Following three steps show how to create 'C' code, makefile and subagent code respectively.

mib2c -c mib2c.scalar.conf agentxTutorial
mib2c -c mfd-makefile.m2m agentxTutorial
mib2c -c subagent.m2c agentxTutorial

Here is shell output.

vishalj@dreams:~/workspace/agentx-tutorial⟫ mib2c -c mib2c.scalar.conf agentxTutorial
writing to agentxTutorial.h
writing to agentxTutorial.c
running indent on agentxTutorial.c
running indent on agentxTutorial.h
vishalj@dreams:~/workspace/agentx-tutorial⟫ mib2c -c mfd-makefile.m2m agentxTutorial
writing to agentxTutorial_Makefile
vishalj@dreams:~/workspace/agentx-tutorial⟫ mib2c -c subagent.m2c agentxTutorial                                                                                                              
writing to agentxTutorial_subagent.c
running indent on agentxTutorial_subagent.c
vishalj@dreams:~/workspace/agentx-tutorial⟫
Checkin! (git tag: step1_scalars_autogenerated)

I checkin the code now so explorer can see what was generated by mib2c. Nothing would compile anything yet.

Modify the code to make it work
  1. Lets introduce two variables that we are going to monitor and name them as myROStringVar and myROIntVar to match our MIB. I added them in subagent source file agentxTutorial_subagent.c.
  2. Then I added an alarm handler which will update their values every 5 seconds. myRWString will hold the unixtime when myROIntVar changed.
  3. Finally in agentxTutorial.c I updated the placeholders to copy the values from application variables into MIB variables.
Compile the code

use make -f agentxTutorial_makefile or use cmake as follows.

vishalj@dreams:~/workspace/agentx-tutorial⟫ mkdir build
vishalj@dreams:~/workspace/agentx-tutorial⟫ cd build/
vishalj@dreams:~/workspace/agentx-tutorial/build⟫ /opt/clion/clion-2016.3.2/bin/cmake/bin/cmake ../
-- The C compiler identification is GNU 6.2.0
-- The CXX compiler identification is GNU 6.2.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/vishalj/workspace/agentx-tutorial/build
vishalj@dreams:~/workspace/agentx-tutorial/build⟫  make
[ 33%] Building C object CMakeFiles/agentx-tutorial.dir/agentxTutorial.c.o
[ 66%] Building C object CMakeFiles/agentx-tutorial.dir/agentxTutorial_subagent.c.o
[100%] Linking C executable agentx-tutorial
[100%] Built target agentx-tutorial
vishalj@dreams:~/workspace/agentx-tutorial/build⟫
Run the code
  1. On one terminal run it as below. Press CTRL C to kill. ``` vishalj@dreams:~/workspace/agentx-tutorial/build⟫ ./agentx-tutorial -f NET-SNMP version 5.7.3 AgentX subagent connected
1. On other terminal run following snmpwalk commands with some wait in between so you can see the values changing.

vishalj@dreams:~/workspace/agentx-tutorial/cmake-build-debug⟫ snmpwalk -On localhost agentxTutorial .1.3.6.1.3.9999.1.1.0 = STRING: last changed = 1484447214 .1.3.6.1.3.9999.1.2.0 = INTEGER: 1 vishalj@dreams:~/workspace/agentx-tutorial/cmake-build-debug⟫ snmpwalk -On localhost agentxTutorial .1.3.6.1.3.9999.1.1.0 = STRING: last changed = 1484447214 .1.3.6.1.3.9999.1.2.0 = INTEGER: 1 vishalj@dreams:~/workspace/agentx-tutorial/cmake-build-debug⟫ snmpwalk -On localhost agentxTutorial .1.3.6.1.3.9999.1.1.0 = STRING: last changed = 1484447219 .1.3.6.1.3.9999.1.2.0 = INTEGER: 2 vishalj@dreams:~/workspace/agentx-tutorial/cmake-build-debug⟫ snmpwalk -On localhost agentxTutorial .1.3.6.1.3.9999.1.1.0 = STRING: last changed = 1484447219 .1.3.6.1.3.9999.1.2.0 = INTEGER: 2 vishalj@dreams:~/workspace/agentx-tutorial/cmake-build-debug⟫ snmpwalk -On localhost agentxTutorial .1.3.6.1.3.9999.1.1.0 = STRING: last changed = 1484447224 .1.3.6.1.3.9999.1.2.0 = INTEGER: 3 vishalj@dreams:~/workspace/agentx-tutorial/cmake-build-debug⟫ snmpwalk -On localhost agentxTutorial .1.3.6.1.3.9999.1.1.0 = STRING: last changed = 1484447254 .1.3.6.1.3.9999.1.2.0 = INTEGER: 9 vishalj@dreams:~/workspace/agentx-tutorial/cmake-build-debug⟫ snmpwalk localhost agentxTutorial AGENTX-TUTORIAL-MIB::myROString.0 = STRING: last changed = 1484447289 AGENTX-TUTORIAL-MIB::myROInteger.0 = INTEGER: 16 vishalj@dreams:~/workspace/agentx-tutorial/cmake-build-debug⟫ ```

Checkin! (git tag: step1_scalars_implement)

I checkin the code. Thats it for step 1.

results matching ""

    No results matching ""