* fix anomalous name * add gather worker * fix encoding of binary storage * store results back to job * now parsing mbis report to translate * fix the broken report parser * rename gather to snapshot * implement walk code copied from SNMP::Info * can now bulkwalk and parse mibs report and store resolved walk in cache * add func/glob aliasing broken * better aliasing * implement aliasing from globals and funcs * fix regexp for matching netdisco-mibs report * fake cache entry for all ND2 methods called, add comments * also save to logs/snapshots/IP * add doc for netdisco-do * add is_pseudo column to device table * support for loading cache for pseudo devices * check for hrSystemUptime as well as sysUpTime for snmp connect * display pseudo devices with yellow pill for name * color all cells for layers for pseudo * no need to b64 encode binary data in scalars as we b64 whole thing after * tweaked uptime check * store snapshot to database instead of Job * expose snapshots in device details tab * small ux improvements on snap download * fixes for errors in subnet mask searching * hide snapshot management for pseudo devices * update to use new netdisco-mibs object cache * update for new format oids file * start of work on loading walk into db for browsing * store values and meta * add auto increment col and oid index to browser * start web plugin for browser * add virtual search for oid children * have all oid in separte table (60 seconds load on my laptop) * rename table and add relation * store oid as int array * fix sql for children * make jstree start working * working very slow tree expand * fix to work when first displaying tree * store both oid and oid_parts * simplify SQL to speed up (more complicated perl) * fix sql bug, add better index, prettify tree * render the snmp node detail * add node template, make scrollable, pretty print data values (insecure) * store munge hint * some dubious code to munge the data * make sure to filter by IP on device_browser * make safer the rendering of value data (but need to come back to key ordering) * fix sorting on object values * limit the opening of child nodes to keep response good and unclutter * factor out the munge and make safer * reject unknown mungers * show the munger and option (not working) to change * additional js for munge select * complete custom munge * change so that saving to database is only at CLI and on request * hide snmp tab if no browser rows in the db * add helpful message when no browser rows for the device * stub handler for search and add recurse control * working search * minor ui fixes * implement typeahead for leaf search * limit rows in typeahead * make sure device_browser is visited in delete and renumber * add requirements for this branch * update manifest * make sure node search and typeahead are restricted to current device only
		
			
				
	
	
		
			278 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			Perl
		
	
	
	
	
	
			
		
		
	
	
			278 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			Perl
		
	
	
	
	
	
package App::Netdisco::DB::ResultSet::DevicePort;
 | 
						|
use base 'App::Netdisco::DB::ResultSet';
 | 
						|
 | 
						|
use strict;
 | 
						|
use warnings;
 | 
						|
 | 
						|
use Try::Tiny;
 | 
						|
require Dancer::Logger;
 | 
						|
 | 
						|
__PACKAGE__->load_components(qw/
 | 
						|
  +App::Netdisco::DB::ExplicitLocking
 | 
						|
/);
 | 
						|
 | 
						|
=head1 ADDITIONAL METHODS
 | 
						|
 | 
						|
=head2 with_times
 | 
						|
 | 
						|
This is a modifier for any C<search()> (including the helpers below) which
 | 
						|
will add the following additional synthesized columns to the result set:
 | 
						|
 | 
						|
=over 4
 | 
						|
 | 
						|
=item lastchange_stamp
 | 
						|
 | 
						|
=back
 | 
						|
 | 
						|
=cut
 | 
						|
 | 
						|
sub with_times {
 | 
						|
  my ($rs, $cond, $attrs) = @_;
 | 
						|
 | 
						|
  return $rs
 | 
						|
    ->search_rs($cond, $attrs)
 | 
						|
    ->search({},
 | 
						|
      {
 | 
						|
        '+columns' => { lastchange_stamp =>
 | 
						|
          \("to_char(device.last_discover - (device.uptime - me.lastchange) / 100 * interval '1 second', "
 | 
						|
            ."'YYYY-MM-DD HH24:MI:SS')") },
 | 
						|
        join => 'device',
 | 
						|
      });
 | 
						|
}
 | 
						|
 | 
						|
=head2 with_is_free
 | 
						|
 | 
						|
This is a modifier for any C<search()> (including the helpers below) which
 | 
						|
will add the following additional synthesized columns to the result set:
 | 
						|
 | 
						|
=over 4
 | 
						|
 | 
						|
=item is_free
 | 
						|
 | 
						|
=back
 | 
						|
 | 
						|
In the C<$cond> hash (the first parameter) pass in the C<age_num> which must
 | 
						|
be an integer, and the C<age_unit> which must be a string of either C<days>,
 | 
						|
C<weeks>, C<months> or C<years>.
 | 
						|
 | 
						|
=cut
 | 
						|
 | 
						|
sub with_is_free {
 | 
						|
  my ($rs, $cond, $attrs) = @_;
 | 
						|
 | 
						|
  my $interval = (delete $cond->{age_num}) .' '. (delete $cond->{age_unit});
 | 
						|
 | 
						|
  return $rs
 | 
						|
    ->search_rs($cond, $attrs)
 | 
						|
    ->search({},
 | 
						|
      {
 | 
						|
        '+columns' => { is_free =>
 | 
						|
          # NOTE this query is in `git grep 'THREE PLACES'`
 | 
						|
          \["me.up_admin = 'up' AND me.up != 'up' AND me.type != 'propVirtual' AND "
 | 
						|
              ."((age(now(), to_timestamp(extract(epoch from device.last_discover) - (device.uptime/100))) < ?::interval "
 | 
						|
              ."AND (last_node.time_last IS NULL OR age(now(), last_node.time_last) > ?::interval)) "
 | 
						|
              ."OR age(now(), to_timestamp(extract(epoch from device.last_discover) - (device.uptime - me.lastchange)/100)) > ?::interval)",
 | 
						|
            [{} => $interval],[ {} => $interval],[ {} => $interval]] },
 | 
						|
        join => [qw/device last_node/],
 | 
						|
      });
 | 
						|
}
 | 
						|
 | 
						|
=head2 only_free_ports
 | 
						|
 | 
						|
This is a modifier for any C<search()> (including the helpers below) which
 | 
						|
will restrict results based on whether the port is considered "free".
 | 
						|
 | 
						|
In the C<$cond> hash (the first parameter) pass in the C<age_num> which must
 | 
						|
be an integer, and the C<age_unit> which must be a string of either C<days>,
 | 
						|
C<weeks>, C<months> or C<years>.
 | 
						|
 | 
						|
=cut
 | 
						|
 | 
						|
sub only_free_ports {
 | 
						|
  my ($rs, $cond, $attrs) = @_;
 | 
						|
 | 
						|
  my $interval = (delete $cond->{age_num}) .' '. (delete $cond->{age_unit});
 | 
						|
 | 
						|
  return $rs
 | 
						|
    ->search_rs($cond, $attrs)
 | 
						|
    ->search(
 | 
						|
      {
 | 
						|
        # NOTE this query is in `git grep 'THREE PLACES'`
 | 
						|
        'me.up_admin' => 'up',
 | 
						|
        'me.up'       => { '!=' => 'up' },
 | 
						|
        'me.type'     => { '!=' => 'propVirtual' },
 | 
						|
        -or => [
 | 
						|
          -and => [
 | 
						|
            \["age(now(), to_timestamp(extract(epoch from device.last_discover) - (device.uptime/100))) < ?::interval",
 | 
						|
              [{} => $interval]],
 | 
						|
            -or => [
 | 
						|
              'last_node.time_last' => undef,
 | 
						|
              \["age(now(), last_node.time_last) > ?::interval", [{} => $interval]],
 | 
						|
            ]
 | 
						|
          ],
 | 
						|
          \["age(now(), to_timestamp(extract(epoch from device.last_discover) - (device.uptime - me.lastchange)/100)) > ?::interval",
 | 
						|
            [{} => $interval]],
 | 
						|
        ],
 | 
						|
      },{ join => [qw/device last_node/] },
 | 
						|
    );
 | 
						|
}
 | 
						|
 | 
						|
=head2 with_properties
 | 
						|
 | 
						|
This is a modifier for any C<search()> which
 | 
						|
will add the following additional synthesized columns to the result set:
 | 
						|
 | 
						|
=over 4
 | 
						|
 | 
						|
=item error_disable_cause
 | 
						|
 | 
						|
=item remote_is_wap (boolean)
 | 
						|
 | 
						|
=item remote_is_phone (boolean)
 | 
						|
 | 
						|
=back
 | 
						|
 | 
						|
=cut
 | 
						|
 | 
						|
sub with_properties {
 | 
						|
  my ($rs, $cond, $attrs) = @_;
 | 
						|
 | 
						|
  return $rs
 | 
						|
    ->search_rs($cond, $attrs)
 | 
						|
    ->search({},
 | 
						|
      {
 | 
						|
        '+select' => [qw/
 | 
						|
          properties.error_disable_cause
 | 
						|
          properties.remote_is_wap
 | 
						|
          properties.remote_is_phone
 | 
						|
        /],
 | 
						|
        '+as' => [qw/
 | 
						|
          error_disable_cause
 | 
						|
          remote_is_wap remote_is_phone
 | 
						|
        /],
 | 
						|
        join => 'properties',
 | 
						|
      });
 | 
						|
}
 | 
						|
 | 
						|
=head2 with_remote_inventory
 | 
						|
 | 
						|
This is a modifier for any C<search()> which
 | 
						|
will add the following additional synthesized columns to the result set:
 | 
						|
 | 
						|
=over 4
 | 
						|
 | 
						|
=item remote_vendor
 | 
						|
 | 
						|
=item remote_model
 | 
						|
 | 
						|
=item remote_os_ver
 | 
						|
 | 
						|
=item remote_serial
 | 
						|
 | 
						|
=back
 | 
						|
 | 
						|
=cut
 | 
						|
 | 
						|
sub with_remote_inventory {
 | 
						|
  my ($rs, $cond, $attrs) = @_;
 | 
						|
 | 
						|
  return $rs
 | 
						|
    ->search_rs($cond, $attrs)
 | 
						|
    ->search({},
 | 
						|
      {
 | 
						|
        '+select' => [qw/
 | 
						|
          properties.remote_vendor
 | 
						|
          properties.remote_model
 | 
						|
          properties.remote_os_ver
 | 
						|
          properties.remote_serial
 | 
						|
        /],
 | 
						|
        '+as' => [qw/
 | 
						|
          remote_vendor remote_model remote_os_ver remote_serial
 | 
						|
        /],
 | 
						|
        join => 'properties',
 | 
						|
      });
 | 
						|
}
 | 
						|
 | 
						|
=head2 with_vlan_count
 | 
						|
 | 
						|
This is a modifier for any C<search()> (including the helpers below) which
 | 
						|
will add the following additional synthesized columns to the result set:
 | 
						|
 | 
						|
=over 4
 | 
						|
 | 
						|
=item vlan_count
 | 
						|
 | 
						|
=back
 | 
						|
 | 
						|
=cut
 | 
						|
 | 
						|
sub with_vlan_count {
 | 
						|
  my ($rs, $cond, $attrs) = @_;
 | 
						|
 | 
						|
  return $rs
 | 
						|
    ->search_rs($cond, $attrs)
 | 
						|
    ->search({},
 | 
						|
      {
 | 
						|
        '+columns' => { vlan_count =>
 | 
						|
          $rs->result_source->schema->resultset('DevicePortVlan')
 | 
						|
            ->search(
 | 
						|
              {
 | 
						|
                'dpv.ip'   => { -ident => 'me.ip' },
 | 
						|
                'dpv.port' => { -ident => 'me.port' },
 | 
						|
              },
 | 
						|
              { alias => 'dpv' }
 | 
						|
            )->count_rs->as_query
 | 
						|
        },
 | 
						|
      });
 | 
						|
}
 | 
						|
 | 
						|
=head1 SPECIAL METHODS
 | 
						|
 | 
						|
=head2 delete( \%options? )
 | 
						|
 | 
						|
Overrides the built-in L<DBIx::Class> delete method to more efficiently
 | 
						|
handle the removal or archiving of nodes.
 | 
						|
 | 
						|
=cut
 | 
						|
 | 
						|
sub _plural { (shift || 0) == 1 ? 'entry' : 'entries' };
 | 
						|
 | 
						|
sub delete {
 | 
						|
  my $self = shift;
 | 
						|
 | 
						|
  my $schema = $self->result_source->schema;
 | 
						|
  my $ports = $self->search(undef, { columns => 'ip' });
 | 
						|
 | 
						|
  my $ip = undef;
 | 
						|
  {
 | 
						|
    no autovivification;
 | 
						|
    try { $ip ||= ${ $ports->{attrs}->{where}->{ip}->{'-in'} }->[1]->[1] };
 | 
						|
    try { $ip ||= $ports->{attrs}->{where}->{'me.ip'} };
 | 
						|
  }
 | 
						|
  $ip ||= 'netdisco';
 | 
						|
 | 
						|
  foreach my $set (qw/
 | 
						|
    DevicePortPower
 | 
						|
    DevicePortProperties
 | 
						|
    DevicePortSsid
 | 
						|
    DevicePortVlan
 | 
						|
    DevicePortWireless
 | 
						|
  /) {
 | 
						|
      my $gone = $schema->resultset($set)->search(
 | 
						|
        { ip => { '-in' => $ports->as_query }},
 | 
						|
      )->delete;
 | 
						|
 | 
						|
      Dancer::Logger::debug( sprintf( ' [%s] db/ports - removed %d port %s from %s',
 | 
						|
        $ip, $gone, _plural($gone), $set ) ) if defined Dancer::Logger::logger();
 | 
						|
  }
 | 
						|
 | 
						|
  $schema->resultset('Node')->search(
 | 
						|
    { switch => { '-in' => $ports->as_query }},
 | 
						|
  )->delete(@_);
 | 
						|
 | 
						|
  # now let DBIC do its thing
 | 
						|
  return $self->next::method();
 | 
						|
}
 | 
						|
 | 
						|
1;
 |