« blib/lib/App/Netdisco/Worker/Plugin/Discover/CanonicalIP.pm
↑ Summary
blib/lib/App/Netdisco/Worker/Plugin/Discover/Hooks.pm »
| 1 | package App::Netdisco::Worker::Plugin::Discover::Entities; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use App::Netdisco::Worker::Plugin; | ||
| 5 | use aliased 'App::Netdisco::Worker::Status'; | ||
| 6 | |||
| 7 | use App::Netdisco::Transport::SNMP (); | ||
| 8 | use App::Netdisco::Util::Permission 'acl_matches'; | ||
| 9 | use Dancer::Plugin::DBIC 'schema'; | ||
| 10 | use String::Util 'trim'; | ||
| 11 | use App::Netdisco::Util::SNMPText 'decode_snmp_text'; | ||
| 12 | use App::Netdisco::Util::SNMP qw/walk_mark incomplete_walks_for incomplete_walk_message | ||
| 13 | incomplete_walk_level/; | ||
| 14 | |||
| 15 | my $clean = sub { | ||
| 16 | my $device = shift; | ||
| 17 | my $rs = schema('netdisco')->resultset('DeviceModule'); | ||
| 18 | |||
| 19 | $rs->upsert([{ | ||
| 20 | ip => $device->ip, | ||
| 21 | index => 1, | ||
| 22 | parent => 0, | ||
| 23 | name => 'chassis', | ||
| 24 | class => 'chassis', | ||
| 25 | pos => -1, | ||
| 26 | # cleared so a chassis entity from an earlier full walk leaves no stale values | ||
| 27 | type => undef, | ||
| 28 | hw_ver => undef, | ||
| 29 | fw_ver => undef, | ||
| 30 | description => undef, | ||
| 31 | sw_ver => $device->os_ver, | ||
| 32 | serial => $device->serial, | ||
| 33 | model => $device->model, | ||
| 34 | fru => 'false', | ||
| 35 | last_discover => \'LOCALTIMESTAMP', | ||
| 36 | }], { touch => ['last_discover'] }); | ||
| 37 | |||
| 38 | my $gone = $rs->remove_unseen({ ip => $device->ip }, [{ index => 1 }]); | ||
| 39 | debug sprintf ' [%s] modules - removed %d chassis modules', | ||
| 40 | $device->ip, $gone; | ||
| 41 | }; | ||
| 42 | |||
| 43 | # a walk that was cut short must not look like an emptied table, so the stored | ||
| 44 | # rows stand | ||
| 45 | my $keep_stored = sub { | ||
| 46 | my ($device, @incomplete) = @_; | ||
| 47 | my $message = incomplete_walk_message($device, 'device_module', @incomplete); | ||
| 48 | warning $message; | ||
| 49 | my $level = incomplete_walk_level(@incomplete); | ||
| 50 | return Status->$level($message); | ||
| 51 | }; | ||
| 52 | |||
| 53 | register_worker({ phase => 'main', driver => 'snmp' }, sub { | ||
| 54 | my ($job, $workerconf) = @_; | ||
| 55 | |||
| 56 | my $device = $job->device; | ||
| 57 | return unless $device->in_storage; | ||
| 58 | |||
| 59 | if (acl_matches($device, 'skip_modules') or not setting('store_modules')) { | ||
| 60 | schema('netdisco')->txn_do($clean, $device); | ||
| 61 | return Status->info( | ||
| 62 | sprintf ' [%s] modules - store_modules is disabled (added one pseudo for chassis)', | ||
| 63 | $device->ip); | ||
| 64 | } | ||
| 65 | |||
| 66 | my $snmp = App::Netdisco::Transport::SNMP->reader_for($device) | ||
| 67 | or return Status->defer("discover failed: could not SNMP connect to $device"); | ||
| 68 | my $walk_mark = walk_mark($snmp); | ||
| 69 | my $e_index = $snmp->e_index; | ||
| 70 | |||
| 71 | # checked before the undef branch, so a cut-short e_index skips instead of | ||
| 72 | # wiping the modules to a chassis row | ||
| 73 | my @incomplete = incomplete_walks_for($device, $snmp, $walk_mark, 'e_index'); | ||
| 74 | return $keep_stored->($device, @incomplete) if scalar @incomplete; | ||
| 75 | |||
| 76 | if (!defined $e_index) { | ||
| 77 | schema('netdisco')->txn_do($clean, $device); | ||
| 78 | return Status->info( | ||
| 79 | sprintf ' [%s] modules - 0 chassis components (added one pseudo for chassis)', | ||
| 80 | $device->ip); | ||
| 81 | } | ||
| 82 | |||
| 83 | my $e_descr = $snmp->e_descr; | ||
| 84 | my $e_type = $snmp->e_type; | ||
| 85 | my $e_parent = $snmp->e_parent; | ||
| 86 | my $e_name = $snmp->e_name; | ||
| 87 | my $e_class = $snmp->e_class; | ||
| 88 | my $e_pos = $snmp->e_pos; | ||
| 89 | my $e_hwver = $snmp->e_hwver; | ||
| 90 | my $e_fwver = $snmp->e_fwver; | ||
| 91 | my $e_swver = $snmp->e_swver; | ||
| 92 | my $e_model = $snmp->e_model; | ||
| 93 | my $e_serial = $snmp->e_serial; | ||
| 94 | my $e_fru = $snmp->e_fru; | ||
| 95 | |||
| 96 | @incomplete = incomplete_walks_for($device, $snmp, $walk_mark, qw/ | ||
| 97 | e_index e_descr e_type e_parent e_name e_class e_pos e_hwver e_fwver | ||
| 98 | e_swver e_model e_serial e_fru/); | ||
| 99 | return $keep_stored->($device, @incomplete) if scalar @incomplete; | ||
| 100 | |||
| 101 | # build device modules list for DBIC | ||
| 102 | my (@modules, %seen_idx); | ||
| 103 | foreach my $entry (sort keys %$e_index) { | ||
| 104 | next unless defined $e_index->{$entry}; | ||
| 105 | next if $seen_idx{ $e_index->{$entry} }++; | ||
| 106 | |||
| 107 | if ($e_index->{$entry} !~ m/^[0-9]+$/) { | ||
| 108 | debug sprintf ' [%s] modules - index %s is not an integer', | ||
| 109 | $device->ip, $e_index->{$entry}; | ||
| 110 | next; | ||
| 111 | } | ||
| 112 | |||
| 113 | push @modules, { | ||
| 114 | ip => $device->ip, | ||
| 115 | index => $e_index->{$entry}, | ||
| 116 | type => $e_type->{$entry}, | ||
| 117 | parent => $e_parent->{$entry}, | ||
| 118 | name => trim(decode_snmp_text($e_name->{$entry}, $device, "entity $e_index->{$entry} name")), | ||
| 119 | class => $e_class->{$entry}, | ||
| 120 | pos => $e_pos->{$entry}, | ||
| 121 | hw_ver => trim(decode_snmp_text($e_hwver->{$entry}, $device, "entity $e_index->{$entry} hwver")), | ||
| 122 | fw_ver => trim(decode_snmp_text($e_fwver->{$entry}, $device, "entity $e_index->{$entry} fwver")), | ||
| 123 | sw_ver => trim(decode_snmp_text($e_swver->{$entry}, $device, "entity $e_index->{$entry} swver")), | ||
| 124 | model => trim(decode_snmp_text($e_model->{$entry}, $device, "entity $e_index->{$entry} model")), | ||
| 125 | serial => trim(decode_snmp_text($e_serial->{$entry}, $device, "entity $e_index->{$entry} serial")), | ||
| 126 | fru => $e_fru->{$entry}, | ||
| 127 | description => trim(decode_snmp_text($e_descr->{$entry}, $device, "entity $e_index->{$entry} descr")), | ||
| 128 | last_discover => \'LOCALTIMESTAMP', | ||
| 129 | }; | ||
| 130 | } | ||
| 131 | |||
| 132 | foreach my $m (@modules){ | ||
| 133 | if ($m->{parent} and not exists $seen_idx{ $m->{parent} }){ | ||
| 134 | # Some combined devices like Nexus with FEX or ASR with Satellites can return invalid | ||
| 135 | # EntityMIB trees. This workaround relocates entitites with invalid parents to the root | ||
| 136 | # of the tree, so they are at least visible in the Modules tab (see #710) | ||
| 137 | |||
| 138 | debug sprintf ' [%s] Entity %s (%s) has invalid parent %s - attaching as root entity instead', | ||
| 139 | $device->ip, ($m->{index} || '"unknown index"'), ($m->{name} || '"unknown name"'), $m->{parent}; | ||
| 140 | $m->{parent} = undef; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | schema('netdisco')->txn_do(sub { | ||
| 145 | my $rs = schema('netdisco')->resultset('DeviceModule'); | ||
| 146 | $rs->upsert(\@modules, { touch => ['last_discover'] }); | ||
| 147 | my $gone = $rs->remove_unseen({ ip => $device->ip }, | ||
| 148 | [ map {{ index => $_->{index} }} @modules ]); | ||
| 149 | debug sprintf ' [%s] modules - removed %d chassis modules', | ||
| 150 | $device->ip, $gone; | ||
| 151 | |||
| 152 | return Status->info(sprintf ' [%s] modules - added %d new chassis modules', | ||
| 153 | $device->ip, scalar @modules); | ||
| 154 | }); | ||
| 155 | }); | ||
| 156 | |||
| 157 | true; |