1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#!/usr/local/bin/perl -w
use strict;
use SNMP_Session;
use BER;
use Time::HiRes qw(gettimeofday tv_interval);
sub usage();
$SNMP_Session::suppress_warnings = 1;
my ($host, $community, $interval, $port, $factor, @OIDS);
$interval = 5;
$port = 161;
$factor = 1;
while ($#ARGV >= 0) {
$_ = shift @ARGV;
if (/^-t$/) {
$interval = shift @ARGV;
} elsif (/^-p$/) {
$port = shift @ARGV;
} elsif (/^-b$/) {
$factor = 8;
} elsif (! defined ($host)) {
$host = $_;
} elsif (! defined ($community)) {
$community = $_;
} else {
push @OIDS, $_;
}
}
usage() if !defined $host || !defined $community || !@OIDS;
my @encoded_oids = @OIDS;
grep (($_ = encode_oid (split ('\.',$_)) || die "cannot encode $_"),
@encoded_oids);
my $session = SNMP_Session->open ($host, $community, $port)
|| die "couldn't open SNMP session to $host";
my %last_values;
my $last_time;
get_initial_values ($session, @encoded_oids)
|| die "Couldn't get initial values";
while (1) {
sleep $interval;
print_value_changes ($session, @encoded_oids);
}
$session->close ();
1;
sub usage() {
die "Usage: $0 [-t interval] [-p port] host community OID...";
}
sub get_initial_values ($@) {
my ($session, @encoded_oids) = @_;
if (!$session->get_request_response (@encoded_oids)) {
print STDERR "Request to $host failed: $SNMP_Session::errmsg\n";
} else {
my $response = $session->pdu_buffer;
my ($bindings) = $session->decode_get_response ($response);
$last_time = [gettimeofday()];
while ($bindings ne '') {
my $binding;
($binding,$bindings) = decode_sequence ($bindings);
my ($oid,$value) = decode_by_template ($binding, "%O%@");
grep ($_=pretty_print $_, $oid, $value);
$last_values{$oid} = $value;
}
}
1;
}
sub print_value_changes ($@) {
my ($session, @encoded_oids) = @_;
if (!$session->get_request_response (@encoded_oids)) {
print STDERR "Request to $host failed: $SNMP_Session::errmsg\n";
} else {
my $this_time = [gettimeofday()];
my $response = $session->pdu_buffer;
my ($bindings) = $session->decode_get_response ($response);
my $real_interval = tv_interval ($last_time, $this_time);
$last_time = $this_time;
while ($bindings ne '') {
my $binding;
($binding,$bindings) = decode_sequence ($bindings);
my ($oid,$value) = decode_by_template ($binding, "%O%@");
grep ($_=pretty_print $_, $oid, $value);
my $diff = $value - $last_values{$oid};
printf "%12.2f",$diff/$real_interval*$factor,"\n";
$last_values{$oid} = $value;
}
print "\n";
}
1;
}
|