Tut:Extending snmpd using perl
Right now there is not a whole lot here, but this should eventually be a tutorial for using all the various perl modules, including how to embed perl directly within the net-snmp agent (similar to how mod_perl support allows you to embed perl directly into the apache web server).
For the time being, I'll offer a perl module source code which can be used as a perl SNMP agent, perl subagent, or sourced directly within a agent containing embedded perl support. To make it work directly within your agent, you must have compiled the net-snmp package using --enable-embedded-perl and then in your snmpd.conf file you can put:
perl do "/path/to/perl_module.pl";
The following shows a bit more about snmpwalk ...
#!/usr/bin/perl # # perl do "/path/to/perl_module.pl"; # # # use snmpd -f to debug ... # # Owen Brotherwood 2007 # Based on original perl module example # GNU General Public Lincense V3 # $regat = '.1.3.6.1.4.1.8072.999'; $mibdata = '/root/mib.csv'; $debugging = 0; $verbose = 1; use NetSNMP::OID (':all'); use NetSNMP::agent (':all'); use NetSNMP::ASN (':all'); BEGIN { print STDERR "Starting "; } sub my_snmp_handler { my ($handler, $registration_info, $request_info, $requests) = @_; my $request; my %my_oid = (); # oid: 1.2.$jndex.$index # csv: indexer for each $jndex per line # name::type::value1:value2 # for this example, wasteful read in every time ... open(MIB,$mibdata); @mibdata = <MIB>; close(MIB); # we append .1 to $regat for fun $base_oid = new NetSNMP::OID($regat . '.1'); # fill in the blanks so getnext works $this_oid = new NetSNMP::OID($regat); $next_oid = new NetSNMP::OID($base_oid . '.1.1'); $oid_next{$this_oid} = $next_oid; $this_oid = $base_oid; $oid_next{$base_oid} = $next_oid; # start taking in values undef($prev_oid); $jndex = 1; foreach $line (@mibdata) { # fill in the blanks so getnext works $this_oid = new NetSNMP::OID($base_oid . '.' . $jndex); $next_oid = new NetSNMP::OID($base_oid . '.' . $jndex . '.1'); $oid_next{$this_oid} = $next_oid; # fill the hash pipe chomp $line; ($index_name, $index_type, $index_values) = split(/::/, $line); @value = split(/:/, $index_values); $index = 1; foreach $mibit (@value) { $this_oid = new NetSNMP::OID($base_oid . '.' . $jndex . '.' . $index); $oid_type{$this_oid} = $index_type; # for now ... $oid_value{$this_oid} = $mibit; $oid_index{$this_oid} = $index; $oid_jndex{$this_oid} = $jndex; if (defined($prev_oid)){ $oid_next{$prev_oid} = $this_oid; } $prev_oid = $this_oid; print STDERR "Loading $this_oid $oid_type{$this_oid}::$oid_value{$this_oid} \n" if ($debugging); $index++; } $jndex++; } for ($request = $requests; $request; $request = $request->next()) { $oid = $request->getOID(); print STDERR $oid if ($debugging); if ($request_info->getMode() == MODE_GET) { # easy to get print STDERR ":GET" if ($debugging); if (exists $oid_value{$oid}) { print STDERR "->$oid_value{$oid}\n" if ($debugging); $request->setValue($oid_type{$oid}, $oid_value{$oid}); }else{ print STDERR " No value ...\n"; } }elsif ($request_info->getMode() == MODE_GETNEXT) { # long way to walk print STDERR ":GETNEXT" if($debugging); if (defined($oid_next{$oid})) { $next_oid = $oid_next{$oid}; $type_oid = $oid_type{$next_oid}; $value_oid = $oid_value{$next_oid}; $request->setOID($next_oid); $request->setValue($type_oid, $value_oid); }elsif ($oid < new NetSNMP::OID($regat . '.1.1.1')) { $this_oid = new NetSNMP::OID($regat . '.1.1.1'); $request->setOID($oid_next{$this_oid}); $request->setValue($oid_type{$this_oid}, $oid_value{$this_oid}); } } } } sub shut_it_down { $running = 0; } { print STDERR " loaded ok\n"; # if we're not embedded, this will get auto-set below to 1 $subagent = 0; # where we are going to hook onto my $regoid = new NetSNMP::OID($regat); print STDERR "Registering at " . $regoid . " (" . $regat .")\n" if ($debugging); if (!$agent) { $agent = new NetSNMP::agent('Name' => 'test', # reads test.conf 'AgentX' => 1); # make us a subagent $subagent = 1; print STDERR "started us as a subagent ($agent)\n" } $agent->register('myname',$regoid, \&my_snmp_handler); if ($subagent) { # We need to perform a loop here waiting for snmp requests. We # aren't doing anything else here, but we could. $SIG{'INT'} = \&shut_it_down; $SIG{'QUIT'} = \&shut_it_down; $running = 1; while($running) { $agent->agent_check_and_process(1); # 1 = block print STDERR "mainloop excercised\n" if ($debugging); } $agent->shutdown(); } }
Contents
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 Net-SNMP 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
Net-SNMP Command Line Applications
These tutorial pages discuss the command line tools provided in the Net-SNMP suite of tools. Nearly all the example commands in these tutorials works if you try it yourself, as they're all examples that talk to our online Net-SNMP test agent. Given them a shot!
- snmptranslate: learning about the MIB tree.
- snmpget: retrieving data from a host.
- snmpgetnext: retrieving unknown indexed data.
- snmpwalk: retrieving lots of data at once!
- snmptable: displaying a table.
- 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:
- Writing mib2c config files
Application Configuration
All of our applications support configuration to allow you to customize how they behave.
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
- SNMP Notification Receiver (snmptrapd)
- Agent Monitoring
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
- 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.
- 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
- Header files and autoconf
Debugging SNMP Applications and Agents
All our tools and applications have extensive debugging output. These tutorials talk about how the debugging system works and how you can add your own debugging statements to you code:
- Debugging output printed using the -D command line option
- Using -Ddump to display packet breakdowns
- Debugging using GDB
Operating System Specific Tutorials
- Building With Visual Studio 2005 Express
- Building Net-SNMP 64-bit with Visual C++ 2010 Express
- Net-Snmp on Ubuntu
- Net-SNMP and lm-sensors on Ubuntu 10.04
- Net-SNMP for windows: