Tut:Extending snmpd using perl

From Net-SNMP Wiki
Revision as of 18:37, 22 September 2007 by Oxo (Talk | contribs) (Input CSV)

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";

Extended example of extending snmpd using perl subagent

I hope the following can help with ideas / Owen Brotherwood, DK 2007.

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 so allow other snmp programs access to intresting information instead of, for example, txt files or SQL DB's.

Good luck!

"Code"

#!/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 Lincense V3 
# 

$regat = '.1.3.6.1.4.1.8072.999'; 
$mibdata = '/etc/snmp/<perl_module>.csv'; 

$debugging = 1; 
$verbose = 0; 


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