implement port properties table and error disable gathering to it

This commit is contained in:
Oliver Gorwits
2018-02-20 22:45:53 +00:00
parent 3e7ffe8045
commit 7191a1cd00
7 changed files with 114 additions and 1 deletions

View File

@@ -11,7 +11,7 @@ __PACKAGE__->load_namespaces(
); );
our # try to hide from kwalitee our # try to hide from kwalitee
$VERSION = 46; # schema version used for upgrades, keep as integer $VERSION = 47; # schema version used for upgrades, keep as integer
use Path::Class; use Path::Class;
use File::ShareDir 'dist_dir'; use File::ShareDir 'dist_dir';

View File

@@ -171,6 +171,17 @@ __PACKAGE__->has_many(
'ip', { join_type => 'RIGHT' } 'ip', { join_type => 'RIGHT' }
); );
=head2 properties_ports
Returns the set of ports known to have recorded properties
=cut
__PACKAGE__->has_many(
properties_ports => 'App::Netdisco::DB::Result::DevicePortProperties',
'ip', { join_type => 'RIGHT' }
);
=head2 powered_ports =head2 powered_ports
Returns the set of ports known to have PoE capability Returns the set of ports known to have PoE capability

View File

@@ -166,6 +166,17 @@ __PACKAGE__->might_have( power => 'App::Netdisco::DB::Result::DevicePortPower',
'foreign.ip' => 'self.ip', 'foreign.port' => 'self.port', 'foreign.ip' => 'self.ip', 'foreign.port' => 'self.port',
}); });
=head2 properties
Returns a row from the C<device_port_properties> table if one refers to this
device port.
=cut
__PACKAGE__->might_have( properties => 'App::Netdisco::DB::Result::DevicePortProperties', {
'foreign.ip' => 'self.ip', 'foreign.port' => 'self.port',
});
=head2 ssid =head2 ssid
Returns a row from the C<device_port_ssid> table if one refers to this Returns a row from the C<device_port_ssid> table if one refers to this

View File

@@ -0,0 +1,32 @@
use utf8;
package App::Netdisco::DB::Result::DevicePortProperties;
use strict;
use warnings;
use base 'DBIx::Class::Core';
__PACKAGE__->table("device_port_properties");
__PACKAGE__->add_columns(
"ip",
{ data_type => "inet", is_nullable => 0 },
"port",
{ data_type => "text", is_nullable => 0 },
"error_disable_cause",
{ data_type => "text", is_nullable => 1 },
);
__PACKAGE__->set_primary_key("port", "ip");
=head1 RELATIONSHIPS
=head2 port
Returns the entry from the C<port> table for which this Power entry applies.
=cut
__PACKAGE__->belongs_to( port => 'App::Netdisco::DB::Result::DevicePort', {
'foreign.ip' => 'self.ip', 'foreign.port' => 'self.port',
});
1;

View File

@@ -0,0 +1,48 @@
package App::Netdisco::Worker::Plugin::Discover::PortProperties;
use Dancer ':syntax';
use App::Netdisco::Worker::Plugin;
use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Transport::SNMP ();
use Dancer::Plugin::DBIC 'schema';
register_worker({ phase => 'main', driver => 'snmp' }, sub {
my ($job, $workerconf) = @_;
my $device = $job->device;
return unless $device->in_storage;
my $snmp = App::Netdisco::Transport::SNMP->reader_for($device)
or return Status->defer("discover failed: could not SNMP connect to $device");
my $interfaces = $snmp->interfaces;
my $err_cause = $snmp->i_err_disable_cause;
if (!defined $err_cause or !defined $interfaces) {
return Status->info(sprintf ' [%s] props - 0 errored ports', $device->ip);
}
# build device port properties info suitable for DBIC
my @portproperties;
foreach my $entry (keys %$err_cause) {
my $port = $interfaces->{$entry};
next unless $port;
push @portproperties, {
port => $port,
error_disable_cause => $err_cause->{$entry},
};
}
schema('netdisco')->txn_do(sub {
my $gone = $device->properties_ports->delete;
debug sprintf ' [%s] props - removed %d ports with properties',
$device->ip, $gone;
$device->properties_ports->populate(\@portproperties);
return Status->info(sprintf ' [%s] props - added %d new port properties',
$device->ip, scalar @portproperties);
});
});
true;

View File

@@ -341,6 +341,7 @@ worker_plugins:
- 'Discover::Neighbors' - 'Discover::Neighbors'
- 'Discover::Neighbors::Routed' - 'Discover::Neighbors::Routed'
- 'Discover::PortPower' - 'Discover::PortPower'
- 'Discover::PortProperties'
- 'Discover::Properties' - 'Discover::Properties'
- 'Discover::VLANs' - 'Discover::VLANs'
- 'Discover::Wireless' - 'Discover::Wireless'

View File

@@ -0,0 +1,10 @@
BEGIN;
CREATE TABLE device_port_properties (
"ip" "inet",
"port" "text",
"error_disable_cause" "text",
PRIMARY KEY ("port", "ip")
);
COMMIT;