Simple SNMP Agent
Traditional SNMP agents typically give you all the monitoring and management features you could want. Sometimes you may wish you could write your own SNMP agent. Anyone who has looked at the Net-SNMP agent code knows how complex it is. Well, have no fear! The Net-SNMP package now comes with Perl bindings that allow you to create a Perl SNMP agent in a number of ways:
Standalone SNMP agent
AgentX subagent
Embedded agent within the normal Net-SNMP agent
For this section, we thought it would be interesting to present a fully working Perl agent. This isn't so much to write yet another SNMP agent but rather to show how an SNMP agent operates.
The basic flow through the agent is as follows:
Create a new agent object
Register the top of the agent's OID tree
Register a callback subroutine
Sit and wait for requests
It's pretty straightforward, and so is the code itself. First things first, though. You need to get a copy of the Net-SNMP package from http://www.net-snmp.org. Follow the instructions for building and installing the package and Perl modules.
The complete agent source follows:
205 #!/usr/bin/perl 206 207 # 208 # File: agent.pl 209 # 210 211 use NetSNMP::agent (':all'); 212 use NetSNMP::default_store (':all'); 213 use NetSNMP::ASN (':all'); 214 use NetSNMP::OID; 215 use SNMP; 216 217 my $port = "9161"; 218 my $host = ".1.3.6.1.4.1.8072.25"; 219 my $hrMemorySize = $host.".2.2"; 220 221 sub myHandler{ 222 my ($handler, $registration_info, $request_info, $requests) = @_; 223 ...
Get Essential SNMP, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.