Altoids.pm
Altoids - SNMP get/walk functionality with built-in name to OID translation
use Altoids;
$snmp = Altoids->new(\%hints, @oid_files);
$snmp->open($gateway, $community, $port);
%retblock = $snmp->get($name, $index);
%retblock = $snmp->walk($name);
$snmp->close();
Altoids is based upon SNMP_Session.pm and BER.pm, a self-contained,
low-level perl SNMP implementation.
This module mainly provides two simple calling interfaces:
get
and walk
, which are the roughly equivalent to the correspondingly
named snmpget and snmpwalk commands of the CMU SNMP package.
Additionally, it is able to assimilate information from SunNet Manager
file.oid files so that short alpha names (eg. ``sysName'') are used rather
than having to use the cumbersome dotted-decimal OIDs (eg. ``1.3.6.1.2.1.1.5'').
(It does this as an alternative to a full-blown MIB compiler to translate
OIDs - hence the name Altoids.)
-
$snmp->oid(SHORTNAME)
-
Takes a short name and returns a dotted-decimal OID in a scalar context, or
array of the OID decimals in an array context.
-
$snmp->walk(SHORTNAME)
-
Takes a short name and returns a hash where the human-readable OID segments
are the keys and either hashes or, ultimately, the OID value(s) as the
values. That is, it is a hash of hashes of values in the simplest case.
If no name can be translated to an OID or the named variables are not
available returns an empty hash. Returns (-1, -1) on other errors.
-
$snmp->get(SHORTNAME, INDEX)
-
Takes a short name and index number and returns a hash where the human-readable
OID segments are the keys and either hashes or, ultimately, the OID
values is the value. That is, it is a hash of a hash of a value in the
simplest case.
If the name can't be translated to an OID or the named variable is not
available returns an empty hash. Returns (-1, -1) on other errors.
.
use Altoids;
$snmp = Altoids->new(<~/.oid/RFC*.oid>);
$snmp->open('localhost', 'public', 161);
%retblock = $snmp->get('sysDescr', 0);
print($retblock{sysDescr}{0}, "\n") if defined $retblock{sysDescr}{0};
$snmp->close();
$snmp->open('localhost', 'public', 161);
%retblock = $snmp->walk('system');
hashprint(%retblock);
$snmp->close();
sub hashprint {
my $prefix = shift @_;
my %hash = @_;
my ($val, $key);
while (($key, $val) = each(%hash)) {
if (scalar($val) =~ m/^HASH\(/) {
hashprint($prefix? $prefix . '.' . $key : $key, %{$val})
} else {
print $prefix, '.', "$key = $val\n"
}
}
}
Enable verbose mode (informational messages to STDERR
) by:
$Altoids::verbose = 1;
This package sometimes die
s when bad things happen.
Dave Plonka <plonka@doit.wisc.edu>