« blib/lib/App/Netdisco/DB/Result/Virtual/CidrIps.pm
↑ Summary
blib/lib/App/Netdisco/DB/Result/Virtual/DeviceDnsMismatch.pm »
| 1 | package App::Netdisco::DB::Result::Virtual::CidrIpsByDomain; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use utf8; | ||
| 7 | use base 'DBIx::Class::Core'; | ||
| 8 | |||
| 9 | __PACKAGE__->table_class('DBIx::Class::ResultSource::View'); | ||
| 10 | |||
| 11 | __PACKAGE__->table('cidr_ips_by_domain'); | ||
| 12 | __PACKAGE__->result_source_instance->is_virtual(1); | ||
| 13 | __PACKAGE__->result_source_instance->view_definition(<<'ENDSQL'); | ||
| 14 | WITH addr AS ( | ||
| 15 | SELECT ?::inet AS prefix | ||
| 16 | ), | ||
| 17 | -- one row per stored subnet overlapping the prefix, as the host offsets it | ||
| 18 | -- covers there, so l3_domain_of runs per subnet, not per generated address; | ||
| 19 | -- DISTINCT only folds subnets clamped to the same range and domain. | ||
| 20 | -- LEAST compares offsets: inets of two lengths order by masked network first | ||
| 21 | cand AS ( | ||
| 22 | SELECT DISTINCT | ||
| 23 | GREATEST(network(s.net) - network(a.prefix), 1) AS lo, | ||
| 24 | LEAST(broadcast(s.net) - network(a.prefix), broadcast(a.prefix) - network(a.prefix) - 1) AS hi, | ||
| 25 | CASE WHEN s.vrf = '' THEN 0 ELSE l3_domain_of(s.router_last, s.vrf) END AS domain | ||
| 26 | FROM subnets s, addr a | ||
| 27 | WHERE s.net && a.prefix | ||
| 28 | ), | ||
| 29 | -- a NULL (ambiguous) domain holds nothing, so it adds no domain 0 row | ||
| 30 | held AS ( | ||
| 31 | SELECT DISTINCT generate_series(lo, hi) AS int, domain | ||
| 32 | FROM cand | ||
| 33 | WHERE domain IS NOT NULL | ||
| 34 | ) | ||
| 35 | SELECT host(network(a.prefix) + o.int)::inet AS ip, | ||
| 36 | NULL::macaddr AS mac, | ||
| 37 | NULL::timestamp AS time_first, | ||
| 38 | NULL::timestamp AS time_last, | ||
| 39 | NULL::text AS dns, | ||
| 40 | false::boolean AS active, | ||
| 41 | false::boolean AS node, | ||
| 42 | replace( date_trunc( 'minute', age( LOCALTIMESTAMP, NULL::timestamp ) ) ::text, 'mon', 'month') AS age, | ||
| 43 | NULL::text AS vendor, | ||
| 44 | NULL::text AS nbname, | ||
| 45 | o.domain | ||
| 46 | FROM addr a, ( | ||
| 47 | SELECT int, domain FROM held | ||
| 48 | UNION ALL | ||
| 49 | SELECT g.int, 0 FROM addr, generate_series(1, (broadcast(prefix) - network(prefix) - 1)) AS g(int) | ||
| 50 | WHERE NOT EXISTS (SELECT 1 FROM held h WHERE h.int = g.int) | ||
| 51 | ) AS o | ||
| 52 | ENDSQL | ||
| 53 | |||
| 54 | __PACKAGE__->add_columns( | ||
| 55 | "ip", | ||
| 56 | { data_type => "inet", is_nullable => 0 }, | ||
| 57 | "mac", | ||
| 58 | { data_type => "macaddr", is_nullable => 1 }, | ||
| 59 | "time_first", | ||
| 60 | { | ||
| 61 | data_type => "timestamp", | ||
| 62 | is_nullable => 1, | ||
| 63 | }, | ||
| 64 | "time_last", | ||
| 65 | { | ||
| 66 | data_type => "timestamp", | ||
| 67 | is_nullable => 1, | ||
| 68 | }, | ||
| 69 | "dns", | ||
| 70 | { data_type => "text", is_nullable => 1 }, | ||
| 71 | "active", | ||
| 72 | { data_type => "boolean", is_nullable => 1 }, | ||
| 73 | "node", | ||
| 74 | { data_type => "boolean", is_nullable => 1 }, | ||
| 75 | "age", | ||
| 76 | { data_type => "text", is_nullable => 1 }, | ||
| 77 | "vendor", | ||
| 78 | { data_type => "text", is_nullable => 1 }, | ||
| 79 | "nbname", | ||
| 80 | { data_type => "text", is_nullable => 1 }, | ||
| 81 | "domain", | ||
| 82 | { data_type => "integer", is_nullable => 0 }, | ||
| 83 | ); | ||
| 84 | |||
| 85 | 1; |