blib/lib/App/Netdisco/Worker/Plugin/Discover/NextHopNeighbors.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 0.0% 0 / 8 Podpod n/a totaltotal 0.0% 0 / 165 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package App::Netdisco::Worker::Plugin::Discover::NextHopNeighbors;
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 use NetAddr::IP;
12  
13 register_worker({ phase => 'main', driver => 'snmp' }, sub {
14 my ($job, $workerconf) = @_;
15 my $device = $job->device;
16  
17 return unless $device->in_storage and ($device->has_layer(3)
18 or acl_matches($device, 'force_arpnip')
19 or acl_matches($device, 'ignore_layers'));
20  
21 if (acl_matches($device, 'skip_neighbors')
22 or not setting('discover_neighbors')
23 or not setting('discover_routed_neighbors')) {
24  
25 return Status->info(
26 sprintf ' [%s] neigh - routed neighbor discovery is disabled on this device',
27 $device->ip);
28 }
29  
30 my $snmp = App::Netdisco::Transport::SNMP->reader_for($device)
31 or return Status->defer("discover failed: could not SNMP connect to $device");
32  
33 my $ospf_peers = $snmp->ospf_peers || {};
34 my $ospf_routers = $snmp->ospf_peer_id || {};
35 my $isis_peers = $snmp->isis_peers || {};
36 my $bgp_peers = $snmp->bgp_peer_addr || {};
37 my $eigrp_peers = $snmp->eigrp_peers || {};
38  
39 return Status->info(" [$device] neigh - no BGP, OSPF, IS-IS, or EIGRP peers")
40 unless ((scalar values %$ospf_peers) or (scalar values %$ospf_routers)
41 or (scalar values %$bgp_peers) or (scalar values %$eigrp_peers)
42 or (scalar values %$isis_peers));
43  
44 foreach my $ip ((values %$ospf_peers), (values %$ospf_routers),
45 (values %$bgp_peers), (values %$eigrp_peers),
46 (values %$isis_peers)) {
47  
48 push @{ vars->{'next_hops'} }, $ip;
49 }
50  
51 return Status->info(sprintf " [%s] neigh - found %s routed peers.",
52 $device, scalar @{ vars->{'next_hops'} });
53 });
54  
55 register_worker({ phase => 'store' }, sub {
56 my ($job, $workerconf) = @_;
57 my $device = $job->device;
58  
59 my $nh = vars->{'next_hops'};
60 return unless ref [] eq ref $nh and scalar @$nh;
61  
62 my $count = 0;
63 foreach my $host (@$nh) {
64 my $ip = NetAddr::IP->new($host);
65 if (not $ip or $ip->addr eq '0.0.0.0'
66 or acl_matches($ip->addr, 'group:__LOOPBACK_ADDRESSES__')) {
67  
68 debug sprintf ' [%s] neigh - skipping routed peer %s is not valid',
69 $device, $host;
70 next;
71 }
72  
73 my $peer = get_device($ip);
74 next if $peer->in_storage or not is_discoverable($peer);
75 next if vars->{'queued'}->{$peer->ip};
76  
77 jq_insert({
78 device => $peer->ip,
79 action => 'discover',
80 });
81  
82 $count++;
83 vars->{'queued'}->{$peer->ip} += 1;
84 debug sprintf ' [%s] neigh - queued %s for discovery (peer)', $device, $peer->ip;
85 }
86  
87 return Status->info(" [$device] neigh - $count peers added to queue.");
88 });
89  
90 true;