| 1 | package App::Netdisco::DB::ResultSet::DeviceIp; | ||
| 2 | use base 'App::Netdisco::DB::ResultSet'; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use NetAddr::IP::Lite; | ||
| 8 | |||
| 9 | my $AGE = q/replace( date_trunc( 'minute', age( LOCALTIMESTAMP, %s ) ) ::text, 'mon', 'month') AS age/; | ||
| 10 | |||
| 11 | # looked up only for the rows returned, not for every candidate row | ||
| 12 | my $VENDOR = q{COALESCE(me.vendor, CASE WHEN me.node THEN (SELECT m.company FROM manufacturer m} | ||
| 13 | . q{ WHERE m.range @> ('x' || lpad(translate(me.mac::text, ':', ''), 16, '0'))::bit(64)::bigint LIMIT 1) END)}; | ||
| 14 | |||
| 15 | my $DOMAIN = q/CASE WHEN me.vrf = '' THEN 0 ELSE l3_domain_of(%s, me.vrf) END AS domain/; | ||
| 16 | my $DOMAIN_NAME = q{(SELECT ld.name FROM l3_domain ld WHERE ld.id = me.domain) AS vrf}; | ||
| 17 | |||
| 18 | =head1 ADDITIONAL METHODS | ||
| 19 | |||
| 20 | =head2 resolve_alias( $ip ) | ||
| 21 | |||
| 22 | Returns the IP of the device that owns the address C<$ip>, or C<undef>. A | ||
| 23 | device's own address wins in any VRF; otherwise the C<device_ip_resolved> view | ||
| 24 | decides. An address held in two VRFs by different devices resolves to no device | ||
| 25 | when no default-table row and no own-address row claims it. | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | sub resolve_alias { | ||
| 30 | my ($rs, $ip) = @_; | ||
| 31 | my $own = $rs->search({ ip => $ip, alias => $ip }, { rows => 1 })->first; | ||
| 32 | return $own->ip if defined $own; | ||
| 33 | |||
| 34 | my $row = $rs->result_source->schema->resultset('DeviceIpResolved') | ||
| 35 | ->search({ alias => $ip }, { rows => 1 })->first; | ||
| 36 | return defined $row ? $row->ip : undef; | ||
| 37 | } | ||
| 38 | |||
| 39 | =head2 ip_inventory( $subnet, \%opts ) | ||
| 40 | |||
| 41 | Returns the IP Inventory report rows for C<$subnet> (a L<NetAddr::IP::Lite>): | ||
| 42 | device interface addresses, node ARP and IPv6 neighbor cache entries, and | ||
| 43 | NetBIOS entries, one row per IP in each L3 domain, plus every unused address in | ||
| 44 | the subnet when C<never> is set. Each row's C<vrf> is its domain's name; NetBIOS | ||
| 45 | entries are in the default domain, an unused address is in the domain of each | ||
| 46 | stored subnet holding it (the default domain when none does), and a row whose | ||
| 47 | VRF name gives no single domain is left out. The query starts from C<device_ip> | ||
| 48 | and unions the node tables onto it, which is why it lives in this class. | ||
| 49 | |||
| 50 | Options: C<never>, C<age_invert>, C<limit>, and C<start> with C<end> (timestamps | ||
| 51 | as C<YYYY-MM-DD HH:MM:SS>). For an IP with several rows the device's own | ||
| 52 | active address wins, then an active node, then an inactive node, then a | ||
| 53 | generated never-seen address; among those, the most recently seen wins. | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | # the domain-aware never-seen form costs about twice the plain one on a /16, | ||
| 58 | # so a prefix no named-VRF subnet touches (every prefix with the switch off) | ||
| 59 | # keeps the plain form | ||
| 60 | sub _named_vrf_subnet_overlaps { | ||
| 61 | my ($schema, $subnet) = @_; | ||
| 62 | return $schema->resultset('Subnet')->search({ | ||
| 63 | vrf => { '!=' => '' }, | ||
| 64 | net => { '&&' => $subnet->cidr }, | ||
| 65 | }, { rows => 1 })->count; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub ip_inventory { | ||
| 69 | my ($rs, $subnet, $opts) = @_; | ||
| 70 | $opts ||= {}; | ||
| 71 | my $schema = $rs->result_source->schema; | ||
| 72 | |||
| 73 | my $devices = $rs->search_rs(undef, { | ||
| 74 | join => ['device', 'device_port'], | ||
| 75 | select => [ | ||
| 76 | 'alias AS ip', 'device_port.mac as mac', 'creation AS time_first', | ||
| 77 | 'device.last_discover AS time_last', 'dns', \'true AS active', \'false AS node', | ||
| 78 | \(sprintf $AGE, 'device.last_discover'), 'device.vendor', \'null AS nbname', | ||
| 79 | \(sprintf $DOMAIN, 'me.ip'), | ||
| 80 | ], | ||
| 81 | as => [qw( ip mac time_first time_last dns active node age vendor nbname domain )], | ||
| 82 | })->hri; | ||
| 83 | |||
| 84 | my $node_ips = $schema->resultset('NodeIp')->search_rs(undef, { | ||
| 85 | join => ['netbios'], | ||
| 86 | columns => [qw( ip mac time_first time_last dns active )], | ||
| 87 | '+select' => [ \'true AS node', \(sprintf $AGE, 'me.time_last'), \'NULL AS vendor', 'netbios.nbname', | ||
| 88 | \(sprintf $DOMAIN, 'me.router_last') ], | ||
| 89 | '+as' => [qw( node age vendor nbname domain )], | ||
| 90 | })->hri; | ||
| 91 | |||
| 92 | my $netbios = $schema->resultset('NodeNbt')->search_rs(undef, { | ||
| 93 | columns => [qw( ip mac time_first time_last )], | ||
| 94 | '+select' => [ \'null AS dns', 'active', \'true AS node', \(sprintf $AGE, 'time_last'), \'NULL AS vendor', 'nbname', | ||
| 95 | \'0 AS domain' ], | ||
| 96 | '+as' => [qw( dns active node age vendor nbname domain )], | ||
| 97 | })->hri; | ||
| 98 | |||
| 99 | my $union = $devices->union_all([ $node_ips, $netbios ]); | ||
| 100 | |||
| 101 | if ($opts->{never}) { | ||
| 102 | $subnet = NetAddr::IP::Lite->new('0.0.0.0/32') if ($subnet->bits ne 32); | ||
| 103 | my $unused = _named_vrf_subnet_overlaps($schema, $subnet) | ||
| 104 | ? $schema->resultset('Virtual::CidrIpsByDomain')->search_rs(undef, { | ||
| 105 | bind => [ $subnet->cidr ], | ||
| 106 | columns => [qw( ip mac time_first time_last dns active node age vendor nbname domain )], | ||
| 107 | })->hri | ||
| 108 | : $schema->resultset('Virtual::CidrIps')->search_rs(undef, { | ||
| 109 | bind => [ $subnet->cidr ], | ||
| 110 | columns => [qw( ip mac time_first time_last dns active node age vendor nbname )], | ||
| 111 | '+select' => [ \'0 AS domain' ], | ||
| 112 | '+as' => [ 'domain' ], | ||
| 113 | })->hri; | ||
| 114 | $union = $union->union_all([ $unused ]); | ||
| 115 | } | ||
| 116 | |||
| 117 | my $one_per_ip = $union->search_rs({ ip => { '<<' => $subnet->cidr }, domain => { '!=' => undef } }, { | ||
| 118 | select => [ | ||
| 119 | \'DISTINCT ON (ip, domain) domain', 'ip', 'mac', 'dns', | ||
| 120 | \q/date_trunc('second', time_last) AS time_last/, | ||
| 121 | \q/date_trunc('second', time_first) AS time_first/, | ||
| 122 | 'active', 'node', 'age', 'vendor', 'nbname', | ||
| 123 | ], | ||
| 124 | as => [qw( domain ip mac dns time_last time_first active node age vendor nbname )], | ||
| 125 | # a generated never-seen row (active and node both false) must lose to any | ||
| 126 | # real binding, even an inactive one, or a seen address reads as never seen | ||
| 127 | order_by => [ {-asc => 'ip'}, {-asc => 'domain'}, | ||
| 128 | \'CASE WHEN active AND NOT node THEN 0 WHEN active THEN 1 WHEN node THEN 2 ELSE 3 END', | ||
| 129 | \'time_last DESC NULLS LAST', {-asc => 'dns'}, {-asc => 'mac'} ], | ||
| 130 | })->as_query; | ||
| 131 | |||
| 132 | my $when = undef; | ||
| 133 | if ($opts->{start} and $opts->{end}) { | ||
| 134 | $when = $opts->{age_invert} | ||
| 135 | ? { -or => [ time_first => [ undef ], | ||
| 136 | time_last => [ { '<', $opts->{start} }, { '>', $opts->{end} } ] ] } | ||
| 137 | : { -or => [ -and => [ time_first => undef, time_last => undef ], | ||
| 138 | -and => [ time_last => { '>=', $opts->{start} }, | ||
| 139 | time_last => { '<=', $opts->{end} } ] ] }; | ||
| 140 | } | ||
| 141 | |||
| 142 | my $order = [ {-desc => 'age'}, {-asc => 'ip'}, {-asc => 'domain'} ]; | ||
| 143 | my $limited = $union->search_rs($when, { from => { me => $one_per_ip } }) | ||
| 144 | ->order_by($order)->limit($opts->{limit} || 256); | ||
| 145 | |||
| 146 | return $union->search_rs(undef, { | ||
| 147 | from => { me => $limited->as_query }, | ||
| 148 | columns => [qw( ip mac time_first time_last dns active node age nbname )], | ||
| 149 | '+select' => [ \$VENDOR, \$DOMAIN_NAME ], | ||
| 150 | '+as' => [ 'vendor', 'vrf' ], | ||
| 151 | order_by => $order, | ||
| 152 | })->hri; | ||
| 153 | } | ||
| 154 | |||
| 155 | 1; |