diff --git a/bin/netdisco-do b/bin/netdisco-do index baef827c..8d3c12a7 100755 --- a/bin/netdisco-do +++ b/bin/netdisco-do @@ -428,15 +428,16 @@ Performs an snmp walk from L<.1.3.6.1> on the device and builds a data structure which SNMP::Info can use to mimic the device. The structure is saved into the L database table. -Pass a value to the L<-p> parameter (mnemonic: persist) and the base64 encoded -data will also be saved to L (where L is -the canonical IP of the device passed). Netdisco will load this for any pseudo -device with the same canonical IP. +Optionally, pass a value to the L<-p> parameter (mnemonic: persist) and the +base64 encoded data will also be saved to L +(where L is the canonical IP of the device passed). Netdisco will load +this for any pseudo device with the same canonical IP. ~/bin/netdisco-do snapshot -d 192.0.2.1 -p yes -Pass a value to the L<-e> parameter (mnemonic: explore> and the data is also -saved into the Netdisco database for web browsing (under Device->SNMP tab). +Optionally, pass a value to the L<-e> parameter (mnemonic: explore> and the +data is also saved into the Netdisco database for web browsing (under +Device->SNMP tab). ~/bin/netdisco-do snapshot -d 192.0.2.1 -e yes diff --git a/lib/App/Netdisco/Transport/SNMP.pm b/lib/App/Netdisco/Transport/SNMP.pm index 7bd57842..40fca87f 100644 --- a/lib/App/Netdisco/Transport/SNMP.pm +++ b/lib/App/Netdisco/Transport/SNMP.pm @@ -65,7 +65,10 @@ sub reader_for { my $device = get_device($ip) or return undef; my $pseudo_cache = catfile( catdir(($ENV{NETDISCO_HOME} || $ENV{HOME}), 'logs', 'snapshots'), $device->ip ); - return undef if $device->in_storage and $device->is_pseudo and ! -f $pseudo_cache; + if ($device->in_storage and $device->is_pseudo and ! -f $pseudo_cache) { + error sprintf 'transport error - cannot act on pseudo-device [%s] without offline cache', $device->ip; + return undef; + } my $readers = $class->instance->readers or return undef; return $readers->{$device->ip} if exists $readers->{$device->ip}; @@ -168,6 +171,7 @@ sub _snmp_connect_generic { my $pseudo_cache = catfile( catdir(($ENV{NETDISCO_HOME} || $ENV{HOME}), 'logs', 'snapshots'), $device->ip ); $snmp_args{Cache} = thaw( decode_base64( read_text($pseudo_cache) ) ); $snmp_args{Offline} = 1; + debug sprintf 'snmp transport running in offline mode for: [%s]', $device->ip; } # get the community string(s) diff --git a/lib/App/Netdisco/Web/Plugin/Device/SNMP.pm b/lib/App/Netdisco/Web/Plugin/Device/SNMP.pm index b5eba3ca..54db9bca 100644 --- a/lib/App/Netdisco/Web/Plugin/Device/SNMP.pm +++ b/lib/App/Netdisco/Web/Plugin/Device/SNMP.pm @@ -19,7 +19,7 @@ register_device_tab({ tag => 'snmp', label => 'SNMP', get '/ajax/content/device/snmp' => require_login sub { my $device = try { schema('netdisco')->resultset('Device') - ->find( param('ip') ) } + ->find( param('q') ) } or send_error('Bad Device', 404); template 'ajax/device/snmp.tt', { device => $device->ip }, diff --git a/lib/App/Netdisco/Worker/Plugin/Snapshot.pm b/lib/App/Netdisco/Worker/Plugin/Snapshot.pm index 94ef0463..3f35c5a5 100644 --- a/lib/App/Netdisco/Worker/Plugin/Snapshot.pm +++ b/lib/App/Netdisco/Worker/Plugin/Snapshot.pm @@ -25,127 +25,59 @@ register_worker({ phase => 'check' }, sub { register_worker({ phase => 'main', driver => 'snmp' }, sub { my ($job, $workerconf) = @_; my $device = $job->device; + + my $save_browser = $job->extra; my $save_file = $job->port; - my $save_db = $job->extra; # needed to avoid $var being returned with leafname and breaking loop checks $SNMP::use_numeric = 1; + + # might restore a cache if there's one on disk my $snmp = App::Netdisco::Transport::SNMP->reader_for($device) or return Status->defer("snapshot failed: could not SNMP connect to $device"); my %oidmap = getoidmap($device, $snmp); - my %walk = walker($device, $snmp, '.1.3.6.1'); # 10205 rows - # my %walk = walker($device, $snmp, '.1.3.6.1.2.1.2.2.1.6'); # 22 rows, i_mac/ifPhysAddress + my %munges = get_munges($snmp); - my %munge = %{ $snmp->munge() }; - my %munge_set = (); + # only if not pseudo device + walk_and_store($device, $snmp, %oidmap) if not $device->is_pseudo; - # take the snmpwalk of the device which is numeric (no MIB translateObj), - # resolve to MIB identifiers using netdisco-mibs, then store in SNMP::Info - # instance cache + # load the cache + my %cache = %{ $snmp->cache() }; - my (%tables, %leaves, @realoids) = ((), (), ()); - OID: foreach my $orig_oid (keys %walk) { - my $oid = $orig_oid; - my $idx = ''; + # finally, freeze the cache, then base64 encode, store in the DB, + # optionally store browsing data, and optionally save file. - while (length($oid) and !exists $oidmap{$oid}) { - $oid =~ s/\.(\d+)$//; - $idx = ($idx ? "${1}.${idx}" : $1); - } - - if (exists $oidmap{$oid}) { - $idx =~ s/^\.//; - my $leaf = $oidmap{$oid}; - - if ($idx eq 0) { - push @realoids, $oid; - $leaves{ $leaf } = $walk{$orig_oid}; - $munge_set{$leaf} = subname($munge{$leaf}) if exists $munge{$leaf}; - } - else { - push @realoids, $oid if !exists $tables{ $leaf }; - $tables{ $leaf }->{$idx} = $walk{$orig_oid}; - $munge_set{$leaf} = subname($munge{$leaf}) if exists $munge{$leaf}; - } - - # debug "snapshot $device - cached $oidmap{$oid}($idx)"; - next OID; - } - - debug "snapshot $device - missing OID $orig_oid in netdisco-mibs"; - } - - $snmp->_cache($_, $leaves{$_}) for keys %leaves; - $snmp->_cache($_, $tables{$_}) for keys %tables; - - # we want to add in the GLOBALS and FUNCS aliases which users - # have created in the SNMP::Info device class, with binary copy - # of data so that it can be frozen - - my %cache = %{ $snmp->cache() }; - my %funcs = %{ $snmp->funcs() }; - my %globals = %{ $snmp->globals() }; - - while (my ($alias, $leaf) = each %globals) { - if (exists $cache{"_$leaf"} and !exists $cache{"_$alias"}) { - $snmp->_cache($alias, $cache{"_$leaf"}); - } - $munge_set{$leaf} = subname($munge{$alias}) if exists $munge{$alias}; - } - - while (my ($alias, $leaf) = each %funcs) { - if (exists $cache{store}->{$leaf} and !exists $cache{store}->{$alias}) { - $snmp->_cache($alias, dclone $cache{store}->{$leaf}); - } - $munge_set{$leaf} = subname($munge{$alias}) if exists $munge{$alias}; - } - - # now for any other SNMP::Info method in GLOBALS or FUNCS which Netdisco - # might call, but will not have data, we fake a cache entry to avoid - # throwing errors - - # refresh the cache - %cache = %{ $snmp->cache() }; - - while (my $method = ) { - $method =~ s/\s//g; - next unless length $method and !exists $cache{"_$method"}; - - $snmp->_cache($method, {}) if exists $funcs{$method}; - $snmp->_cache($method, '') if exists $globals{$method}; - } - - # finally, freeze the cache, then base64 encode, store in the DB, and - # optionally save file. - - # refresh the cache again - %cache = %{ $snmp->cache() }; - - debug "snapshot $device - cacheing snapshot bundle"; - my $frozen = encode_base64( nfreeze( \%cache ) ); - $device->update_or_create_related('snapshot', {cache => $frozen}); - - if ($save_db) { + if ($save_browser) { debug "snapshot $device - cacheing snapshot for browsing"; + my %seenoid = (); + my @browser = map {{ oid => $_, oid_parts => [ grep {length} (split m/\./, $_) ], leaf => $oidmap{$_}, - munge => $munge_set{ $oidmap{$_} }, + munge => $munges{ $oidmap{$_} }, value => do { my $m = $oidmap{$_}; encode_base64( nfreeze( [$snmp->$m] ) ); }, - }} sort {sortable_oid($a) cmp sortable_oid($b)} @realoids; + }} sort {sortable_oid($a) cmp sortable_oid($b)} + grep {not $seenoid{$_}++} + grep {m/^\.1\.3\.6\.1/} + map {s/^_//; $_} + keys %cache; schema('netdisco')->txn_do(sub { my $gone = $device->oids->delete; - debug sprintf ' [%s] snapshot - removed %d oids', + debug sprintf 'snapshot %s - removed %d oids from db', $device->ip, $gone; $device->oids->populate(\@browser); - debug sprintf ' [%s] snapshot - added %d new oids', + debug sprintf 'snapshot %s - added %d new oids to db', $device->ip, scalar @browser; }); } + debug "snapshot $device - cacheing snapshot bundle"; + my $frozen = encode_base64( nfreeze( \%cache ) ); + $device->update_or_create_related('snapshot', {cache => $frozen}); + if ($save_file) { my $target_dir = catdir(($ENV{NETDISCO_HOME} || $ENV{HOME}), 'logs', 'snapshots'); make_path($target_dir); @@ -181,6 +113,112 @@ sub getoidmap { return %oidmap; } +sub get_munges { + my $snmp = shift; + my %munge_set = (); + + my %munge = %{ $snmp->munge() }; + my %funcs = %{ $snmp->funcs() }; + my %globals = %{ $snmp->globals() }; + + while (my ($alias, $leaf) = each %globals) { + $munge_set{$leaf} = subname($munge{$leaf}) if exists $munge{$leaf}; + $munge_set{$leaf} = subname($munge{$alias}) if exists $munge{$alias}; + } + + while (my ($alias, $leaf) = each %funcs) { + $munge_set{$leaf} = subname($munge{$leaf}) if exists $munge{$leaf}; + $munge_set{$leaf} = subname($munge{$alias}) if exists $munge{$alias}; + } + + return %munge_set; +} + +sub walk_and_store { + my ($device, $snmp, %oidmap) = @_; + + my %walk = walker($device, $snmp, '.1.3.6.1'); # 10205 rows + # my %walk = walker($device, $snmp, '.1.3.6.1.2.1.2.2.1.6'); # 22 rows, i_mac/ifPhysAddress + + # take the snmpwalk of the device which is numeric (no MIB translateObj), + # resolve to MIB identifiers using netdisco-mibs, then store in SNMP::Info + # instance cache + + my (%tables, %leaves, @realoids) = ((), (), ()); + OID: foreach my $orig_oid (keys %walk) { + my $oid = $orig_oid; + my $idx = ''; + + while (length($oid) and !exists $oidmap{$oid}) { + $oid =~ s/\.(\d+)$//; + $idx = ($idx ? "${1}.${idx}" : $1); + } + + if (exists $oidmap{$oid}) { + $idx =~ s/^\.//; + my $leaf = $oidmap{$oid}; + + if ($idx eq 0) { + push @realoids, $oid; + $leaves{ $leaf } = $walk{$orig_oid}; + } + else { + push @realoids, $oid if !exists $tables{ $leaf }; + $tables{ $leaf }->{$idx} = $walk{$orig_oid}; + } + + # debug "snapshot $device - cached $oidmap{$oid}($idx)"; + next OID; + } + + debug "snapshot $device - missing OID $orig_oid in netdisco-mibs"; + } + + $snmp->_cache($_, $leaves{$_}) for keys %leaves; + $snmp->_cache($_, $tables{$_}) for keys %tables; + + # add in any GLOBALS and FUNCS aliases which users have created in the + # SNMP::Info device class, with binary copy of data so that it can be frozen + + my %cache = %{ $snmp->cache() }; + my %funcs = %{ $snmp->funcs() }; + my %globals = %{ $snmp->globals() }; + + while (my ($alias, $leaf) = each %globals) { + if (exists $cache{"_$leaf"} and !exists $cache{"_$alias"}) { + $snmp->_cache($alias, $cache{"_$leaf"}); + } + } + + while (my ($alias, $leaf) = each %funcs) { + if (exists $cache{store}->{$leaf} and !exists $cache{store}->{$alias}) { + $snmp->_cache($alias, dclone $cache{store}->{$leaf}); + } + } + + # now for any other SNMP::Info method in GLOBALS or FUNCS which Netdisco + # might call, but will not have data, we fake a cache entry to avoid + # throwing errors + + # refresh the cache + %cache = %{ $snmp->cache() }; + + while (my $method = ) { + $method =~ s/\s//g; + next unless length $method and !exists $cache{"_$method"}; + + $snmp->_cache($method, {}) if exists $funcs{$method}; + $snmp->_cache($method, '') if exists $globals{$method}; + } + + # put into the cache an oid ref to each leaf name + # this allows rebuild of browser data from a frozen cache + foreach my $oid (@realoids) { + my $leaf = $oidmap{$oid} or next; + $snmp->_cache($oid, $snmp->$leaf); + } +} + # taken from SNMP::Info and adjusted to work on walks outside a single table sub walker { my ($device, $snmp, $base) = @_; diff --git a/share/views/ajax/device/snmp.tt b/share/views/ajax/device/snmp.tt index c4f374bd..125328d0 100644 --- a/share/views/ajax/device/snmp.tt +++ b/share/views/ajax/device/snmp.tt @@ -115,8 +115,15 @@ }); $('#jstree').on("search.jstree", function (e, data) { if (data.res.length) { - document.getElementById( data.res[0] + '_anchor' ).scrollIntoView(); $('#node').load('[% uri_base | none %]/ajax/content/device/[% device %]/snmpnode/' + data.res[0]); + + $("#jstree").jstree().deselect_all(true); + $('#jstree').jstree('select_node', data.res[0] + '_anchor'); + + var node = $('#jstree').jstree("get_selected", true); + var path = $('#jstree').jstree().get_path(node[0], false, true); + var parent = path[path.length - 2]; + document.getElementById( parent ).scrollIntoView(); } }); $("#searchTreeForm").submit(function(e) {