« blib/lib/App/Netdisco/Transport/SSH.pm
↑ Summary
blib/lib/App/Netdisco/Web/Plugin/Search/Device.pm »
| 1 | package App::Netdisco::SSHCollector::Platform::EOS; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | App::Netdisco::SSHCollector::Platform::EOS | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | Collect ARP and IPv6 neighbor entries, MAC table, and connected subnet data | ||
| 10 | from Arista EOS devices. | ||
| 11 | |||
| 12 | =cut | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | |||
| 17 | use Dancer ':script'; | ||
| 18 | use Moo; | ||
| 19 | use NetAddr::MAC qw/mac_as_ieee/; | ||
| 20 | use JSON qw(decode_json); | ||
| 21 | |||
| 22 | use App::Netdisco::Util::VRF qw/netdisco_vrf/; | ||
| 23 | |||
| 24 | =head1 PUBLIC METHODS | ||
| 25 | |||
| 26 | =over 4 | ||
| 27 | |||
| 28 | =item B<arpnip($host, $ssh)> | ||
| 29 | |||
| 30 | Retrieve ARP entries from device. C<$host> is the hostname or IP address | ||
| 31 | of the device. C<$ssh> is a Net::OpenSSH connection to the device. | ||
| 32 | |||
| 33 | Returns a list of hashrefs in the format C<< { mac => MACADDR, ip => IPADDR } >>. | ||
| 34 | IPv4 ARP and IPv6 neighbor entries from all VRFs are flattened into one list. | ||
| 35 | |||
| 36 | =item B<arpnip_by_vrf($host, $ssh, $args)> | ||
| 37 | |||
| 38 | As C<arpnip>, used when C<store_vrfs> is on. Names each entry's VRF from the | ||
| 39 | JSON key it appears under (C<default> is C<''>). A VRF is read whole only when | ||
| 40 | every command that returned usable JSON listed it with its neighbor table. | ||
| 41 | |||
| 42 | Returns C<< { entries => [ { ip, mac, vrf } ], vrf_read => { VRF => 0|1 }, | ||
| 43 | vrf_list_complete => 0|1, families => [4, 6] } >>, or undef when neither | ||
| 44 | command returned usable JSON. C<families> names the address families | ||
| 45 | C<vrf_read> covers: C<[4]> or C<[6]> when only one command did (absent or | ||
| 46 | malformed would mean C<[4]>). IPv4 entries also carry C<iface>, as in | ||
| 47 | C<arpnip>; one with no interface is still returned, without C<iface>. | ||
| 48 | |||
| 49 | A subclass that overrides C<arpnip> must also override C<arpnip_by_vrf>, or | ||
| 50 | with C<store_vrfs> on this C<arpnip_by_vrf> is used instead of the override. | ||
| 51 | |||
| 52 | =item B<macsuck($host, $ssh)> | ||
| 53 | |||
| 54 | Retrieve MAC address table entries from device, skipping entries on the | ||
| 55 | C<Router> port. Returns a hashref in the format | ||
| 56 | C<< { VLAN => { PORT => { MACADDR => COUNT } } } >>. | ||
| 57 | |||
| 58 | =item B<subnets($host, $ssh)> | ||
| 59 | |||
| 60 | Retrieve directly connected IPv4 subnets from every VRF's routing table, | ||
| 61 | without host routes. Returns a list of CIDR strings such as C<10.1.1.0/24>. | ||
| 62 | |||
| 63 | =item B<subnets_by_vrf($host, $ssh, $args)> | ||
| 64 | |||
| 65 | As C<subnets>, used when C<store_vrfs> is on, and reading IPv6 as well. Runs | ||
| 66 | C<show ip route vrf all connected> and C<show ipv6 route vrf all connected> | ||
| 67 | and names each prefix's VRF from the JSON key it appears under (the worker | ||
| 68 | maps C<default> to C<''>). | ||
| 69 | |||
| 70 | Returns C<< { VRF => [ { prefix => '10.1.1.0', len => 24 }, ... ] } >>, or | ||
| 71 | undef when neither command returned usable JSON. A VRF with no connected routes | ||
| 72 | maps to an empty list. Host routes and link-local prefixes are returned as | ||
| 73 | EOS lists them: the worker filters them. A command that fails contributes | ||
| 74 | nothing and the other family's prefixes are still returned. | ||
| 75 | |||
| 76 | A subclass that overrides C<subnets> must also override C<subnets_by_vrf>, or | ||
| 77 | with C<store_vrfs> on this C<subnets_by_vrf> is used instead of the override. | ||
| 78 | |||
| 79 | =back | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | my $if_name_map = { | ||
| 84 | Vl => "Vlan", | ||
| 85 | Lo => "Loopback", | ||
| 86 | Eth => "Ethernet", | ||
| 87 | Po => "Port-channel", | ||
| 88 | }; | ||
| 89 | |||
| 90 | my @ROUTE_COMMANDS = ( | ||
| 91 | { name => 'IPv4 route', command => "show ip route vrf all connected | json | no-more\n" }, | ||
| 92 | { name => 'IPv6 route', command => "show ipv6 route vrf all connected | json | no-more\n" }, | ||
| 93 | ); | ||
| 94 | |||
| 95 | my @NEIGHBOR_FAMILIES = ( | ||
| 96 | { name => 'ARP', family => 4, key => 'ipV4Neighbors', split_iface => 1, | ||
| 97 | command => "show ip arp vrf all | json | no-more\n" }, | ||
| 98 | { name => 'IPv6 neighbor', family => 6, key => 'ipV6Neighbors', split_iface => 0, | ||
| 99 | command => "show ipv6 neighbor vrf all | json | no-more\n" }, | ||
| 100 | ); | ||
| 101 | |||
| 102 | sub arpnip { | ||
| 103 | my ($self, $hostlabel, $ssh, $args) = @_; | ||
| 104 | debug "$hostlabel $$ arpnip()"; | ||
| 105 | |||
| 106 | my @arpentries; | ||
| 107 | foreach my $family (@NEIGHBOR_FAMILIES) { | ||
| 108 | my ($vrfs, $error) = _neighbors_by_vrf($ssh, $family); | ||
| 109 | # arpnip has only ever logged the SSH error, not unparsed JSON | ||
| 110 | info "$hostlabel $$ $error" if $ssh->error; | ||
| 111 | push @arpentries, map { @{ $_->[1] || [] } } @{ $vrfs || [] }; | ||
| 112 | } | ||
| 113 | |||
| 114 | return @arpentries; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub arpnip_by_vrf { | ||
| 118 | my ($self, $hostlabel, $ssh, $args) = @_; | ||
| 119 | debug "$hostlabel $$ arpnip_by_vrf()"; | ||
| 120 | |||
| 121 | my (@entries, %listed, %seen, @families); | ||
| 122 | foreach my $family (@NEIGHBOR_FAMILIES) { | ||
| 123 | my ($vrfs, $error) = _neighbors_by_vrf($ssh, $family, 1); | ||
| 124 | unless ($vrfs) { | ||
| 125 | info "$hostlabel $$ $error, so no VRF is vouched for that family"; | ||
| 126 | next; | ||
| 127 | } | ||
| 128 | push @families, $family->{family}; | ||
| 129 | foreach my $vrf (sort { $a->[0] cmp $b->[0] } @$vrfs) { | ||
| 130 | my $name = netdisco_vrf($vrf->[0]); | ||
| 131 | ++$seen{$name}; | ||
| 132 | next unless $vrf->[1]; | ||
| 133 | ++$listed{$name}; | ||
| 134 | push @entries, map { +{ %$_, vrf => $name } } @{ $vrf->[1] }; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | return undef unless scalar @families; | ||
| 138 | |||
| 139 | my %vrf_read = map { | ||
| 140 | ($_ => (($listed{$_} // 0) == scalar @families) ? 1 : 0) | ||
| 141 | } keys %seen; | ||
| 142 | |||
| 143 | # a command that decoded lists every VRF, so the list is whole for the | ||
| 144 | # families read | ||
| 145 | return { entries => \@entries, vrf_read => \%vrf_read, vrf_list_complete => 1, | ||
| 146 | families => \@families }; | ||
| 147 | } | ||
| 148 | |||
| 149 | # returns [ [VRF name as EOS gives it, [entries]], ... ] in hash order, or | ||
| 150 | # undef and why; a VRF without the family's neighbor list has undef entries | ||
| 151 | sub _neighbors_by_vrf { | ||
| 152 | my ($ssh, $family, $keep_without_iface) = @_; | ||
| 153 | |||
| 154 | my @out = $ssh->capture({ stdin_data => $family->{command} }); | ||
| 155 | return (undef, "error running $family->{name} command: " . $ssh->error) | ||
| 156 | if $ssh->error; | ||
| 157 | |||
| 158 | my $data = eval { decode_json(join '', @out) }; | ||
| 159 | return (undef, "failed to parse $family->{name} JSON") | ||
| 160 | unless $data && $data->{vrfs}; | ||
| 161 | |||
| 162 | my @vrfs; | ||
| 163 | foreach my $name (keys %{ $data->{vrfs} }) { | ||
| 164 | my $neighbors = $data->{vrfs}->{$name}->{ $family->{key} }; | ||
| 165 | push @vrfs, [ $name, ($neighbors ? [ map { _neighbor_entries($family, $_, $keep_without_iface) } @$neighbors ] : undef) ]; | ||
| 166 | } | ||
| 167 | return \@vrfs; | ||
| 168 | } | ||
| 169 | |||
| 170 | # arpnip drops an IPv4 neighbor with no interface, as it always has | ||
| 171 | sub _neighbor_entries { | ||
| 172 | my ($family, $n, $keep_without_iface) = @_; | ||
| 173 | return () unless $n->{address} && $n->{hwAddress}; | ||
| 174 | return { ip => $n->{address}, mac => $n->{hwAddress} } unless $family->{split_iface}; | ||
| 175 | |||
| 176 | # some entries have multiple interfaces: "Vlan3134, Port-Channel46" | ||
| 177 | my @ifaces = split /\s*,\s*/, ($n->{interface} // ''); | ||
| 178 | return { ip => $n->{address}, mac => $n->{hwAddress} } | ||
| 179 | if $keep_without_iface and not grep { $_ } @ifaces; | ||
| 180 | return map { | ||
| 181 | +{ ip => $n->{address}, mac => $n->{hwAddress}, ( $_ ? (iface => $_) : () ) } | ||
| 182 | } @ifaces; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub macsuck { | ||
| 186 | my ($self, $hostlabel, $ssh, $args) = @_; | ||
| 187 | |||
| 188 | unless ($ssh) { | ||
| 189 | info "$hostlabel $$ macsuck() - no SSH session"; | ||
| 190 | return; | ||
| 191 | } | ||
| 192 | |||
| 193 | debug "$hostlabel $$ macsuck()"; | ||
| 194 | |||
| 195 | my $cmd = "show mac address-table | json | no-more\n"; | ||
| 196 | my @out = $ssh->capture({ stdin_data => $cmd }); | ||
| 197 | if ($ssh->error) { | ||
| 198 | info "$hostlabel $$ error running command: " . $ssh->error; | ||
| 199 | return; | ||
| 200 | } | ||
| 201 | |||
| 202 | my $json = join '', @out; | ||
| 203 | my $data = eval { decode_json($json) }; | ||
| 204 | if ($@ or not $data->{unicastTable}->{tableEntries}) { | ||
| 205 | info "$hostlabel $$ failed to parse JSON: $@"; | ||
| 206 | return; | ||
| 207 | } | ||
| 208 | |||
| 209 | my $macentries = {}; | ||
| 210 | |||
| 211 | foreach my $entry (@{ $data->{unicastTable}->{tableEntries} }) { | ||
| 212 | my $vlan = $entry->{vlanId} // 0; | ||
| 213 | my $mac = mac_as_ieee($entry->{macAddress}); | ||
| 214 | my $port = $entry->{interface}; | ||
| 215 | |||
| 216 | # Skip bogus/no-port entries | ||
| 217 | next unless $mac && $port && $port ne 'Router'; | ||
| 218 | |||
| 219 | |||
| 220 | ++$macentries->{$vlan}->{$port}->{$mac}; | ||
| 221 | |||
| 222 | debug sprintf "Parsed MAC vlan=%s mac=%s port=%s type=%s", | ||
| 223 | $vlan, $mac, $port, $entry->{entryType}; | ||
| 224 | } | ||
| 225 | |||
| 226 | return $macentries; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub subnets { | ||
| 230 | my ($self, $hostlabel, $ssh, $args) = @_; | ||
| 231 | debug "$hostlabel $$ subnets()"; | ||
| 232 | |||
| 233 | my $cmd = "show ip route vrf all connected | json | no-more\n"; | ||
| 234 | my @out = $ssh->capture({ stdin_data => $cmd }); | ||
| 235 | if ($ssh->error) { | ||
| 236 | info "$hostlabel $$ error running route command: " . $ssh->error; | ||
| 237 | return; | ||
| 238 | } | ||
| 239 | |||
| 240 | my $data = eval { decode_json(join '', @out) }; | ||
| 241 | if ($@ or not $data->{vrfs}) { | ||
| 242 | info "$hostlabel $$ failed to parse JSON routes: $@"; | ||
| 243 | return; | ||
| 244 | } | ||
| 245 | |||
| 246 | my @subnets; | ||
| 247 | foreach my $vrf (values %{ $data->{vrfs} }) { | ||
| 248 | foreach my $cidr (keys %{ $vrf->{routes} }) { | ||
| 249 | next if $cidr =~ m/\/32$/; # skip host routes | ||
| 250 | push @subnets, $cidr if $cidr =~ m{^(\d{1,3}\.){3}\d{1,3}/\d{1,2}$}; | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | return @subnets; | ||
| 255 | } | ||
| 256 | |||
| 257 | sub subnets_by_vrf { | ||
| 258 | my ($self, $hostlabel, $ssh, $args) = @_; | ||
| 259 | debug "$hostlabel $$ subnets_by_vrf()"; | ||
| 260 | |||
| 261 | my (%subnets, $read_any); | ||
| 262 | foreach my $route (@ROUTE_COMMANDS) { | ||
| 263 | my @out = $ssh->capture({ stdin_data => $route->{command} }); | ||
| 264 | if ($ssh->error) { | ||
| 265 | info "$hostlabel $$ error running $route->{name} command: " . $ssh->error; | ||
| 266 | next; | ||
| 267 | } | ||
| 268 | |||
| 269 | my $data = eval { decode_json(join '', @out) }; | ||
| 270 | unless ($data and ref {} eq ref $data and ref {} eq ref $data->{vrfs}) { | ||
| 271 | info "$hostlabel $$ failed to parse $route->{name} JSON"; | ||
| 272 | next; | ||
| 273 | } | ||
| 274 | |||
| 275 | $read_any = 1; | ||
| 276 | foreach my $vrf (keys %{ $data->{vrfs} }) { | ||
| 277 | unless (length $vrf) { | ||
| 278 | info "$hostlabel $$ $route->{name} table has a VRF with an empty name, so it contributes nothing"; | ||
| 279 | next; | ||
| 280 | } | ||
| 281 | my $table = $data->{vrfs}->{$vrf}; | ||
| 282 | my $routes = (ref {} eq ref $table) ? ($table->{routes} || {}) : undef; | ||
| 283 | unless (ref {} eq ref $routes) { | ||
| 284 | info "$hostlabel $$ $route->{name} table for VRF $vrf is not a hash, so it contributes nothing"; | ||
| 285 | next; | ||
| 286 | } | ||
| 287 | push @{ $subnets{$vrf} ||= [] }, map { | ||
| 288 | my ($prefix, $len) = m{^(.+)/(\d+)$} ? ($1, $2) : ($_, undef); | ||
| 289 | +{ prefix => $prefix, len => $len } | ||
| 290 | } sort keys %$routes; | ||
| 291 | } | ||
| 292 | } | ||
| 293 | |||
| 294 | return $read_any ? \%subnets : undef; | ||
| 295 | } | ||
| 296 | |||
| 297 | 1; |