Net-SNMP-Util
view release on metacpan or search on metacpan
lib/Net/SNMP/Util.pm view on Meta::CPAN
-retries => $opt{r} || 1,
-community => $opt{c} || "public" },
oids => { descr => '1.3.6.1.2.1.1.1.0',
uptime => '1.3.6.1.2.1.1.3.0',
name => '1.3.6.1.2.1.1.5.0',
location => '1.3.6.1.2.1.1.6.0',
}
);
die "[ERROR] $err\n" unless defined $ret;
foreach my $h ( @ARGV ){
if ( $ret->{$h} ){
printf "%s @%s (up %s) - %s\n",
map { $ret->{$h}{$_} or 'N/A' } qw(name location uptime descr);
} else {
printf "%s [ERROR]%s\n", $h, $err->{$h};
}
}
__END__
=head2 2. Realtime monitor of host interfaces (SNMPv2c)
This program shows realtime traffic throughput of interfaces of a host on your
console with using C<snmpwalk()> and callbacking.
Notice: This program is for devices which can deal SNMP version 2c.
#!/usr/local/bin/perl
use strict;
use warnings;
use Getopt::Std;
use Term::ANSIScreen qw/:color :screen :constants/;
use Net::SNMP::Util;
my %opt;
getopts('hv:c:w:x:', \%opt);
my $host = shift @ARGV;
sub HELP_MESSAGE {
print "Usage: $0 [-c COMMUNITY_NAME] [-w WAIT] [-x REGEXP] HOST\n";
exit 1;
}
HELP_MESSAGE() if ( !$host || $opt{h} );
my ($wait,$regexp) = ($opt{w}||5, $opt{x}? qr/$opt{x}/: '');
my $console = Term::ANSIScreen->new();
local $| = 1;
# make session
my ($ses, $err) = Net::SNMP->session(
-hostname => $host,
-version => "2",
-community => ($opt{c} || "public")
);
die "[ERROR] $err\n" unless defined $ses;
# main loop
my (%pdata, %cdata); # flag, previous and current octets data
my $first = 1;
while ( 1 ){
%cdata = ();
(my $ret, $err) = snmpwalk(
snmp => $ses,
oids => {
sysUpTime => '1.3.6.1.2.1.1.3',
ifTable => [
'1.3.6.1.2.1.31.1.1.1.1', # [0] ifName
'1.3.6.1.2.1.2.2.1.7', # [1] ifAdminStatus
'1.3.6.1.2.1.2.2.1.8', # [2] ifOperStatus
'1.3.6.1.2.1.31.1.1.1.6', # [3] ifHCInOctets
'1.3.6.1.2.1.31.1.1.1.10', # [4] ifHCOutOctets
'1.3.6.1.2.1.31.1.1.1.15', # [5] ifHighSpeed
] },
-mycallback => sub {
my ($s, $host, $key, $val) = @_;
return 1 if $key ne 'ifTable';
my $name = $val->[0][1];
return 0 if ( $regexp && $name !~ /$regexp/ );
# storing current octets data
$cdata{$name}{t} = time;
$cdata{$name}{i} = $val->[3][1];
$cdata{$name}{o} = $val->[4][1];
return 1;
}
);
die "[ERROR] $err\n" unless $ret;
# header
$console->Cls();
$console->Cursor(0, 0);
printf "%s, up %s - %s\n\n",
BOLD.$host.CLEAR, $ret->{sysUpTime}{0}, scalar(localtime(time));
# matrix
printf "%s%-30s (%-10s) %2s %2s %10s %10s %10s%s\n",
UNDERSCORE, qw/ifName ifIndex Ad Op BW(Mbps) InBps(M) OutBps(M)/, CLEAR;
my $iftable = $ret->{ifTable};
foreach my $i ( sort { $a <=> $b } keys %{$iftable->[1]} )
{
my ($name, $astat, $ostat, $bw)
= map { $iftable->[$_]{$i} } qw( 0 1 2 5 );
if ( $first ){
printf "%-30s (%-10d) %2d %2d %10.1f %10s %10s\n",
$name, $i, $astat, $ostat, $bw/1000, '-', '-';
next; # skip first
}
# calculate (k)bps
my $td = $cdata{$name}{t} - $pdata{$name}{t};
my ($inbps, $outbps) = map {
my $delta = $cdata{$name}{$_} - $pdata{$name}{$_};
$delta<0? 0: $delta / $td / 1000; # Kbps
} qw( i o );
printf "%-30s (%-10d) %2d %2d %10.1f %10.1f %10.1f\n",
$name, $i, $astat, $ostat, map { $_/1000 } ($bw, $inbps, $outbps);
}
%pdata = %cdata;
$first = 0;
sleep $wait;
}
__END__
=head2 3. Tiny MRTG with RRDTool (SNMPv2c)
With installing Tobias Oetiker's RRDTool and RRD::Simple, this sample will do
like MRTG. (It is better to execute this by cron.)
If Environmental variables, PATH2DATADIR and URL2HTMLDIR, are defined, files will
be stored under PATH2DATADIR and URL pathes will include URL2HTMLDIR in html.
Or Modify $datadir and $htmldir to decide these path and URL where browser can
access through your http service.
Notice: This program is for devices which can deal SNMP version 2c.
#!/usr/local/bin/perl
use strict;
use warnings;
use Getopt::Std;
use CGI qw(:html);
use RRD::Simple; # install the "RRDTool" and RRD::Simple
use Net::SNMP::Util qw(:para);
my %opt;
getopts('hc:x:', \%opt);
my @hosts = @ARGV;
sub HELP_MESSAGE {
print "Usage: $0 [-c COMMUNITY_NAME] [-x REGEXP] HOST [HOST [...]]\n";
exit 1;
}
HELP_MESSAGE() if ( !@hosts || $opt{h} );
my $datadir = $ENV{PATH2DATADIR} || "/path/to/datadir"; # !!! Modify !!!
my $htmldir = $ENV{URL2HTMLDIR} || "/path/to/htmldir"; # !!! Modify !!!
my $regexp = $opt{x}? qr/$opt{x}/: '';
my %sesopts = ( -version => 2, -community=> ($opt{c} || 'public') );
sub escname {
my $n = shift;
$n =~ tr/\\\/\*\?\|"<>:,;%/_/;
return $n;
}
# gather traffic data and store to RRD
my ($result, $error) = snmpparawalk(
hosts => \@hosts,
snmp => \%sesopts,
oids => {
ifData => [ '1.3.6.1.2.1.31.1.1.1.1', # ifName
'1.3.6.1.2.1.31.1.1.1.6', # ifHCInOctets
'1.3.6.1.2.1.31.1.1.1.10' ] # ifHCOutOctets
},
# this callback will work everything of necessary
-mycallback => sub {
( run in 1.389 second using v1.01-cache-2.11-cpan-39bf76dae61 )