Difference between revisions of "Tut:Extending snmpd using perl"

From Net-SNMP Wiki
Jump to: navigation, search
(Diverse)
(Input CSV)
Line 49: Line 49:
 
By the way:
 
By the way:
 
The 4 is ASN_OCTET_STR on my system. I would have liked to have written ASN_OCTET_STR as type  
 
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 ...:) )
+
but my perl programming experience couldn't help me in taking the ASN_OCTET_STR and use it directly in the reply: I would just like to take the type as written and not use a if else test sequence ... (Input please ...:) )
  
 
===ARGV===
 
===ARGV===

Revision as of 08:17, 27 September 2007

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.

Oxo 10:10, 25 September 2007 (PDT)

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.

I focus on using perl from a "perl do" in snmpd.conf (I don't understand the other uses).

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.

So check for OID < $regat.1.1.1 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". For example with $delimT='=' and $delimV=':' (The oidname isn't used in the code and is there for referance when writting csv file)

oidname1=4=value1:value2
oidname2=4=value3:value4

Which gives relative to $regat

1.1.1 : value1 (string)
1.1.2 : value2 (string)
1.2.1 : value3 (string)
1.2.2 : value4 (string)

By the way: 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: I would just like to take the type as written and not use a if else test sequence ... (Input please ...:) )

ARGV

Doesn't seem to work ...

Work around

We define the needed parameters in snmpd.conf and test in code:

Here is a "fun" one: walk passwd

perl print STDERR "Perl extentsions:\n"
perl $regat = '.1.3.6.1.4.1.8072.998'; $mibdata = '/etc/passwd'; $delimT=''; $delimV=':'; do "/etc/snmp/myext.pl";

Note that if $delimT="" we assume the input is string.

SF Tracker Request

http://sourceforge.net/tracker/index.php?func=detail&aid=1800323&group_id=12694&atid=362694

"Code" : perl do

"snapshot" or see http://svn.berlios.de/wsvn/odp/trunk/bin/snmpagent.pl?op=file&rev=0&sc=0

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

# running from snmpd.conf  or command line
if (!defined($regat)) {
        $regat = $ARGV[0];          # Register at this OID
        $mibdata = $ARGV[1];        # File for data Name_for_Values$delimTType_of_Values$delimTValue1$delimVValue2$delimV
        $delimT = $ARGV[2];         # Delimeter between Name $delimT Type $delimV Values
        $delimV = $ARGV[3];         # Delimeter between Values
}

# For print STDERR which can be seen when using -f option for snmpd
$debugging = 1;
$verbose = 0;

use NetSNMP::OID (':all');
use NetSNMP::agent (':all');
use NetSNMP::ASN (':all');

BEGIN {
    print STDERR "Starting " . $0;
}

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');
# start taking in values
        undef($prev_oid);
        $jndex = 1;
        foreach $line (@mibdata) {
# fill the hash pipe
                chomp $line;
                if ($delimT != ''){
                        ($index_name, $index_type, $index_values) = split(/$delimT/, $line);
                }else{
                        $index_values = $line;
                        $index_name = 'Unknown';
                        $index_type = '4';
                }
                @value = split(/$delimV/, $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 ($verbose);
                        $index++;
                }
                $jndex++;
        }
        $mjndex = $jndex;
        $mindex = $index;
# fill in some blanks
        for ($jndex = 1; $jndex < $mjndex; $jndex++) {
                $this_oid = new NetSNMP::OID($base_oid . '.' . $jndex);
                $next_oid = new NetSNMP::OID($this_oid . '.1');
                $oid_next{$this_oid} = $next_oid;
        }
        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 <= $base_oid) {
                                $next_oid = new NetSNMP::OID($base_oid . '.1.1');
                                $type_oid = $oid_type{$next_oid};
                                $value_oid = $oid_value{$next_oid};
                                $request->setOID($next_oid);
                                $request->setValue($type_oid, $value_oid);
                        }else {
                                print STDERR "Hit by a truck whilst walking ...\n";
                        }
                }
        }
}
#--------------------------------
# Standard Example from here ...
#--------------------------------
sub shut_it_down {
  $running = 0;
}


        print STDERR " loaded ok\n";
        print STDERR "Parameters:$regat, $mibdata, $delimT, $delimV\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();
        }

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 ...(need a structure for $oid_next etc so it is one hash ...
  • Find another way ...

The following doesn't work and is a bit annoying and I don't know why so I hope for feedback from the developers.
Only one entry will work:

perl print STDERR "Perl extentsions:\n"
perl $regat = '.1.3.6.1.4.1.8072.998'; $mibdata = '/etc/passwd'; $delimT=''; $delimV=':'; do "/etc/snmp/myext.pl";
perl $regat = '.1.3.6.1.4.1.8072.999'; $mibdata = '/etc/group'; $delimT=''; $delimV=':'; do "/etc/snmp/myext.pl";

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!