« blib/lib/App/Netdisco/Worker/Plugin/Discover/Hooks.pm
↑ Summary
blib/lib/App/Netdisco/Worker/Plugin/Discover/NextHopNeighbors.pm »
| 1 | package App::Netdisco::Worker::Plugin::Discover::Neighbors::DOCSIS; | ||
| 2 | use Dancer ':syntax'; | ||
| 3 | |||
| 4 | use App::Netdisco::Worker::Plugin; | ||
| 5 | use App::Netdisco::Transport::SNMP; | ||
| 6 | use aliased 'App::Netdisco::Worker::Status'; | ||
| 7 | |||
| 8 | use App::Netdisco::Util::Device qw/get_device is_discoverable/; | ||
| 9 | use App::Netdisco::Util::Permission 'acl_matches'; | ||
| 10 | use App::Netdisco::JobQueue 'jq_insert'; | ||
| 11 | |||
| 12 | register_worker({ phase => 'main', driver => 'snmp' }, sub { | ||
| 13 | my ($job, $workerconf) = @_; | ||
| 14 | |||
| 15 | my $device = $job->device; | ||
| 16 | return unless $device->in_storage; | ||
| 17 | |||
| 18 | if (acl_matches($device, 'skip_neighbors') or not setting('discover_neighbors')) { | ||
| 19 | return Status->info( | ||
| 20 | sprintf ' [%s] neigh - DOCSIS modems discovery is disabled on this device', | ||
| 21 | $device->ip); | ||
| 22 | } | ||
| 23 | |||
| 24 | my $snmp = App::Netdisco::Transport::SNMP->reader_for($device) | ||
| 25 | or return Status->defer("discover failed: could not SNMP connect to $device"); | ||
| 26 | |||
| 27 | my $modems = $snmp->docs_if_cmts_cm_status_inet_address() || {}; | ||
| 28 | |||
| 29 | return Status->info(" [$device] neigh - no modems (probably not a DOCSIS device)") | ||
| 30 | unless (scalar values %$modems); | ||
| 31 | |||
| 32 | my $count = 0; | ||
| 33 | foreach my $ip (values %$modems) { | ||
| 34 | |||
| 35 | # Some modems may be registered, but not have an IP assigned (they could be offline, disabled, etc) | ||
| 36 | next if $ip eq ''; | ||
| 37 | |||
| 38 | my $peer = get_device($ip); | ||
| 39 | next if $peer->in_storage or not is_discoverable($peer); | ||
| 40 | next if vars->{'queued'}->{$ip}; | ||
| 41 | |||
| 42 | jq_insert({ | ||
| 43 | device => $ip, | ||
| 44 | action => 'discover', | ||
| 45 | }); | ||
| 46 | |||
| 47 | $count++; | ||
| 48 | vars->{'queued'}->{$ip} += 1; | ||
| 49 | debug sprintf ' [%s] queue - queued %s for discovery (DOCSIS peer)', $device, $ip; | ||
| 50 | } | ||
| 51 | |||
| 52 | return Status->info(" [$device] neigh - $count DOCSIS peers (modems) added to queue."); | ||
| 53 | }); | ||
| 54 | |||
| 55 | true; |