instantiate helper SNMP::Info instances with better defaults

This commit is contained in:
Oliver Gorwits
2023-09-27 13:32:14 +01:00
parent 993edd0c6a
commit 582163aa3e
3 changed files with 33 additions and 18 deletions

View File

@@ -3,7 +3,7 @@ package App::Netdisco::Transport::SNMP;
use Dancer qw/:syntax :script/;
use Dancer::Plugin::DBIC 'schema';
use App::Netdisco::Util::SNMP 'get_communities';
use App::Netdisco::Util::SNMP qw/get_communities get_mibdirs/;
use App::Netdisco::Util::Device 'get_device';
use App::Netdisco::Util::Permission 'acl_matches';
use App::Netdisco::Util::Snapshot qw/load_cache_for_device add_snmpinfo_aliases/;
@@ -11,7 +11,6 @@ use App::Netdisco::Util::Snapshot qw/load_cache_for_device add_snmpinfo_aliases/
use SNMP::Info;
use Try::Tiny;
use Module::Load ();
use Path::Class 'dir';
use NetAddr::IP::Lite ':lower';
use List::Util qw/pairkeys pairfirst/;
@@ -136,7 +135,7 @@ sub _snmp_connect_generic {
BulkWalk => ((defined setting('bulkwalk_off') && setting('bulkwalk_off'))
? 0 : 1),
BulkRepeaters => (setting('bulkwalk_repeaters') || 20),
MibDirs => [ _build_mibdirs() ],
MibDirs => [ get_mibdirs() ],
IgnoreNetSNMPConf => 1,
Debug => ($ENV{INFO_TRACE} || 0),
DebugSNMP => ($ENV{SNMP_TRACE} || 0),
@@ -372,16 +371,4 @@ sub _mk_info_commargs {
);
}
sub _build_mibdirs {
my $home = (setting('mibhome') || dir(($ENV{NETDISCO_HOME} || $ENV{HOME}), 'netdisco-mibs'));
return map { dir($home, $_)->stringify }
@{ setting('mibdirs') || _get_mibdirs_content($home) };
}
sub _get_mibdirs_content {
my $home = shift;
my @list = map {s|$home/||; $_} grep { m|/[a-z0-9-]+$| } grep {-d} glob("$home/*");
return \@list;
}
true;

View File

@@ -3,6 +3,7 @@ package App::Netdisco::Util::SNMP;
use Dancer qw/:syntax :script !to_json !from_json/;
use App::Netdisco::Util::DeviceAuth 'get_external_credentials';
use Path::Class 'dir';
use File::Spec::Functions qw/splitdir catdir catfile/;
use MIME::Base64 qw/decode_base64/;
use Storable qw/thaw/;
@@ -14,6 +15,7 @@ our @EXPORT = ();
our @EXPORT_OK = qw/
get_communities
snmp_comm_reindex
get_mibdirs
%ALL_MUNGERS
decode_and_munge
sortable_oid
@@ -142,6 +144,24 @@ sub snmp_comm_reindex {
return $snmp;
}
=head2 get_mibdirs
Return a list of directories in the `netdisco-mibs` folder.
=cut
sub get_mibdirs {
my $home = (setting('mibhome') || dir(($ENV{NETDISCO_HOME} || $ENV{HOME}), 'netdisco-mibs'));
return map { dir($home, $_)->stringify }
@{ setting('mibdirs') || _get_mibdirs_content($home) };
}
sub _get_mibdirs_content {
my $home = shift;
my @list = map {s|$home/||; $_} grep { m|/[a-z0-9-]+$| } grep {-d} glob("$home/*");
return \@list;
}
our %ALL_MUNGERS = (
'SNMP::Info::munge_speed' => \&SNMP::Info::munge_speed,
'SNMP::Info::munge_highspeed' => \&SNMP::Info::munge_highspeed,

View File

@@ -3,7 +3,7 @@ package App::Netdisco::Util::Snapshot;
use Dancer qw/:syntax :script !to_json !from_json/;
use Dancer::Plugin::DBIC 'schema';
use App::Netdisco::Util::SNMP 'sortable_oid';
use App::Netdisco::Util::SNMP qw/get_mibdirs sortable_oid/;
use File::Spec::Functions qw/splitdir catdir catfile/;
use MIME::Base64 qw/encode_base64 decode_base64/;
@@ -150,8 +150,12 @@ sub snmpwalk_to_cache {
my $info = SNMP::Info->new({
Offline => 1,
Cache => {},
AutoSpecify => 0,
Session => {},
MibDirs => [ get_mibdirs() ],
AutoSpecify => 0,
IgnoreNetSNMPConf => 1,
Debug => ($ENV{INFO_TRACE} || 0),
DebugSNMP => ($ENV{SNMP_TRACE} || 0),
});
foreach my $attr (keys %leaves) {
@@ -318,8 +322,12 @@ sub add_snmpinfo_aliases {
$info = SNMP::Info->new({
Offline => 1,
Cache => $info,
AutoSpecify => 0,
Session => {},
MibDirs => [ get_mibdirs() ],
AutoSpecify => 0,
IgnoreNetSNMPConf => 1,
Debug => ($ENV{INFO_TRACE} || 0),
DebugSNMP => ($ENV{SNMP_TRACE} || 0),
});
}