From c2ac19e6477038caa419c4214f614d59b713842a Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Thu, 4 Apr 2013 19:32:14 +0100 Subject: [PATCH] implementation of store_power --- Netdisco/lib/App/Netdisco/DB/Result/Device.pm | 8 +++ .../lib/App/Netdisco/DB/Result/DevicePower.pm | 9 +++ .../Netdisco/Daemon/Worker/Poller/Discover.pm | 2 +- .../lib/App/Netdisco/Util/DiscoverAndStore.pm | 71 ++++++++++++++++++- 4 files changed, 88 insertions(+), 2 deletions(-) diff --git a/Netdisco/lib/App/Netdisco/DB/Result/Device.pm b/Netdisco/lib/App/Netdisco/DB/Result/Device.pm index 3fd16244..d920b3df 100644 --- a/Netdisco/lib/App/Netdisco/DB/Result/Device.pm +++ b/Netdisco/lib/App/Netdisco/DB/Result/Device.pm @@ -110,6 +110,14 @@ Returns the set of ports on this Device. __PACKAGE__->has_many( ports => 'App::Netdisco::DB::Result::DevicePort', 'ip' ); +=head2 power_modules + +Returns the set of power modules on this Device. + +=cut + +__PACKAGE__->has_many( power_modules => 'App::Netdisco::DB::Result::DevicePower', 'ip' ); + =head2 port_vlans Returns the set of VLANs known to be configured on Ports on this Device, diff --git a/Netdisco/lib/App/Netdisco/DB/Result/DevicePower.pm b/Netdisco/lib/App/Netdisco/DB/Result/DevicePower.pm index 1a70858c..2f2cbbe5 100644 --- a/Netdisco/lib/App/Netdisco/DB/Result/DevicePower.pm +++ b/Netdisco/lib/App/Netdisco/DB/Result/DevicePower.pm @@ -25,6 +25,15 @@ __PACKAGE__->set_primary_key("ip", "module"); # Created by DBIx::Class::Schema::Loader v0.07015 @ 2012-01-07 14:20:02 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:awZRI/IH2VewzGlxISsr7w +=head1 RELATIONSHIPS + +=head2 device + +Returns the entry from the C table on which this power module was discovered. + +=cut + +__PACKAGE__->belongs_to( device => 'App::Netdisco::DB::Result::Device', 'ip' ); # You can replace this text with custom code or comments, and it will be preserved on regeneration 1; diff --git a/Netdisco/lib/App/Netdisco/Daemon/Worker/Poller/Discover.pm b/Netdisco/lib/App/Netdisco/Daemon/Worker/Poller/Discover.pm index c4a630c2..074d9f81 100644 --- a/Netdisco/lib/App/Netdisco/Daemon/Worker/Poller/Discover.pm +++ b/Netdisco/lib/App/Netdisco/Daemon/Worker/Poller/Discover.pm @@ -45,7 +45,7 @@ sub discover { store_interfaces($device, $snmp); store_wireless($device, $snmp); store_vlans($device, $snmp); - #store_power($device, $snmp); + store_power($device, $snmp); #store_modules($device, $snmp); return job_done("Ended Discover for $host"); diff --git a/Netdisco/lib/App/Netdisco/Util/DiscoverAndStore.pm b/Netdisco/lib/App/Netdisco/Util/DiscoverAndStore.pm index bcb4dd74..2fb815f2 100644 --- a/Netdisco/lib/App/Netdisco/Util/DiscoverAndStore.pm +++ b/Netdisco/lib/App/Netdisco/Util/DiscoverAndStore.pm @@ -10,7 +10,7 @@ use base 'Exporter'; our @EXPORT = (); our @EXPORT_OK = qw/ store_device store_interfaces store_wireless - store_vlans + store_vlans store_power /; our %EXPORT_TAGS = (all => \@EXPORT_OK); @@ -342,4 +342,73 @@ sub store_vlans { }); } +=head2 store_power( $device, $snmp ) + +Given a Device database object, and a working SNMP connection, discover and +store the device's PoE information. + +The Device database object can be a fresh L object which is +not yet stored to the database. + +=cut + +sub store_power { + my ($device, $snmp) = @_; + + my $p_watts = $snmp->peth_power_watts; + my $p_status = $snmp->peth_power_status; + + if (!defined $p_watts) { + # TODO log + return; + } + + # build device module power info suitable for DBIC + my @devicepower; + foreach my $entry (keys %$p_watts) { + push @devicepower, { + module => $entry, + power => $p_watts->{$entry}, + status => $p_status->{$entry}, + }; + } + + my $interfaces = $snmp->interfaces; + my $p_ifindex = $snmp->peth_port_ifindex; + my $p_admin = $snmp->peth_port_admin; + my $p_pstatus = $snmp->peth_port_status; + my $p_class = $snmp->peth_port_class; + my $p_power = $snmp->peth_port_power; + + # build device port power info suitable for DBIC + my %portpower; + foreach my $entry (keys %$p_ifindex) { + my $port = $interfaces->{ $p_ifindex->{$entry} }; + next unless $port; + + my ($module) = split m/\./, $entry; + $portpower{$port} = []; + + push @{$portpower{$port}}, { + module => $module, + admin => $p_admin->{$entry}, + status => $p_pstatus->{$entry}, + class => $p_class->{$entry}, + power => $p_power->{$entry}, + + }; + } + + schema('netdisco')->txn_do(sub { + $device->power_modules->delete; + $device->power_modules->populate(\@devicepower); + + foreach my $port (keys %portpower) { + my $port = $device->ports->find({port => $port}); + $port->power->delete if $port->power; + $port->create_related('power', $portpower{$port}); + } + }); +} + 1;