TUT - Writing A MIB Module - Net-SNMP Wiki
TUT - Writing A MIB Module - Net-SNMP Wiki
TUT - Writing A MIB Module - Net-SNMP Wiki
Contents
1 The "Big Picture" 2 Recommended Reading List 3 Writing Your First Module 3.1 MIBs For Dummies and mib2c 3.2 A simple scalar attached to a variable 3.3 A simple scalar with the value returned from code 3.4 A table of data, stored within the agent 3.5 Sending SNMP notifications (traps and informs) from inside the agent. 3.6 The older (and thus most backward compatible) ucd-snmp 4.X API 4 The MIB Module API 5 Compiling in your new MIB module 6 Set Processing 7 Tutorial Sections 7.1 About the SNMP Protocol 7.2 Net-SNMP Command Line Applications 7.3 Application Configuration 7.4 Net-SNMP Daemons 7.5 Coding Tutorials 7.6 Debugging SNMP Applications and Agents 7.7 Operating System Specific Tutorials
www.net-snmp.org/wiki/index.php/TUT:Writing_a_MIB_Module
1/7
15/08/13
In the example code, the module's name is "scalar_int", hence the name of the initialization function. Within the initialization function, the API function netsnmp_register_int_instance (http://www.netsnmp.org/dev/agent/group__instance.html#ga20) is called to actually register the integer at a specified OID. The prototype of that API function is:
i n tn e t s n m p _ r e g i s t e r _ i n t _ i n s t a n c e( c o n s tc h a r* n a m e , o i d * r e g _ o i d , s i z e _ t r e g _ o i d _ l e n , i n t * i t , N e t s n m p _ N o d e _ H a n d l e r* s u b h a n d l e r ) ;
The first argument, name, is a the short description of the object being registered. The second argument is the OID to assign to this object the length of the OID is stored in the third argument. It is recommended to use the O I D _ L E N G T Hmacro which will only work with statically allocated OIDs (arrays work, pointers don't).
www.net-snmp.org/wiki/index.php/TUT:Writing_a_MIB_Module 2/7
15/08/13
For the sake of simplicity the default subhandler is used in the example code, therefore N U L Lis passed as fifth argument. The default handler allows the integer to be read with a G E Trequest as well as set with a S E Trequest. Simple integer registration functions, including read-only registration and the default handlers, are declared in a g e n t / i n s t a n c e . h .
Sending SNMP notifications (traps and informs) from inside the agent.
Here is an example of how to use the agent's internal notification sending API (http://www.netsnmp.org/dev/agent/notification_8c-example.html) to send a notification to all of the agent's trap receivers.
The older (and thus most backward compatible) ucd-snmp 4.X API
Of course, you can continue to write code using the older API set. A tutorial on it can be found here (http://www.net-snmp.org/tutorial/tutorial-4/toolkit/mib_module/) .
15/08/13
would put the nstAgentModuleObject.h and nstAgentModuleObject.c files into the net-snmp source code directory. You do this by copying them into a agent/mibgroup/nstAgentModuleObject.h and agent/mibgroup/nstAgentModuleObject.c file. Next, you have to configure the package to find them and compile them into the agent. To do this, you run the configure script giving it the extra module names you want it to load:
%. / c o n f i g u r ew i t h m i b m o d u l e s = " n s t A g e n t M o d u l e O b j e c t "
If you had multiple modules to include (like a second "XXX" module, for example), you can separate them with spaces inside the quotes (e.g., --with-mib-modules="nstAgentModuleObject XXX"). Note that nstAgentModuleObject is the prefix and the configure script will actually look for a nstAgentModuleObject.h and a nstAgentModuleObject.c file. You must have a .h file and you can not get it to work with just a .c file. Build your new agent with your new code in it by running make:
%m a k e
Now that the agent is installed, you need to add some basic configuration (see other tutorials for more configuration options). Here we just add a simple read-only and read-write community strings (tutget and tutset , respectively) for the tutorial MIB branch:
%e c h o" r o c o m m u n i t yt u t g e t. 1 . 3 . 6 . 1 . 4 . 1 . 8 0 7 2 . 2 . 4 ">/ u s r / l o c a l / e t c / s n m p d . c o n f %e c h o" r w c o m m u n i t yt u t s e t. 1 . 3 . 6 . 1 . 4 . 1 . 8 0 7 2 . 2 . 4 ">/ u s r / l o c a l / e t c / s n m p d . c o n f
You can test out the functionality by starting the snmpd agent:
%/ u s r / l o c a l / s b i n / s n m p dfLdp9 9 9 9
This runs the agent on a temporary port ( -p 9999) so that it doesn't need special priviledges, or interfere with your 'normal' agent. Incoming and outgoing packets are printed (-d), to show what's happening, and the agent is run as a 'non-daemon' command (-f -L) so that you can see these messages. Note that this ties up the current shell, so you'll need to run the following checks in a different terminal window. And then running snmpget and snmpset on the scalar object:
%s n m p g e tv 2 cct u t g e tl o c a l h o s t : 9 9 9 9N E T S N M P T U T O R I A L M I B : : n s t A g e n t M o d u l e O b j e c t . 0 N E T S N M P T U T O R I A L M I B : : n s t A g e n t M o d u l e O b j e c t . 0=I N T E G E R :1 %s n m p s e tv 2 cct u t s e tl o c a l h o s t : 9 9 9 9N E T S N M P T U T O R I A L M I B : : n s t A g e n t M o d u l e O b j e c t . 0=5 N E T S N M P T U T O R I A L M I B : : n s t A g e n t M o d u l e O b j e c t . 0=I N T E G E R :5
www.net-snmp.org/wiki/index.php/TUT:Writing_a_MIB_Module 4/7
15/08/13
%s n m p g e tv 2 cct u t g e tl o c a l h o s t : 9 9 9 9N E T S N M P T U T O R I A L M I B : : n s t A g e n t M o d u l e O b j e c t . 0 N E T S N M P T U T O R I A L M I B : : n s t A g e n t M o d u l e O b j e c t . 0=I N T E G E R :5
You can also compile your code into a "subagent" which then attaches itself to the master agent using the AgentX subagent protocol (http://www.scguild.com/agentx/) . Our libraries provide support to make this easy to do and this is discussed in greater detail in a later section. Finally, you can also compile your code into pluggable shared object and tell the snmpd agent to load it. This is also discussed in greater detail in a later section .
Set Processing
To process an SNMP-SET, the agent must use a series of calls to the mib module code to ensure that processing of all sets in the incoming packet can be successful. This gives you or other mib modules the chance to bail out early on in the transaction sequence and thus stop all of the transactions in the set from happening. This is important for continuity. However, it makes set code processing a bit more complex. Let's examine a simple state diagram that the master agent uses at each step of the way: Error creating thumbnail: Unable to save thumbnail to destination In a perfect operation with no failures, we take the vertical path on the left. If any of the mib modules being acted upon returns an error of any kind, we will branch to the right to one of the failure states where you must clean up and possibly undo what you did in previous steps. If you need even finer-grained SET processing with even more states, check out the baby_steps helper module.
Tutorial Sections
About the SNMP Protocol
These tutorial links talk about SNMP generically and how the protocol itself works. They are good introductory reading material and the concepts are important to understand before diving into the later tutorials about NetSNMP itself. How SNMP Works: About the protocol itself (GETs, GETNEXTs, etc) What data is in SNMP: All about SNMP Management Information Bases (MIBs) Securing SNMP: How to use the SNMP protocol securely
15/08/13
snmpset: peforming write operations. snmpbulkget: communicates with a network entity using SNMP GETBULK request snmpbulkwalk: retrieve a sub-tree of management values using SNMP GETBULK requests. snmptrap: Sending and receiving traps, and acting upon them. Traps/informs with SNMPv3/USM: Sending and receiving SNMPv3/USM TRAPs and INFORMs Sending Traps/Informs via AgentX: Sending notifications from the command line through snmpd Common command line options: Using and loading MIBS SNMPv3/USM Options Using SNMPv3 over TLS and DTLS Customized Output Formats Writing mib2c config files
Application Configuration
All of our applications support configuration to allow you to customize how they behave. Configuration files for Net-SNMP applications
Net-SNMP Daemons
Net-SNMP comes with two long-running daemons: a SNMP agent (snmpd) for responding to management requests and a notification receiver (snmptrapd) for receiving SNMP notifications. SNMP Agent (snmpd) Configuration Configuration Basics Access Control (VACM) snmpconf SNMP Notification Receiver (snmptrapd) Configuring snmptrapd Configuring SNMPv3 notifications Configuring snmptrapd to understand vendor-specific MIBS (Cisco) Agent Monitoring DisMan Monitoring Monitoring with MRTG
Coding Tutorials
Net-SNMP comes with a highly flexible and extensible API. The API allows you to create your own commands, add extensions to the agent to support your own MIBs and perform specialized processing of notifications. Client / Manager Coding Tutorials Writing a simple application Writing a simple asynchronous application Agent Coding Tutorials The Agent Architecture page might be worth reading before or after the agent coding tutorials, and describes how the Agent Helpers work under the hood.
www.net-snmp.org/wiki/index.php/TUT:Writing_a_MIB_Module 6/7
15/08/13
Writing a mib module to serve information described by an SNMP MIB, and how to compile it into the net-snmp snmpd agent. Writing a Dynamically Loadable Object that can be loaded into the SNMP agent. Writing a Subagent that can be run to attach to the snmpd master agent. Writing a perl plugin to extend the agent using the NetSNMP::agent module. Writing shell scripts to extend the agent Using mib2c to help write an agent code template for you General mib2c Overview Using the mib2c-update script to recode your code mib2c.mfd.conf tutorial (http://www.net-snmp.org/tutorial/tutorial-5/toolkit/mfd/) Header files and autoconf
www.net-snmp.org/wiki/index.php/TUT:Writing_a_MIB_Module
7/7