implement cache and offline mode

This commit is contained in:
Oliver Gorwits
2014-06-09 21:21:26 +01:00
parent d0722d3677
commit 2c88544158
2 changed files with 111 additions and 8 deletions

84
Info.pm
View File

@@ -1053,6 +1053,20 @@ SNMP::Session object to use instead of connecting on own.
(default creates session automatically) (default creates session automatically)
=item Offline
Causes SNMP::Info to avoid network activity and return data only from its
cache. If you ask for something not in the cache, an error is thrown. See
also the C<cache()> and C<offline()> methods.
(default 0, which means "online")
=item Cache
Pass in a HashRef to prime the cache of retrieved data. Useful for creating an
instance in C<Offline> mode from a previously dumped cache. See also the
C<cache()> method to retrieve a cache after running actial queries.
=item OTHER =item OTHER
All other arguments are passed to SNMP::Session. All other arguments are passed to SNMP::Session.
@@ -1142,6 +1156,16 @@ sub new {
delete $sess_args{IgnoreNetSNMPConf}; delete $sess_args{IgnoreNetSNMPConf};
} }
if ( defined $args{Offline} ) {
$new_obj->{Offline} = $args{Offline} || 0;
delete $sess_args{Offline};
}
if ( defined $args{Cache} and ref {} eq ref $args{Cache} ) {
$new_obj->{$_} = $args{Cache}->{$_} for keys %{$args{Cache}};
delete $sess_args{Cache};
}
my $sess = undef; my $sess = undef;
if ( defined $args{Session} ) { if ( defined $args{Session} ) {
$sess = $args{Session}; $sess = $args{Session};
@@ -1266,6 +1290,9 @@ data from a method.
Run $info->clear_cache() to clear the cache to allow reload of both globals Run $info->clear_cache() to clear the cache to allow reload of both globals
and table methods. and table methods.
The cache can be retreved or set using the $info->cache() method. This works
together with the C<Offline> option.
=head2 Object Scalar Methods =head2 Object Scalar Methods
These are for package related data, not directly supplied These are for package related data, not directly supplied
@@ -1314,6 +1341,50 @@ sub debug {
return $self->{debug}; return $self->{debug};
} }
=item $info->offline([1|0])
Returns if offline mode is currently turned on for this object.
Optionally sets the Offline parameter.
=cut
sub offline {
my $self = shift;
my $ol = shift;
if ( defined $ol ) {
$self->{Offline} = $ol;
}
return $self->{Offline};
}
=item $info->cache([new_cache])
Returns a HashRef of all cached data in this object. There will be a C<store>
key for table data and then one key for each leaf.
Optionally sets the cache parameters if passed a HashRef.
=cut
sub cache {
my $self = shift;
my $data = shift;
if ( defined $data and ref {} eq ref $data ) {
$self->{$_} = $data->{$_} for keys %$data;
}
my $cache = { store => $self->{store} };
foreach my $key ( keys %$self ) {
next unless defined $key;
next unless $key =~ /^_/;
$cache->{$key} = $self->{$key};
}
return $cache;
}
=item $info->bulkwalk([1|0]) =item $info->bulkwalk([1|0])
Returns if bulkwalk is currently turned on for this object. Returns if bulkwalk is currently turned on for this object.
@@ -3649,6 +3720,12 @@ sub _global {
} }
} }
if ( $self->{Offline} ) {
$self->error_throw(
"SNMP::Info::_global: Offline but $attr is not in cache\n" );
return;
}
if ( $self->debug() ) { if ( $self->debug() ) {
# Let's get the MIB Module and leaf name along with the OID # Let's get the MIB Module and leaf name along with the OID
my $qual_leaf = SNMP::translateObj($oid,0,1) || ''; my $qual_leaf = SNMP::translateObj($oid,0,1) || '';
@@ -3973,6 +4050,12 @@ sub _load_attr {
&& !$load && !$load
&& !defined $partial ); && !defined $partial );
if ( $self->{Offline} ) {
$self->error_throw(
"SNMP::Info::_load_atrr: Offline but $attr is not in cache\n" );
return;
}
# We want the qualified leaf name so that we can # We want the qualified leaf name so that we can
# specify the Module (MIB) in the case of private leaf naming # specify the Module (MIB) in the case of private leaf naming
# conflicts. Example: ALTEON-TIGON-SWITCH-MIB::agSoftwareVersion # conflicts. Example: ALTEON-TIGON-SWITCH-MIB::agSoftwareVersion
@@ -4182,6 +4265,7 @@ sub snmp_connect_ip {
my $ver = $self->snmp_ver(); my $ver = $self->snmp_ver();
my $comm = $self->snmp_comm(); my $comm = $self->snmp_comm();
return if $self->{Offline};
return if ( $ip eq '0.0.0.0' ) or ( $ip =~ /^127\./ ); return if ( $ip eq '0.0.0.0' ) or ( $ip =~ /^127\./ );
# Create session object # Create session object

View File

@@ -42,6 +42,7 @@ my $EMPTY = q{};
my $class = $EMPTY; my $class = $EMPTY;
my @dump = (); my @dump = ();
my $debug = 0; my $debug = 0;
my $cache = 0;
my $device = ''; my $device = '';
my $comm = ''; my $comm = '';
my $ver = 2; my $ver = 2;
@@ -58,9 +59,10 @@ GetOptions(
'v|ver=i' => \$ver, 'v|ver=i' => \$ver,
'i|ignore' => \$ignore, 'i|ignore' => \$ignore,
'p|print=s' => \@dump, 'p|print=s' => \@dump,
'x|debug+' => \$debug,
'm|mibdir=s' => \$mibdirs, 'm|mibdir=s' => \$mibdirs,
'n|nobulk' => \$nobulk, 'n|nobulk' => \$nobulk,
'x|debug+' => \$debug,
'k|cache' => \$cache,
'h|?|help' => sub { pod2usage(1); }, 'h|?|help' => sub { pod2usage(1); },
); );
@@ -170,6 +172,15 @@ foreach my $fn (@dump) {
if ( !$dumped{$fn} ) { test_fn( $dev, $fn ) } if ( !$dumped{$fn} ) { test_fn( $dev, $fn ) }
} }
if ($cache) {
eval {
require Data::Printer;
} && eval {
print "\nDumping cache...\n\n";
Data::Printer::p $dev;
};
}
#-------------------------------- #--------------------------------
sub test_global { sub test_global {
@@ -244,10 +255,11 @@ Options:
-s|comm SNMP community -s|comm SNMP community
-v|ver SNMP version -v|ver SNMP version
-p|print Print values -p|print Print values
-x|debug Debugging flag
-i|ignore Ignore Net-SNMP configuration file -i|ignore Ignore Net-SNMP configuration file
-m|mibdir Directory containing MIB Files -m|mibdir Directory containing MIB Files
-n|nobulk Disable bulkwalk -n|nobulk Disable bulkwalk
-x|debug Debugging flag
-k|cache Dump cache (requires Data::Printer to be installed)
-h|?|help Brief help message -h|?|help Brief help message
=head1 OPTIONS =head1 OPTIONS
@@ -286,12 +298,6 @@ multiple times.
-print i_description -print i_type -print i_description -print i_type
=item B<-debug>
Turns on SNMP::Info debug.
-debug
=item B<-ignore > =item B<-ignore >
Ignore Net-SNMP configuration file snmp.conf. If this used mibdirs must be Ignore Net-SNMP configuration file snmp.conf. If this used mibdirs must be
@@ -312,6 +318,19 @@ Disable SNMP bulkwalk. Default bulkwalk is on and utilized with version 2.
-nobulk -nobulk
=item B<-debug>
Turns on SNMP::Info debug.
-debug
=item B<-cache>
Dumps the table and leaf cache at the end of running. Requires that the
L<Data::Printer> module be installed, otherwise does nothing.
-cache
=item B<-help> =item B<-help>
Print help message and exits. Print help message and exits.