| 1 | package App::Netdisco::Worker::Plugin::Discover::Properties; | ||
| 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 qw/acl_matches acl_matches_only/; | ||
| 9 | use App::Netdisco::Util::FastResolver 'hostnames_resolve_async'; | ||
| 10 | use App::Netdisco::Util::Device qw/get_device is_discoverable/; | ||
| 11 | use App::Netdisco::Util::DNS 'hostname_from_ip'; | ||
| 12 | use App::Netdisco::Util::SNMP qw/snmp_comm_reindex snmp_vrf_reindex get_mibdirs_shortnames walk_mark | ||
| 13 | incomplete_walks incomplete_walks_for incomplete_walk_message incomplete_walk_level/; | ||
| 14 | use App::Netdisco::Util::VRF | ||
| 15 | qw/vrf_collection_enabled vrf_instances_for vrf_skip_test netdisco_vrf each_vrf_instance/; | ||
| 16 | use App::Netdisco::Util::L3Domain qw/recompute_l3_domains l3_domain_recompute_needed/; | ||
| 17 | use App::Netdisco::Util::Web 'sort_port'; | ||
| 18 | use App::Netdisco::Util::SNMPText qw/decode_snmp_text decode_text_with/; | ||
| 19 | |||
| 20 | use Dancer::Plugin::DBIC 'schema'; | ||
| 21 | use Scope::Guard 'guard'; | ||
| 22 | use Try::Tiny; | ||
| 23 | use NetAddr::IP::Lite ':lower'; | ||
| 24 | use Storable 'dclone'; | ||
| 25 | use List::MoreUtils (); | ||
| 26 | use JSON::PP (); | ||
| 27 | use Path::Class qw/file dir/; | ||
| 28 | use Encode; | ||
| 29 | |||
| 30 | register_worker({ phase => 'early', driver => 'snmp', | ||
| 31 | title => 'basic device details (and creation)'}, sub { | ||
| 32 | my ($job, $workerconf) = @_; | ||
| 33 | |||
| 34 | my $device = $job->device; | ||
| 35 | my $snmp = App::Netdisco::Transport::SNMP->reader_for($device) | ||
| 36 | or return Status->defer("discover failed: could not SNMP connect to $device"); | ||
| 37 | |||
| 38 | # for field_protection | ||
| 39 | my $orig_device = { $device->get_columns }; | ||
| 40 | |||
| 41 | # VTP Management Domain -- assume only one. | ||
| 42 | my $vtpdomains = $snmp->vtp_d_name; | ||
| 43 | if (defined $vtpdomains and scalar values %$vtpdomains) { | ||
| 44 | $device->set_column( vtp_domain => (values %$vtpdomains)[-1] ); | ||
| 45 | } | ||
| 46 | my $vtpmodes = $snmp->vtp_d_mode; | ||
| 47 | if (defined $vtpmodes and scalar values %$vtpmodes) { | ||
| 48 | $device->set_column( vtp_mode => (values %$vtpmodes)[-1] ); | ||
| 49 | } | ||
| 50 | |||
| 51 | my $now = vars->{'timestamp'}; | ||
| 52 | my $hostname = hostname_from_ip($device->ip); | ||
| 53 | $device->set_column( dns => $hostname ) if $hostname; | ||
| 54 | |||
| 55 | _set_raw_properties($snmp, $device); | ||
| 56 | |||
| 57 | my @text_properties = qw/ | ||
| 58 | description name | ||
| 59 | ps1_type ps2_type | ||
| 60 | fan | ||
| 61 | os os_ver | ||
| 62 | /; | ||
| 63 | |||
| 64 | my %trimmed_text_properties = ( | ||
| 65 | qw( model model | ||
| 66 | serial serial | ||
| 67 | serial1 chassis_id | ||
| 68 | contact contact | ||
| 69 | location location | ||
| 70 | ), | ||
| 71 | ); | ||
| 72 | |||
| 73 | $device->set_column( num_ports => ($snmp->ports || 0) ); | ||
| 74 | $device->set_column( snmp_class => $snmp->class ); | ||
| 75 | $device->set_column( snmp_engineid => unpack('H*', ($snmp->snmpEngineID || '')) ); | ||
| 76 | |||
| 77 | $device->set_column( last_discover => \$now ); | ||
| 78 | |||
| 79 | my $enterprises_mib = qr/(?:\.?1.3.6.1.4.1|enterprises)\.\d+/; | ||
| 80 | my $try_vendor = | ||
| 81 | ($snmp->id and $snmp->id =~ m/^${enterprises_mib}/) ? $snmp->id | ||
| 82 | : ($device->model and $device->model =~ m/^${enterprises_mib}/) ? $device->model | ||
| 83 | : undef; | ||
| 84 | $try_vendor =~ s/^(?:\.?1.3.6.1.4.1|enterprises)// if $try_vendor; | ||
| 85 | |||
| 86 | # fix up unknown vendor (enterprise number -> organization) | ||
| 87 | my $vendor_from_enterprise = 0; | ||
| 88 | if ((not $device->vendor or $device->vendor eq 'unknown' | ||
| 89 | or $device->vendor =~ m/^${enterprises_mib}/) | ||
| 90 | and $try_vendor and $try_vendor =~ m/^\.(\d+)/) { | ||
| 91 | |||
| 92 | my $number = $1; | ||
| 93 | debug sprintf ' searching for Enterprise Number "%s"', $number; | ||
| 94 | my $ent = schema('netdisco')->resultset('Enterprise')->find($number); | ||
| 95 | if ($ent) { | ||
| 96 | debug sprintf ' ... found Enterprise "%s"', $ent->organization; | ||
| 97 | $device->set_column( vendor => $ent->organization ); | ||
| 98 | $vendor_from_enterprise = 1; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | # decoded only now, so snmp_charset ACLs such as vendor: and os: see this | ||
| 103 | # discover's values, and the fix-ups' database values are not re-decoded | ||
| 104 | foreach my $property (@text_properties) { | ||
| 105 | $device->set_column( $property => | ||
| 106 | _device_text_value($snmp, $property, $device) ); | ||
| 107 | } | ||
| 108 | foreach my $property (keys %trimmed_text_properties) { | ||
| 109 | $device->set_column( $trimmed_text_properties{$property} => | ||
| 110 | _device_text_value($snmp, $property, $device, 'trim') ); | ||
| 111 | } | ||
| 112 | $device->set_column( vendor => _device_text_value($snmp, 'vendor', $device) ) | ||
| 113 | unless $vendor_from_enterprise; | ||
| 114 | |||
| 115 | # fix up model using products OID cache | ||
| 116 | if ((not $device->model or $device->model eq 'unknown' | ||
| 117 | or $device->model =~ m/(?:product|enterprise|1\.3\.6\.1)/i) | ||
| 118 | and $try_vendor) { | ||
| 119 | |||
| 120 | my $oid = '.1.3.6.1.4.1' . $try_vendor; | ||
| 121 | debug sprintf ' searching for Product ID "%s"', ('enterprises' . $try_vendor); | ||
| 122 | my $object = schema('netdisco')->resultset('Product')->find($oid); | ||
| 123 | if ($object) { | ||
| 124 | debug sprintf ' ...found Product "%s" (replaced "%s")', | ||
| 125 | $object->leaf, ($device->model || ''); | ||
| 126 | $device->set_column( model => $object->leaf ); | ||
| 127 | } | ||
| 128 | else { | ||
| 129 | debug ' also searching netdisco-mibs...'; | ||
| 130 | my $file = file((setting('mibhome') || dir(($ENV{NETDISCO_HOME} || $ENV{HOME}), 'netdisco-mibs')), | ||
| 131 | 'EXTRAS', 'reports', 'all_leafs_only'); | ||
| 132 | (my $target = quotemeta($try_vendor)) =~ s/^\\\.//; | ||
| 133 | my $leaf = `grep -E '^$target,' '$file' | cut -d, -f2`; # BACKTICKS | ||
| 134 | if ($leaf) { | ||
| 135 | chomp($leaf); | ||
| 136 | debug sprintf ' ...found Product "%s" (replaced "%s")', | ||
| 137 | $leaf, ($device->model || ''); | ||
| 138 | $device->set_column( model => $leaf ); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | # remove superfluous vendor names from model names | ||
| 144 | if ($device->model) { | ||
| 145 | my @extravendors = qw/jnxProduct jnxProductName/; | ||
| 146 | my @vendors = sort {length($b) <=> length($a)} (@extravendors, @{ get_mibdirs_shortnames() }); | ||
| 147 | my $vend_pat = '^(?:' . join('|', @vendors) . ')'; | ||
| 148 | my $vend_re = qr/$vend_pat/; | ||
| 149 | |||
| 150 | (my $model = $device->model) =~ s/$vend_re//i; | ||
| 151 | if ($model and $model ne $device->model) { | ||
| 152 | debug sprintf ' fixing Product Name to be "%s" (was "%s")', | ||
| 153 | $model, $device->model; | ||
| 154 | $device->set_column( model => $model ); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | # protection for failed SNMP gather | ||
| 159 | if (setting('enable_field_protection') and not $device->is_pseudo) { | ||
| 160 | my $category = ($device->in_storage ? 'device' : 'new_device'); | ||
| 161 | my $protect = setting('field_protection')->{$category} || {}; | ||
| 162 | |||
| 163 | my %dirty = $device->get_dirty_columns; | ||
| 164 | my $ip = $device->ip; | ||
| 165 | foreach my $field (keys %$protect) { | ||
| 166 | next if $device->in_storage and !exists $dirty{$field}; # field didn't change | ||
| 167 | next unless acl_matches_only($ip, $protect->{$field}); | ||
| 168 | |||
| 169 | if ($field eq 'snmp_class') { # reject SNMP::Info (it would not be empty) | ||
| 170 | return $job->cancel("discover cancelled: $ip returned unwanted class SNMP::Info") | ||
| 171 | if $dirty{$field} eq 'SNMP::Info'; | ||
| 172 | } | ||
| 173 | else { | ||
| 174 | if (not $device->in_storage) { # new | ||
| 175 | return $job->cancel("discover cancelled: $ip failed to return valid $field") | ||
| 176 | if !defined $dirty{$field} or not length $dirty{$field}; | ||
| 177 | } | ||
| 178 | elsif ($device->in_storage | ||
| 179 | and ($orig_device->{$field} or length $orig_device->{$field})) { # existing | ||
| 180 | return $job->cancel("rediscover cancelled: $ip failed to return valid $field") | ||
| 181 | if !defined $dirty{$field} or ($orig_device->{$field} and not $dirty{$field}); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | # check discover_no again, now that we have SNMP fields, to allow nuanced filtering | ||
| 188 | if (not $device->is_pseudo and not is_discoverable($device)) { | ||
| 189 | return $job->cancel("discover cancelled: not discoverable after retrieving device context"); | ||
| 190 | } | ||
| 191 | |||
| 192 | # for existing device, filter custom_fields | ||
| 193 | if ($device->in_storage) { | ||
| 194 | my $coder = JSON::PP->new->utf8(0)->allow_nonref(1)->allow_unknown(1); | ||
| 195 | |||
| 196 | # get the custom_fields | ||
| 197 | my $fields = $coder->decode(Encode::encode('UTF-8',$device->custom_fields) || '{}'); | ||
| 198 | my %ok_fields = map {$_ => 1} | ||
| 199 | grep {defined} | ||
| 200 | map {$_->{name}} | ||
| 201 | @{ setting('custom_fields')->{device} || [] }; | ||
| 202 | |||
| 203 | # filter custom_fields for current valid fields | ||
| 204 | foreach my $field (keys %ok_fields) { | ||
| 205 | $ok_fields{$field} = exists $fields->{$field} | ||
| 206 | ? decode_text_with($fields->{$field}, []) : undef; | ||
| 207 | } | ||
| 208 | |||
| 209 | # set new custom_fields | ||
| 210 | $device->set_column( custom_fields => $coder->encode(\%ok_fields) ); | ||
| 211 | } | ||
| 212 | |||
| 213 | # support for Hooks | ||
| 214 | vars->{'hook_data'} = { $device->get_columns }; | ||
| 215 | delete vars->{'hook_data'}->{'snmp_comm'}; # for privacy | ||
| 216 | |||
| 217 | # support for new_device Hook | ||
| 218 | vars->{'new_device'} = 1 if not $device->in_storage; | ||
| 219 | |||
| 220 | # support for updated_device Hook - detect meaningful DEVICE-LEVEL changes | ||
| 221 | { | ||
| 222 | my $ignore = (setting('updated_device_hook_ignored_fields') || {})->{'device'} || []; | ||
| 223 | my %ignore_field = map {($_ => 1)} @$ignore; | ||
| 224 | my %dirty = $device->get_dirty_columns; | ||
| 225 | # DBIC's dirty flag latches true on the first set_column() that differs | ||
| 226 | # and is never cleared by a later set_column() call, even one that lands | ||
| 227 | # back on the original value (model/vendor are set multiple times per | ||
| 228 | # job via the enterprise/product lookup fallbacks above) - so also | ||
| 229 | # require the final value to actually differ from what was loaded. | ||
| 230 | # Uses the same $orig_device lookup cache as the field_protection feature. | ||
| 231 | my @changed = sort grep { | ||
| 232 | not $ignore_field{$_} | ||
| 233 | and (defined $orig_device->{$_} ? $orig_device->{$_} : '') ne | ||
| 234 | (defined $dirty{$_} ? $dirty{$_} : '') | ||
| 235 | } keys %dirty; | ||
| 236 | |||
| 237 | if (scalar @changed) { | ||
| 238 | vars->{'device_changed'} = 1; | ||
| 239 | foreach my $field (@changed) { | ||
| 240 | debug sprintf ' [%s] hooks - device field %s changed: "%s" -> "%s"', | ||
| 241 | $device->ip, $field, | ||
| 242 | (defined $orig_device->{$field} ? $orig_device->{$field} : ''), | ||
| 243 | (defined $dirty{$field} ? $dirty{$field} : ''); | ||
| 244 | } | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | schema('netdisco')->txn_do(sub { | ||
| 249 | if ($device->serial and setting('delete_duplicate_serials')) { | ||
| 250 | my $gone = schema('netdisco')->resultset('Device')->search({ | ||
| 251 | ip => { '!=' => $device->ip }, serial => $device->serial, | ||
| 252 | })->delete; | ||
| 253 | debug sprintf ' removed %s devices with the same serial number', ($gone || '0'); | ||
| 254 | } | ||
| 255 | |||
| 256 | my $new = ($device->in_storage ? 'existing' : 'new'); | ||
| 257 | $device->update_or_insert(undef, {for => 'update'}); | ||
| 258 | return Status->done(sprintf 'Successful discover for %s device %s', $new, $device->ip); | ||
| 259 | }); | ||
| 260 | }); | ||
| 261 | |||
| 262 | register_worker({ phase => 'early', driver => 'snmp', | ||
| 263 | title => 'cancel if device canonical IP is known to another device'}, sub { | ||
| 264 | my ($job, $workerconf) = @_; | ||
| 265 | |||
| 266 | my $device = $job->device; | ||
| 267 | return unless $device->in_storage and vars->{'new_device'}; | ||
| 268 | |||
| 269 | my $db_device = get_device($device->ip); | ||
| 270 | if ($device->ip ne $db_device->ip) { | ||
| 271 | return schema('netdisco')->txn_do(sub { | ||
| 272 | $device->delete; | ||
| 273 | return $job->cancel("discover cancelled: $device already known as $db_device"); | ||
| 274 | }); | ||
| 275 | } | ||
| 276 | |||
| 277 | return Status->info(" [$device] device - OK to continue discover (not a duplicate)"); | ||
| 278 | }); | ||
| 279 | |||
| 280 | register_worker({ phase => 'early', driver => 'snmp', | ||
| 281 | title => 'cancel if no valid interfaces found; cache ports'}, sub { | ||
| 282 | my ($job, $workerconf) = @_; | ||
| 283 | |||
| 284 | my $device = $job->device; | ||
| 285 | return unless $device->in_storage; | ||
| 286 | |||
| 287 | my $snmp = App::Netdisco::Transport::SNMP->reader_for($device) | ||
| 288 | or return Status->defer("discover failed: could not SNMP connect to $device"); | ||
| 289 | |||
| 290 | # clear the cached uptime and get a new one | ||
| 291 | my $dev_uptime = ($device->is_pseudo ? $snmp->uptime : $snmp->load_uptime); | ||
| 292 | if (!defined $dev_uptime) { | ||
| 293 | return $job->cancel("discover cancelled: $device cannot report its uptime"); | ||
| 294 | } | ||
| 295 | |||
| 296 | my $pass = Status->info(" [$device] device - OK to continue discover (valid interfaces)"); | ||
| 297 | |||
| 298 | # cache the device ports to save hitting the database for many single rows | ||
| 299 | vars->{'device_ports'} = { map {($_->port => $_)} | ||
| 300 | $device->ports(undef, {prefetch => 'properties'})->reset->all }; | ||
| 301 | my $walk_mark = walk_mark($snmp); | ||
| 302 | my $interfaces = $snmp->interfaces; | ||
| 303 | vars->{'incomplete_walks'}{ $device->ip }{'interfaces'} | ||
| 304 | = [ incomplete_walks($snmp, $walk_mark) ]; | ||
| 305 | |||
| 306 | # OK if no interfaces | ||
| 307 | return $pass if 0 == scalar keys %$interfaces; | ||
| 308 | # OK if any value is not the same as key | ||
| 309 | return $pass if scalar grep {$_ ne $interfaces->{$_}} keys %$interfaces; | ||
| 310 | # OK if any non-digit in values | ||
| 311 | return $pass if scalar grep {$_ !~ m/^[0-9]+$/} values %$interfaces; | ||
| 312 | # OK if no ports | ||
| 313 | return $pass if 0 == scalar keys %{ vars->{'device_ports'} }; | ||
| 314 | # OK if any interface value is a port name | ||
| 315 | foreach my $port (keys %{ vars->{'device_ports'} }) { | ||
| 316 | return $pass if scalar grep {$port eq $_} values %$interfaces; | ||
| 317 | } | ||
| 318 | |||
| 319 | # else cancel | ||
| 320 | return $job->cancel("discover cancelled: $device failed to return valid interfaces"); | ||
| 321 | }); | ||
| 322 | |||
| 323 | register_worker({ phase => 'early', driver => 'snmp', | ||
| 324 | title => 'get device IP aliases and their subnets'}, sub { | ||
| 325 | my ($job, $workerconf) = @_; | ||
| 326 | |||
| 327 | my $device = $job->device; | ||
| 328 | return unless $device->in_storage; | ||
| 329 | my $snmp = App::Netdisco::Transport::SNMP->reader_for($device) | ||
| 330 | or return Status->defer("discover failed: could not SNMP connect to $device"); | ||
| 331 | |||
| 332 | my $now = vars->{'timestamp'}; | ||
| 333 | |||
| 334 | my $instances = vrf_instances_for($device, $snmp); | ||
| 335 | return _store_aliases_per_vrf($device, $snmp, $instances, $now) if $instances; | ||
| 336 | |||
| 337 | my $walk_mark = walk_mark($snmp); | ||
| 338 | # one read of the VRF list, so the guard exempts the contexts that were walked | ||
| 339 | my @vrf_list = _get_vrf_list($device, $snmp); | ||
| 340 | my @aliases = _dedupe_aliases(_get_ipv4_aliases($device, $snmp, \@vrf_list), | ||
| 341 | _get_ipv6_aliases($device, $snmp, \@vrf_list)); | ||
| 342 | |||
| 343 | my @incomplete = _outside_vrf_contexts(\@vrf_list, | ||
| 344 | incomplete_walks_for($device, $snmp, $walk_mark, qw/ | ||
| 345 | vrf_name ip_index ip_table ip_netmask ipv6_index ipv6_addr ipv6_type | ||
| 346 | ipv6_addr_prefixlength interfaces/)); | ||
| 347 | return _keep_stored_device_ips($device, @incomplete) if scalar @incomplete; | ||
| 348 | |||
| 349 | my @subnets = List::MoreUtils::uniq grep {defined} map {$_->{subnet}} @aliases; | ||
| 350 | |||
| 351 | debug sprintf ' resolving %d aliases with max %d outstanding requests', | ||
| 352 | scalar @aliases, $ENV{'PERL_ANYEVENT_MAX_OUTSTANDING_DNS'}; | ||
| 353 | my $resolved_aliases = hostnames_resolve_async(\@aliases); | ||
| 354 | |||
| 355 | # fake one aliases entry for devices not providing ip_index | ||
| 356 | # or if we're discovering on an IP not listed in ip_index | ||
| 357 | my $device_addr = _canonical_addr($device->ip); | ||
| 358 | push @$resolved_aliases, { alias => $device_addr, dns => $device->dns } | ||
| 359 | if 0 == scalar grep {$_->{alias} eq $device_addr} @aliases; | ||
| 360 | |||
| 361 | # support for Hooks | ||
| 362 | vars->{'hook_data'}->{'device_ips'} = $resolved_aliases; | ||
| 363 | |||
| 364 | my @rows = map {{ | ||
| 365 | ip => $device->ip, | ||
| 366 | alias => $_->{alias}, | ||
| 367 | vrf => '', | ||
| 368 | subnet => $_->{subnet}, | ||
| 369 | port => $_->{port}, | ||
| 370 | dns => $_->{dns}, | ||
| 371 | creation => \'LOCALTIMESTAMP', | ||
| 372 | }} @$resolved_aliases; | ||
| 373 | |||
| 374 | my $vrfs_gone = 0; | ||
| 375 | my $status = schema('netdisco')->txn_do(sub { | ||
| 376 | my $rs = schema('netdisco')->resultset('DeviceIp'); | ||
| 377 | $rs->upsert(\@rows); | ||
| 378 | my $gone = $rs->remove_unseen({ ip => $device->ip }, | ||
| 379 | [ map {{ alias => $_->{alias}, vrf => '' }} @rows ]); | ||
| 380 | debug sprintf ' [%s] device - removed %d aliases', $device->ip, $gone; | ||
| 381 | $vrfs_gone = _remove_vrfs_when_not_collected($device); | ||
| 382 | |||
| 383 | schema('netdisco')->resultset('Subnet')->upsert_subnet($_, $now, '', $device->ip) for @subnets; | ||
| 384 | |||
| 385 | return Status->info(sprintf ' [%s] aliases - added %d new aliases and %d subnets', | ||
| 386 | $device->ip, scalar @aliases, scalar @subnets); | ||
| 387 | }); | ||
| 388 | |||
| 389 | # removed VRFs are changed evidence, which the recompute gate must be told | ||
| 390 | _recompute_l3_domains_after($device, $vrfs_gone) if $vrfs_gone; | ||
| 391 | return $status; | ||
| 392 | }); | ||
| 393 | |||
| 394 | # A device no longer VRF-collected (store_vrfs off, skip_vrf_devices, pseudo) | ||
| 395 | # has just lost its named-VRF addresses, so its VRFs go too, and the recompute | ||
| 396 | # then drops its inferred domain members. Manual members are kept. A device | ||
| 397 | # still collected but whose class lists no VRFs keeps the rows a CLI | ||
| 398 | # collector recorded. | ||
| 399 | sub _remove_vrfs_when_not_collected { | ||
| 400 | my $device = shift; | ||
| 401 | return 0 if vrf_collection_enabled($device); | ||
| 402 | my $gone = schema('netdisco')->resultset('DeviceVrf')->remove_unseen({ ip => $device->ip }, []); | ||
| 403 | debug sprintf ' [%s] device - removed %d VRFs, as VRFs are not collected for it', $device->ip, $gone | ||
| 404 | if $gone; | ||
| 405 | return $gone; | ||
| 406 | } | ||
| 407 | |||
| 408 | sub _canonical_addr { | ||
| 409 | my $ip = NetAddr::IP::Lite->new($_[0]); | ||
| 410 | return $ip ? $ip->addr : $_[0]; | ||
| 411 | } | ||
| 412 | |||
| 413 | # the first entry for an address wins, so the sorted iteration of the callers | ||
| 414 | # decides which port and subnet are kept | ||
| 415 | sub _dedupe_aliases { | ||
| 416 | my %seen; | ||
| 417 | return grep { not $seen{ _canonical_addr($_->{alias}) }++ } @_; | ||
| 418 | } | ||
| 419 | |||
| 420 | # store_vrfs path: each instance SNMP::Info lists is read in its own context and | ||
| 421 | # stored under its own VRF, and one that cannot be read whole keeps its rows | ||
| 422 | sub _store_aliases_per_vrf { | ||
| 423 | my ($device, $snmp, $instances, $now) = @_; | ||
| 424 | # read before any switch, as update() clears SNMP::Info's cache | ||
| 425 | my $list_complete = $snmp->vrf_instances_complete; | ||
| 426 | my $default_interfaces = $snmp->interfaces || {}; | ||
| 427 | |||
| 428 | my @reads = _read_vrf_instances($device, $snmp, $instances, $default_interfaces); | ||
| 429 | my @aliases = map { @{ $_->{aliases} } } grep { $_->{complete} } @reads; | ||
| 430 | my @subnets = map { @{ $_->{subnets} } } grep { $_->{complete} } @reads; | ||
| 431 | my %kept = map { ($_->{vrf} => 1) } grep { not $_->{complete} } @reads; | ||
| 432 | my %stored_vrfs = map { ($_ => 1) } | ||
| 433 | _unlisted_stored_vrfs($device, $instances, $list_complete); | ||
| 434 | $kept{$_} = 1 for keys %stored_vrfs; | ||
| 435 | |||
| 436 | my @kept_rows = _stored_device_ips($device, \%kept); | ||
| 437 | |||
| 438 | my @rows = _vrf_device_ip_rows($device, \@aliases, \@kept_rows); | ||
| 439 | my @seen = ((map {{ alias => $_->{alias}, vrf => $_->{vrf} }} @rows), @kept_rows); | ||
| 440 | my @vrf_rows = map { _device_vrf_row($device, $_, $now) } @reads; | ||
| 441 | my @vrf_seen = ((map {{ vrf => $_->{vrf} }} @vrf_rows), | ||
| 442 | map {{ vrf => $_ }} sort keys %stored_vrfs, _cli_listed_vrfs($device)); | ||
| 443 | |||
| 444 | my $evidence_changed = 0; | ||
| 445 | my $status = schema('netdisco')->txn_do(sub { | ||
| 446 | # read before the upsert, in the transaction that replaces the rows | ||
| 447 | my %before = _stored_vrf_evidence($device); | ||
| 448 | $evidence_changed = _evidence_changed(\%before, \@vrf_rows, \@vrf_seen); | ||
| 449 | my $rs = schema('netdisco')->resultset('DeviceIp'); | ||
| 450 | $rs->upsert(\@rows); | ||
| 451 | my $gone = $rs->remove_unseen({ ip => $device->ip }, \@seen); | ||
| 452 | debug sprintf ' [%s] device - removed %d aliases', $device->ip, $gone; | ||
| 453 | |||
| 454 | schema('netdisco')->resultset('Subnet') | ||
| 455 | ->upsert_subnet($_->{net}, $now, $_->{vrf}, $device->ip) for @subnets; | ||
| 456 | |||
| 457 | my $vrfs = schema('netdisco')->resultset('DeviceVrf'); | ||
| 458 | $vrfs->upsert(\@vrf_rows); | ||
| 459 | $vrfs->remove_unseen({ ip => $device->ip }, \@vrf_seen); | ||
| 460 | |||
| 461 | return Status->info(sprintf ' [%s] aliases - added %d new aliases and %d subnets in %d VRFs', | ||
| 462 | $device->ip, scalar @aliases, scalar @subnets, scalar @reads); | ||
| 463 | }); | ||
| 464 | |||
| 465 | _recompute_l3_domains_after($device, $evidence_changed); | ||
| 466 | return $status; | ||
| 467 | } | ||
| 468 | |||
| 469 | # VRFs a CLI collector recorded (record_cli_vrfs) are that collector's to retire | ||
| 470 | sub _cli_listed_vrfs { | ||
| 471 | my $device = shift; | ||
| 472 | my %hri = (result_class => 'DBIx::Class::ResultClass::HashRefInflator'); | ||
| 473 | return map { $_->{vrf} } grep { ($_->{listed_by} // '') eq 'cli' } | ||
| 474 | $device->vrfs(undef, { columns => [qw/vrf listed_by/], %hri })->all; | ||
| 475 | } | ||
| 476 | |||
| 477 | sub _stored_vrf_evidence { | ||
| 478 | my $device = shift; | ||
| 479 | return _named_vrf_evidence(schema('netdisco')->resultset('DeviceVrf') | ||
| 480 | ->search({ ip => $device->ip, vrf => { '!=' => '' } }, | ||
| 481 | { columns => [qw/vrf rt_import rt_export l3_isid/], | ||
| 482 | result_class => 'DBIx::Class::ResultClass::HashRefInflator' })->all); | ||
| 483 | } | ||
| 484 | |||
| 485 | # Route-target sets are cleaned as the SQL key does (no NULL or empty, no | ||
| 486 | # repeats, byte order), so a set that merely came back in another order is the | ||
| 487 | # same evidence. | ||
| 488 | sub _evidence_tuple { | ||
| 489 | my $row = shift; | ||
| 490 | my $clean = sub { my %seen; join ',', sort grep { defined and length and not $seen{$_}++ } @{ $_[0] || [] } }; | ||
| 491 | return join "\0", $clean->($row->{rt_import}), $clean->($row->{rt_export}), $row->{l3_isid} // ''; | ||
| 492 | } | ||
| 493 | |||
| 494 | sub _named_vrf_evidence { | ||
| 495 | return map { ($_->{vrf} => _evidence_tuple($_)) } grep { length($_->{vrf} // '') } @_; | ||
| 496 | } | ||
| 497 | |||
| 498 | # the named VRFs the device will hold once the refresh commits: the stored ones | ||
| 499 | # the refresh keeps, with the rows it writes laid over them | ||
| 500 | sub _evidence_changed { | ||
| 501 | my ($before, $written, $seen) = @_; | ||
| 502 | my %keep = map { ($_->{vrf} => 1) } @$seen; | ||
| 503 | my %after = (map { ($_ => $before->{$_}) } grep { $keep{$_} } keys %$before); | ||
| 504 | %after = (%after, _named_vrf_evidence(@$written)); | ||
| 505 | return 0 if join("\n", map { "$_\t$before->{$_}" } sort keys %$before) | ||
| 506 | eq join("\n", map { "$_\t$after{$_}" } sort keys %after); | ||
| 507 | return 1; | ||
| 508 | } | ||
| 509 | |||
| 510 | # outside the discover transaction: the recompute is network-wide and takes its | ||
| 511 | # own lock; the rows are committed, so a failure waits for the next discover | ||
| 512 | sub _recompute_l3_domains_after { | ||
| 513 | my ($device, $evidence_changed) = @_; | ||
| 514 | try { | ||
| 515 | if (l3_domain_recompute_needed(schema('netdisco'), $evidence_changed)) { recompute_l3_domains(schema('netdisco')) } | ||
| 516 | else { debug sprintf ' [%s] device - L3 domain recompute skipped, no VRF evidence changed', $device->ip } | ||
| 517 | } | ||
| 518 | catch { warning sprintf ' [%s] device - L3 domain recompute failed, the next discover that finds a change retries: %s', | ||
| 519 | $device->ip, $_ }; | ||
| 520 | } | ||
| 521 | |||
| 522 | # the device's own address gets a default-table row only when no VRF holds it, | ||
| 523 | # as today's path fakes one when ip_index does not list it | ||
| 524 | sub _vrf_device_ip_rows { | ||
| 525 | my ($device, $aliases, $kept_rows) = @_; | ||
| 526 | my $resolved_aliases = hostnames_resolve_async([ @$aliases ]); | ||
| 527 | my $device_addr = _canonical_addr($device->ip); | ||
| 528 | push @$resolved_aliases, { alias => $device_addr, vrf => '', dns => $device->dns } | ||
| 529 | if 0 == scalar grep {_canonical_addr($_->{alias}) eq $device_addr} @$aliases, @$kept_rows; | ||
| 530 | vars->{'hook_data'}->{'device_ips'} = $resolved_aliases; | ||
| 531 | |||
| 532 | return map {{ | ||
| 533 | ip => $device->ip, | ||
| 534 | alias => $_->{alias}, | ||
| 535 | vrf => $_->{vrf}, | ||
| 536 | subnet => $_->{subnet}, | ||
| 537 | port => $_->{port}, | ||
| 538 | dns => $_->{dns}, | ||
| 539 | creation => \'LOCALTIMESTAMP', | ||
| 540 | }} @$resolved_aliases; | ||
| 541 | } | ||
| 542 | |||
| 543 | # the instance walk is shared with arpnip; in default-first order, as the | ||
| 544 | # device_ip and subnet rows were built before it was shared | ||
| 545 | sub _read_vrf_instances { | ||
| 546 | my ($device, $snmp, $instances, $default_interfaces) = @_; | ||
| 547 | my $reads = each_vrf_instance($device, $snmp, $instances, | ||
| 548 | _vrf_alias_reader($snmp, $default_interfaces), walk_methods => ['interfaces']); | ||
| 549 | return map { | ||
| 550 | _vrf_instance_read($device, $instances->{$_}, $reads->{ netdisco_vrf($_) }) | ||
| 551 | } ('default', sort grep { $_ ne 'default' } keys %$instances); | ||
| 552 | } | ||
| 553 | |||
| 554 | # a context's own interfaces win, as in _get_ipv4_aliases' merge; the walk | ||
| 555 | # check includes the interfaces walk, as a short one alone would null every port | ||
| 556 | sub _vrf_alias_reader { | ||
| 557 | my ($snmp, $default_interfaces) = @_; | ||
| 558 | return sub { | ||
| 559 | my ($name, $instance, $switched) = @_; | ||
| 560 | my $interfaces = $switched | ||
| 561 | ? { %$default_interfaces, %{ $snmp->interfaces || {} } } : $default_interfaces; | ||
| 562 | return { interfaces => $interfaces, | ||
| 563 | addresses => ($snmp->vrf_addresses($name) || {}), | ||
| 564 | subnets => ($snmp->vrf_subnets($name) || {}) }; | ||
| 565 | }; | ||
| 566 | } | ||
| 567 | |||
| 568 | sub _vrf_instance_read { | ||
| 569 | my ($device, $instance, $read) = @_; | ||
| 570 | my $vrf = netdisco_vrf($instance->{name}); | ||
| 571 | my $data = $read->{data} | ||
| 572 | or return { instance => $instance, vrf => $vrf, complete => 0 }; | ||
| 573 | my ($addresses, $subnets) = @{$data}{qw/addresses subnets/}; | ||
| 574 | |||
| 575 | my @aliases = _dedupe_aliases(map { | ||
| 576 | _vrf_alias($device, $addresses->{$_}, $data->{interfaces}, $vrf) | ||
| 577 | } sort keys %$addresses); | ||
| 578 | my @nets = List::MoreUtils::uniq grep {defined} map { | ||
| 579 | _vrf_subnet($subnets->{$_}) | ||
| 580 | } sort keys %$subnets; | ||
| 581 | |||
| 582 | return { instance => $instance, vrf => $vrf, complete => $read->{complete}, | ||
| 583 | aliases => \@aliases, subnets => [ map {{ net => $_, vrf => $vrf }} @nets ] }; | ||
| 584 | } | ||
| 585 | |||
| 586 | # the same addresses are left out as by _get_ipv4_aliases and _get_ipv6_aliases | ||
| 587 | sub _vrf_address_ignored { | ||
| 588 | my ($ip, $is_v6) = @_; | ||
| 589 | return 1 if $ip->addr eq '0.0.0.0' or $ip->addr eq '0:0:0:0:0:0:0:0'; | ||
| 590 | return 1 if acl_matches($ip, 'group:__LOOPBACK_ADDRESSES__'); | ||
| 591 | return 1 if not $is_v6 and setting('ignore_private_nets') and $ip->is_rfc1918; | ||
| 592 | return 1 if $is_v6 and acl_matches($ip, 'group:__LOCAL_ADDRESSES__'); | ||
| 593 | return 0; | ||
| 594 | } | ||
| 595 | |||
| 596 | sub _vrf_alias { | ||
| 597 | my ($device, $entry, $interfaces, $vrf) = @_; | ||
| 598 | my $ip = NetAddr::IP::Lite->new($entry->{ip} // '') or return; | ||
| 599 | return if _vrf_address_ignored($ip, ($entry->{ip} =~ m/:/)); | ||
| 600 | my $addr = $ip->addr; | ||
| 601 | my $subnet = $entry->{prefix_len} | ||
| 602 | ? NetAddr::IP::Lite->new($addr .'/'. $entry->{prefix_len})->network->cidr | ||
| 603 | : undef; | ||
| 604 | |||
| 605 | debug sprintf ' [%s] device - aliased as %s in VRF "%s"', $device->ip, $addr, $vrf; | ||
| 606 | return { alias => $addr, vrf => $vrf, subnet => $subnet, dns => undef, | ||
| 607 | port => (defined $entry->{ifindex} ? $interfaces->{ $entry->{ifindex} } : undef) }; | ||
| 608 | } | ||
| 609 | |||
| 610 | sub _vrf_subnet { | ||
| 611 | my $entry = shift; | ||
| 612 | return unless defined $entry->{prefix} and defined $entry->{len}; | ||
| 613 | my $net = NetAddr::IP::Lite->new($entry->{prefix} .'/'. $entry->{len}) or return; | ||
| 614 | return if _vrf_address_ignored($net, ($entry->{prefix} =~ m/:/)); | ||
| 615 | return $net->network->cidr; | ||
| 616 | } | ||
| 617 | |||
| 618 | # a short instance list may leave out a VRF the device still has, so its | ||
| 619 | # stored rows stand; a skipped VRF is retired all the same | ||
| 620 | sub _unlisted_stored_vrfs { | ||
| 621 | my ($device, $instances, $list_complete) = @_; | ||
| 622 | return () if $list_complete; | ||
| 623 | my %listed = map { (netdisco_vrf($_) => 1) } keys %$instances; | ||
| 624 | my %hri = (result_class => 'DBIx::Class::ResultClass::HashRefInflator'); | ||
| 625 | my @stored = ((map { $_->{vrf} } $device->vrfs(undef, { columns => ['vrf'], %hri })->all), | ||
| 626 | (map { $_->{vrf} } $device->device_ips(undef, { columns => ['vrf'], distinct => 1, %hri })->all)); | ||
| 627 | my $skipped = vrf_skip_test($device); | ||
| 628 | return List::MoreUtils::uniq grep { not $listed{$_} and not $skipped->($_) } @stored; | ||
| 629 | } | ||
| 630 | |||
| 631 | sub _stored_device_ips { | ||
| 632 | my ($device, $kept) = @_; | ||
| 633 | return () unless scalar keys %$kept; | ||
| 634 | return map {{ alias => $_->{alias}, vrf => $_->{vrf} }} grep { $kept->{ $_->{vrf} } } | ||
| 635 | $device->device_ips(undef, { columns => [qw/alias vrf/], | ||
| 636 | result_class => 'DBIx::Class::ResultClass::HashRefInflator' })->all; | ||
| 637 | } | ||
| 638 | |||
| 639 | my %unset_rd = map { ($_ => 1) } ('', 'None', '0:0', '<not set>'); | ||
| 640 | |||
| 641 | sub _device_vrf_row { | ||
| 642 | my ($device, $read, $now) = @_; | ||
| 643 | my $instance = $read->{instance}; | ||
| 644 | my $rd = $instance->{rd}; | ||
| 645 | return { | ||
| 646 | ip => $device->ip, | ||
| 647 | vrf => $read->{vrf}, | ||
| 648 | kind => $instance->{kind}, | ||
| 649 | rd => ((defined $rd and not $unset_rd{$rd}) ? $rd : undef), | ||
| 650 | rt_import => [ @{ $instance->{rt_import} || [] } ], | ||
| 651 | rt_export => [ @{ $instance->{rt_export} || [] } ], | ||
| 652 | l3_isid => $instance->{l3_isid}, | ||
| 653 | listed_by => $instance->{listed_by}, | ||
| 654 | complete => $read->{complete}, | ||
| 655 | last_discover => \$now, | ||
| 656 | }; | ||
| 657 | } | ||
| 658 | |||
| 659 | # VRF collection is not reliable yet, so a failed VRF walk drops that VRF's | ||
| 660 | # addresses for one discover, as before, rather than freezing the device's rows. | ||
| 661 | # An absent walk means the poller's MIBs are broken wherever it ran, and | ||
| 662 | # walk_status holds only the last VRF's outcome, so absent always counts. | ||
| 663 | sub _outside_vrf_contexts { | ||
| 664 | my ($vrf_list, @outcomes) = @_; | ||
| 665 | # a VRF name that decodes to no context was walked in the base context | ||
| 666 | my %vrf_context = map {($_ => 1)} grep {length} @$vrf_list; | ||
| 667 | return grep { ($_->{reason} // '') eq 'absent' | ||
| 668 | or (($_->{method} // '') ne 'vrf_name' | ||
| 669 | and not $vrf_context{ $_->{context} // '' }) } @outcomes; | ||
| 670 | } | ||
| 671 | |||
| 672 | # a walk that was cut short must not look like an emptied table, so the stored | ||
| 673 | # rows stand and the hook payload shows them rather than the partial walk | ||
| 674 | sub _keep_stored_device_ips { | ||
| 675 | my ($device, @incomplete) = @_; | ||
| 676 | vars->{'hook_data'}->{'device_ips'} = [ $device->device_ips(undef, { | ||
| 677 | columns => [qw/alias subnet port dns/], order_by => 'alias', | ||
| 678 | result_class => 'DBIx::Class::ResultClass::HashRefInflator', | ||
| 679 | })->all ]; | ||
| 680 | my $message = incomplete_walk_message($device, 'device_ip', @incomplete); | ||
| 681 | warning $message; | ||
| 682 | my $level = incomplete_walk_level(@incomplete); | ||
| 683 | return Status->$level($message); | ||
| 684 | } | ||
| 685 | |||
| 686 | |||
| 687 | # the walks that build device_port rows, plus the job-wide interfaces record | ||
| 688 | my @PORT_WALKS = qw/ | ||
| 689 | interfaces i_type i_ignore i_description i_mtu i_speed i_speed_admin i_mac | ||
| 690 | i_up i_up_admin i_name i_duplex i_duplex_admin i_stp_state i_vlan | ||
| 691 | i_lastchange agg_ports i_subinterfaces | ||
| 692 | /; | ||
| 693 | |||
| 694 | my @PORT_COLUMNS = qw/ | ||
| 695 | port descr up up_admin mac speed speed_admin mtu name duplex duplex_admin | ||
| 696 | stp type vlan pvid has_subinterfaces is_master slave_of lastchange | ||
| 697 | /; | ||
| 698 | |||
| 699 | # NOTE must come after the IP Aliases gathering for ignore ACLs to work | ||
| 700 | register_worker({ phase => 'early', driver => 'snmp', | ||
| 701 | title => 'get port details'}, sub { | ||
| 702 | my ($job, $workerconf) = @_; | ||
| 703 | |||
| 704 | my $device = $job->device; | ||
| 705 | return unless $device->in_storage; | ||
| 706 | my $snmp = App::Netdisco::Transport::SNMP->reader_for($device) | ||
| 707 | or return Status->defer("discover failed: could not SNMP connect to $device"); | ||
| 708 | |||
| 709 | my $walk_mark = walk_mark($snmp); | ||
| 710 | my $interfaces = $snmp->interfaces; | ||
| 711 | my $i_type = $snmp->i_type; | ||
| 712 | my $i_ignore = $snmp->i_ignore; | ||
| 713 | my $i_descr = $snmp->i_description; | ||
| 714 | my $i_mtu = $snmp->i_mtu; | ||
| 715 | my $i_speed = $snmp->i_speed; | ||
| 716 | my $i_speed_admin = $snmp->i_speed_admin; | ||
| 717 | my $i_mac = $snmp->i_mac; | ||
| 718 | my $i_up = $snmp->i_up; | ||
| 719 | my $i_up_admin = $snmp->i_up_admin; | ||
| 720 | my $i_name = $snmp->i_name; | ||
| 721 | my $i_duplex = $snmp->i_duplex; | ||
| 722 | my $i_duplex_admin = $snmp->i_duplex_admin; | ||
| 723 | my $i_stp_state = $snmp->i_stp_state; | ||
| 724 | my $i_vlan = $snmp->i_vlan; | ||
| 725 | my $i_lastchange = $snmp->i_lastchange; | ||
| 726 | my $agg_ports = $snmp->agg_ports; | ||
| 727 | my $i_subs = $snmp->i_subinterfaces; | ||
| 728 | |||
| 729 | # clear the cached uptime and get a new one | ||
| 730 | my $dev_uptime = ($device->is_pseudo ? $snmp->uptime : $snmp->load_uptime); | ||
| 731 | if (!defined $dev_uptime) { | ||
| 732 | return Status->info(sprintf ' [%s] interfaces - skipped, no uptime from device', | ||
| 733 | $device->ip); | ||
| 734 | } | ||
| 735 | |||
| 736 | # used to track how many times the device uptime wrapped | ||
| 737 | my $dev_uptime_wrapped = 0; | ||
| 738 | |||
| 739 | # use SNMP-FRAMEWORK-MIB::snmpEngineTime if available to | ||
| 740 | # fix device uptime if wrapped | ||
| 741 | if (defined $snmp->snmpEngineTime) { | ||
| 742 | $dev_uptime_wrapped = int( $snmp->snmpEngineTime * 100 / 2**32 ); | ||
| 743 | if ($dev_uptime_wrapped > 0) { | ||
| 744 | debug sprintf ' [%s] interfaces - device uptime wrapped %d times - correcting', | ||
| 745 | $device->ip, $dev_uptime_wrapped; | ||
| 746 | $device->uptime( $dev_uptime + $dev_uptime_wrapped * 2**32 ); | ||
| 747 | } | ||
| 748 | } | ||
| 749 | |||
| 750 | # build device interfaces suitable for DBIC | ||
| 751 | my %deviceports; | ||
| 752 | PORT: foreach my $entry (keys %$interfaces) { | ||
| 753 | my $port = $interfaces->{$entry}; | ||
| 754 | |||
| 755 | if (not $port) { | ||
| 756 | debug sprintf ' [%s] interfaces - ignoring %s (no port mapping)', | ||
| 757 | $device->ip, $entry; | ||
| 758 | next PORT; | ||
| 759 | } | ||
| 760 | |||
| 761 | if (exists $i_ignore->{$entry}) { | ||
| 762 | debug sprintf ' [%s] interfaces - ignoring %s (%s) (%s) (SNMP::Info::i_ignore)', | ||
| 763 | $device->ip, $entry, $port, ($i_type->{$entry} || ''); | ||
| 764 | next PORT; | ||
| 765 | } | ||
| 766 | |||
| 767 | # create a DBIx::Class row for this port which can be used to test ACLs | ||
| 768 | # also include the Device IP alias if we have one for L3 interfaces | ||
| 769 | $deviceports{$port} = { | ||
| 770 | port => $port, | ||
| 771 | descr => decode_snmp_text($i_descr->{$entry}, $device, "port $port descr"), | ||
| 772 | up => $i_up->{$entry}, | ||
| 773 | up_admin => $i_up_admin->{$entry}, | ||
| 774 | mac => $i_mac->{$entry}, | ||
| 775 | speed => $i_speed->{$entry}, | ||
| 776 | speed_admin => $i_speed_admin->{$entry}, | ||
| 777 | mtu => $i_mtu->{$entry}, | ||
| 778 | name => decode_snmp_text($i_name->{$entry}, $device, "port $port name"), | ||
| 779 | duplex => $i_duplex->{$entry}, | ||
| 780 | duplex_admin => $i_duplex_admin->{$entry}, | ||
| 781 | stp => $i_stp_state->{$entry}, | ||
| 782 | type => $i_type->{$entry}, | ||
| 783 | vlan => $i_vlan->{$entry}, | ||
| 784 | pvid => $i_vlan->{$entry}, | ||
| 785 | has_subinterfaces => 'false', | ||
| 786 | is_master => 'false', | ||
| 787 | slave_of => undef, | ||
| 788 | }; | ||
| 789 | } | ||
| 790 | |||
| 791 | if (scalar @{ setting('ignore_deviceports') }) { | ||
| 792 | my $port_map = {}; | ||
| 793 | |||
| 794 | map { push @{ $port_map->{ $_->port } }, $_ } | ||
| 795 | grep { $_->port } | ||
| 796 | $device->device_ips()->all; | ||
| 797 | |||
| 798 | map { push @{ $port_map->{ $_->{port} } }, $_ } | ||
| 799 | values %{ dclone (\%deviceports || {}) }; | ||
| 800 | |||
| 801 | foreach my $map (@{ setting('ignore_deviceports')}) { | ||
| 802 | next unless ref {} eq ref $map; | ||
| 803 | |||
| 804 | foreach my $key (sort keys %$map) { | ||
| 805 | # lhs matches device, rhs matches port | ||
| 806 | next unless $key and $map->{$key}; | ||
| 807 | next unless acl_matches($device, $key); | ||
| 808 | |||
| 809 | foreach my $port (sort { sort_port( $a, $b) } keys %$port_map) { | ||
| 810 | next unless acl_matches($port_map->{$port}, $map->{$key}); | ||
| 811 | |||
| 812 | debug sprintf ' [%s] interfaces - ignoring %s (config:ignore_deviceports)', | ||
| 813 | $device->ip, $port; | ||
| 814 | delete $deviceports{$port}; | ||
| 815 | } | ||
| 816 | } | ||
| 817 | } | ||
| 818 | } | ||
| 819 | |||
| 820 | # protection for failed/partial SNMP port-table gather - shares config | ||
| 821 | # shape and ACL-scoping with the leaf "protection for failed SNMP | ||
| 822 | # gather" check on the device row (see basic device details worker | ||
| 823 | # above), but a bad table value keeps the prior value and the job | ||
| 824 | # continues rather than aborting: by this point in the job the device | ||
| 825 | # row has already been committed in an earlier, separate transaction, | ||
| 826 | # so cancelling here couldn't undo that anyway, and one bad port table | ||
| 827 | # shouldn't discard everything else (neighbors, modules, VLANs, etc) | ||
| 828 | # that came back fine in the same discover. Checked after | ||
| 829 | # ignore_deviceports filtering so it only judges ports that are | ||
| 830 | # actually about to be stored. | ||
| 831 | if (setting('enable_field_protection') and not $device->is_pseudo) { | ||
| 832 | my $protect = setting('field_protection')->{'device_port'} || {}; | ||
| 833 | |||
| 834 | # some platforms fall back to reporting the interface's own short or | ||
| 835 | # long name as i_name when the real ifAlias walk comes back empty | ||
| 836 | # (e.g. ifAlias "Gi3/0/14" on port GigabitEthernet3/0/14) - a genuine | ||
| 837 | # human-authored description doesn't echo its own port id, so don't | ||
| 838 | # count that as "good" ifAlias data either. Compare trailing | ||
| 839 | # slot/subslot/port digits rather than the letter prefix, since | ||
| 840 | # that's exact and vendor-abbreviation-proof. | ||
| 841 | my $looks_like_own_port_id = sub { | ||
| 842 | my ($name, $port) = @_; | ||
| 843 | return false unless defined $name and defined $port and length $name; | ||
| 844 | my ($nsuffix) = $name =~ m{(\d+(?:/\d+)*)$}; | ||
| 845 | my ($psuffix) = $port =~ m{(\d+(?:/\d+)*)$}; | ||
| 846 | return false unless defined $nsuffix and defined $psuffix; | ||
| 847 | return ($nsuffix eq $psuffix and $name =~ /^[A-Za-z]/) ? true : false; | ||
| 848 | }; | ||
| 849 | |||
| 850 | my %good_test = ( | ||
| 851 | name => sub { my ($val, $port) = @_; | ||
| 852 | defined $val and length $val | ||
| 853 | and not $looks_like_own_port_id->($val, $port) }, | ||
| 854 | up_admin => sub { defined $_[0] and $_[0] eq 'up' }, | ||
| 855 | up => sub { defined $_[0] and $_[0] eq 'up' }, | ||
| 856 | ); | ||
| 857 | my %label = (name => 'i_name/ifAlias', up_admin => 'i_up_admin', up => 'i_up'); | ||
| 858 | |||
| 859 | foreach my $field (keys %$protect) { | ||
| 860 | next unless exists $good_test{$field}; | ||
| 861 | next unless acl_matches_only($device->ip, $protect->{$field}); | ||
| 862 | |||
| 863 | my $good = $good_test{$field}; | ||
| 864 | my $old_good = scalar grep { $good->($_->$field, $_->port) } | ||
| 865 | values %{ vars->{'device_ports'} }; | ||
| 866 | my $new_good = scalar grep { $good->($deviceports{$_}->{$field}, $_) } | ||
| 867 | keys %deviceports; | ||
| 868 | |||
| 869 | if ($old_good and not $new_good) { | ||
| 870 | warning sprintf | ||
| 871 | ' [%s] interfaces - %s walk came back with no ports "up"/set' | ||
| 872 | .' but %d were previously - assuming failed SNMP walk, keeping existing %s', | ||
| 873 | $device->ip, ($label{$field} || $field), $old_good, $field; | ||
| 874 | |||
| 875 | foreach my $port (keys %deviceports) { | ||
| 876 | $deviceports{$port}->{$field} = vars->{'device_ports'}->{$port}->$field | ||
| 877 | if exists vars->{'device_ports'}->{$port} | ||
| 878 | and vars->{'device_ports'}->{$port}->can($field); | ||
| 879 | } | ||
| 880 | } | ||
| 881 | } | ||
| 882 | } | ||
| 883 | |||
| 884 | # 981 must do this after filtering %deviceports to avoid weird data | ||
| 885 | UPTIME: foreach my $entry (sort keys %$interfaces) { | ||
| 886 | my $port = $interfaces->{$entry}; | ||
| 887 | next unless exists $deviceports{$port}; | ||
| 888 | my $lc = $i_lastchange->{$entry} || 0; | ||
| 889 | |||
| 890 | # allow three minutes skew during boot, in case lc is larger than uptime | ||
| 891 | # because of different counters starting at different times | ||
| 892 | if (not $dev_uptime_wrapped and $lc > ($dev_uptime + 18000)) { | ||
| 893 | debug sprintf ' [%s] interfaces - device uptime wrapped (%s) - correcting', | ||
| 894 | $device->ip, $port; | ||
| 895 | $device->uptime( $dev_uptime + 2**32 ); | ||
| 896 | $dev_uptime_wrapped = 1; | ||
| 897 | } | ||
| 898 | |||
| 899 | if ($device->is_column_changed('uptime') and $lc and $lc < $dev_uptime) { | ||
| 900 | # ambiguous: lastchange could be sysUptime before or after wrap | ||
| 901 | if ($dev_uptime > 30000 and $lc < 30000) { | ||
| 902 | # uptime wrap more than 5min ago but lastchange within 5min | ||
| 903 | # assume lastchange was directly after boot -> no action | ||
| 904 | } | ||
| 905 | else { | ||
| 906 | # uptime wrap less than 5min ago or lastchange > 5min ago | ||
| 907 | # to be on safe side, assume lastchange after counter wrap | ||
| 908 | debug sprintf | ||
| 909 | ' [%s] interfaces - correcting LastChange for %s, assuming sysUptime wrap', | ||
| 910 | $device->ip, $port; | ||
| 911 | $lc += $dev_uptime_wrapped * 2**32; | ||
| 912 | } | ||
| 913 | } | ||
| 914 | |||
| 915 | $deviceports{$port}->{lastchange} = $lc; | ||
| 916 | } | ||
| 917 | |||
| 918 | # must do this after building %deviceports so that we can set is_master | ||
| 919 | foreach my $sidx (keys %$agg_ports) { | ||
| 920 | my $slave = $interfaces->{$sidx} or next; | ||
| 921 | next unless defined $agg_ports->{$sidx}; # slave without a master?! | ||
| 922 | my $master = $interfaces->{ $agg_ports->{$sidx} } or next; | ||
| 923 | next unless exists $deviceports{$slave} and exists $deviceports{$master}; | ||
| 924 | |||
| 925 | $deviceports{$slave}->{slave_of} = $master; | ||
| 926 | $deviceports{$master}->{is_master} = 'true'; | ||
| 927 | } | ||
| 928 | |||
| 929 | # also for VLAN subinterfaces | ||
| 930 | foreach my $pidx (keys %$i_subs) { | ||
| 931 | my $parent = $interfaces->{$pidx} or next; | ||
| 932 | # parent without subinterfaces? | ||
| 933 | next unless defined $i_subs->{$pidx} | ||
| 934 | and ref [] eq ref $i_subs->{$pidx} | ||
| 935 | and scalar @{ $i_subs->{$pidx} } | ||
| 936 | and exists $deviceports{$parent}; | ||
| 937 | |||
| 938 | $deviceports{$parent}->{has_subinterfaces} = 'true'; | ||
| 939 | foreach my $sidx (@{ $i_subs->{$pidx} }) { | ||
| 940 | my $sub = $interfaces->{$sidx} or next; | ||
| 941 | next unless exists $deviceports{$sub}; | ||
| 942 | $deviceports{$sub}->{slave_of} = $parent; | ||
| 943 | } | ||
| 944 | } | ||
| 945 | |||
| 946 | my @incomplete = incomplete_walks_for($device, $snmp, $walk_mark, @PORT_WALKS); | ||
| 947 | return _keep_stored_device_ports($device, @incomplete) if scalar @incomplete; | ||
| 948 | |||
| 949 | # update num_ports | ||
| 950 | $device->num_ports( scalar values %deviceports ); | ||
| 951 | |||
| 952 | # support for updated_device Hook - detect meaningful PORT-LEVEL changes | ||
| 953 | unless (vars->{'device_changed'}) { | ||
| 954 | my $ignore = (setting('updated_device_hook_ignored_fields') || {})->{'device_port'} || []; | ||
| 955 | my %ignore_field = map {($_ => 1)} @$ignore; | ||
| 956 | |||
| 957 | # boolean columns: ->hri bypasses DBIC's column handling, so an | ||
| 958 | # existing row's true/false comes back as the driver's raw "1"/"0" | ||
| 959 | # while our freshly built %deviceports uses the literal 'true'/'false' | ||
| 960 | # strings - normalize both before comparing so this isn't a false positive | ||
| 961 | my $normalize_bool = sub { | ||
| 962 | my $val = shift; | ||
| 963 | return (defined $val and grep {$val eq $_} qw/1 t true/) ? 1 : 0; | ||
| 964 | }; | ||
| 965 | |||
| 966 | if (join("\0", sort {$a cmp $b} keys %{ vars->{'device_ports'} }) ne join("\0", sort keys %deviceports)) { | ||
| 967 | vars->{'device_changed'} = 1; | ||
| 968 | debug sprintf ' [%s] hooks - port set changed: had [%s], now [%s]', | ||
| 969 | $device->ip, (join ', ', sort {$a cmp $b} keys %{ vars->{'device_ports'} }), | ||
| 970 | (join ', ', sort {$a cmp $b} keys %deviceports); | ||
| 971 | } | ||
| 972 | else { | ||
| 973 | my $ports_rsrc = schema('netdisco')->resultset('DevicePort')->result_source; | ||
| 974 | PORTDIFF: foreach my $port (sort {$a cmp $b} keys %deviceports) { | ||
| 975 | my $old = vars->{'device_ports'}->{$port}; | ||
| 976 | my $new = $deviceports{$port}; | ||
| 977 | foreach my $field (sort {$a cmp $b} keys %$new) { | ||
| 978 | next if $ignore_field{$field}; | ||
| 979 | my $oldval = (defined $old->$field ? $old->$field : ''); | ||
| 980 | my $newval = (defined $new->{$field} ? $new->{$field} : ''); | ||
| 981 | if ($ports_rsrc->column_info($field)->{'data_type'} =~ m/bool/i) { | ||
| 982 | $oldval = $normalize_bool->($oldval); | ||
| 983 | $newval = $normalize_bool->($newval); | ||
| 984 | } | ||
| 985 | next if $oldval eq $newval; | ||
| 986 | vars->{'device_changed'} = 1; | ||
| 987 | debug sprintf ' [%s] hooks - port %s field %s changed: "%s" -> "%s"', | ||
| 988 | $device->ip, $port, $field, $oldval, $newval; | ||
| 989 | last PORTDIFF; | ||
| 990 | } | ||
| 991 | } | ||
| 992 | } | ||
| 993 | } | ||
| 994 | |||
| 995 | # support for Hooks, after the port comparison, which never saw custom_fields | ||
| 996 | foreach my $port (keys %deviceports) { | ||
| 997 | next unless exists vars->{'device_ports'}->{$port}; | ||
| 998 | $deviceports{$port}->{custom_fields} | ||
| 999 | = _configured_custom_fields(vars->{'device_ports'}->{$port}->custom_fields); | ||
| 1000 | } | ||
| 1001 | vars->{'hook_data'}->{'ports'} = [values %deviceports]; | ||
| 1002 | |||
| 1003 | my @rows = map { _device_port_row($device, $deviceports{$_}) } sort keys %deviceports; | ||
| 1004 | my @vanished = sort grep { not exists $deviceports{$_} } keys %{ vars->{'device_ports'} }; | ||
| 1005 | |||
| 1006 | my $status = schema('netdisco')->txn_do(sub { | ||
| 1007 | # serializes port writes for one device, in place of a table lock | ||
| 1008 | $device->result_source->resultset | ||
| 1009 | ->search({ ip => $device->ip }, { for => 'update' })->first; | ||
| 1010 | |||
| 1011 | my $rs = schema('netdisco')->resultset('DevicePort'); | ||
| 1012 | $rs->upsert(\@rows); | ||
| 1013 | my $gone = $rs->retire_ports($device->ip, \@vanished, 'vanished'); | ||
| 1014 | debug sprintf ' [%s] interfaces - removed %d interfaces', | ||
| 1015 | $device->ip, $gone; | ||
| 1016 | |||
| 1017 | # uptime and num_ports changed | ||
| 1018 | $device->update(); | ||
| 1019 | |||
| 1020 | return Status->info(sprintf ' [%s] interfaces - added %d new interfaces', | ||
| 1021 | $device->ip, scalar values %deviceports); | ||
| 1022 | }); | ||
| 1023 | |||
| 1024 | # per-port writers later in this job only write ports found in the cache | ||
| 1025 | vars->{'device_ports'} = { map {($_->port => $_)} | ||
| 1026 | $device->ports(undef, {prefetch => 'properties'})->reset->all }; | ||
| 1027 | |||
| 1028 | return $status; | ||
| 1029 | }); | ||
| 1030 | |||
| 1031 | # custom_fields, tags, remote_*, is_uplink and manual_topo are left out, so the | ||
| 1032 | # upsert keeps their stored values and an insert takes the column defaults | ||
| 1033 | sub _device_port_row { | ||
| 1034 | my ($device, $port) = @_; | ||
| 1035 | my %row = map {($_ => $port->{$_})} @PORT_COLUMNS; | ||
| 1036 | $row{$_} = ((defined $row{$_} and $row{$_} eq 'true') ? 1 : 0) | ||
| 1037 | for qw/has_subinterfaces is_master/; | ||
| 1038 | return { %row, ip => $device->ip, creation => \'LOCALTIMESTAMP' }; | ||
| 1039 | } | ||
| 1040 | |||
| 1041 | # the stored custom_fields JSON reduced to the configured device_port fields, | ||
| 1042 | # as the hook payload has always carried them; the upsert never writes it | ||
| 1043 | sub _configured_custom_fields { | ||
| 1044 | my $stored = shift; | ||
| 1045 | my $coder = JSON::PP->new->utf8(0)->allow_nonref(1)->allow_unknown(1); | ||
| 1046 | my $fields = $coder->decode(Encode::encode('UTF-8', $stored || '{}')); | ||
| 1047 | |||
| 1048 | my %configured = (); | ||
| 1049 | foreach my $field (grep {defined} map {$_->{name}} | ||
| 1050 | @{ setting('custom_fields')->{device_port} || [] }) { | ||
| 1051 | $configured{$field} = exists $fields->{$field} | ||
| 1052 | ? decode_text_with($fields->{$field}, []) : undef; | ||
| 1053 | } | ||
| 1054 | return $coder->encode(\%configured); | ||
| 1055 | } | ||
| 1056 | |||
| 1057 | # a walk that was cut short must not look like vanished ports, so the stored | ||
| 1058 | # rows stand, the hook payload shows them, and the port comparison is skipped | ||
| 1059 | sub _keep_stored_device_ports { | ||
| 1060 | my ($device, @incomplete) = @_; | ||
| 1061 | my @ports = $device->ports(undef, { | ||
| 1062 | columns => [@PORT_COLUMNS, 'custom_fields'], order_by => 'port', | ||
| 1063 | result_class => 'DBIx::Class::ResultClass::HashRefInflator', | ||
| 1064 | })->all; | ||
| 1065 | |||
| 1066 | # the same shape the walk gives: 'true'/'false' and configured fields only | ||
| 1067 | foreach my $port (@ports) { | ||
| 1068 | foreach my $column (qw/has_subinterfaces is_master/) { | ||
| 1069 | my $value = $port->{$column}; | ||
| 1070 | $port->{$column} = (defined $value and grep {$value eq $_} qw/1 t true/) | ||
| 1071 | ? 'true' : 'false'; | ||
| 1072 | } | ||
| 1073 | $port->{custom_fields} = _configured_custom_fields($port->{custom_fields}); | ||
| 1074 | } | ||
| 1075 | vars->{'hook_data'}->{'ports'} = \@ports; | ||
| 1076 | |||
| 1077 | # uptime may have been corrected above | ||
| 1078 | $device->update(); | ||
| 1079 | |||
| 1080 | my $message = incomplete_walk_message($device, 'device_port', @incomplete); | ||
| 1081 | warning $message; | ||
| 1082 | my $level = incomplete_walk_level(@incomplete); | ||
| 1083 | return Status->$level($message); | ||
| 1084 | } | ||
| 1085 | |||
| 1086 | # description and name are set raw too, for the snmp_charset ACLs on a new | ||
| 1087 | # device; they are decoded afterwards with the other text properties | ||
| 1088 | sub _set_raw_properties { | ||
| 1089 | my ($snmp, $device) = @_; | ||
| 1090 | my @properties = qw/ | ||
| 1091 | snmp_ver | ||
| 1092 | uptime | ||
| 1093 | layers mac | ||
| 1094 | ps1_status ps2_status | ||
| 1095 | slots | ||
| 1096 | vendor os model | ||
| 1097 | description name | ||
| 1098 | /; | ||
| 1099 | |||
| 1100 | foreach my $property (@properties) { | ||
| 1101 | my $val = $snmp->$property; | ||
| 1102 | $val = [values %$val]->[0] if ref $val eq 'HASH'; | ||
| 1103 | $val = undef if $val and $val =~ m/^HASH\(/; | ||
| 1104 | $device->set_column( $property => $val ); | ||
| 1105 | } | ||
| 1106 | } | ||
| 1107 | |||
| 1108 | sub _device_text_value { | ||
| 1109 | my ($snmp, $method, $device, $trim) = @_; | ||
| 1110 | my $val = $snmp->$method; | ||
| 1111 | $val = [values %$val]->[0] if ref $val eq 'HASH'; | ||
| 1112 | return undef if $val and $val =~ m/^HASH\(/; | ||
| 1113 | return decode_snmp_text($val, $device, $method) unless $trim; | ||
| 1114 | |||
| 1115 | (my $text = decode_snmp_text(($val || ''), $device, $method)) =~ s/\s+$//; | ||
| 1116 | return $text; | ||
| 1117 | } | ||
| 1118 | |||
| 1119 | # return a list of VRF which are OK to connect | ||
| 1120 | sub _get_vrf_list { | ||
| 1121 | my ($device, $snmp) = @_; | ||
| 1122 | |||
| 1123 | return () if ! $snmp->cisco_comm_indexing; | ||
| 1124 | |||
| 1125 | my @ok_vrfs = (); | ||
| 1126 | my $vrf_name = $snmp->vrf_name || {}; | ||
| 1127 | |||
| 1128 | # sorted so an address in two VRFs keeps the same port and subnet each run | ||
| 1129 | foreach my $idx (sort keys %$vrf_name) { | ||
| 1130 | my $vrf = $vrf_name->{$idx}; | ||
| 1131 | if ($vrf =~ /^\S+$/) { | ||
| 1132 | my $ctx_name = pack("C*",split(/\./,$idx)); | ||
| 1133 | $ctx_name =~ s/.*[^[:print:]]+//; | ||
| 1134 | debug sprintf(' [%s] Discover VRF %s with SNMP Context %s', $device->ip, $vrf, $ctx_name); | ||
| 1135 | push (@ok_vrfs, $ctx_name); | ||
| 1136 | } | ||
| 1137 | } | ||
| 1138 | |||
| 1139 | return @ok_vrfs; | ||
| 1140 | } | ||
| 1141 | |||
| 1142 | sub _get_ipv4_aliases { | ||
| 1143 | my ($device, $snmp, $vrf_list) = @_; | ||
| 1144 | my @aliases; | ||
| 1145 | |||
| 1146 | my $ip_index = $snmp->ip_index || {}; | ||
| 1147 | my $ip_table = $snmp->ip_table || {}; | ||
| 1148 | my $interfaces = $snmp->interfaces || {}; | ||
| 1149 | my $ip_netmask = $snmp->ip_netmask || {}; | ||
| 1150 | |||
| 1151 | # Get IP Table per VRF if supported | ||
| 1152 | my @vrf_list = @{ $vrf_list || [ _get_vrf_list($device, $snmp) ] }; | ||
| 1153 | if (scalar @vrf_list) { | ||
| 1154 | my $guard = guard { snmp_comm_reindex($snmp, $device, 0) }; | ||
| 1155 | foreach my $vrf (@vrf_list) { | ||
| 1156 | snmp_vrf_reindex($snmp, $device, $vrf); | ||
| 1157 | $ip_index = { %$ip_index, %{$snmp->ip_index || {}} }; | ||
| 1158 | $ip_table = { %$ip_table, %{$snmp->ip_table || {}} }; | ||
| 1159 | $interfaces = { %$interfaces, %{$snmp->interfaces || {}} }; | ||
| 1160 | $ip_netmask = { %$ip_netmask, %{$snmp->ip_netmask || {}} }; | ||
| 1161 | } | ||
| 1162 | } | ||
| 1163 | |||
| 1164 | # build device aliases suitable for DBIC | ||
| 1165 | foreach my $entry (sort keys %$ip_index) { | ||
| 1166 | my $ip = NetAddr::IP::Lite->new($ip_table->{$entry}) || NetAddr::IP::Lite->new($entry) | ||
| 1167 | or next; | ||
| 1168 | my $addr = $ip->addr; | ||
| 1169 | |||
| 1170 | next if $addr eq '0.0.0.0'; | ||
| 1171 | next if acl_matches($ip, 'group:__LOOPBACK_ADDRESSES__'); | ||
| 1172 | next if setting('ignore_private_nets') and $ip->is_rfc1918; | ||
| 1173 | |||
| 1174 | my $iid = $ip_index->{$entry}; | ||
| 1175 | my $port = $interfaces->{$iid}; | ||
| 1176 | my $subnet = $ip_netmask->{$entry} | ||
| 1177 | ? NetAddr::IP::Lite->new($addr, $ip_netmask->{$entry})->network->cidr | ||
| 1178 | : undef; | ||
| 1179 | |||
| 1180 | debug sprintf ' [%s] device - aliased as %s', $device->ip, $addr; | ||
| 1181 | push @aliases, { | ||
| 1182 | alias => $addr, | ||
| 1183 | port => $port, | ||
| 1184 | subnet => $subnet, | ||
| 1185 | dns => undef, | ||
| 1186 | }; | ||
| 1187 | } | ||
| 1188 | |||
| 1189 | return @aliases; | ||
| 1190 | } | ||
| 1191 | |||
| 1192 | sub _get_ipv6_aliases { | ||
| 1193 | my ($device, $snmp, $vrf_list) = @_; | ||
| 1194 | my @aliases; | ||
| 1195 | |||
| 1196 | my $ipv6_index = $snmp->ipv6_index || {}; | ||
| 1197 | my $ipv6_addr = $snmp->ipv6_addr || {}; | ||
| 1198 | my $ipv6_type = $snmp->ipv6_type || {}; | ||
| 1199 | my $ipv6_pfxlen = $snmp->ipv6_addr_prefixlength || {}; | ||
| 1200 | my $interfaces = $snmp->interfaces || {}; | ||
| 1201 | |||
| 1202 | # Get IP Table per VRF if supported | ||
| 1203 | my @vrf_list = @{ $vrf_list || [ _get_vrf_list($device, $snmp) ] }; | ||
| 1204 | if (scalar @vrf_list) { | ||
| 1205 | my $guard = guard { snmp_comm_reindex($snmp, $device, 0) }; | ||
| 1206 | foreach my $vrf (@vrf_list) { | ||
| 1207 | snmp_vrf_reindex($snmp, $device, $vrf); | ||
| 1208 | $ipv6_index = { %$ipv6_index, %{$snmp->ipv6_index || {}} }; | ||
| 1209 | $ipv6_addr = { %$ipv6_addr, %{$snmp->ipv6_addr || {}} }; | ||
| 1210 | $ipv6_type = { %$ipv6_type, %{$snmp->ipv6_type || {}} }; | ||
| 1211 | $ipv6_pfxlen = { %$ipv6_pfxlen, %{$snmp->ipv6_addr_prefixlength || {}} }; | ||
| 1212 | $interfaces = { %$interfaces, %{$snmp->interfaces || {}} }; | ||
| 1213 | } | ||
| 1214 | } | ||
| 1215 | |||
| 1216 | # build device aliases suitable for DBIC | ||
| 1217 | foreach my $iid (sort keys %$ipv6_index) { | ||
| 1218 | next unless $ipv6_type->{$iid} and $ipv6_type->{$iid} eq 'unicast'; | ||
| 1219 | my $entry = $ipv6_addr->{$iid} or next; | ||
| 1220 | my $ip = NetAddr::IP::Lite->new($entry) or next; | ||
| 1221 | my $addr = $ip->addr; | ||
| 1222 | |||
| 1223 | next if $addr eq '::0'; | ||
| 1224 | next if acl_matches($ip, 'group:__LOOPBACK_ADDRESSES__'); | ||
| 1225 | |||
| 1226 | if (acl_matches($ip, 'group:__LOCAL_ADDRESSES__')) { | ||
| 1227 | debug sprintf | ||
| 1228 | ' [%s] device - skipping alias %s as potentially not unique', | ||
| 1229 | $device->ip, $addr; | ||
| 1230 | next; | ||
| 1231 | } | ||
| 1232 | |||
| 1233 | my $port = $interfaces->{ $ipv6_index->{$iid} }; | ||
| 1234 | my $subnet = $ipv6_pfxlen->{$iid} | ||
| 1235 | ? NetAddr::IP::Lite->new($addr .'/'. $ipv6_pfxlen->{$iid})->network->cidr | ||
| 1236 | : undef; | ||
| 1237 | |||
| 1238 | debug sprintf ' [%s] device - aliased as %s', $device->ip, $addr; | ||
| 1239 | push @aliases, { | ||
| 1240 | alias => $addr, | ||
| 1241 | port => $port, | ||
| 1242 | subnet => $subnet, | ||
| 1243 | dns => undef, | ||
| 1244 | }; | ||
| 1245 | } | ||
| 1246 | |||
| 1247 | return @aliases; | ||
| 1248 | } | ||
| 1249 | |||
| 1250 | true; |