Tut:Extending snmpd using perl

From Net-SNMP Wiki
Revision as of 16:42, 22 September 2007 by Oxo (Talk | contribs)

Jump to: navigation, search

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(); 
        } 
} 

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.

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!

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.

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.

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:

Operating System Specific Tutorials