« blib/lib/App/Netdisco/Worker/Plugin/Macsuck/Hooks.pm
↑ Summary
blib/lib/App/Netdisco/Worker/Plugin/Macsuck/PortAccessEntity.pm »
| 1 | package App::Netdisco::Worker::Plugin::Macsuck::InterfacesStatus; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use App::Netdisco::Worker::Plugin; | ||
| 5 | use aliased 'App::Netdisco::Worker::Status'; | ||
| 6 | use Dancer::Plugin::DBIC 'schema'; | ||
| 7 | |||
| 8 | register_worker({ phase => 'early', | ||
| 9 | title => 'prepare to keep reported port status'}, sub { | ||
| 10 | |||
| 11 | vars->{'port_status_reported'} = {}; | ||
| 12 | }); | ||
| 13 | |||
| 14 | register_worker({ phase => 'main', driver => 'snmp', | ||
| 15 | title => 'gather interfaces status from snmp'}, sub { | ||
| 16 | |||
| 17 | my ($job, $workerconf) = @_; | ||
| 18 | my $device = $job->device; | ||
| 19 | |||
| 20 | my $snmp = App::Netdisco::Transport::SNMP->reader_for($device) | ||
| 21 | or return Status->info("skip: could not SNMP connect to $device"); | ||
| 22 | |||
| 23 | my $interfaces = $snmp->interfaces || {}; | ||
| 24 | my $reverse_interfaces = { reverse %{ $interfaces } }; # might squash but prob not | ||
| 25 | |||
| 26 | my $i_up = $snmp->i_up; | ||
| 27 | my $i_up_admin = $snmp->i_up_admin; | ||
| 28 | |||
| 29 | # make sure ports reflect their latest state as reported by device | ||
| 30 | foreach my $port (keys %{ vars->{'device_ports'} }) { | ||
| 31 | my $iid = $reverse_interfaces->{$port} or next; | ||
| 32 | |||
| 33 | debug sprintf ' [%s] macsuck - updating port %s status : %s/%s', | ||
| 34 | $device->ip, $port, ($i_up_admin->{$iid} || '-'), ($i_up->{$iid} || '-'); | ||
| 35 | |||
| 36 | vars->{'device_ports'}->{$port}->set_column(up => $i_up->{$iid}); | ||
| 37 | vars->{'device_ports'}->{$port}->set_column(up_admin => $i_up_admin->{$iid}); | ||
| 38 | vars->{'port_status_reported'}->{$port} = 1 if defined $i_up->{$iid}; | ||
| 39 | } | ||
| 40 | |||
| 41 | return Status->info('interfaces status from snmp complete'); | ||
| 42 | }); | ||
| 43 | |||
| 44 | register_worker({ phase => 'store', | ||
| 45 | title => 'update interfaces status in database'}, sub { | ||
| 46 | |||
| 47 | my ($job, $workerconf) = @_; | ||
| 48 | my $device = $job->device; | ||
| 49 | |||
| 50 | # a port with nodes is up, for data that carries no port status (offline | ||
| 51 | # submissions, ports the device did not report); the device's own status | ||
| 52 | # wins, as port security and self MACs leave entries on down ports. a lag | ||
| 53 | # master is skipped: we can still see nodes without a functioning aggregate | ||
| 54 | |||
| 55 | my %port_seen = (); | ||
| 56 | foreach my $vlan (reverse sort keys %{ vars->{'fwtable'} }) { | ||
| 57 | foreach my $port (keys %{ vars->{'fwtable'}->{$vlan} }) { | ||
| 58 | next if $port_seen{$port}; | ||
| 59 | ++$port_seen{$port}; | ||
| 60 | |||
| 61 | next unless scalar keys %{ vars->{'fwtable'}->{$vlan}->{$port} }; | ||
| 62 | next unless exists vars->{'device_ports'}->{$port}; | ||
| 63 | next if vars->{'port_status_reported'}->{$port}; | ||
| 64 | next if vars->{'device_ports'}->{$port}->is_master; | ||
| 65 | |||
| 66 | debug sprintf ' [%s] macsuck - updating port %s status up/up due to node presence', | ||
| 67 | $device->ip, $port; | ||
| 68 | |||
| 69 | vars->{'device_ports'}->{$port}->set_column(up => 'up'); | ||
| 70 | vars->{'device_ports'}->{$port}->set_column(up_admin => 'up'); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | my $updated = 0; | ||
| 75 | foreach my $port (values %{ vars->{'device_ports'} }) { | ||
| 76 | next unless $port->is_changed(); | ||
| 77 | $port->update(); | ||
| 78 | ++$updated; | ||
| 79 | } | ||
| 80 | |||
| 81 | return Status->info(sprintf '%s interfaces status updated in database', $updated); | ||
| 82 | }); | ||
| 83 | |||
| 84 | true; |