implement port properties table and error disable gathering to it
This commit is contained in:
		| @@ -11,7 +11,7 @@ __PACKAGE__->load_namespaces( | ||||
| ); | ||||
|  | ||||
| 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 File::ShareDir 'dist_dir'; | ||||
|   | ||||
| @@ -171,6 +171,17 @@ __PACKAGE__->has_many( | ||||
|     '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 | ||||
|  | ||||
| Returns the set of ports known to have PoE capability | ||||
|   | ||||
| @@ -166,6 +166,17 @@ __PACKAGE__->might_have( power => 'App::Netdisco::DB::Result::DevicePortPower', | ||||
|   '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 | ||||
|  | ||||
| Returns a row from the C<device_port_ssid> table if one refers to this | ||||
|   | ||||
							
								
								
									
										32
									
								
								lib/App/Netdisco/DB/Result/DevicePortProperties.pm
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								lib/App/Netdisco/DB/Result/DevicePortProperties.pm
									
									
									
									
									
										Normal 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; | ||||
							
								
								
									
										48
									
								
								lib/App/Netdisco/Worker/Plugin/Discover/PortProperties.pm
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								lib/App/Netdisco/Worker/Plugin/Discover/PortProperties.pm
									
									
									
									
									
										Normal 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; | ||||
		Reference in New Issue
	
	Block a user