SNMP_Session
view release on metacpan or search on metacpan
Net_SNMP_util.pm view on Meta::CPAN
snmpMIB_to_OID(shift(@Net_SNMP_util::MIB_Files));
$Net_SNMP_util::SuppressWarnings = $tmp;
($oid, $tmp) = &Check_OID($var);
if ($oid) {
open(CACHE, ">>$Net_SNMP_util::CacheFile");
print CACHE "$tmp\t$oid\n";
close(CACHE);
}
}
if ($oid) {
$var =~ s/^$tmp/$oid/;
} else {
carp("Unknown SNMP var $var\n")
unless ($Net_SNMP_util::SuppressWarnings > 1);
next;
}
while ($var =~ /\"([^\"]*)\"/) {
$tmp = sprintf("%d.%s", length($1), join(".", map(ord, split(//, $1))));
$var =~ s/\"$1\"/$tmp/;
}
print "toOID: $var\n" if $Net_SNMP_util::Debug;
push(@retvar, $var);
}
return @retvar;
}
#
# Check to see if an OID is in the text-to-OID cache.
# Returns the OID and the corresponding text as two separate
# elements.
#
sub Check_OID ($) {
my($var) = @_;
my($tmp, $tmpv, $oid);
if ($var =~ /^[a-zA-Z][\w\-]*(\.[a-zA-Z][\w\-]*)*/) {
$tmp = $&;
$tmpv = $tmp;
for (;;) {
last if exists($Net_SNMP_util::OIDS{$tmpv});
last if !($tmpv =~ s/^[^\.]*\.//);
}
$oid = $Net_SNMP_util::OIDS{$tmpv};
if ($oid) {
return ($oid, $tmp);
} else {
my @empty = ();
return @empty;
}
}
return ($var, $var);
}
sub snmpwalk_flg ($$@) {
my($host, $hash_sub, @vars) = @_;
my($session, %args, @enoid, @poid, $toid, $oid, $got);
my($val, $ret, %soid, %nsoid, @retvals, $tmp);
my(%rethash, $h_ref, @tmprefs);
my($stop);
$h_ref = (ref $vars[$#vars] eq "HASH") ? pop(@vars) : \%rethash;
$session = &snmpopen($host, 0, \@vars);
if (!defined($session)) {
carp "SNMPWALK Problem for $host"
unless ($Net_SNMP_util::SuppressWarnings > 1);
if (defined($hash_sub)) {
return ($h_ref) if ($SNMP_util::Return_hash_refs);
return (%$h_ref);
} else {
@retvals = ();
return (@retvals);
}
}
@enoid = toOID(@vars);
if ($#enoid < 0) {
if (defined($hash_sub)) {
return ($h_ref) if ($SNMP_util::Return_hash_refs);
return (%$h_ref);
} else {
@retvals = ();
return (@retvals);
}
}
#
# Create/Refresh a reversed hash with oid -> name
#
if (defined($hash_sub) and ($RevNeeded)) {
%revOIDS = reverse %Net_SNMP_util::OIDS;
$RevNeeded = 0;
}
#
# Create temporary array of refs to return values
#
foreach $oid (0..$#enoid) {
my $tmparray = [];
$tmprefs[$oid] = $tmparray;
$nsoid{$oid} = $oid;
}
$got = 0;
@poid = @enoid;
if ($Net_SNMP_util::Version > 1 and $Net_SNMP_util::MaxRepetitions > 0) {
$args{'-maxrepetitions'} = $Net_SNMP_util::MaxRepetitions;
}
if ($Net_SNMP_util::Version > 2) {
$args{'-contextengineid'} = $Net_SNMP_util::ContextEngineID
if (defined($Net_SNMP_util::ContextEngineID));
$args{'-contextname'} = $Net_SNMP_util::ContextName
if (defined($Net_SNMP_util::ContextName));
}
while($#poid >= 0) {
$args{'-varbindlist'} = \@poid;
if (($Net_SNMP_util::Version > 1)
and ($Net_SNMP_util::MaxRepetitions > 1)) {
$ret = $session->get_bulk_request(%args);
} else {
$ret = $session->get_next_request(%args);
}
last if (!defined($ret));
%soid = %nsoid;
undef %nsoid;
$stop = 0;
foreach $oid (&Net::SNMP::oid_lex_sort(keys %$ret)) {
$got = 1;
$tmp = -1;
foreach $toid (@enoid) {
$tmp++;
if (&Net::SNMP::oid_base_match($toid, $oid)
and (!exists($soid{$toid}) or ($oid ne $soid{$toid}))) {
$nsoid{$toid} = $oid;
if (defined($hash_sub)) {
#
# extract name of the oid, if possible, the rest becomes the
# instance
#
my $inst = "";
my $upo = $toid;
while (!exists($revOIDS{$upo}) and length($upo)) {
$upo =~ s/(\.\d+?)$//;
if (defined($1) and length($1)) {
$inst = $1 . $inst;
} else {
$upo = "";
last;
}
}
if (length($upo) and exists($revOIDS{$upo})) {
$upo = $revOIDS{$upo} . $inst;
} else {
$upo = $toid;
}
my $qoid = $oid;
my $tmpo;
$inst = "";
while (!exists($revOIDS{$qoid}) and length($qoid)) {
$qoid =~ s/(\.\d+?)$//;
if (defined($1) and length($1)) {
$inst = $1 . $inst;
} else {
$qoid = "";
last;
}
}
if (length($qoid) and exists($revOIDS{$qoid})) {
$tmpo = $qoid;
$qoid = $revOIDS{$qoid};
} else {
$qoid = $oid;
$tmpo = $toid;
$inst = substr($oid, length($tmpo)+1);
}
#
# call hash_sub
#
&$hash_sub($h_ref, $host, $qoid, $tmpo, $inst, $ret->{$oid}, $upo);
} else {
my $tmpo;
my $tmpv = $ret->{$oid};
$tmpo = substr($oid, length($toid)+1);
push @{$tmprefs[$tmp]}, "$tmpo:$tmpv";
}
} else {
$stop = 1 if ($#enoid == 0);
}
}
}
undef @poid;
@poid = values %nsoid if (!$stop);
}
if ($got) {
if (defined($hash_sub)) {
return ($h_ref) if ($Net_SNMP_util::ReturnHashRefs);
return (%$h_ref);
} elsif ($Net_SNMP_util::Return_array_refs) {
return (@tmprefs);
} else {
do {
$got = 0;
foreach $toid (0..$#enoid) {
next if (scalar(@{$tmprefs[$toid]}) <= 0);
$got = 1;
$oid = shift(@{$tmprefs[$toid]});
if ($#enoid > 0) {
($oid, $val) = split(':', $oid, 2);
$oid = $enoid[$toid] . '.' . $oid;
push(@retvals, "$oid:$val");
} else {
push(@retvals, $oid);
}
}
} while($got);
return (@retvals);
}
} else {
$ret = join(' ', @vars);
error_msg("SNMPWALK Problem for $ret on ${host}: " . $session->error());
if (defined($hash_sub)) {
return ($h_ref) if ($SNMP_util::Return_hash_refs);
return (%$h_ref);
} else {
@retvals = ();
return (@retvals);
}
}
}
#
# When passed a string, return the ASN.1 type that corresponds to the
# string.
#
sub ASNtype($) {
my($type) = @_;
$type =~ tr/A-Z/a-z/;
if ($type eq "int") {
$type = 0x02;
} elsif ($type eq "integer") {
$type = 0x02;
} elsif ($type eq "string") {
$type = 0x04;
} elsif ($type eq "octetstring") {
$type = 0x04;
} elsif ($type eq "octet string") {
$type = 0x04;
} elsif ($type eq "oid") {
$type = 0x06;
} elsif ($type eq "object id") {
$type = 0x06;
} elsif ($type eq "object identifier") {
$type = 0x06;
} elsif ($type eq "ipaddr") {
$type = 0x40;
} elsif ($type eq "ip address") {
$type = 0x40;
} elsif ($type eq "timeticks") {
$type = 0x43;
} elsif ($type eq "uint") {
$type = 0x47;
} elsif ($type eq "uinteger") {
$type = 0x47;
} elsif ($type eq "uinteger32") {
$type = 0x47;
( run in 1.358 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )