blib/lib/App/Netdisco/Worker/Plugin/Macsuck/InterfacesStatus.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 4 Podpod n/a totaltotal 0.0% 0 / 85 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
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 => 'main', driver => 'snmp',
9 title => 'gather interfaces status from snmp'}, sub {
10  
11 my ($job, $workerconf) = @_;
12 my $device = $job->device;
13  
14 my $snmp = App::Netdisco::Transport::SNMP->reader_for($device)
15 or return Status->info("skip: could not SNMP connect to $device");
16  
17 my $interfaces = $snmp->interfaces || {};
18 my $reverse_interfaces = { reverse %{ $interfaces } }; # might squash but prob not
19  
20 my $i_up = $snmp->i_up;
21 my $i_up_admin = $snmp->i_up_admin;
22  
23 # make sure ports reflect their latest state as reported by device
24 foreach my $port (keys %{ vars->{'device_ports'} }) {
25 my $iid = $reverse_interfaces->{$port} or next;
26  
27 debug sprintf ' [%s] macsuck - updating port %s status : %s/%s',
28 $device->ip, $port, ($i_up_admin->{$iid} || '-'), ($i_up->{$iid} || '-');
29  
30 vars->{'device_ports'}->{$port}->set_column(up => $i_up->{$iid});
31 vars->{'device_ports'}->{$port}->set_column(up_admin => $i_up_admin->{$iid});
32 }
33  
34 return Status->info('interfaces status from snmp complete');
35 });
36  
37 register_worker({ phase => 'store',
38 title => 'update interfaces status in database'}, sub {
39  
40 my ($job, $workerconf) = @_;
41 my $device = $job->device;
42  
43 # make sure ports are UP in netdisco (unless it's a lag master,
44 # because we can still see nodes without a functioning aggregate)
45  
46 my %port_seen = ();
47 foreach my $vlan (reverse sort keys %{ vars->{'fwtable'} }) {
48 foreach my $port (keys %{ vars->{'fwtable'}->{$vlan} }) {
49 next if $port_seen{$port};
50 ++$port_seen{$port};
51  
52 next unless scalar keys %{ vars->{'fwtable'}->{$vlan}->{$port} };
53 next unless exists vars->{'device_ports'}->{$port};
54 next if vars->{'device_ports'}->{$port}->is_master;
55  
56 debug sprintf ' [%s] macsuck - updating port %s status up/up due to node presence',
57 $device->ip, $port;
58  
59 vars->{'device_ports'}->{$port}->set_column(up => 'up');
60 vars->{'device_ports'}->{$port}->set_column(up_admin => 'up');
61 }
62 }
63  
64 my $updated = 0;
65 foreach my $port (values %{ vars->{'device_ports'} }) {
66 next unless $port->is_changed();
67 $port->update();
68 ++$updated;
69 }
70  
71 return Status->info(sprintf '%s interfaces status updated in database', $updated);
72 });
73  
74 true;