« blib/lib/App/Netdisco/Worker/Plugin/DiscoverAll.pm
↑ Summary
blib/lib/App/Netdisco/Worker/Plugin/Expire.pm »
| 1 | package App::Netdisco::Worker::Plugin::DumpInfoCache; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use App::Netdisco::Worker::Plugin; | ||
| 5 | use aliased 'App::Netdisco::Worker::Status'; | ||
| 6 | |||
| 7 | use Module::Load (); | ||
| 8 | use Data::Dumper; | ||
| 9 | use Storable 'dclone'; | ||
| 10 | |||
| 11 | use App::Netdisco::Transport::SNMP; | ||
| 12 | |||
| 13 | register_worker({ phase => 'check' }, sub { | ||
| 14 | my ($job, $workerconf) = @_; | ||
| 15 | my ($device, $port, $extra) = map {$job->$_} qw/device port extra/; | ||
| 16 | |||
| 17 | Module::Load::load 'Module::Info'; | ||
| 18 | Module::Load::load 'Data::Tie::Watch'; | ||
| 19 | |||
| 20 | return Status->error('Missing device (-d).') unless $device; | ||
| 21 | return Status->error(sprintf "unknown device: %s.", $device) | ||
| 22 | unless $device->in_storage; | ||
| 23 | |||
| 24 | return Status->done('Dump info cache is able to run'); | ||
| 25 | }); | ||
| 26 | |||
| 27 | register_worker({ phase => 'main', driver => 'snmp' }, sub { | ||
| 28 | my ($job, $workerconf) = @_; | ||
| 29 | my ($device, $class, $dumpclass) = map {$job->$_} qw/device port extra/; | ||
| 30 | |||
| 31 | $class = 'SNMP::Info::'.$class if $class and $class !~ m/^SNMP::Info::/; | ||
| 32 | my $snmp = App::Netdisco::Transport::SNMP->reader_for($device, $class); | ||
| 33 | |||
| 34 | $dumpclass = 'SNMP::Info::'.$dumpclass if $dumpclass and $dumpclass !~ m/^SNMP::Info::/; | ||
| 35 | $dumpclass ||= ($snmp->class || $device->snmp_class); | ||
| 36 | |||
| 37 | debug sprintf 'inspecting class %s', $dumpclass; | ||
| 38 | my %sh = Module::Info->new_from_loaded($dumpclass)->subroutines; | ||
| 39 | my @subs = grep { $_ !~ m/^_/ } | ||
| 40 | map { $_ =~ s/^.+:://; $_ } ## no critic | ||
| 41 | keys %sh; | ||
| 42 | |||
| 43 | my $cache = {}; | ||
| 44 | my $fetch = sub { | ||
| 45 | my($self, $key) = @_; | ||
| 46 | my $val = $self->Fetch($key); | ||
| 47 | #return $val if !defined $val; | ||
| 48 | |||
| 49 | my @ignore = qw(munge globals funcs Offline store sess debug snmp_ver); | ||
| 50 | return $val if scalar grep { $_ eq $key } @ignore; | ||
| 51 | |||
| 52 | (my $stripped = $key) =~ s/^_//; | ||
| 53 | if (exists $snmp->{store}->{$stripped}) { | ||
| 54 | $cache->{$key} = 1; | ||
| 55 | $cache->{store}->{$stripped} = dclone $snmp->{store}->{$stripped}; | ||
| 56 | } | ||
| 57 | return $val if exists $snmp->{store}->{$stripped}; | ||
| 58 | |||
| 59 | #print "In fetch callback, key=$key\n"; | ||
| 60 | #Data::Printer::p( $val ); print "\n"; | ||
| 61 | |||
| 62 | $cache->{$key} = $val; | ||
| 63 | return $val; | ||
| 64 | }; | ||
| 65 | |||
| 66 | my $watch = Data::Tie::Watch->new( | ||
| 67 | -variable => $snmp, | ||
| 68 | -fetch => [$fetch], | ||
| 69 | ); | ||
| 70 | |||
| 71 | $snmp->$_ for @subs; | ||
| 72 | $watch->Unwatch; | ||
| 73 | |||
| 74 | print Dumper( $cache ); | ||
| 75 | return Status->done( | ||
| 76 | sprintf "Dumped %s cache for %s", $dumpclass, $device->ip); | ||
| 77 | }); | ||
| 78 | |||
| 79 | true; |