« blib/lib/App/Netdisco/Worker/Plugin/Macsuck/Nodes.pm
↑ Summary
blib/lib/App/Netdisco/Worker/Runner.pm »
| 1 | package App::Netdisco::DB::ResultSet::Device; | ||
| 2 | use base 'App::Netdisco::DB::ResultSet'; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Try::Tiny; | ||
| 8 | use Regexp::Common 'net'; | ||
| 9 | use NetAddr::IP::Lite ':lower'; | ||
| 10 | use NetAddr::MAC (); | ||
| 11 | use App::Netdisco::Util::L3Domain 'with_l3_domain_lock'; | ||
| 12 | |||
| 13 | # a device by its own address or an alias; the device half covers devices | ||
| 14 | # with no row for their own address in device_ip | ||
| 15 | sub _own_or_alias { | ||
| 16 | my $cidr = shift; | ||
| 17 | return \[ 'SELECT ip FROM device WHERE ip <<= ? UNION SELECT ip FROM device_ip WHERE alias <<= ?', | ||
| 18 | $cidr, $cidr ]; | ||
| 19 | } | ||
| 20 | |||
| 21 | require Dancer::Logger; | ||
| 22 | |||
| 23 | =head1 ADDITIONAL METHODS | ||
| 24 | |||
| 25 | =head2 device_ips_with_address_or_name( $address_or_name ) | ||
| 26 | |||
| 27 | Returns a correlated subquery for the set of C<device_ip> entries for each | ||
| 28 | device. The IP alias or dns matches the supplied C<address_or_name>, using | ||
| 29 | C<ILIKE>. | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | sub device_ips_with_address_or_name { | ||
| 34 | my ($rs, $q, $ipbind) = @_; | ||
| 35 | $q ||= '255.255.255.255/32'; | ||
| 36 | |||
| 37 | return $rs->search(undef,{ | ||
| 38 | # NOTE: bind param list order is significant | ||
| 39 | join => ['device_ips_by_address_or_name'], | ||
| 40 | bind => [$q, $ipbind, $q], | ||
| 41 | }); | ||
| 42 | } | ||
| 43 | |||
| 44 | =head2 with_module_serials | ||
| 45 | |||
| 46 | Adds the C<module_serials.serial> field to the results using | ||
| 47 | C<module_serials> relation. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | sub with_module_serials { | ||
| 52 | my $rs = shift; | ||
| 53 | return $rs->search(undef, { | ||
| 54 | join => 'module_serials', | ||
| 55 | '+columns' => [ qw/ module_serials.ip module_serials.index module_serials.serial / ], | ||
| 56 | collapse => 1, | ||
| 57 | distinct => 0, | ||
| 58 | }); | ||
| 59 | } | ||
| 60 | |||
| 61 | =head2 ports_with_mac( $mac ) | ||
| 62 | |||
| 63 | Returns a correlated subquery for the set of C<device_port> entries for each | ||
| 64 | device. The port MAC address matches the supplied C<mac>, using C<ILIKE>. | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub ports_with_mac { | ||
| 69 | my ($rs, $mac) = @_; | ||
| 70 | $mac ||= '00:00:00:00:00:00'; | ||
| 71 | |||
| 72 | return $rs->search(undef,{ | ||
| 73 | # NOTE: bind param list order is significant | ||
| 74 | join => ['ports_by_mac'], | ||
| 75 | bind => [$mac], | ||
| 76 | }); | ||
| 77 | } | ||
| 78 | |||
| 79 | =head2 ports_with_exact_mac( $mac ) | ||
| 80 | |||
| 81 | Like C<ports_with_mac>, for one complete MAC address in IEEE form, compared by | ||
| 82 | equality rather than as text. | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub ports_with_exact_mac { | ||
| 87 | my ($rs, $mac) = @_; | ||
| 88 | |||
| 89 | return $rs->search(undef,{ | ||
| 90 | # NOTE: bind param list order is significant | ||
| 91 | join => ['ports_by_exact_mac'], | ||
| 92 | bind => [$mac], | ||
| 93 | }); | ||
| 94 | } | ||
| 95 | |||
| 96 | =head2 with_times | ||
| 97 | |||
| 98 | This is a modifier for any C<search()> (including the helpers below) which | ||
| 99 | will add the following additional synthesized columns to the result set: | ||
| 100 | |||
| 101 | =over 4 | ||
| 102 | |||
| 103 | =item uptime_age | ||
| 104 | |||
| 105 | =item first_seen_stamp | ||
| 106 | |||
| 107 | =item last_discover_stamp | ||
| 108 | |||
| 109 | =item last_macsuck_stamp | ||
| 110 | |||
| 111 | =item last_arpnip_stamp | ||
| 112 | |||
| 113 | =item since_first_seen | ||
| 114 | |||
| 115 | =item since_last_discover | ||
| 116 | |||
| 117 | =item since_last_macsuck | ||
| 118 | |||
| 119 | =item since_last_arpnip | ||
| 120 | |||
| 121 | =back | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | sub with_times { | ||
| 126 | my ($rs, $cond, $attrs) = @_; | ||
| 127 | |||
| 128 | return $rs | ||
| 129 | ->search_rs($cond, $attrs) | ||
| 130 | ->search({}, | ||
| 131 | { | ||
| 132 | '+columns' => { | ||
| 133 | uptime_age => \("replace(age(timestamp 'epoch' + me.uptime / 100 * interval '1 second', " | ||
| 134 | ."timestamp '1970-01-01 00:00:00-00')::text, 'mon', 'month')"), | ||
| 135 | first_seen_stamp => \"to_char(me.creation, 'YYYY-MM-DD HH24:MI')", | ||
| 136 | last_discover_stamp => \"to_char(me.last_discover, 'YYYY-MM-DD HH24:MI')", | ||
| 137 | last_macsuck_stamp => \"to_char(me.last_macsuck, 'YYYY-MM-DD HH24:MI')", | ||
| 138 | last_arpnip_stamp => \"to_char(me.last_arpnip, 'YYYY-MM-DD HH24:MI')", | ||
| 139 | since_first_seen => \"extract(epoch from (age(LOCALTIMESTAMP, me.creation)))", | ||
| 140 | since_last_discover => \"extract(epoch from (age(LOCALTIMESTAMP, me.last_discover)))", | ||
| 141 | since_last_macsuck => \"extract(epoch from (age(LOCALTIMESTAMP, me.last_macsuck)))", | ||
| 142 | since_last_arpnip => \"extract(epoch from (age(LOCALTIMESTAMP, me.last_arpnip)))", | ||
| 143 | }, | ||
| 144 | }); | ||
| 145 | } | ||
| 146 | |||
| 147 | =head2 search_aliases( {$name or $ip or $prefix}, \%options? ) | ||
| 148 | |||
| 149 | Tries to find devices in Netdisco which have an identity corresponding to | ||
| 150 | C<$name>, C<$ip> or C<$prefix>. | ||
| 151 | |||
| 152 | The search is across all aliases of the device, as well as its "root IP" | ||
| 153 | identity. Note that this search will try B<not> to use DNS, in case the current | ||
| 154 | name for an IP does not correspond to the data within Netdisco. | ||
| 155 | |||
| 156 | Passing a zero value to the C<partial> key of the C<options> hashref will | ||
| 157 | prevent partial matching of a host name. Otherwise the default is to perform | ||
| 158 | a partial, case-insensitive search on the host name fields. | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | sub search_aliases { | ||
| 163 | my ($rs, $q, $options) = @_; | ||
| 164 | $q ||= '255.255.255.255'; # hack to return empty resultset on error | ||
| 165 | $options ||= {}; | ||
| 166 | $options->{partial} = 1 if !defined $options->{partial}; | ||
| 167 | |||
| 168 | # rough approximation of IP addresses (v4 in v6 not supported). | ||
| 169 | # this helps us avoid triggering any DNS. | ||
| 170 | my $by_ip = ($q =~ m{^(?:$RE{net}{IPv4}|$RE{net}{IPv6})(?:/\d+)?$}i) ? 1 : 0; | ||
| 171 | |||
| 172 | my ($clause, $sorter); | ||
| 173 | if ($by_ip) { | ||
| 174 | my $ip = NetAddr::IP::Lite->new($q) | ||
| 175 | or return undef; # could be a MAC address! | ||
| 176 | $clause = [ 'me.ip' => { -in => _own_or_alias($ip->cidr) } ]; | ||
| 177 | $sorter = \[q{CASE WHEN (me.ip <<= ?) THEN 1 ELSE 0 END}, $ip->cidr]; | ||
| 178 | } | ||
| 179 | else { | ||
| 180 | $q = "\%$q\%" if ($options->{partial} and $q !~ m/\%/); | ||
| 181 | $clause = [ | ||
| 182 | 'me.name' => { '-ilike' => $q }, | ||
| 183 | 'me.dns' => { '-ilike' => $q }, | ||
| 184 | 'device_ips.dns' => { '-ilike' => $q }, | ||
| 185 | ]; | ||
| 186 | $sorter = \[q{CASE WHEN (me.name ILIKE ? OR me.dns ILIKE ?) THEN 1 ELSE 0 END}, $q, $q]; | ||
| 187 | } | ||
| 188 | |||
| 189 | return $rs->search( | ||
| 190 | { | ||
| 191 | -or => $clause, | ||
| 192 | }, | ||
| 193 | { | ||
| 194 | '+select' => [ { coalesce => $sorter, -as => 'in_device' } ], | ||
| 195 | order_by => [{ -desc => 'in_device' }, { -asc => [qw/ me.dns me.ip /] } ], | ||
| 196 | group_by => ['me.ip'], | ||
| 197 | ($by_ip ? () : (join => 'device_ips')), | ||
| 198 | } | ||
| 199 | ); | ||
| 200 | } | ||
| 201 | |||
| 202 | =head2 search_for_device( $name or $ip or $prefix ) | ||
| 203 | |||
| 204 | This is a wrapper for C<search_aliases> which: | ||
| 205 | |||
| 206 | =over 4 | ||
| 207 | |||
| 208 | =item * | ||
| 209 | |||
| 210 | Disables partial matching on host names | ||
| 211 | |||
| 212 | =item * | ||
| 213 | |||
| 214 | Returns only the first result of any found devices | ||
| 215 | |||
| 216 | =back | ||
| 217 | |||
| 218 | If no matching devices are found, C<undef> is returned. | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | sub search_for_device { | ||
| 223 | my ($rs, $q, $options) = @_; | ||
| 224 | $options ||= {}; | ||
| 225 | $options->{partial} = 0; | ||
| 226 | return $rs->search_aliases($q, $options)->first(); | ||
| 227 | } | ||
| 228 | |||
| 229 | =head2 search_by_field( \%cond, \%attrs? ) | ||
| 230 | |||
| 231 | This variant of the standard C<search()> method returns a ResultSet of Device | ||
| 232 | entries. It is written to support web forms which accept fields that match and | ||
| 233 | locate Devices in the database. | ||
| 234 | |||
| 235 | The hashref parameter should contain fields from the Device table which will | ||
| 236 | be intelligently used in a search query. | ||
| 237 | |||
| 238 | In addition, you can provide the key C<matchall> which, given a True or False | ||
| 239 | value, controls whether fields must all match or whether any can match, to | ||
| 240 | select a row. | ||
| 241 | |||
| 242 | Supported keys: | ||
| 243 | |||
| 244 | =over 4 | ||
| 245 | |||
| 246 | =item matchall | ||
| 247 | |||
| 248 | If a True value, fields must all match to return a given row of the Device | ||
| 249 | table, otherwise any field matching will cause the row to be included in | ||
| 250 | results. | ||
| 251 | |||
| 252 | =item name | ||
| 253 | |||
| 254 | Can match the C<name> field as a substring. | ||
| 255 | |||
| 256 | =item location | ||
| 257 | |||
| 258 | Can match the C<location> field as a substring. | ||
| 259 | |||
| 260 | =item description | ||
| 261 | |||
| 262 | Can match the C<description> field as a substring (usually this field contains | ||
| 263 | a description of the vendor operating system). | ||
| 264 | |||
| 265 | =item mac | ||
| 266 | |||
| 267 | Will match exactly the C<mac> field of the Device or any of its Interfaces. | ||
| 268 | |||
| 269 | =item model | ||
| 270 | |||
| 271 | Will match exactly the C<model> field. | ||
| 272 | |||
| 273 | =item os | ||
| 274 | |||
| 275 | Will match exactly the C<os> field, which is the operating system. | ||
| 276 | |||
| 277 | =item os_ver | ||
| 278 | |||
| 279 | Will match exactly the C<os_ver> field, which is the operating system software version. | ||
| 280 | |||
| 281 | =item vendor | ||
| 282 | |||
| 283 | Will match exactly the C<vendor> (manufacturer). | ||
| 284 | |||
| 285 | =item dns | ||
| 286 | |||
| 287 | Can match any of the Device IP address aliases as a substring. | ||
| 288 | |||
| 289 | =item ip | ||
| 290 | |||
| 291 | Can be a string IP or a NetAddr::IP object, either way being treated as an | ||
| 292 | IPv4 or IPv6 prefix within which the device must have one IP address alias. | ||
| 293 | |||
| 294 | =item layers | ||
| 295 | |||
| 296 | OSI Layers which the device must support. | ||
| 297 | |||
| 298 | =back | ||
| 299 | |||
| 300 | =cut | ||
| 301 | |||
| 302 | sub search_by_field { | ||
| 303 | my ($rs, $p, $attrs) = @_; | ||
| 304 | |||
| 305 | die "condition parameter to search_by_field must be hashref\n" | ||
| 306 | if ref {} ne ref $p or 0 == scalar keys %$p; | ||
| 307 | |||
| 308 | my $op = $p->{matchall} ? '-and' : '-or'; | ||
| 309 | |||
| 310 | # this is a bit of an inelegant trick to catch junk data entry, | ||
| 311 | # whilst avoiding returning *all* entries in the table | ||
| 312 | if ($p->{ip} and 'NetAddr::IP::Lite' ne ref $p->{ip}) { | ||
| 313 | $p->{ip} = ( NetAddr::IP::Lite->new($p->{ip}) | ||
| 314 | || NetAddr::IP::Lite->new('255.255.255.255') ); | ||
| 315 | } | ||
| 316 | |||
| 317 | # For Search on Layers | ||
| 318 | my $layers = $p->{layers}; | ||
| 319 | my @layer_select = (); | ||
| 320 | if ( defined $layers && ref $layers ) { | ||
| 321 | foreach my $layer (@$layers) { | ||
| 322 | next unless defined $layer and length($layer); | ||
| 323 | next if ( $layer < 1 || $layer > 7 ); | ||
| 324 | push @layer_select, | ||
| 325 | \[ 'substring(me.layers,9-?, 1)::int = 1', $layer ]; | ||
| 326 | } | ||
| 327 | } | ||
| 328 | elsif ( defined $layers ) { | ||
| 329 | push @layer_select, | ||
| 330 | \[ 'substring(me.layers,9-?, 1)::int = 1', $layers ]; | ||
| 331 | } | ||
| 332 | |||
| 333 | # get IEEE MAC format | ||
| 334 | my $mac = NetAddr::MAC->new(mac => ($p->{mac} || '')); | ||
| 335 | undef $mac if | ||
| 336 | ($mac and $mac->as_ieee | ||
| 337 | and (($mac->as_ieee eq '00:00:00:00:00:00') | ||
| 338 | or ($mac->as_ieee !~ m/^$RE{net}{MAC}$/i))); | ||
| 339 | |||
| 340 | my @joins = ( | ||
| 341 | ($mac ? qw/ports/ : ()), | ||
| 342 | ($p->{dns} ? qw/device_ips/ : ()), | ||
| 343 | ); | ||
| 344 | |||
| 345 | return $rs | ||
| 346 | ->search_rs({}, $attrs) | ||
| 347 | ->search({ | ||
| 348 | $op => [ | ||
| 349 | ($p->{name} ? ('me.name' => | ||
| 350 | { '-ilike' => "\%$p->{name}\%" }) : ()), | ||
| 351 | ($p->{location} ? ('me.location' => | ||
| 352 | { '-ilike' => "\%$p->{location}\%" }) : ()), | ||
| 353 | ($p->{description} ? ('me.description' => | ||
| 354 | { '-ilike' => "\%$p->{description}\%" }) : ()), | ||
| 355 | |||
| 356 | ($mac ? ( | ||
| 357 | -or => [ | ||
| 358 | 'me.mac' => $mac->as_ieee, | ||
| 359 | 'ports.mac' => $mac->as_ieee, | ||
| 360 | ]) : ()), | ||
| 361 | |||
| 362 | ($p->{model} ? ('me.model' => | ||
| 363 | { '-in' => $p->{model} }) : ()), | ||
| 364 | ($p->{os} ? ('me.os' => | ||
| 365 | { '-in' => $p->{os} }) : ()), | ||
| 366 | ($p->{os_ver} ? ('me.os_ver' => | ||
| 367 | { '-in' => $p->{os_ver} }) : ()), | ||
| 368 | ($p->{vendor} ? ('me.vendor' => | ||
| 369 | { '-in' => $p->{vendor} }) : ()), | ||
| 370 | |||
| 371 | ($p->{layers} ? (-or => \@layer_select) : ()), | ||
| 372 | |||
| 373 | ($p->{dns} ? ( | ||
| 374 | -or => [ | ||
| 375 | 'me.dns' => { '-ilike' => "\%$p->{dns}\%" }, | ||
| 376 | 'device_ips.dns' => { '-ilike' => "\%$p->{dns}\%" }, | ||
| 377 | ]) : ()), | ||
| 378 | |||
| 379 | # with match all and a name, the subnet must hold on the same alias | ||
| 380 | # row that matched the name, which only the join can express | ||
| 381 | ($p->{ip} ? (($p->{matchall} and $p->{dns}) | ||
| 382 | ? (-or => [ | ||
| 383 | 'me.ip' => { '<<=' => $p->{ip}->cidr }, | ||
| 384 | 'device_ips.alias' => { '<<=' => $p->{ip}->cidr }, | ||
| 385 | ]) | ||
| 386 | : ('me.ip' => { -in => _own_or_alias($p->{ip}->cidr) })) : ()), | ||
| 387 | ], | ||
| 388 | }, | ||
| 389 | { | ||
| 390 | order_by => [qw/ me.dns me.ip /], | ||
| 391 | ((scalar @joins) ? ( | ||
| 392 | join => \@joins, | ||
| 393 | distinct => 1, | ||
| 394 | ) : ()), | ||
| 395 | } | ||
| 396 | ); | ||
| 397 | } | ||
| 398 | |||
| 399 | =head2 search_fuzzy( $value ) | ||
| 400 | |||
| 401 | This method accepts a single parameter only and returns a ResultSet of rows | ||
| 402 | from the Device table where one field matches the passed parameter. | ||
| 403 | |||
| 404 | The following fields are inspected for a match: | ||
| 405 | |||
| 406 | =over 4 | ||
| 407 | |||
| 408 | =item contact | ||
| 409 | |||
| 410 | =item serial | ||
| 411 | |||
| 412 | =item chassis_id | ||
| 413 | |||
| 414 | =item module serials (exact) | ||
| 415 | |||
| 416 | =item location | ||
| 417 | |||
| 418 | =item name | ||
| 419 | |||
| 420 | =item mac (including port addresses) | ||
| 421 | |||
| 422 | =item description | ||
| 423 | |||
| 424 | =item dns | ||
| 425 | |||
| 426 | =item ip (including aliases) | ||
| 427 | |||
| 428 | =back | ||
| 429 | |||
| 430 | =cut | ||
| 431 | |||
| 432 | sub search_fuzzy { | ||
| 433 | my ($rs, $q) = @_; | ||
| 434 | |||
| 435 | die "missing param to search_fuzzy\n" | ||
| 436 | unless $q; | ||
| 437 | $q = "\%$q\%" if $q !~ m/\%/; | ||
| 438 | (my $qc = $q) =~ s/\%//g; | ||
| 439 | |||
| 440 | # basic IP check is a string match | ||
| 441 | my $ip_clause = [ | ||
| 442 | 'me.ip::text' => { '-ilike' => $q }, | ||
| 443 | 'device_ips_by_address_or_name.alias::text' => { '-ilike' => $q }, | ||
| 444 | ]; | ||
| 445 | my $ipbind = '255.255.255.255/32'; | ||
| 446 | |||
| 447 | # but also allow prefix search | ||
| 448 | if ($qc =~ m{^(?:$RE{net}{IPv4}|$RE{net}{IPv6})(?:/\d+)?$}i | ||
| 449 | and my $ip = NetAddr::IP::Lite->new($qc)) { | ||
| 450 | |||
| 451 | $ip_clause = [ 'me.ip' => { -in => _own_or_alias($ip->cidr) } ]; | ||
| 452 | $ipbind = $ip->cidr; | ||
| 453 | } | ||
| 454 | |||
| 455 | # get IEEE MAC format | ||
| 456 | my $mac = NetAddr::MAC->new(mac => ($q || '')); | ||
| 457 | undef $mac if | ||
| 458 | ($mac and $mac->as_ieee | ||
| 459 | and (($mac->as_ieee eq '00:00:00:00:00:00') | ||
| 460 | or ($mac->as_ieee !~ m/^$RE{net}{MAC}$/i))); | ||
| 461 | my $exact = ($mac ? 1 : 0); | ||
| 462 | $mac = ($mac ? $mac->as_ieee : $q); | ||
| 463 | |||
| 464 | my $with_ports = $exact ? $rs->ports_with_exact_mac($mac) : $rs->ports_with_mac($mac); | ||
| 465 | my @mac_clause = $exact | ||
| 466 | ? ( 'me.mac' => $mac, 'ports_by_exact_mac.mac' => $mac ) | ||
| 467 | : ( 'me.mac::text' => { '-ilike' => $mac}, 'ports_by_mac.mac::text' => { '-ilike' => $mac} ); | ||
| 468 | |||
| 469 | return $with_ports | ||
| 470 | ->device_ips_with_address_or_name($q, $ipbind) | ||
| 471 | ->search( | ||
| 472 | { | ||
| 473 | -or => [ | ||
| 474 | 'me.contact' => { '-ilike' => $q }, | ||
| 475 | 'me.serial' => { '-ilike' => $q }, | ||
| 476 | 'me.chassis_id' => { '-ilike' => $q }, | ||
| 477 | 'me.location' => { '-ilike' => $q }, | ||
| 478 | 'me.name' => { '-ilike' => $q }, | ||
| 479 | 'me.description' => { '-ilike' => $q }, | ||
| 480 | 'me.ip' => { '-in' => | ||
| 481 | $rs->search({ 'modules.serial' => $qc }, | ||
| 482 | { join => 'modules', columns => 'ip' })->as_query() | ||
| 483 | }, | ||
| 484 | -or => [ @mac_clause ], | ||
| 485 | -or => [ | ||
| 486 | 'me.dns' => { '-ilike' => $q }, | ||
| 487 | 'device_ips_by_address_or_name.dns' => { '-ilike' => $q }, | ||
| 488 | ], | ||
| 489 | -or => $ip_clause, | ||
| 490 | ], | ||
| 491 | }, | ||
| 492 | { | ||
| 493 | order_by => [qw/ me.dns me.ip /], | ||
| 494 | distinct => 1, | ||
| 495 | } | ||
| 496 | ); | ||
| 497 | } | ||
| 498 | |||
| 499 | =head2 carrying_vlan( \%cond, \%attrs? ) | ||
| 500 | |||
| 501 | my $set = $rs->carrying_vlan({ vlan => 123 }); | ||
| 502 | |||
| 503 | Like C<search()>, this returns a ResultSet of matching rows from the Device | ||
| 504 | table. | ||
| 505 | |||
| 506 | The returned devices each are aware of the given Vlan. | ||
| 507 | |||
| 508 | =over 4 | ||
| 509 | |||
| 510 | =item * | ||
| 511 | |||
| 512 | The C<cond> parameter must be a hashref containing a key C<vlan> with | ||
| 513 | the value to search for. | ||
| 514 | |||
| 515 | =item * | ||
| 516 | |||
| 517 | Results are ordered by the Device DNS and IP fields. | ||
| 518 | |||
| 519 | =item * | ||
| 520 | |||
| 521 | Column C<pcount> gives a count of the number of ports on the device | ||
| 522 | that are actually configured to carry the VLAN. | ||
| 523 | |||
| 524 | =back | ||
| 525 | |||
| 526 | =cut | ||
| 527 | |||
| 528 | sub carrying_vlan { | ||
| 529 | my ($rs, $cond, $attrs) = @_; | ||
| 530 | |||
| 531 | die "vlan number required for carrying_vlan\n" | ||
| 532 | if ref {} ne ref $cond or !exists $cond->{vlan}; | ||
| 533 | |||
| 534 | return $rs unless $cond->{vlan}; | ||
| 535 | |||
| 536 | return $rs | ||
| 537 | ->search_rs({ 'vlans.vlan' => $cond->{vlan} }, | ||
| 538 | { | ||
| 539 | order_by => [qw/ me.dns me.ip /], | ||
| 540 | select => [{ count => 'ports.vlan' }], | ||
| 541 | as => ['pcount'], | ||
| 542 | columns => [ | ||
| 543 | 'me.ip', 'me.dns', | ||
| 544 | 'me.model', 'me.os', | ||
| 545 | 'me.vendor', 'vlans.vlan', | ||
| 546 | 'vlans.description' | ||
| 547 | ], | ||
| 548 | join => {'vlans' => 'ports'}, | ||
| 549 | distinct => 1, | ||
| 550 | }) | ||
| 551 | ->search({}, $attrs); | ||
| 552 | } | ||
| 553 | |||
| 554 | =head2 carrying_vlan_name( \%cond, \%attrs? ) | ||
| 555 | |||
| 556 | my $set = $rs->carrying_vlan_name({ name => 'Branch Office' }); | ||
| 557 | |||
| 558 | Like C<search()>, this returns a ResultSet of matching rows from the Device | ||
| 559 | table. | ||
| 560 | |||
| 561 | The returned devices each are aware of the named Vlan. | ||
| 562 | |||
| 563 | =over 4 | ||
| 564 | |||
| 565 | =item * | ||
| 566 | |||
| 567 | The C<cond> parameter must be a hashref containing a key C<name> with | ||
| 568 | the value to search for. The value may optionally include SQL wildcard | ||
| 569 | characters. | ||
| 570 | |||
| 571 | =item * | ||
| 572 | |||
| 573 | Results are ordered by the Device DNS and IP fields. | ||
| 574 | |||
| 575 | =item * | ||
| 576 | |||
| 577 | Column C<pcount> gives a count of the number of ports on the device | ||
| 578 | that are actually configured to carry the VLAN. | ||
| 579 | |||
| 580 | =back | ||
| 581 | |||
| 582 | =cut | ||
| 583 | |||
| 584 | sub carrying_vlan_name { | ||
| 585 | my ($rs, $cond, $attrs) = @_; | ||
| 586 | |||
| 587 | die "vlan name required for carrying_vlan_name\n" | ||
| 588 | if ref {} ne ref $cond or !exists $cond->{name}; | ||
| 589 | |||
| 590 | $cond->{'vlans.vlan'} = { '>' => 0 }; | ||
| 591 | $cond->{'vlans.description'} = { '-ilike' => delete $cond->{name} }; | ||
| 592 | |||
| 593 | return $rs | ||
| 594 | ->search_rs({}, { | ||
| 595 | order_by => [qw/ me.dns me.ip /], | ||
| 596 | select => [{ count => 'ports.vlan' }], | ||
| 597 | as => ['pcount'], | ||
| 598 | columns => [ | ||
| 599 | 'me.ip', 'me.dns', | ||
| 600 | 'me.model', 'me.os', | ||
| 601 | 'me.vendor', 'vlans.vlan', | ||
| 602 | 'vlans.description' | ||
| 603 | ], | ||
| 604 | join => {'vlans' => 'ports'}, | ||
| 605 | distinct => 1, | ||
| 606 | }) | ||
| 607 | ->search($cond, $attrs); | ||
| 608 | } | ||
| 609 | |||
| 610 | =head2 has_layer( $layer ) | ||
| 611 | |||
| 612 | my $rset = $rs->has_layer(3); | ||
| 613 | |||
| 614 | This predefined C<search()> returns a ResultSet of matching rows from the | ||
| 615 | Device table of devices advertising support of the supplied layer in the | ||
| 616 | OSI Model. | ||
| 617 | |||
| 618 | =over 4 | ||
| 619 | |||
| 620 | =item * | ||
| 621 | |||
| 622 | The C<layer> parameter must be an integer between 1 and 7. | ||
| 623 | |||
| 624 | =cut | ||
| 625 | |||
| 626 | sub has_layer { | ||
| 627 | my ( $rs, $layer ) = @_; | ||
| 628 | |||
| 629 | die "layer required and must be between 1 and 7\n" | ||
| 630 | if !$layer || $layer < 1 || $layer > 7; | ||
| 631 | |||
| 632 | return $rs->search_rs( \[ 'substring(layers,9-?, 1)::int = 1', $layer ] ); | ||
| 633 | } | ||
| 634 | |||
| 635 | =back | ||
| 636 | |||
| 637 | =head2 get_platforms | ||
| 638 | |||
| 639 | Returns a sorted list of Device models with the following columns only: | ||
| 640 | |||
| 641 | =over 4 | ||
| 642 | |||
| 643 | =item vendor | ||
| 644 | |||
| 645 | =item model | ||
| 646 | |||
| 647 | =item count | ||
| 648 | |||
| 649 | =back | ||
| 650 | |||
| 651 | Where C<count> is the number of instances of that Vendor's Model in the | ||
| 652 | Netdisco database. | ||
| 653 | |||
| 654 | =cut | ||
| 655 | |||
| 656 | sub get_platforms { | ||
| 657 | my $rs = shift; | ||
| 658 | return $rs->search({}, { | ||
| 659 | 'columns' => [ 'vendor', 'model' ], | ||
| 660 | '+select' => [{ count => 'ip' }], | ||
| 661 | '+as' => ['count'], | ||
| 662 | group_by => [qw/vendor model/], | ||
| 663 | order_by => [{-asc => 'vendor'}, {-asc => 'model'}], | ||
| 664 | }); | ||
| 665 | } | ||
| 666 | |||
| 667 | =head2 get_releases | ||
| 668 | |||
| 669 | Returns a sorted list of Device OS releases with the following columns only: | ||
| 670 | |||
| 671 | =over 4 | ||
| 672 | |||
| 673 | =item os | ||
| 674 | |||
| 675 | =item os_ver | ||
| 676 | |||
| 677 | =item count | ||
| 678 | |||
| 679 | =back | ||
| 680 | |||
| 681 | Where C<count> is the number of devices running that OS release in the | ||
| 682 | Netdisco database. | ||
| 683 | |||
| 684 | =cut | ||
| 685 | |||
| 686 | sub get_releases { | ||
| 687 | my $rs = shift; | ||
| 688 | return $rs->search({}, { | ||
| 689 | columns => ['os', 'os_ver'], | ||
| 690 | '+select' => [ { count => 'ip' } ], | ||
| 691 | '+as' => [qw/count/], | ||
| 692 | group_by => [qw/os os_ver/], | ||
| 693 | order_by => [{-asc => 'os'}, {-asc => 'os_ver'}], | ||
| 694 | }) | ||
| 695 | |||
| 696 | } | ||
| 697 | |||
| 698 | =head2 with_layer_features | ||
| 699 | |||
| 700 | This is a modifier for any C<search()> which | ||
| 701 | will add the following additional synthesized columns to the result set: | ||
| 702 | |||
| 703 | =over 4 | ||
| 704 | |||
| 705 | =item is_discoverable | ||
| 706 | |||
| 707 | =item is_macsuckable | ||
| 708 | |||
| 709 | =item is_arpnipable | ||
| 710 | |||
| 711 | =back | ||
| 712 | |||
| 713 | =cut | ||
| 714 | |||
| 715 | sub with_layer_features { | ||
| 716 | my ($rs, $cond, $attrs) = @_; | ||
| 717 | |||
| 718 | return $rs | ||
| 719 | ->search_rs($cond, $attrs) | ||
| 720 | ->search({}, | ||
| 721 | { | ||
| 722 | '+columns' => { | ||
| 723 | is_discoverable => | ||
| 724 | $rs->result_source->schema->resultset('DeviceSkip') | ||
| 725 | ->search( | ||
| 726 | { 'ds.device' => { -ident => 'me.ip' }, '-not' => \q{'discover' = ANY(actionset)} }, | ||
| 727 | { alias => 'ds' }, | ||
| 728 | )->count_rs->as_query, | ||
| 729 | is_macsuckable => | ||
| 730 | $rs->result_source->schema->resultset('DeviceSkip') | ||
| 731 | ->search( | ||
| 732 | { 'ds.device' => { -ident => 'me.ip' }, '-not' => \q{'macsuck' = ANY(actionset)} }, | ||
| 733 | { alias => 'ds' }, | ||
| 734 | )->count_rs->as_query, | ||
| 735 | is_arpnipable => | ||
| 736 | $rs->result_source->schema->resultset('DeviceSkip') | ||
| 737 | ->search( | ||
| 738 | { 'ds.device' => { -ident => 'me.ip' }, '-not' => \q{'arpnip' = ANY(actionset)} }, | ||
| 739 | { alias => 'ds' }, | ||
| 740 | )->count_rs->as_query, | ||
| 741 | }, | ||
| 742 | }); | ||
| 743 | } | ||
| 744 | |||
| 745 | =head2 with_port_count | ||
| 746 | |||
| 747 | This is a modifier for any C<search()> which | ||
| 748 | will add the following additional synthesized column to the result set: | ||
| 749 | |||
| 750 | =over 4 | ||
| 751 | |||
| 752 | =item port_count | ||
| 753 | |||
| 754 | =back | ||
| 755 | |||
| 756 | =cut | ||
| 757 | |||
| 758 | sub with_port_count { | ||
| 759 | my ($rs, $cond, $attrs) = @_; | ||
| 760 | |||
| 761 | return $rs | ||
| 762 | ->search_rs($cond, $attrs) | ||
| 763 | ->search({}, | ||
| 764 | { | ||
| 765 | '+columns' => { | ||
| 766 | port_count => | ||
| 767 | $rs->result_source->schema->resultset('DevicePort') | ||
| 768 | ->search( | ||
| 769 | { | ||
| 770 | 'dp.ip' => { -ident => 'me.ip' }, | ||
| 771 | 'dp.type' => [ '-or' => | ||
| 772 | { '=' => undef }, | ||
| 773 | { '!~*' => '^(53|ieee8023adLag|propVirtual|l2vlan|l3ipvlan|135|136|137)$' }, | ||
| 774 | ], | ||
| 775 | }, | ||
| 776 | { alias => 'dp' } | ||
| 777 | )->count_rs->as_query, | ||
| 778 | }, | ||
| 779 | }); | ||
| 780 | } | ||
| 781 | |||
| 782 | =head1 SPECIAL METHODS | ||
| 783 | |||
| 784 | =head2 delete( \%options? ) | ||
| 785 | |||
| 786 | Overrides the built-in L<DBIx::Class> delete method to more efficiently | ||
| 787 | handle the removal or archiving of nodes. | ||
| 788 | |||
| 789 | =cut | ||
| 790 | |||
| 791 | sub _plural { (shift || 0) == 1 ? 'entry' : 'entries' }; | ||
| 792 | |||
| 793 | sub delete { | ||
| 794 | my $self = shift; | ||
| 795 | |||
| 796 | my $schema = $self->result_source->schema; | ||
| 797 | my $devices = $self->search(undef, { columns => 'ip' }); | ||
| 798 | |||
| 799 | my $ip = undef; | ||
| 800 | { | ||
| 801 | no autovivification; | ||
| 802 | try { $ip ||= $devices->{attrs}->{where}->{ip} }; | ||
| 803 | try { $ip ||= $devices->{attrs}->{where}->{'me.ip'} }; | ||
| 804 | } | ||
| 805 | $ip = ((ref {} eq ref $ip) ? [%$ip]->[1] : $ip); | ||
| 806 | $ip ||= 'netdisco'; | ||
| 807 | |||
| 808 | # an empty set must not queue behind a running recompute for nothing | ||
| 809 | if ($devices->search(undef, { rows => 1 })->count) { | ||
| 810 | with_l3_domain_lock($schema, sub { | ||
| 811 | foreach my $set (qw/ | ||
| 812 | Community | ||
| 813 | DeviceBrowser | ||
| 814 | DeviceIp | ||
| 815 | DeviceModule | ||
| 816 | DevicePower | ||
| 817 | DeviceVlan | ||
| 818 | DeviceVrf | ||
| 819 | L3DomainMember | ||
| 820 | /) { | ||
| 821 | my $gone = $schema->resultset($set)->search( | ||
| 822 | { ip => { '-in' => $devices->as_query } }, | ||
| 823 | )->delete; | ||
| 824 | |||
| 825 | Dancer::Logger::debug( sprintf( ' [%s] db/device - removed %d %s from %s', | ||
| 826 | $ip, $gone, _plural($gone), $set ) ) if defined Dancer::Logger::logger(); | ||
| 827 | } | ||
| 828 | }); | ||
| 829 | } | ||
| 830 | |||
| 831 | $schema->resultset('Admin')->search({ | ||
| 832 | device => { '-in' => $devices->as_query }, | ||
| 833 | })->delete; | ||
| 834 | |||
| 835 | $schema->resultset('DeviceSkip')->search( | ||
| 836 | { device => { '-in' => $devices->as_query } }, | ||
| 837 | )->delete; | ||
| 838 | |||
| 839 | my $gone = $schema->resultset('Topology')->search({ | ||
| 840 | -or => [ | ||
| 841 | { dev1 => { '-in' => $devices->as_query } }, | ||
| 842 | { dev2 => { '-in' => $devices->as_query } }, | ||
| 843 | ], | ||
| 844 | })->delete; | ||
| 845 | |||
| 846 | Dancer::Logger::debug( sprintf( ' [%s] db/device - removed %d manual topology %s', | ||
| 847 | $ip, $gone, _plural($gone) ) ) if defined Dancer::Logger::logger(); | ||
| 848 | |||
| 849 | $schema->resultset('DevicePort')->search( | ||
| 850 | { ip => { '-in' => $devices->as_query } }, | ||
| 851 | )->delete(@_); | ||
| 852 | |||
| 853 | # now let DBIC do its thing | ||
| 854 | return $self->next::method(); | ||
| 855 | } | ||
| 856 | |||
| 857 | 1; | ||
| 858 | |||
| 859 | __END__ |