« blib/lib/App/Netdisco/Util/MCE.pm
↑ Summary
blib/lib/App/Netdisco/SSHCollector/Platform/FortiOS.pm »
| 1 | package App::Netdisco::DB::ResultSet::Node; | ||
| 2 | use base 'App::Netdisco::DB::ResultSet'; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | __PACKAGE__->load_components(qw/ | ||
| 8 | +App::Netdisco::DB::ExplicitLocking | ||
| 9 | /); | ||
| 10 | |||
| 11 | =head1 ADDITIONAL METHODS | ||
| 12 | |||
| 13 | =head2 search_by_mac( \%cond, \%attrs? ) | ||
| 14 | |||
| 15 | my $set = $rs->search_by_mac({mac => '00:11:22:33:44:55', active => 1}); | ||
| 16 | |||
| 17 | Like C<search()>, this returns a ResultSet of matching rows from the Node | ||
| 18 | table. | ||
| 19 | |||
| 20 | =over 4 | ||
| 21 | |||
| 22 | =item * | ||
| 23 | |||
| 24 | The C<cond> parameter must be a hashref containing a key C<mac> with | ||
| 25 | the value to search for. | ||
| 26 | |||
| 27 | =item * | ||
| 28 | |||
| 29 | Results are ordered by time last seen. | ||
| 30 | |||
| 31 | =item * | ||
| 32 | |||
| 33 | Additional columns C<time_first_stamp> and C<time_last_stamp> provide | ||
| 34 | preformatted timestamps of the C<time_first> and C<time_last> fields. | ||
| 35 | |||
| 36 | =item * | ||
| 37 | |||
| 38 | A JOIN is performed on the Device table and the Device C<dns> column | ||
| 39 | prefetched. | ||
| 40 | |||
| 41 | =back | ||
| 42 | |||
| 43 | To limit results only to active nodes, set C<< {active => 1} >> in C<cond>. | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | sub search_by_mac { | ||
| 48 | my ($rs, $cond, $attrs) = @_; | ||
| 49 | |||
| 50 | die "mac address required for search_by_mac\n" | ||
| 51 | if ref {} ne ref $cond or !exists $cond->{mac}; | ||
| 52 | |||
| 53 | $cond->{'me.mac'} = delete $cond->{mac}; | ||
| 54 | |||
| 55 | return $rs | ||
| 56 | ->search_rs({}, { | ||
| 57 | order_by => {'-desc' => 'time_last'}, | ||
| 58 | '+columns' => [ | ||
| 59 | 'device.dns', | ||
| 60 | { time_first_stamp => \"to_char(time_first, 'YYYY-MM-DD HH24:MI')" }, | ||
| 61 | { time_last_stamp => \"to_char(time_last, 'YYYY-MM-DD HH24:MI')" }, | ||
| 62 | ], | ||
| 63 | join => 'device', | ||
| 64 | }) | ||
| 65 | ->search($cond, $attrs); | ||
| 66 | } | ||
| 67 | |||
| 68 | =head2 with_port_context | ||
| 69 | |||
| 70 | my $set = $rs->search(...)->with_port_context; | ||
| 71 | |||
| 72 | Adds columns describing the switch port of each Node, so that API clients can | ||
| 73 | tell access ports from transit ports (uplinks, LAGs) without fetching the | ||
| 74 | ports of every switch: | ||
| 75 | |||
| 76 | =over 4 | ||
| 77 | |||
| 78 | =item * | ||
| 79 | |||
| 80 | C<port_is_uplink>, C<port_remote_ip>, C<port_is_master>, C<port_slave_of> | ||
| 81 | from C<device_port>, and C<port_remote_is_wap>, C<port_remote_is_phone> from | ||
| 82 | C<device_port_properties>. | ||
| 83 | |||
| 84 | =item * | ||
| 85 | |||
| 86 | C<lag_remote_ip> is a member's C<remote_ip> for a LAG master, or the | ||
| 87 | master's for a LAG member. | ||
| 88 | |||
| 89 | =item * | ||
| 90 | |||
| 91 | C<neighbor_is_device> is true when the port's remote IP, or one of its LAG | ||
| 92 | members', is a known device address. | ||
| 93 | |||
| 94 | =item * | ||
| 95 | |||
| 96 | C<port_mac_count> is the number of distinct active MACs on the port, across | ||
| 97 | all VLANs. | ||
| 98 | |||
| 99 | =back | ||
| 100 | |||
| 101 | The columns use correlated subqueries per row, so callers should make this | ||
| 102 | opt-in for searches that can return many rows. | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub with_port_context { | ||
| 107 | my $rs = shift; | ||
| 108 | return $rs->search(undef, { | ||
| 109 | '+columns' => [ | ||
| 110 | { port_is_uplink => 'device_port.is_uplink' }, | ||
| 111 | { port_remote_ip => 'device_port.remote_ip' }, | ||
| 112 | { port_is_master => 'device_port.is_master' }, | ||
| 113 | { port_slave_of => 'device_port.slave_of' }, | ||
| 114 | { port_remote_is_wap => 'properties.remote_is_wap' }, | ||
| 115 | { port_remote_is_phone => 'properties.remote_is_phone' }, | ||
| 116 | # LLDP is usually on the members, not the master, and nodes on | ||
| 117 | # members get moved to the master by macsuck | ||
| 118 | { lag_remote_ip => \q{COALESCE( | ||
| 119 | (SELECT agg.remote_ip FROM device_port agg | ||
| 120 | WHERE agg.ip = me.switch AND agg.port = device_port.slave_of), | ||
| 121 | (SELECT mem.remote_ip FROM device_port mem | ||
| 122 | WHERE mem.ip = me.switch AND mem.slave_of = me.port | ||
| 123 | AND mem.remote_ip IS NOT NULL | ||
| 124 | ORDER BY mem.port LIMIT 1))} }, | ||
| 125 | { neighbor_is_device => \q{EXISTS( | ||
| 126 | SELECT 1 FROM device_ip di WHERE di.alias = device_port.remote_ip | ||
| 127 | OR di.alias IN (SELECT mem.remote_ip FROM device_port mem | ||
| 128 | WHERE mem.ip = me.switch AND mem.slave_of = me.port))} }, | ||
| 129 | { port_mac_count => \q{(SELECT count(DISTINCT pn.mac) FROM node pn | ||
| 130 | WHERE pn.switch = me.switch AND pn.port = me.port AND pn.active)} }, | ||
| 131 | ], | ||
| 132 | join => { device_port => 'properties' }, | ||
| 133 | }); | ||
| 134 | } | ||
| 135 | |||
| 136 | =head1 SPECIAL METHODS | ||
| 137 | |||
| 138 | =head2 delete( \%options? ) | ||
| 139 | |||
| 140 | Overrides the built-in L<DBIx::Class> delete method to more efficiently | ||
| 141 | handle the removal or archiving of nodes. | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | sub delete { | ||
| 146 | my $self = shift; | ||
| 147 | my ($opts) = @_; | ||
| 148 | $opts = {} if (ref {} ne ref $opts); | ||
| 149 | |||
| 150 | my $schema = $self->result_source->schema; | ||
| 151 | my $nodes = $self->search(undef, { columns => 'mac' }); | ||
| 152 | |||
| 153 | if (exists $opts->{archive_nodes} and $opts->{archive_nodes}) { | ||
| 154 | foreach my $set (qw/ | ||
| 155 | NodeIp | ||
| 156 | NodeNbt | ||
| 157 | NodeMonitor | ||
| 158 | Node | ||
| 159 | /) { | ||
| 160 | $schema->resultset($set)->search( | ||
| 161 | { mac => { '-in' => $nodes->as_query }}, | ||
| 162 | )->update({ active => \'false' }); | ||
| 163 | } | ||
| 164 | |||
| 165 | $schema->resultset('NodeWireless') | ||
| 166 | ->search({ mac => { '-in' => $nodes->as_query }})->delete; | ||
| 167 | |||
| 168 | # avoid letting DBIC delete nodes | ||
| 169 | return 0E0; | ||
| 170 | } | ||
| 171 | elsif (exists $opts->{only_nodes} and $opts->{only_nodes}) { | ||
| 172 | # now let DBIC do its thing | ||
| 173 | return $self->next::method(); | ||
| 174 | } | ||
| 175 | elsif (exists $opts->{keep_nodes} and $opts->{keep_nodes}) { | ||
| 176 | # avoid letting DBIC delete nodes | ||
| 177 | return 0E0; | ||
| 178 | } | ||
| 179 | else { | ||
| 180 | foreach my $set (qw/ | ||
| 181 | NodeMonitor | ||
| 182 | NodeWireless | ||
| 183 | NodeNbt | ||
| 184 | /) { | ||
| 185 | $schema->resultset($set)->search( | ||
| 186 | { mac => { '-in' => $nodes->as_query }}, | ||
| 187 | )->delete; | ||
| 188 | } | ||
| 189 | |||
| 190 | # for node_ip only delete if there are no longer | ||
| 191 | # any nodes referencing the IP. | ||
| 192 | |||
| 193 | my @mac_restrict_aq = @{${ $nodes->as_query }}; | ||
| 194 | my @macport_restrict_aq = @{${ $nodes->search(undef, { '+columns' => 'port' })->as_query }}; | ||
| 195 | my $mac_restrict = shift @mac_restrict_aq; | ||
| 196 | my $macport_restrict = shift @macport_restrict_aq; | ||
| 197 | |||
| 198 | my $deleted = $schema->storage->dbh_do(sub { | ||
| 199 | my ($storage, $dbh, @extra) = @_; | ||
| 200 | local $dbh->{TraceLevel} = | ||
| 201 | ($ENV{DBIC_TRACE} ? '1|SQL' : $dbh->{TraceLevel}); | ||
| 202 | $dbh->do(<<SQL | ||
| 203 | DELETE FROM node_ip WHERE mac IN ( | ||
| 204 | SELECT mac FROM node_ip | ||
| 205 | LEFT OUTER JOIN ( | ||
| 206 | SELECT mac, port FROM node | ||
| 207 | EXCEPT ($macport_restrict)) exceptions | ||
| 208 | USING (mac) | ||
| 209 | WHERE node_ip.mac IN ($mac_restrict) | ||
| 210 | AND exceptions.port IS NULL) | ||
| 211 | SQL | ||
| 212 | , undef, (map {$_->[1]} (@macport_restrict_aq, @mac_restrict_aq))); | ||
| 213 | }); | ||
| 214 | |||
| 215 | # now let DBIC do its thing | ||
| 216 | return $self->next::method(); | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | 1; |