78 lines
2.4 KiB
Perl
78 lines
2.4 KiB
Perl
package App::Netdisco::Worker::Plugin::PortControl;
|
|
|
|
use Dancer ':syntax';
|
|
use App::Netdisco::Worker::Plugin;
|
|
use aliased 'App::Netdisco::Worker::Status';
|
|
|
|
use App::Netdisco::Transport::SNMP;
|
|
use App::Netdisco::Util::Port ':all';
|
|
|
|
register_worker({ stage => 'check' }, sub {
|
|
my ($job, $workerconf) = @_;
|
|
return Status->error('Missing device (-d).') unless defined $job->device;
|
|
return Status->error('Missing port (-p).') unless defined $job->port;
|
|
return Status->error('Missing status (-e).') unless defined $job->subaction;
|
|
|
|
my $port = get_port($job->device, $job->port)
|
|
or return Status->error(sprintf "Unknown port name [%s] on device %s",
|
|
$job->port, $job->device);
|
|
|
|
my $vlan_reconfig_check = vlan_reconfig_check($port);
|
|
return Status->error("Cannot alter vlan: $vlan_reconfig_check")
|
|
if $vlan_reconfig_check;
|
|
|
|
return Status->done('PortControl is able to run');
|
|
});
|
|
|
|
register_worker({ stage => 'main' }, sub {
|
|
my ($job, $workerconf) = @_;
|
|
my ($device, $pn) = map {$job->$_} qw/device port/;
|
|
my $port = get_port($device, $pn);
|
|
|
|
# need to remove "-other" which appears for power/portcontrol
|
|
(my $sa = $job->subaction) =~ s/-\w+//;
|
|
$job->subaction($sa);
|
|
|
|
if ($sa eq 'bounce') {
|
|
$job->subaction('down');
|
|
my $status = _action($job, $port);
|
|
return $status if $status->not_ok;
|
|
$job->subaction('up');
|
|
}
|
|
|
|
return _action($job, $port);
|
|
});
|
|
|
|
sub _action {
|
|
my ($job, $port) = @_;
|
|
my ($device, $pn, $data) = map {$job->$_} qw/device port subaction/;
|
|
|
|
# snmp connect using rw community
|
|
my $snmp = App::Netdisco::Transport::SNMP->writer_for($device)
|
|
or return Status->defer("failed to connect to $device to update up_admin");
|
|
|
|
my $iid = get_iid($snmp, $port)
|
|
or return Status->error("Failed to get port ID for [$pn] from $device");
|
|
|
|
my $rv = $snmp->set_i_up_admin($data, $iid);
|
|
|
|
if (!defined $rv) {
|
|
return Status->error(sprintf 'Failed to set [%s] up_admin to [%s] on $device: %s',
|
|
$pn, $data, ($snmp->error || ''));
|
|
}
|
|
|
|
# confirm the set happened
|
|
$snmp->clear_cache;
|
|
my $state = ($snmp->i_up_admin($iid) || '');
|
|
if (ref {} ne ref $state or $state->{$iid} ne $data) {
|
|
return Status->error("Verify of [$pn] up_admin failed on $device");
|
|
}
|
|
|
|
# update netdisco DB
|
|
$port->update({up_admin => $data});
|
|
|
|
return Status->done("Updated [$pn] up_admin on [$device] to [$data]");
|
|
}
|
|
|
|
true;
|