« blib/lib/App/Netdisco/Web/Statistics.pm
↑ Summary
blib/lib/App/Netdisco/Worker/Plugin/Vlan/Core.pm »
| 1 | package App::Netdisco::Worker::Plugin::PortControl; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use App::Netdisco::Worker::Plugin; | ||
| 5 | use aliased 'App::Netdisco::Worker::Status'; | ||
| 6 | |||
| 7 | use App::Netdisco::Transport::SNMP; | ||
| 8 | use App::Netdisco::Util::Port ':all'; | ||
| 9 | |||
| 10 | register_worker({ phase => 'check' }, sub { | ||
| 11 | my ($job, $workerconf) = @_; | ||
| 12 | my ($device, $port, $data) = map {$job->$_} qw/device port extra/; | ||
| 13 | |||
| 14 | return Status->error('PortControl failed: unable to interpret device param') | ||
| 15 | unless defined $device; | ||
| 16 | |||
| 17 | return Status->error("PortControl skipped: $device not yet discovered") | ||
| 18 | unless $device->in_storage; | ||
| 19 | |||
| 20 | return Status->error('Missing port (-p).') unless defined $job->port; | ||
| 21 | |||
| 22 | vars->{'port'} = get_port($device, $port) | ||
| 23 | or return Status->error("Unknown port name [$port] on device $device"); | ||
| 24 | |||
| 25 | return Status->error('Missing status (-e).') unless defined $job->subaction; | ||
| 26 | |||
| 27 | return Status->error("Permission denied to change port status") | ||
| 28 | unless port_acl_service(vars->{'port'}, $device, $job->username); | ||
| 29 | |||
| 30 | return Status->done('PortControl is able to run'); | ||
| 31 | }); | ||
| 32 | |||
| 33 | register_worker({ phase => 'main', driver => 'snmp' }, sub { | ||
| 34 | my ($job, $workerconf) = @_; | ||
| 35 | my ($device, $pn) = map {$job->$_} qw/device port/; | ||
| 36 | |||
| 37 | # need to remove "-other" which appears for power/portcontrol | ||
| 38 | (my $sa = $job->subaction) =~ s/-\w+//; | ||
| 39 | $job->subaction($sa); | ||
| 40 | |||
| 41 | if ($sa eq 'bounce') { | ||
| 42 | $job->subaction('down'); | ||
| 43 | my $status = _action($job); | ||
| 44 | return $status if $status->not_ok; | ||
| 45 | $job->subaction('up'); | ||
| 46 | } | ||
| 47 | |||
| 48 | return _action($job); | ||
| 49 | }); | ||
| 50 | |||
| 51 | sub _action { | ||
| 52 | my $job = shift; | ||
| 53 | my ($device, $pn, $data) = map {$job->$_} qw/device port subaction/; | ||
| 54 | |||
| 55 | # snmp connect using rw community | ||
| 56 | my $snmp = App::Netdisco::Transport::SNMP->writer_for($device) | ||
| 57 | or return Status->defer("failed to connect to $device to update up_admin"); | ||
| 58 | |||
| 59 | my $iid = get_iid($snmp, vars->{'port'}) | ||
| 60 | or return Status->error("Failed to get port ID for [$pn] from $device"); | ||
| 61 | |||
| 62 | my $rv = $snmp->set_i_up_admin($data, $iid); | ||
| 63 | |||
| 64 | if (!defined $rv) { | ||
| 65 | return Status->error(sprintf "Failed to set [%s] up_admin to [%s] on $device: %s", | ||
| 66 | $pn, $data, ($snmp->error || '')); | ||
| 67 | } | ||
| 68 | |||
| 69 | # confirm the set happened | ||
| 70 | $snmp->clear_cache; | ||
| 71 | my $state = ($snmp->i_up_admin($iid) || ''); | ||
| 72 | if (ref {} ne ref $state or $state->{$iid} ne $data) { | ||
| 73 | return Status->error("Verify of [$pn] up_admin failed on $device"); | ||
| 74 | } | ||
| 75 | |||
| 76 | # update netdisco DB | ||
| 77 | vars->{'port'}->update({up_admin => $data}); | ||
| 78 | |||
| 79 | return Status->done("Updated [$pn] up_admin on [$device] to [$data]"); | ||
| 80 | } | ||
| 81 | |||
| 82 | true; |