| 1 | use utf8; | ||
| 2 | package App::Netdisco::DB::Result::NodeIp; | ||
| 3 | |||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use NetAddr::MAC; | ||
| 9 | use App::Netdisco::Util::L3Domain 'row_l3_domain'; | ||
| 10 | |||
| 11 | use base 'App::Netdisco::DB::Result'; | ||
| 12 | __PACKAGE__->table("node_ip"); | ||
| 13 | __PACKAGE__->add_columns( | ||
| 14 | "mac", | ||
| 15 | { data_type => "macaddr", is_nullable => 0 }, | ||
| 16 | "ip", | ||
| 17 | { data_type => "inet", is_nullable => 0 }, | ||
| 18 | "dns", | ||
| 19 | { data_type => "text", is_nullable => 1 }, | ||
| 20 | "active", | ||
| 21 | { data_type => "boolean", is_nullable => 1 }, | ||
| 22 | "time_first", | ||
| 23 | { | ||
| 24 | data_type => "timestamp", | ||
| 25 | default_value => \"LOCALTIMESTAMP", | ||
| 26 | is_nullable => 1, | ||
| 27 | original => { default_value => \"LOCALTIMESTAMP" }, | ||
| 28 | }, | ||
| 29 | "time_last", | ||
| 30 | { | ||
| 31 | data_type => "timestamp", | ||
| 32 | default_value => \"LOCALTIMESTAMP", | ||
| 33 | is_nullable => 1, | ||
| 34 | original => { default_value => \"LOCALTIMESTAMP" }, | ||
| 35 | }, | ||
| 36 | "seen_on_router_first", | ||
| 37 | { data_type => "jsonb", is_nullable => 0, default_value => \"{}" }, | ||
| 38 | "seen_on_router_last", | ||
| 39 | { data_type => "jsonb", is_nullable => 0, default_value => \"{}" }, | ||
| 40 | "vrf", | ||
| 41 | { data_type => "text", is_nullable => 0, default => '' }, | ||
| 42 | "router_last", | ||
| 43 | { data_type => "inet", is_nullable => 1 }, | ||
| 44 | ); | ||
| 45 | __PACKAGE__->set_primary_key("mac", "ip", "vrf"); | ||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | =head1 RELATIONSHIPS | ||
| 50 | |||
| 51 | =head2 oui | ||
| 52 | |||
| 53 | DEPRECATED: USE MANUFACTURER INSTEAD | ||
| 54 | |||
| 55 | Returns the C<oui> table entry matching this Node. You can then join on this | ||
| 56 | relation and retrieve the Company name from the related table. | ||
| 57 | |||
| 58 | The JOIN is of type LEFT, in case the OUI table has not been populated. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | __PACKAGE__->belongs_to( oui => 'App::Netdisco::DB::Result::Oui', | ||
| 63 | sub { | ||
| 64 | my $args = shift; | ||
| 65 | return { | ||
| 66 | "$args->{foreign_alias}.oui" => | ||
| 67 | { '=' => \"substring(cast($args->{self_alias}.mac as varchar) for 8)" } | ||
| 68 | }; | ||
| 69 | }, | ||
| 70 | { join_type => 'LEFT' } | ||
| 71 | ); | ||
| 72 | |||
| 73 | =head2 manufacturer | ||
| 74 | |||
| 75 | Returns the C<manufacturer> table entry matching this Node. You can then join on this | ||
| 76 | relation and retrieve the Company name from the related table. | ||
| 77 | |||
| 78 | The JOIN is of type LEFT, in case the Manufacturer table has not been populated. | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | __PACKAGE__->belongs_to( manufacturer => 'App::Netdisco::DB::Result::Manufacturer', | ||
| 83 | sub { | ||
| 84 | my $args = shift; | ||
| 85 | return { | ||
| 86 | "$args->{foreign_alias}.range" => { '@>' => | ||
| 87 | \qq{('x' || lpad( translate( $args->{self_alias}.mac ::text, ':', ''), 16, '0')) ::bit(64) ::bigint} }, | ||
| 88 | }; | ||
| 89 | }, | ||
| 90 | { join_type => 'LEFT' } | ||
| 91 | ); | ||
| 92 | |||
| 93 | =head2 router | ||
| 94 | |||
| 95 | Returns the C<device> table entry matching this Node's router. You can then join on | ||
| 96 | this relation and retrieve the Device DNS name. | ||
| 97 | |||
| 98 | The JOIN is of type LEFT, in case there's no recorded router on this record. | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | __PACKAGE__->belongs_to( router => 'App::Netdisco::DB::Result::Device', | ||
| 103 | sub { | ||
| 104 | my $args = shift; | ||
| 105 | return { | ||
| 106 | "host($args->{foreign_alias}.ip)" => { '=' => | ||
| 107 | \q{(SELECT key FROM json_each_text(seen_on_router_last::json) ORDER BY value::timestamp DESC LIMIT 1)} }, | ||
| 108 | }; | ||
| 109 | }, | ||
| 110 | { join_type => 'LEFT' } | ||
| 111 | ); | ||
| 112 | |||
| 113 | =head2 node_ips | ||
| 114 | |||
| 115 | Returns the set of all C<node_ip> entries which are associated together with | ||
| 116 | this IP. That is, all the IP addresses hosted on the same interface (MAC | ||
| 117 | address) as the current Node IP entry. | ||
| 118 | |||
| 119 | Note that the set will include the original Node IP object itself. If you wish | ||
| 120 | to find the I<other> IPs excluding this one, see the C<ip_aliases> helper | ||
| 121 | routine, below. | ||
| 122 | |||
| 123 | Remember you can pass a filter to this method to find only active or inactive | ||
| 124 | nodes, but do take into account that both the C<node> and C<node_ip> tables | ||
| 125 | include independent C<active> fields. | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | __PACKAGE__->has_many( node_ips => 'App::Netdisco::DB::Result::NodeIp', | ||
| 130 | { 'foreign.mac' => 'self.mac' } ); | ||
| 131 | |||
| 132 | =head2 nodes | ||
| 133 | |||
| 134 | Returns the set of C<node> entries associated with this IP. That is, all the | ||
| 135 | MAC addresses recorded which have ever hosted this IP Address. | ||
| 136 | |||
| 137 | Remember you can pass a filter to this method to find only active or inactive | ||
| 138 | nodes, but do take into account that both the C<node> and C<node_ip> tables | ||
| 139 | include independent C<active> fields. | ||
| 140 | |||
| 141 | See also the C<node_sightings> helper routine, below. | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | __PACKAGE__->has_many( nodes => 'App::Netdisco::DB::Result::Node', | ||
| 146 | { 'foreign.mac' => 'self.mac' }, { order_by => { '-desc' => 'time_last' }} ); | ||
| 147 | |||
| 148 | =head2 netbios | ||
| 149 | |||
| 150 | Returns the set of C<node_nbt> entries associated with the MAC of this IP. | ||
| 151 | That is, all the NetBIOS entries recorded which shared the same MAC with this | ||
| 152 | IP Address. | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | __PACKAGE__->has_many( netbios => 'App::Netdisco::DB::Result::NodeNbt', | ||
| 157 | { 'foreign.mac' => 'self.mac' } ); | ||
| 158 | |||
| 159 | my $search_attr = { | ||
| 160 | order_by => {'-desc' => 'time_last'}, | ||
| 161 | '+columns' => { | ||
| 162 | time_first_stamp => \"to_char(time_first, 'YYYY-MM-DD HH24:MI')", | ||
| 163 | time_last_stamp => \"to_char(time_last, 'YYYY-MM-DD HH24:MI')", | ||
| 164 | }, | ||
| 165 | }; | ||
| 166 | |||
| 167 | =head2 ip_aliases( \%cond, \%attrs? ) | ||
| 168 | |||
| 169 | Returns the set of other C<node_ip> entries hosted on the same interface (MAC | ||
| 170 | address) as the current Node IP, excluding the current IP itself. | ||
| 171 | |||
| 172 | Remember you can pass a filter to this method to find only active or inactive | ||
| 173 | nodes, but do take into account that both the C<node> and C<node_ip> tables | ||
| 174 | include independent C<active> fields. | ||
| 175 | |||
| 176 | =over 4 | ||
| 177 | |||
| 178 | =item * | ||
| 179 | |||
| 180 | Results are ordered by time last seen. | ||
| 181 | |||
| 182 | =item * | ||
| 183 | |||
| 184 | Additional columns C<time_first_stamp> and C<time_last_stamp> provide | ||
| 185 | preformatted timestamps of the C<time_first> and C<time_last> fields. | ||
| 186 | |||
| 187 | =back | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | sub ip_aliases { | ||
| 192 | my ($row, $cond, $attrs) = @_; | ||
| 193 | |||
| 194 | my $rs = $row->node_ips({ip => { '!=' => $row->ip }}); | ||
| 195 | |||
| 196 | return $rs | ||
| 197 | ->search_rs({}, $search_attr) | ||
| 198 | ->search($cond, $attrs); | ||
| 199 | } | ||
| 200 | |||
| 201 | =head2 node_sightings( \%cond, \%attrs? ) | ||
| 202 | |||
| 203 | Returns the set of C<node> entries associated with this IP. That is, all the | ||
| 204 | MAC addresses recorded which have ever hosted this IP Address. | ||
| 205 | |||
| 206 | Remember you can pass a filter to this method to find only active or inactive | ||
| 207 | nodes, but do take into account that both the C<node> and C<node_ip> tables | ||
| 208 | include independent C<active> fields. | ||
| 209 | |||
| 210 | =over 4 | ||
| 211 | |||
| 212 | =item * | ||
| 213 | |||
| 214 | Results are ordered by time last seen. | ||
| 215 | |||
| 216 | =item * | ||
| 217 | |||
| 218 | Additional columns C<time_first_stamp> and C<time_last_stamp> provide | ||
| 219 | preformatted timestamps of the C<time_first> and C<time_last> fields. | ||
| 220 | |||
| 221 | =item * | ||
| 222 | |||
| 223 | A JOIN is performed on the Device table and the Device DNS column prefetched. | ||
| 224 | |||
| 225 | =back | ||
| 226 | |||
| 227 | =cut | ||
| 228 | |||
| 229 | sub node_sightings { | ||
| 230 | my ($row, $cond, $attrs) = @_; | ||
| 231 | |||
| 232 | return $row | ||
| 233 | ->nodes({}, { | ||
| 234 | '+columns' => [qw/ device.dns device.name /], | ||
| 235 | join => 'device', | ||
| 236 | }) | ||
| 237 | ->search_rs({}, $search_attr) | ||
| 238 | ->search($cond, $attrs); | ||
| 239 | } | ||
| 240 | |||
| 241 | =head1 ADDITIONAL COLUMNS | ||
| 242 | |||
| 243 | =head2 time_first_stamp | ||
| 244 | |||
| 245 | Formatted version of the C<time_first> field, accurate to the minute. | ||
| 246 | |||
| 247 | The format is somewhat like ISO 8601 or RFC3339 but without the middle C<T> | ||
| 248 | between the date stamp and time stamp. That is: | ||
| 249 | |||
| 250 | 2012-02-06 12:49 | ||
| 251 | |||
| 252 | =cut | ||
| 253 | |||
| 254 | sub time_first_stamp { return (shift)->get_column('time_first_stamp') } | ||
| 255 | |||
| 256 | =head2 time_last_stamp | ||
| 257 | |||
| 258 | Formatted version of the C<time_last> field, accurate to the minute. | ||
| 259 | |||
| 260 | The format is somewhat like ISO 8601 or RFC3339 but without the middle C<T> | ||
| 261 | between the date stamp and time stamp. That is: | ||
| 262 | |||
| 263 | 2012-02-06 12:49 | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | sub time_last_stamp { return (shift)->get_column('time_last_stamp') } | ||
| 268 | |||
| 269 | =head2 router_ip | ||
| 270 | |||
| 271 | Returns the router IP that most recently reported this MAC-IP pair. | ||
| 272 | |||
| 273 | =cut | ||
| 274 | |||
| 275 | sub router_ip { return (shift)->get_column('router_ip') } | ||
| 276 | |||
| 277 | =head2 router_name | ||
| 278 | |||
| 279 | Returns the router DNS or SysName that most recently reported this MAC-IP pair. | ||
| 280 | |||
| 281 | May be blank if there's no SysName or DNS name, so you have C<router_ip> as well. | ||
| 282 | |||
| 283 | =cut | ||
| 284 | |||
| 285 | sub router_name { return (shift)->get_column('router_name') } | ||
| 286 | |||
| 287 | =head2 net_mac | ||
| 288 | |||
| 289 | Returns the C<mac> column instantiated into a L<NetAddr::MAC> object. | ||
| 290 | |||
| 291 | =cut | ||
| 292 | |||
| 293 | sub net_mac { return NetAddr::MAC->new(mac => ((shift)->mac || '')) } | ||
| 294 | |||
| 295 | |||
| 296 | =head2 l3_domain | ||
| 297 | |||
| 298 | Returns C<< { id, name } >> of the L3 domain this row's VRF belongs to, or undef | ||
| 299 | when it cannot be determined. A default-table row is domain 0 without a query. | ||
| 300 | |||
| 301 | =cut | ||
| 302 | |||
| 303 | sub l3_domain { return row_l3_domain($_[0], ($_[0]->has_column_loaded('router_last') ? $_[0]->get_column('router_last') : undef)) } | ||
| 304 | |||
| 305 | sub TO_JSON { | ||
| 306 | my $self = shift; | ||
| 307 | my $data = $self->next::method(@_); | ||
| 308 | $data->{l3_domain} = $self->l3_domain; | ||
| 309 | return $data; | ||
| 310 | } | ||
| 311 | |||
| 312 | 1; |