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

filter: Statementstmt 0.0% 0 / 120 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 215 CC 12 SCAR 25.0
CC
12
Cov
0%
_build_ssids
30.0
_build_channels
20.0
_by_oid
6.0
CRAP 12.2
1 package App::Netdisco::Worker::Plugin::Discover::Wireless;
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 Dancer::Plugin::DBIC 'schema';
9 use App::Netdisco::Util::SNMPText 'decode_snmp_text';
10 use App::Netdisco::Util::SNMP qw/walk_mark incomplete_walks_for incomplete_walk_message
11 incomplete_walk_level/;
12  
13 # ifIndex and ifIndex.n keys, or a controller's AP MAC and slot keys, in
14 # numeric order, so a duplicate resolves to the same entry on every run
15 # whatever the hash order
16 sub _by_oid {
17 my @left = split m/\./, $a;
18 my @right = split m/\./, $b;
19 while (@left and @right) {
20 my $order = (shift @left) <=> (shift @right);
21 return $order if $order;
22 }
23 return @left <=> @right;
24 }
25  
26 sub _build_ssids {
27 my ($device, $snmp, $device_ports) = @_;
28 my $ssidlist = $snmp->i_ssidlist || {};
29 my $interfaces = $snmp->interfaces || {};
30 my $ssidbcast = $snmp->i_ssidbcast || {};
31 my $ssidmac = $snmp->i_ssidmac || {};
32  
33 my (%ssidseen, @ssids);
34 foreach my $entry (sort _by_oid keys %$ssidlist) {
35 (my $iid = $entry) =~ s/\.\d+$//;
36 my $port = $interfaces->{$iid};
37  
38 if (not $port) {
39 debug sprintf ' [%s] wireless - ignoring %s (no port mapping)',
40 $device->ip, $iid;
41 next;
42 }
43  
44 if (!defined $device_ports->{$port}) {
45 debug sprintf ' [%s] wireless - local port %s already skipped, ignoring',
46 $device->ip, $port;
47 next;
48 }
49  
50 next unless $ssidmac->{$entry};
51  
52 if (exists $ssidseen{$port}{ $ssidmac->{$entry} }) {
53 debug sprintf ' [%s] wireless - duplicate bssid %s on port %s',
54 $device->ip, $ssidmac->{$entry}, $port;
55 next;
56 }
57 ++$ssidseen{$port}{ $ssidmac->{$entry} };
58  
59 push @ssids, {
60 ip => $device->ip,
61 port => $port,
62 ssid => decode_snmp_text($ssidlist->{$entry}, $device, "port $port ssid"),
63 broadcast => $ssidbcast->{$entry},
64 bssid => $ssidmac->{$entry},
65 };
66 }
67 return \@ssids;
68 }
69  
70 sub _build_channels {
71 my ($device, $snmp, $device_ports) = @_;
72 my $interfaces = $snmp->interfaces || {};
73 my $channel = $snmp->i_80211channel || {};
74 my $power = $snmp->dot11_cur_tx_pwr_mw || {};
75  
76 my (%seen, @channels);
77 foreach my $entry (sort _by_oid keys %$channel) {
78 my $port = $interfaces->{$entry};
79  
80 if (not $port) {
81 debug sprintf ' [%s] wireless - ignoring %s (no port mapping)',
82 $device->ip, $entry;
83 next;
84 }
85  
86 if (!defined $device_ports->{$port}) {
87 debug sprintf ' [%s] wireless - local port %s already skipped, ignoring',
88 $device->ip, $port;
89 next;
90 }
91  
92 next if $seen{$port}++;
93  
94 push @channels, {
95 ip => $device->ip,
96 port => $port,
97 channel => $channel->{$entry},
98 power => $power->{$entry},
99 };
100 }
101 return \@channels;
102 }
103  
104 sub _store_ssids {
105 my ($device, $ssids) = @_;
106 schema('netdisco')->txn_do(sub {
107 my $rs = schema('netdisco')->resultset('DevicePortSsid');
108 $rs->upsert($ssids) if scalar @$ssids;
109 my $gone = $rs->remove_unseen({ ip => $device->ip },
110 [ map {{ port => $_->{port}, bssid => $_->{bssid} }} @$ssids ]);
111 debug sprintf ' [%s] wireless - removed %d SSIDs', $device->ip, $gone;
112 debug sprintf ' [%s] wireless - wrote %d SSIDs', $device->ip, scalar @$ssids;
113 });
114 }
115  
116 sub _store_channels {
117 my ($device, $channels) = @_;
118 schema('netdisco')->txn_do(sub {
119 my $rs = schema('netdisco')->resultset('DevicePortWireless');
120 $rs->upsert($channels) if scalar @$channels;
121 my $gone = $rs->remove_unseen({ ip => $device->ip },
122 [ map {{ port => $_->{port} }} @$channels ]);
123 debug sprintf ' [%s] wireless - removed %d wireless channels',
124 $device->ip, $gone;
125 });
126 }
127  
128 # a cut-short walk must not look like a device without SSIDs or channels, so the
129 # stored rows stand; the SSID list gates both tables, so its failure keeps both
130 sub _skip_for {
131 my ($device, $snmp, $mark, $what, @methods) = @_;
132 my @incomplete = incomplete_walks_for($device, $snmp, $mark, @methods);
133 return () unless scalar @incomplete;
134 my $skipped = incomplete_walk_message($device, $what, @incomplete);
135 warning $skipped;
136 return ($skipped, incomplete_walk_level(@incomplete));
137 }
138  
139 register_worker({ phase => 'main', driver => 'snmp' }, sub {
140 my ($job, $workerconf) = @_;
141  
142 my $device = $job->device;
143 return unless $device->in_storage;
144 my $snmp = App::Netdisco::Transport::SNMP->reader_for($device)
145 or return Status->defer("discover failed: could not SNMP connect to $device");
146  
147 # cache the device ports to save hitting the database for many single rows
148 my $device_ports = vars->{'device_ports'}
149 || { map {($_->port => $_)} $device->ports->all };
150  
151 # each table is judged only on the walks its own reads recorded
152 my $ssid_mark = walk_mark($snmp);
153 my $has_ssids = scalar keys %{ $snmp->i_ssidlist || {} };
154 my $ssids = _build_ssids($device, $snmp, $device_ports);
155 my ($ssid_skip, $ssid_level) = _skip_for($device, $snmp, $ssid_mark,
156 'device_port_ssid', qw/interfaces i_ssidlist i_ssidbcast i_ssidmac/);
157  
158 my $channel_mark = walk_mark($snmp);
159 my $channels = $has_ssids ? _build_channels($device, $snmp, $device_ports) : [];
160 my ($chan_skip, $chan_level) = _skip_for($device, $snmp, $channel_mark,
161 'device_port_wireless', qw/interfaces i_ssidlist i_80211channel dot11_cur_tx_pwr_mw/);
162  
163 _store_ssids($device, $ssids) unless defined $ssid_skip;
164 _store_channels($device, $channels) unless defined $chan_skip;
165  
166 my @skips = grep { defined } $ssid_skip, $chan_skip;
167 my $level = (grep { ($_ // '') eq 'error' } $ssid_level, $chan_level) ? 'error' : 'info';
168 my $status = sprintf ' [%s] wireless - %s SSIDs, %s wireless channels',
169 $device->ip,
170 (defined $ssid_skip ? 'kept existing' : 'added ' . scalar @$ssids . ' new'),
171 (defined $chan_skip ? 'kept existing' : 'added ' . scalar @$channels . ' new');
172 $status .= '; ' . join '; ', @skips if scalar @skips;
173 return Status->$level($status);
174 });
175  
176 true;