1package App::Netdisco::Worker::Plugin::Arpnip::Nodes; 2 3use Dancer ':syntax'; 4use Dancer::Plugin::DBIC 'schema'; 5 6use App::Netdisco::Worker::Plugin; 7use aliased 'App::Netdisco::Worker::Status'; 8 9use App::Netdisco::Transport::SSH (); 10use App::Netdisco::Transport::SNMP (); 11 12use App::Netdisco::Util::Node qw/check_mac store_arp/; 13use App::Netdisco::Util::FastResolver 'hostnames_resolve_async'; 14 15use NetAddr::IP::Lite ':lower'; 16use Time::HiRes 'gettimeofday'; 17 18register_worker({ phase => 'store' }, sub { 19 my ($job, $workerconf) = @_; 20 my $device = $job->device; 21 22 # would be possible just to use now() on updated records, but by using this 23 # same value for them all, we _can_ if we want add a job at the end to 24 # select and do something with the updated set (no reason to yet, though) 25 my $now = 'to_timestamp('. (join '.', gettimeofday) .')'; 26 27 # update node_ip with ARP and Neighbor Cache entries 28 29 store_arp(\%$_, $now) for @{ vars->{'v4arps'} }; 30 debug sprintf ' [%s] arpnip - processed %s ARP Cache entries', 31 $device->ip, scalar @{ vars->{'v4arps'} }; 32 33 store_arp(\%$_, $now) for @{ vars->{'v6arps'} }; 34 debug sprintf ' [%s] arpnip - processed %s IPv6 Neighbor Cache entries', 35 $device->ip, scalar @{ vars->{'v6arps'} }; 36 37 $device->update({last_arpnip => \$now}); 38 39 my $status = $job->best_status; 40 return Status->$status("Ended arpnip for $device"); 41}); 42 43register_worker({ phase => 'main', driver => 'snmp' }, sub { 44 my ($job, $workerconf) = @_; 45 46 my $device = $job->device; 47 my $snmp = App::Netdisco::Transport::SNMP->reader_for($device) 48 or return Status->defer("arpnip failed: could not SNMP connect to $device"); 49 50 # cache v4 arp table 51 push @{ vars->{'v4arps'} }, 52 @{ get_arps_snmp($device, $snmp->at_paddr, $snmp->at_netaddr) }; 53 54 # cache v6 neighbor cache 55 push @{ vars->{'v6arps'} }, 56 @{get_arps_snmp($device, $snmp->ipv6_n2p_mac, $snmp->ipv6_n2p_addr) }; 57 58 $device->update({layers => \[q{overlay(layers placing '1' from 6 for 1)}]}); 59 return Status->done("Gathered arp caches from $device"); 60}); 61 62# get an arp table (v4 or v6) 63sub get_arps_snmp { 64 my ($device, $paddr, $netaddr) = @_; 65 my @arps = (); 66 67 while (my ($arp, $node) = each %$paddr) { 68 my $ip = $netaddr->{$arp}; 69 next unless defined $ip; 70 next unless check_mac($node, $device); 71 push @arps, { 72 node => $node, 73 ip => $ip, 74 dns => undef, 75 }; 76 } 77 78 debug sprintf ' resolving %d ARP entries with max %d outstanding requests', 79 scalar @arps, $ENV{'PERL_ANYEVENT_MAX_OUTSTANDING_DNS'}; 80 my $resolved_ips = hostnames_resolve_async(\@arps); 81 82 return $resolved_ips; 83} 84 85register_worker({ phase => 'main', driver => 'cli' }, sub { 86 my ($job, $workerconf) = @_; 87 88 my $device = $job->device; 89 my $cli = App::Netdisco::Transport::SSH->session_for($device) 90 or return Status->defer("arpnip failed: could not SSH connect to $device"); 91 92 # should be both v4 and v6 93 my @arps = @{ get_arps_cli($device, [$cli->arpnip]) }; 94 95 # cache v4 arp table 96 push @{ vars->{'v4arps'} }, 97 grep { NetAddr::IP::Lite->new($_->{ip})->bits == 32 } @arps; 98 99 # cache v6 neighbor cache 100 push @{ vars->{'v6arps'} }, 101 grep { NetAddr::IP::Lite->new($_->{ip})->bits == 128 } @arps; 102 103 $device->update({layers => \[q{overlay(layers placing '1' from 6 for 1)}]}); 104 return Status->done("Gathered arp caches from $device"); 105}); 106 107sub get_arps_cli { 108 my ($device, $entries) = @_; 109 my @arps = (); 110 $entries ||= []; 111 112 foreach my $entry (@$entries) { 113 next unless check_mac($entry->{mac}, $device); 114 push @arps, { 115 node => $entry->{mac}, 116 ip => $entry->{ip}, 117 dns => $entry->{dns}, 118 }; 119 } 120 121 debug sprintf ' resolving %d ARP entries with max %d outstanding requests', 122 scalar @arps, $ENV{'PERL_ANYEVENT_MAX_OUTSTANDING_DNS'}; 123 my $resolved_ips = hostnames_resolve_async(\@arps); 124 125 return $resolved_ips; 126} 127 128true; 129