diff --git a/Info.pm b/Info.pm index db5dfaf5..7994f6fd 100644 --- a/Info.pm +++ b/Info.pm @@ -1053,6 +1053,20 @@ SNMP::Session object to use instead of connecting on own. (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 and C 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 mode from a previously dumped cache. See also the +C method to retrieve a cache after running actial queries. + =item OTHER All other arguments are passed to SNMP::Session. @@ -1142,6 +1156,16 @@ sub new { 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; if ( defined $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 and table methods. +The cache can be retreved or set using the $info->cache() method. This works +together with the C option. + =head2 Object Scalar Methods These are for package related data, not directly supplied @@ -1314,6 +1341,50 @@ sub 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 +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]) 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() ) { # Let's get the MIB Module and leaf name along with the OID my $qual_leaf = SNMP::translateObj($oid,0,1) || ''; @@ -3973,6 +4050,12 @@ sub _load_attr { && !$load && !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 # specify the Module (MIB) in the case of private leaf naming # conflicts. Example: ALTEON-TIGON-SWITCH-MIB::agSoftwareVersion @@ -4182,6 +4265,7 @@ sub snmp_connect_ip { my $ver = $self->snmp_ver(); my $comm = $self->snmp_comm(); + return if $self->{Offline}; return if ( $ip eq '0.0.0.0' ) or ( $ip =~ /^127\./ ); # Create session object diff --git a/t/test_class.pl b/t/test_class.pl index b1b3dab9..9148dd08 100755 --- a/t/test_class.pl +++ b/t/test_class.pl @@ -42,6 +42,7 @@ my $EMPTY = q{}; my $class = $EMPTY; my @dump = (); my $debug = 0; +my $cache = 0; my $device = ''; my $comm = ''; my $ver = 2; @@ -58,9 +59,10 @@ GetOptions( 'v|ver=i' => \$ver, 'i|ignore' => \$ignore, 'p|print=s' => \@dump, - 'x|debug+' => \$debug, 'm|mibdir=s' => \$mibdirs, 'n|nobulk' => \$nobulk, + 'x|debug+' => \$debug, + 'k|cache' => \$cache, 'h|?|help' => sub { pod2usage(1); }, ); @@ -170,6 +172,15 @@ foreach my $fn (@dump) { 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 { @@ -244,10 +255,11 @@ Options: -s|comm SNMP community -v|ver SNMP version -p|print Print values - -x|debug Debugging flag -i|ignore Ignore Net-SNMP configuration file -m|mibdir Directory containing MIB Files -n|nobulk Disable bulkwalk + -x|debug Debugging flag + -k|cache Dump cache (requires Data::Printer to be installed) -h|?|help Brief help message =head1 OPTIONS @@ -286,12 +298,6 @@ multiple times. -print i_description -print i_type -=item B<-debug> - -Turns on SNMP::Info debug. - --debug - =item B<-ignore > 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 +=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 module be installed, otherwise does nothing. + +-cache + =item B<-help> Print help message and exits.