Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

SNMP Test for mib

by Anonymous Monk
on Oct 26, 2009 at 02:32 UTC ( [id://803196]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

has any one use the SNMP_Session or SNMP_util package to do a snmpwalk to test if a device supports a specific mib. I know I could do a run the snmpwalk command and return the result in variable. But I was looking for a perl solution

Replies are listed 'Best First'.
Re: SNMP Test for mib
by leighsharpe (Monk) on Oct 26, 2009 at 04:28 UTC
    You could try using Net::SNMP
    If you do a get_request on an OID which is not supported by the device, it returns and error, and the error_status() is set to 2 (noSuchName). For example:
    #! /usr/local/bin/perl use strict; use Net::SNMP; my ($session, $error) = Net::SNMP->session( -hostname => shift || '192.168.5.242', -community => shift || 'public', -port => shift || 161 ); if (!defined($session)) { printf("ERROR: %s.\n", $error); exit 1; } my $sysUpTime = '1.3.6.1.2.1.1.3.0.1'; my $result = $session->get_request( -varbindlist => [$sysUpTime] ); if (!defined($result)) { printf("ERROR: %s.\n", $session->error); my $error_status=$session->error_status; printf("Error Status: %s \n", $error_status); $session->close; if ($error_status == 2) { print "This device does not support the MIB requested.\n"; } exit 1; } printf("sysUpTime for host '%s' is %s\n", $session->hostname, $result->{$sysUpTime} ); $session->close; exit 0;
    What's wrong with this OID is that I have added '.1' to the end of a normal sysUptime OID.
Re: SNMP Test for mib
by gman (Friar) on Oct 26, 2009 at 14:33 UTC

    Hello

    Here is an solution I use to walk a particular tree

    This includes RRD code also, but you can look beyond that

    #!/usr/bin/perl use strict; use Net::SNMP qw(:snmp); use Data::Dumper; use lib qw( /usr/local/rrdtool-1.2.13/lib/perl ); use RRDs; use File::Copy; my $hostname = shift || "xxx.xxx.xxx.xxx"; my $community = shift || "xxx"; my $rrdname = $hostname; $rrdname =~ s/\.//g; my $rrd = "/<rrdpath>.$rrdname"; my $localaddr = "xxx.xxx.xxx.xxx"; my $version = "snmpv2c"; my $port = 161; my $debug = 1; my $ifTable = "1.3.6.1.4.1.9148.3.2.1.1"; #APSYSMGMT-MIB::apSys"; my %statHash = ( 1=>'apCPUUtil', 2=>'apMemoryUtil', 3=>'apHealthScore', 4=>'apRedundancy', 5=>'apGlobalConSess', 6=>'apGlobalCPS', 7=>'apNATCapacity', 8=>'apARPCapacity', 9=>'apState', 10=>'apLicenseCapacity', 11=>'apSipStatsActiveLocalContacts', 12=>'apMgcpGWEndpoints', 13=>'apH323Registration', ); my %names = (); my %fullstat = (); my ($session, $error) = Net::SNMP->session( -version => $version, -nonblocking => 1, -hostname => $hostname, -community => $community, -port => $port, -debug => 1, ); if (!defined($session)) { printf("ERROR: %s.\n", $error); exit 1; } my $result = $session->get_bulk_request( -callback => [\&table_cb, {}], -maxrepetitions => 10, -varbindlist => [$ifTable] ); if (!defined($result)) { printf("ERROR: %s.\n", $session->error); $session->close; exit 1; } #print Dumper($result); snmp_dispatcher(); $session->close; #print Dumper(%fullstat); foreach my $stat_index (sort keys %fullstat) { print "$stat_index\n"; my $rrd = $rrd . ".$stat_index.rrd"; foreach my $realm_name (sort keys %{$fullstat{$stat_index}}) { print "*\t$realm_name => $fullstat{$stat_index}{$realm_nam +e}\n"; } if(! -e $rrd) { my $startTime = time()-3600; my $meas_hashRef = \%{$fullstat{$stat_index}}; my $step = 3*60; #300 seconds = 5min print "Calling createRRD ($startTime,$rrd,$step,$meas_hash +Ref)\n"; createRRD($startTime,$rrd,$step,$meas_hashRef); } else { #update updateRRD($rrd,\%{$fullstat{$stat_index}}); #my $return = copy("$rrd","/var/www/voipdata/$rrd"); #if( ! $return) { die "Could not rename file $return $ +!"; }; } } exit 0; sub table_cb { my ($session, $table) = @_; if (!defined($session->var_bind_list)) { printf("ERROR: %s\n", $session->error); } else { # print Dumper($session->var_bind_list); # Loop through each of the OIDs in the response and assign # the key/value pairs to the anonymous hash that is passed # to the callback. Make sure that we are still in the table # before assigning the key/values. my $next; foreach my $oid (oid_lex_sort(keys(%{$session->var_bind_list} +))) { if (!oid_base_match($ifTable, $oid)) { $next = undef; last; } $next = $oid; $table->{$oid} = $session->var_bind_list->{$oid}; } # If $next is defined we need to send another request # to get more of the table. if (defined($next)) { $result = $session->get_bulk_request( -callback => [\&table_cb, $table], -maxrepetitions => 10, -varbindlist => [$next] ); if (!defined($result)) { printf("ERROR: %s\n", $session->error); } } else { # We are no longer in the table, so print the results. foreach my $oid (oid_lex_sort(keys(%{$table}))) { printf("*** %s => %s\n", $oid, $table->{$oid}); my ($stat,$index) = (split(/\./,$oid))[-2,-1]; # build names hash #if($oid =~ /$ifTable\.2\.\d+$/) { #this is the apSig +RealmStatsRealmName $names{$index} = $table->{$oid}; #} printf("Index: %s, %s, %s\n",$names{$index},$statHash{ +$stat},$table->{$oid}); $fullstat{$statHash{$stat}}{$statHash{$stat}} = $table +->{$oid}; } } } } sub createRRD { my ($starttime,$rrd,$step,$meas_hashRef) = @_; print "In createRRD: ($starttime,$rrd,$step,$meas_hashRef)\n"; my $DS_string = "$rrd --start $starttime --step $step "; foreach(sort keys %$meas_hashRef) { my $key = $_; $key =~ s/\./-/g; $DS_string = $DS_string . "DS:$key:GAUGE:$step:U:U "; print "$DS_string\n"; } $DS_string = $DS_string . "RRA:AVERAGE:0.5:1:3000 " . "RRA:MIN:.5:1:3000 " . "RRA:MAX:.5:1:3000 " . "RRA:LAST:.5:1:3000 "; print "$DS_string\n"; my $return = `/usr/voip/bin/rrdtool create $DS_string`; print $return; my $ERROR = RRDs::error; die "$0: unable to create $rrd : $ERROR" if $ERROR; } sub updateRRD { my ($rrd,$hashRef) = @_; my $epoc = time(); #my %hash = %$hashRef; print "********* Start Of OID ******************\n($rrd,$hashR +ef)\n"; my $data_string = ''; print "DEBUG: building updateRRD data string\n"; #print Dumper($hashRef); foreach my $item (sort keys %$hashRef) { my $value = $hashRef->{$item}; if($value =~ /\./) { $value =100; }; print "$item => $value\n"; $data_string = $data_string . "$value:"; } $data_string =~ s/:$//g; print "rrdtool update $rrd $epoc:$data_string\n"; RRDs::updatev $rrd, $epoc .":" . $data_string; if (my $ERROR = RRDs::error) { warn "$0: unable to update $rrd : $ERROR"; } } #end sub

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://803196]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-24 20:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found