blib/lib/App/Netdisco/SSHCollector/Platform/VOSS.pm untested

filter: Statementstmt 0.0% 0 / 216 Branchbran 0.0% 0 / 108 Conditioncond 0.0% 0 / 68 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 459 CC 55 SCAR 40.6
CC
55
Cov
0%
_parse_neighbor_blocks
110.0
arpnip
90.0
arpnip_by_vrf
72.0
CRAP 57.9
1 package App::Netdisco::SSHCollector::Platform::VOSS;
2  
3 =head1 NAME
4  
5 App::Netdisco::SSHCollector::Platform::VOSS
6  
7 =head1 DESCRIPTION
8  
9 Collect ARP entries from Extreme VSP devices running the VOSS operating system.
10  
11 With C<store_vrfs> off, SNMP arpnip reads only the GlobalRouter VRF, so this
12 module is the way to collect ARP entries from the other VRFs. With it on, SNMP
13 reads each VRF too, and this module's C<arpnip_by_vrf> stores each entry under
14 its VRF and reads each VRF's IPv6 neighbors as well. C<arpnip> reads IPv4 only.
15  
16 By default this module gets ARP entries from all VRFs (0-512). To specify only certain VRFs in the config:
17  
18 device_auth:
19 - tag: sshvsp
20 driver: cli
21 platform: VOSS
22 only:
23 - 10.1.1.1
24 - 192.168.0.1
25 username: oliver
26 password: letmein
27 vrfs: 1,5,100
28  
29 The VRFs can be specified in any format that the "show ip arp vrfids" command will take. For example:
30  
31 1,2,3,4,5,10
32 1-5,10
33 1-100
34 99
35  
36 =cut
37  
38 use strict;
39 use warnings;
40  
41 use Dancer ':script';
42 use Expect;
43 use Socket ();
44 use Moo;
45  
46 =head1 PUBLIC METHODS
47  
48 =over 4
49  
50 =item B<arpnip($host, $ssh, $args)>
51  
52 Retrieve ARP entries from device. C<$host> is the hostname or IP address
53 of the device. C<$ssh> is a Net::OpenSSH connection to the device. C<$args>
54 is the C<device_auth> entry; its C<vrfs> limits the VRFs read.
55  
56 Returns a list of hashrefs in the format C<{ mac =E<gt> MACADDR, ip =E<gt> IPADDR }>.
57  
58 =item B<arpnip_by_vrf($host, $ssh, $args)>
59  
60 As C<arpnip>, used when C<store_vrfs> is on. Also reads C<show ip vrf> and
61 C<show ipv6 neighbor vrfids>, and names each entry's VRF from the C<IP Arp> or
62 C<Neighbor Information> block it appears in (GlobalRouter is C<''>). IPv6
63 addresses are returned compressed. Neighbor rows are read whatever their TYPE
64 and STATE, and link-local and router-own rows are kept.
65  
66 Returns C<{ entries =E<gt> [ { ip, mac, vrf } ], vrf_read =E<gt> { VRF =E<gt> 0|1 },
67 vrf_list_complete =E<gt> 0|1, families =E<gt> [4, 6] }>, or undef when the session,
68 C<show ip vrf> or the ARP command fails. A block is read whole when its footer
69 counts exactly the rows parsed. A VRF is 1 only when its ARP block and its
70 neighbor block were read whole; a VRF the device says has no IPv6 instance
71 (MgmtRouter) counts its IPv6 as read whole and empty. When the IPv6 command is
72 refused or times out, or the ARP command timed out so it is never sent,
73 C<families> is C<[4]> and C<vrf_read> covers IPv4 only. A C<vrfs> setting other
74 than C<0-512> makes C<vrf_list_complete> 0, so the VRFs it leaves out keep their
75 stored entries.
76  
77 A subclass that overrides C<arpnip> must also override C<arpnip_by_vrf>, or
78 with C<store_vrfs> on this C<arpnip_by_vrf> is used instead of the override.
79  
80 =back
81  
82 =cut
83  
84 sub arpnip {
85 my ($self, $hostlabel, $ssh, $args) = @_;
86  
87 debug "$hostlabel $$ arpnip()";
88  
89 # default to entire range of VRFs
90 my $vrflist = "0-512";
91 # if specified in config, only get ARP from certain VRFs
92 if ($args->{vrfs}) {
93 if ($args->{vrfs} =~ m/^[0-9,\-]+$/) {
94 $vrflist = $args->{vrfs};
95 }
96 }
97  
98 my ($pty, $pid) = $ssh->open2pty;
99 unless ($pty) {
100 debug "unable to run remote command [$hostlabel] " . $ssh->error;
101 return ();
102 }
103 my $expect = Expect->init($pty);
104  
105 my ($pos, $error, $match, $before, $after);
106 my $prompt;
107  
108 $prompt = qr/>/;
109 ($pos, $error, $match, $before, $after) = $expect->expect(10, -re, $prompt);
110
111 $expect->send("terminal more disable\n");
112 ($pos, $error, $match, $before, $after) = $expect->expect(5, -re, $prompt);
113  
114 if ($before =~ m/% Invalid input detected/) {
115 debug "invalid command [$hostlabel]";
116 return ();
117 }
118  
119 $expect->send("show ip arp vrfids $vrflist\n");
120 ($pos, $error, $match, $before, $after) = $expect->expect(60, -re, $prompt);
121 my @lines = split(m/\n/, $before);
122  
123 if ($before =~ m/% Invalid input detected/) {
124 debug "invalid command [$hostlabel]";
125 return ();
126 }
127  
128 if ($before =~ m/Error : ([^\n]+)/) {
129 my $errormsg = $1;
130 if ($errormsg =~ m/Invalid VRF ID/ || $errormsg =~ m/vrfId should be/) {
131 debug "incorrect VRF specified [$hostlabel] : $vrflist : $errormsg";
132 return ();
133 }
134 else {
135 debug "general error fetching ARP [$hostlabel] : $errormsg";
136 return ();
137 }
138 }
139  
140 my @arpentries;
141  
142 my $ipregex = '(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)';
143 my $macregex = '([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}';
144  
145 # IP Address MAC Address VLAN Port Type TTL Tunnel
146 # 172.16.20.15 0024.b269.867d 100 1/1 DYNAMIC 999 device-name
147 foreach my $line (@lines) {
148 next unless $line =~ m/^\s*$ipregex\s+$macregex/;
149 my @fields = split m/\s+/, $line;
150  
151 debug "[$hostlabel] arpnip - mac $fields[1] ip $fields[0]";
152 push @arpentries, { mac => $fields[1], ip => $fields[0] };
153 }
154  
155 $expect->send("exit\n");
156 $expect->soft_close();
157  
158 return @arpentries;
159 }
160  
161 # the prompt as the captures show it (edge1:1>), alone at the end of the
162 # output: a > inside a row (a remote system name in TUNNEL) must not end a read
163 my $PROMPT = qr/^\S+:\d+[>#]\s*\z/m;
164  
165 sub arpnip_by_vrf {
166 my ($self, $hostlabel, $ssh, $args) = @_;
167  
168 debug "$hostlabel $$ arpnip_by_vrf()";
169  
170 my $vrflist = "0-512";
171 if ($args->{vrfs} and $args->{vrfs} =~ m/^[0-9,\-]+$/) {
172 $vrflist = $args->{vrfs};
173 }
174  
175 my $expect = _open_session($hostlabel, $ssh) or return undef;
176  
177 my ($listed, $list_ok, $vrf_by_id) = _read_vrf_list($hostlabel, $expect);
178 # a timed-out VRF list fails the read, as an ARP command error does
179 my ($arp, $arp_whole) = (defined $list_ok
180 ? _read_blocks($hostlabel, $expect, 'ARP', "show ip arp vrfids $vrflist", \&_parse_arp_blocks) : ());
181 unless ($arp) {
182 _close_session($expect);
183 return undef;
184 }
185  
186 # after a timeout the pty still holds the unfinished reply, so nothing
187 # more is sent; a refused or timed-out IPv6 read leaves IPv4 only
188 my ($nd, $nd_whole) = ($arp->{timed_out} ? () : _read_blocks($hostlabel, $expect,
189 'IPv6 neighbors', "show ipv6 neighbor vrfids $vrflist", \&_parse_neighbor_blocks));
190 ($nd, $nd_whole) = () if $nd and $nd->{timed_out};
191 _close_session($expect);
192  
193 # a configured subset never vouches for VRFs it did not ask for, and
194 # after a timeout or a partial block nothing is vouched for
195 my $complete = ($list_ok and $arp_whole and (!$nd or $nd_whole) and $vrflist eq '0-512');
196 my $result = _vrf_result($listed, $vrf_by_id, $arp, $nd, $complete);
197 debug sprintf '[%s] arpnip - families [%s], %d IPv6 neighbor entries, VRF list %s',
198 $hostlabel, join(', ', @{ $result->{families} }), ($nd ? scalar @{ $nd->{entries} } : 0),
199 ($complete ? 'complete' : 'incomplete');
200 return $result;
201 }
202  
203 # a VRF is read whole when its ARP block was and, with IPv6 read, its
204 # neighbor block was or the device said it has no IPv6 instance
205 sub _vrf_result {
206 my ($listed, $vrf_by_id, $arp, $nd, $complete) = @_;
207  
208 my %no_ipv6 = map { (defined $vrf_by_id->{$_} ? ($vrf_by_id->{$_} => 1) : ()) }
209 @{ $nd ? $nd->{no_instance} : [] };
210 my %vrf_read = map { ($_ => 0) } @$listed;
211 foreach my $vrf (keys %{ $arp->{read} }) {
212 my $ipv6_whole = (!$nd or $no_ipv6{$vrf} or $nd->{read}->{$vrf});
213 $vrf_read{$vrf} = ($arp->{read}->{$vrf} and $ipv6_whole) ? 1 : 0;
214 }
215  
216 return {
217 entries => [ @{ $arp->{entries} }, ($nd ? @{ $nd->{entries} } : ()) ],
218 vrf_read => \%vrf_read,
219 vrf_list_complete => ($complete ? 1 : 0),
220 families => ($nd ? [4, 6] : [4]),
221 };
222 }
223  
224 # returns the Expect session after the pager is off, or undef
225 sub _open_session {
226 my ($hostlabel, $ssh) = @_;
227  
228 my ($pty, $pid) = $ssh->open2pty;
229 unless ($pty) {
230 debug "unable to run remote command [$hostlabel] " . $ssh->error;
231 return undef;
232 }
233 my $expect = Expect->init($pty);
234  
235 my ($pos) = $expect->expect(10, -re, $PROMPT);
236 unless ($pos) {
237 info "[$hostlabel] arpnip - no prompt of the form <hostname>:<N>> seen,"
238 . " so VRF collection by CLI was skipped for this device";
239 _close_session($expect);
240 return undef;
241 }
242  
243 $expect->send("terminal more disable\n");
244 my ($more_pos, undef, undef, $before) = $expect->expect(5, -re, $PROMPT);
245 return _abandon($expect, "no prompt after terminal more disable [$hostlabel]")
246 unless $more_pos;
247 return _abandon($expect, "invalid command [$hostlabel]")
248 if $before =~ m/% Invalid input detected/;
249  
250 return $expect;
251 }
252  
253 sub _abandon {
254 my ($expect, $message) = @_;
255 debug $message;
256 _close_session($expect);
257 return undef;
258 }
259  
260 sub _close_session {
261 my $expect = shift;
262 $expect->send("exit\n");
263 $expect->soft_close();
264 }
265  
266 # returns the VRF names listed (GlobalRouter as ''), whether the list is
267 # whole: 1 or 0, or undef on a timeout, after which Expect still holds the
268 # partial output and nothing later can be parsed as fresh, and the names by
269 # VRF id
270 sub _read_vrf_list {
271 my ($hostlabel, $expect) = @_;
272  
273 $expect->send("show ip vrf\n");
274 my ($pos, undef, undef, $before) = $expect->expect(30, -re, $PROMPT);
275  
276 my (@names, %by_id, $shown, $total);
277 foreach my $line (split m/\r?\n/, $before) {
278 if ($line =~ m/^(\S+)\s+(\d+)\s+\d+\s+\d+\s+(?:TRUE|FALSE)\s/) {
279 push @names, ($2 == 0 ? '' : $1);
280 $by_id{$2} = $names[-1];
281 }
282 elsif ($line =~ m/^\s*(\d+) out of (\d+) Total Num of VRF Entries displayed/) {
283 ($shown, $total) = ($1, $2);
284 }
285 }
286  
287 unless ($pos) {
288 info "[$hostlabel] arpnip - show ip vrf timed out, no VRF read";
289 return (\@names, undef, \%by_id);
290 }
291 my $whole = (defined $shown and $shown == $total and $shown == scalar @names) ? 1 : 0;
292 return (\@names, $whole, \%by_id);
293 }
294  
295 # returns ({ entries, read => { VRF => 0|1 }, no_instance => [ids], timed_out },
296 # whether every block was read whole), or nothing when the device answers an error
297 sub _read_blocks {
298 my ($hostlabel, $expect, $what, $command, $parser) = @_;
299  
300 debug "[$hostlabel] arpnip - $command";
301 $expect->send("$command\n");
302 my ($pos, undef, undef, $before) = $expect->expect(60, -re, $PROMPT);
303  
304 if ($before =~ m/% Invalid input detected/ or $before =~ m/^\s*Error\s*:\s*([^\n]*)/m) {
305 debug "error fetching $what [$hostlabel] : $command : " . ($1 // 'invalid command');
306 return;
307 }
308 info "[$hostlabel] arpnip - $command timed out, no VRF read whole"
309 unless $pos;
310  
311 my $blocks = $parser->($hostlabel, $before);
312 # a block is whole only when its own footer counts exactly the rows parsed
313 my %read = map {
314 my $footer = $blocks->{footer}->{$_};
315 ($_ => ($pos and defined $footer and $footer == $blocks->{rows}->{$_}) ? 1 : 0)
316 } keys %{ $blocks->{rows} };
317 my $whole = ($pos and not $blocks->{unrecognized} and not grep { !$_ } values %read) ? 1 : 0;
318  
319 return ({ entries => $blocks->{entries}, read => \%read,
320 no_instance => ($blocks->{no_instance} || []), timed_out => ($pos ? 0 : 1) }, $whole);
321 }
322  
323 sub _parse_arp_blocks {
324 my ($hostlabel, $output) = @_;
325  
326 my $ipregex = '(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)';
327 my $macregex = '(?:[[:xdigit:]]{2}:){5}[[:xdigit:]]{2}';
328  
329 my %blocks = (entries => [], rows => {}, footer => {}, unrecognized => 0);
330 my $vrf;
331 foreach my $line (split m/\r?\n/, $output) {
332 # only "IP Arp - " heads ARP rows; an "IP Arp Extn - " header can name
333 # another VRF (MgmtRouter's names GlobalRouter), so it never switches.
334 # An unparsed header ends the previous block, so its rows are dropped.
335 if ($line =~ m/IP Arp - /) {
336 $vrf = ($line =~ m/^\s*IP Arp - (?:VRF (\S+)|GlobalRouter)\s*$/) ? ($1 // '') : undef;
337 if (defined $vrf) { $blocks{rows}->{$vrf} //= 0 }
338 else { $blocks{unrecognized} = 1; debug "[$hostlabel] arpnip - unparsed header: $line" }
339 next;
340 }
341 next unless defined $vrf;
342 if ($line =~ m/^\s*(\d+) out of \d+ ARP entries displayed/) {
343 $blocks{footer}->{$vrf} = $1;
344 next;
345 }
346 # the footer counts every row, so one without a MAC still counts
347 next unless $line =~ m/^\s*($ipregex)\s+(\S+)/;
348 my ($ip, $mac) = ($1, $2);
349 $blocks{rows}->{$vrf}++;
350 next unless $mac =~ m/^$macregex$/;
351 debug "[$hostlabel] arpnip - vrf '$vrf' mac $mac ip $ip";
352 push @{ $blocks{entries} }, { ip => $ip, mac => $mac, vrf => $vrf };
353 }
354  
355 return \%blocks;
356 }
357  
358 # Each neighbor is two lines: "ADDRESS/" with the columns, then the MAC
359 # indented. TYPE and STATE are not read: only static and local rows were ever
360 # captured, and learned ones carry other values.
361 sub _parse_neighbor_blocks {
362 my ($hostlabel, $output) = @_;
363  
364 my $macregex = '(?:[[:xdigit:]]{2}:){5}[[:xdigit:]]{2}';
365  
366 my %blocks = (entries => [], rows => {}, footer => {}, unrecognized => 0, no_instance => []);
367 my ($vrf, $address);
368 foreach my $line (split m/\r?\n/, $output) {
369 if ($line =~ m/^\s*Info: IPV6 Instance does not exist for VRF with id: ([\d,\-]+)/i) {
370 push @{ $blocks{no_instance} }, _expand_vrf_ids($1);
371 next;
372 }
373 # an unparsed header ends the previous block, so its rows are dropped
374 if ($line =~ m/Neighbor Information - /) {
375 $vrf = ($line =~ m/^\s*Neighbor Information - (?:VRF (\S+)|GlobalRouter)\s*$/) ? ($1 // '') : undef;
376 undef $address;
377 if (defined $vrf) { $blocks{rows}->{$vrf} //= 0 }
378 else { $blocks{unrecognized} = 1; debug "[$hostlabel] arpnip - unparsed header: $line" }
379 next;
380 }
381 next unless defined $vrf;
382 if ($line =~ m/^\s*(\d+) out of \d+ Total Num of Neighbor Entries displayed/) {
383 $blocks{footer}->{$vrf} = $1;
384 next;
385 }
386 if ($line =~ m{^([[:xdigit:]]*:[[:xdigit:]:.]*)/\s}) {
387 $address = $1;
388 $blocks{rows}->{$vrf}++;
389 next;
390 }
391 next unless defined $address and $line =~ m/^\s+($macregex)\s*$/;
392 my $ip = _compress_ipv6($address);
393 undef $address;
394 next unless defined $ip;
395 debug "[$hostlabel] arpnip - vrf '$vrf' mac $1 ip $ip";
396 push @{ $blocks{entries} }, { ip => $ip, mac => $1, vrf => $vrf };
397 }
398  
399 return \%blocks;
400 }
401  
402 # "3-4,512" as (3, 4, 512)
403 sub _expand_vrf_ids {
404 my $list = shift;
405 return map { m/^(\d+)-(\d+)$/ ? ($1 .. $2) : (m/^\d+$/ ? $_ : ()) } split m/,/, $list;
406 }
407  
408 # VOSS prints every group (2001:db8:1001:0:0:0:0:10); other drivers report
409 # the compressed form, so one address keeps one spelling
410 sub _compress_ipv6 {
411 my $address = shift;
412 my $packed = Socket::inet_pton(Socket::AF_INET6(), $address);
413 return (defined $packed ? Socket::inet_ntop(Socket::AF_INET6(), $packed) : undef);
414 }
415  
416 1;