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

filter: Statementstmt 0.0% 0 / 187 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 54 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 376 CC 39 SCAR 36.9
CC
39
Cov
0%
_parse_vrf_list
110.0
arpnip_by_vrf
56.0
_parse_table
42.0
CRAP 40.2
1 package App::Netdisco::SSHCollector::Platform::IOSXR;
2  
3 =head1 NAME
4  
5 App::Netdisco::SSHCollector::Platform::IOSXR
6  
7 =head1 DESCRIPTION
8  
9 Collect ARP entries from Cisco IOS-XR routers using Expect, and, with
10 C<store_vrfs> on, IPv6 neighbor entries as well. Suitable for both 32- and
11 64-bit IOS-XR.
12  
13 =cut
14  
15 use strict;
16 use warnings;
17  
18 use Dancer ':script';
19 use Expect;
20 use NetAddr::MAC qw/mac_as_ieee/;
21 use Moo;
22  
23 =head1 PUBLIC METHODS
24  
25 =over 4
26  
27 =item B<arpnip($host, $ssh)>
28  
29 Retrieve ARP entries from device. C<$host> is the hostname or IP address
30 of the device. C<$ssh> is a Net::OpenSSH connection to the device.
31  
32 Returns a list of hashrefs in the format C<{ mac =E<gt> MACADDR, ip =E<gt> IPADDR }>.
33  
34 =item B<arpnip_by_vrf($host, $ssh, $args)>
35  
36 As C<arpnip>, used when C<store_vrfs> is on. Reads C<show vrf all>, then the
37 global C<show arp> and C<show ipv6 neighbors> and, for each listed VRF,
38 C<show arp vrf NAME> and, when the global C<show ipv6 neighbors> answered,
39 C<show ipv6 neighbors vrf NAME>. Each entry's VRF is the one it was read from
40 (the global table is C<''>). The global C<show ipv6 neighbors> also lists
41 VRF neighbors, so its entries carry C<< mixed => 1 >>. The router's own
42 addresses (state C<Interface>) are kept, as C<arpnip> keeps them.
43  
44 Returns C<{ entries =E<gt> [ { ip, mac, vrf } ], vrf_read =E<gt> { VRF =E<gt> 0|1 },
45 vrf_list_complete =E<gt> 0|1, families =E<gt> [4, 6] }>, or undef when the
46 session or C<show vrf all> fails. C<families> is C<[4]> when the global
47 C<show ipv6 neighbors> did not answer cleanly (IPv6 is unsupported or
48 unreached), and then C<vrf_read> covers IPv4 only; otherwise it is C<[4, 6]>.
49 A table is read whole when it returned to the prompt with no C<%> or
50 C<Invalid> line and every line was a timestamp, a header, a location, an
51 entry or a multicast adjacency. A VRF is 1 only when its table for every
52 family in C<families> was read whole, so a VRF whose IPv6 table fails is 0
53 though its IPv4 entries are reported. A VRF that is empty, misspelled or
54 unknown prints nothing, so an empty table reads as whole. A VRF list row
55 that cannot be parsed into a name makes C<vrf_list_complete> 0, and a VRF
56 name outside C<[A-Za-z0-9_.:-]> or equal to C<all> is 0 with no command sent.
57 Only a VRF named exactly C<default> is skipped as the global table; C<Default>
58 is a user VRF. A C<show vrf all> that returned with its header and no VRF is a whole, empty
59 list. After a timeout the rest of the session is not read, and that VRF and
60 every one not yet read are 0.
61  
62 A subclass that overrides C<arpnip> must also override C<arpnip_by_vrf>, or
63 with C<store_vrfs> on this C<arpnip_by_vrf> is used instead of the override.
64  
65 =back
66  
67 =cut
68  
69 sub arpnip {
70 my ($self, $hostlabel, $ssh, $args) = @_;
71  
72 debug "$hostlabel $$ arpnip()";
73  
74 my ($pty, $pid) = $ssh->open2pty;
75 unless ($pty) {
76 warn "unable to run remote command [$hostlabel] " . $ssh->error;
77 return ();
78 }
79 my $expect = Expect->init($pty);
80  
81 my ($pos, $error, $match, $before, $after);
82 my $prompt = qr/# +$/;
83 my $timeout = 10;
84  
85 ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $prompt);
86  
87 $expect->send("terminal length 0\n");
88 ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $prompt);
89  
90 $expect->send("show arp vrf all\n");
91 ($pos, $error, $match, $before, $after) = $expect->expect($timeout, -re, $prompt);
92  
93 my @arpentries = ();
94 my @data = split(m/\n/, $before);
95  
96 foreach (@data) {
97 my ($ip, $age, $mac, $state, $t, $iface) = split(/\s+/);
98  
99 if ($ip =~ m/(\d{1,3}\.){3}\d{1,3}/
100 && $mac =~ m/([0-9a-f]{4}\.){2}[0-9a-f]{4}/i) {
101 push(@arpentries, { ip => $ip, mac => $mac });
102 }
103 }
104  
105  
106 $expect->send("exit\n");
107 $expect->hard_close();
108  
109 return @arpentries;
110 }
111  
112 # the prompt alone at the end of the output, so a row never ends a read
113 my $VRF_PROMPT = qr/^[^\s#]+#\s*\z/m;
114  
115 my $VRF_NAME = qr/\A[A-Za-z0-9_.:-]+\z/;
116 my $TIMESTAMP = qr/^\s*[A-Z][a-z]{2}\s+[A-Z][a-z]{2}\s+\d+\s+\d\d:\d\d:\d\d(?:\.\d+)?\s+\S+\s*$/;
117 my $DASHES = qr/^\s*-{5,}\s*$/;
118 my $LOCATION = qr{^\s*\d+(?:/\w+)+\s*$};
119 my $VRF_HEADER = qr/^\s*VRF\s+RD\s+RT\s+AFI\s+SAFI\b/;
120 my $IPV4_HEADER = qr/^\s*Address\s+Age\s+Hardware Addr/i;
121 my $IPV6_HEADER = qr/^\s*IPv6 Address\s+Age\s+Link-layer/i;
122 my $MAC_DOTTED = qr/[0-9a-f]{4}\.[0-9a-f]{4}\.[0-9a-f]{4}/i;
123 my $ERROR_LINE = qr/^\s*(?:%|Invalid)/;
124  
125 sub arpnip_by_vrf {
126 my ($self, $hostlabel, $ssh, $args) = @_;
127 debug "$hostlabel $$ arpnip_by_vrf()";
128  
129 my ($exp, $timeout) = _open_vrf_session($hostlabel, $ssh, $args) or return undef;
130  
131 my $list = _run_command($exp, $timeout, 'show vrf all');
132 my ($vrfs, $names_whole, $list_whole) = _parse_vrf_list($list);
133 unless ($list_whole) {
134 info "$hostlabel $$ arpnip_by_vrf() show vrf all did not return a VRF list, so VRF collection by CLI was skipped";
135 _close_vrf_session($exp);
136 return undef;
137 }
138  
139 my $state = { entries => [], seen => {}, whole => {}, unread => {}, timed_out => 0 };
140 my $ipv6_answers = 0;
141 my @plan = ([ '', 4, 'show arp' ], [ '', 6, 'show ipv6 neighbors' ]);
142 while (my $step = shift @plan) {
143 my ($vrf, $family, $command) = @$step;
144 my $errored = _read_table($hostlabel, $state, $exp, $timeout, $vrf, $family, $command);
145 if ($family == 6 and $vrf eq '') {
146 $ipv6_answers = !$errored;
147 push @plan, map { _vrf_steps($hostlabel, $_, $ipv6_answers, $state) } @$vrfs;
148 }
149 if ($state->{timed_out}) {
150 info "$hostlabel $$ arpnip_by_vrf() [$command] timed out, so nothing after it was read";
151 last;
152 }
153 }
154 _close_vrf_session($exp);
155  
156 my @families = ($ipv6_answers ? (4, 6) : (4));
157 my %vrf_read = map {
158 my $vrf = $_;
159 ($vrf => ((!$state->{unread}->{$vrf} and 0 == grep { !$state->{whole}->{$vrf}->{$_} } @families) ? 1 : 0))
160 } ('', @$vrfs);
161  
162 return {
163 entries => $state->{entries}, vrf_read => \%vrf_read,
164 vrf_list_complete => (($state->{timed_out} or !$names_whole) ? 0 : 1),
165 families => \@families,
166 };
167 }
168  
169 # the commands for one VRF; a name that is not plain is never sent, since it
170 # could carry a second command, and "all" would answer for every VRF at once
171 sub _vrf_steps {
172 my ($hostlabel, $vrf, $ipv6_answers, $state) = @_;
173 unless ($vrf =~ $VRF_NAME and lc $vrf ne 'all') {
174 debug "$hostlabel $$ arpnip_by_vrf() VRF name [$vrf] is not usable in a command, so it was not read";
175 $state->{unread}->{$vrf} = 1;
176 return ();
177 }
178 my @steps = ([ $vrf, 4, "show arp vrf $vrf" ]);
179 push @steps, [ $vrf, 6, "show ipv6 neighbors vrf $vrf" ] if $ipv6_answers;
180 return @steps;
181 }
182  
183 # (names, whether every row gave a name, whether this is a whole VRF list).
184 # The global table is not listed ("default" is skipped should a build list it,
185 # but only that exact name, since a complete list retires the VRFs it leaves
186 # out); an indented line continues the row above and holds no name.
187 sub _parse_vrf_list {
188 my ($text) = @_;
189 return ([], 0, 0) unless defined $text;
190  
191 my (@names, %seen);
192 my ($header, $errored, $names_whole, $rows) = (0, 0, 1, 0);
193 foreach my $line (split /\r?\n/, $text) {
194 next if $line =~ m/^\s*$/ or $line =~ $TIMESTAMP;
195 if ($line =~ $ERROR_LINE) { $errored = 1; next }
196 if ($line =~ $VRF_HEADER) { $header = 1; next }
197 if ($line =~ m/^\s/) { $names_whole = 0 unless $rows; next }
198 unless ($line =~ m/^(\S+)\s+\S/) { $names_whole = 0; next }
199 $rows++;
200 my $name = $1;
201 push @names, $name unless $name eq 'default' or $seen{$name}++;
202 }
203  
204 return (\@names, $names_whole, ($header and !$errored) ? 1 : 0);
205 }
206  
207 # returns the Expect session once the pager is off, and the timeout, or nothing
208 sub _open_vrf_session {
209 my ($hostlabel, $ssh, $args) = @_;
210  
211 my ($pty, $pid) = $ssh->open2pty;
212 unless ($pty) {
213 warn "unable to run remote command [$hostlabel] " . $ssh->error;
214 return;
215 }
216  
217 my $exp = Expect->init($pty);
218 $exp->raw_pty(1);
219 my $timeout = ($args && $args->{timeout}) ? $args->{timeout} : 30;
220  
221 my ($pos) = $exp->expect($timeout, -re => $VRF_PROMPT);
222 # width 0 keeps a long command from scrolling the echo, which would spoil the read
223 unless ($pos and defined _run_command($exp, $timeout, 'terminal length 0')
224 and defined _run_command($exp, $timeout, 'terminal width 0')) {
225 info "$hostlabel $$ arpnip_by_vrf() unable to reach CLI prompt";
226 $exp->hard_close();
227 return;
228 }
229  
230 return ($exp, $timeout);
231 }
232  
233 sub _close_vrf_session {
234 my $exp = shift;
235 $exp->send("exit\n");
236 $exp->hard_close();
237 }
238  
239 # the command's output without the echoed command, or undef on a timeout,
240 # after which Expect still holds the partial output
241 sub _run_command {
242 my ($exp, $timeout, $command) = @_;
243  
244 $exp->send("$command\n");
245 my ($pos, undef, undef, $before) = $exp->expect($timeout, -re => $VRF_PROMPT);
246 return undef unless $pos;
247  
248 my @lines = split /\r?\n/, ($before // '');
249 shift @lines if @lines and $lines[0] =~ m/^\s*\Q$command\E\s*$/;
250 return join "\n", @lines;
251 }
252  
253 # runs one command and records its entries and whether it was read whole;
254 # returns true when the device answered with an error or timed out
255 sub _read_table {
256 my ($hostlabel, $state, $exp, $timeout, $vrf, $family, $command) = @_;
257  
258 my $output = _run_command($exp, $timeout, $command);
259 unless (defined $output) {
260 debug "$hostlabel $$ arpnip_by_vrf() command [$command] failed: no prompt within ${timeout}s";
261 $state->{timed_out} = 1;
262 $state->{unread}->{$vrf} = 1;
263 return 1;
264 }
265  
266 my ($entries, $whole, $errored) = _parse_table($output, $family);
267 debug "$hostlabel $$ arpnip_by_vrf() command [$command] returned an error" if $errored;
268 foreach my $entry (@$entries) {
269 my $key = lc join '|', $vrf, @$entry{qw/ip mac/};
270 next if $state->{seen}->{$key}++;
271 # the global show ipv6 neighbors also lists VRF neighbors
272 my @mixed = (($vrf eq '' and $family == 6) ? (mixed => 1) : ());
273 push @{ $state->{entries} }, { %$entry, vrf => $vrf, @mixed };
274 }
275 $state->{whole}->{$vrf}->{$family} = $whole;
276 return $errored;
277 }
278  
279 # returns the entries, whether every line was a known non-row or an entry,
280 # and whether an error line was seen
281 sub _parse_table {
282 my ($output, $family) = @_;
283 my ($header, $row) = ($family == 4
284 ? ($IPV4_HEADER, \&_parse_arp_row) : ($IPV6_HEADER, \&_parse_neighbor_row));
285 my (@entries, $errored);
286 my $whole = 1;
287  
288 foreach my $line (split /\r?\n/, $output) {
289 next if $line =~ m/^\s*$/ or $line =~ $TIMESTAMP or $line =~ $DASHES
290 or $line =~ $LOCATION or $line =~ $header;
291 if ($line =~ $ERROR_LINE) { $errored = 1; $whole = 0; next }
292 my ($recognized, $entry) = $row->($line);
293 $whole = 0 unless $recognized;
294 push @entries, $entry if $entry;
295 }
296  
297 return (\@entries, $whole, $errored);
298 }
299  
300 # returns whether the line is a row, and its entry unless the row is incomplete
301 sub _parse_arp_row {
302 my ($line) = @_;
303 return (0) unless $line =~ m/^\s*(\d{1,3}(?:\.\d{1,3}){3})\s+\S+\s+($MAC_DOTTED)\s+(\S+)\s+\S+\s+\S+\s*$/;
304 my ($ip, $mac, $state) = ($1, $2, $3);
305 return (1, (lc $state eq 'incomplete' ? undef : { ip => $ip, mac => mac_as_ieee($mac) }));
306 }
307  
308 # "[Mcast adjacency]" rows carry no host and a zero MAC on every interface
309 sub _parse_neighbor_row {
310 my ($line) = @_;
311 return (1) if $line =~ m/^\s*\[Mcast adjacency\]\s+\S+\s+0000\.0000\.0000\s/;
312 return (0) unless $line =~ m/^\s*([0-9a-f:.]+)\s+\S+\s+($MAC_DOTTED)\s+([A-Z]+)\s+\S+(?:\s+\S+)?\s*$/i;
313 my ($ip, $mac, $state) = ($1, $2, $3);
314 return (1, (uc $state eq 'INCMP' ? undef : { ip => lc $ip, mac => mac_as_ieee($mac) }));
315 }
316  
317 1;