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";
Contents
Extended example of extending snmpd using perl subagent
I hope the following can help with ideas / Owen Brotherwood, DK 2007.
NetSNMP::agent module
And here we continue where http://www.net-snmp.org/tutorial/tutorial-5/toolkit/perl/perl_module.pl left off with the NetSNMP::agent module.
MODE_GET
"snmpget" is trivial as the requester knows what it wants and, if the agent has it, the reply is a simple hash lookup.
MODE_GETNEXT
"snmpwalk" is harder: took me an afternoon to remember perl and write the first version ... The request almost knows what it wants, and the agent need's to reply to anything that is in the area for the OID it has registered. Let's start.
Take $regat, '.1.3.6.1.4.1.8072.999'. We have a .1 extension that we would like to serve, with values in .1.1 .1.2 etc .2.1 .2.2 etc etc.
The secret is to fill in the blanks in the hash used for values. So if $regat or $regat.1 or $regat.1.1 etc are requested, the agent should supply the "next" OID and it's value.
So for $regat, reply with $regat.1.1.1 and it's value.
Also there is a check for < $regat which replies with $regat.1.1.1 and it's value.
So take a look at the hashs for OID's and pay attention to how $OID_next{$prev_OID} is used in the example.
Input CSV
The format of the input csv is "made for the moment":
oidname1::4::value1:value2 oidname2::4::value3:value4
The 4 is ASN_OCTET_STR on my system. I would have liked to have written ASN_OCTET_STR as type but my perl programming experience couldn't help me in taking the ASN_OCTET_STR and use it directly in the reply (Input please ...:) )
Diverse
Well, reading a big file in every time isn't the best way ... Funny ideas: -Use set to trigger a read -The file is already formatted as a hash ... -Find another way ...
And remember, use the extending of snmpd to allow other snmp programs access to intresting information instead of using, for example, txt files or SQL DB's that are normally used to lock the information in.
Good luck!
"Code" : perl do
#!/usr/bin/perl # # perl do "/path/to/perl_module.pl"; # # # use snmpd -f to debug ... # # Owen Brotherwood, DK 2007 # Based on original perl module example # GNU General Public License V3 # # CHANGE THESE TO REQUIREMENTS # # Accept under this area $regat = '.1.3.6.1.4.1.8072.999'; # # The file used for test data $mibdata = '/etc/snmp/myperlext.csv'; # For print STDERR which can be seen when using -f option for snmpd $debugging = 1; use NetSNMP::OID (':all'); use NetSNMP::agent (':all'); use NetSNMP::ASN (':all'); BEGIN { print STDERR "Starting do perl"; } sub my_snmp_handler { my ($handler, $registration_info, $request_info, $requests) = @_; my $request; my %my_oid = (); # for this example, wasteful read test data in every time ... open(MIB,$mibdata); @mibdata = <MIB>; close(MIB); # we append .1 to $regat for the area which the test data is available $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; $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}); } } } } #-------------------------------- # Standard Example from here ... #-------------------------------- 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(); } }
SF Tracker Request
http://sourceforge.net/tracker/index.php?func=detail&aid=1800323&group_id=12694&atid=362694
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: