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
|