diff --git a/lib/App/Netdisco/Backend/Worker/Interactive/DeviceActions.pm b/lib/App/Netdisco/Backend/Worker/Interactive/DeviceActions.pm deleted file mode 100644 index 262efcbc..00000000 --- a/lib/App/Netdisco/Backend/Worker/Interactive/DeviceActions.pm +++ /dev/null @@ -1,50 +0,0 @@ -package App::Netdisco::Backend::Worker::Interactive::DeviceActions; - -use App::Netdisco::Transport::SNMP; -use App::Netdisco::Util::Device 'get_device'; -use App::Netdisco::Backend::Util ':all'; - -use Role::Tiny; -use namespace::clean; - -sub location { - my ($self, $job) = @_; - return _set_device_generic($job->device, 'location', $job->subaction); -} - -sub contact { - my ($self, $job) = @_; - return _set_device_generic($job->device, 'contact', $job->subaction); -} - -sub _set_device_generic { - my ($ip, $slot, $data) = @_; - $data ||= ''; - - # snmp connect using rw community - my $info = App::Netdisco::Transport::SNMP->writer_for($ip) - or return job_defer("Failed to connect to device [$ip] to update $slot"); - - my $method = 'set_'. $slot; - my $rv = $info->$method($data); - - if (!defined $rv) { - return job_error(sprintf 'Failed to set %s on [%s]: %s', - $slot, $ip, ($info->error || '')); - } - - # confirm the set happened - $info->clear_cache; - my $new_data = ($info->$slot || ''); - if ($new_data ne $data) { - return job_error("Verify of $slot update failed on [$ip]: $new_data"); - } - - # update netdisco DB - my $device = get_device($ip); - $device->update({$slot => $data}); - - return job_done("Updated $slot on [$ip] to [$data]"); -} - -1; diff --git a/lib/App/Netdisco/Backend/Worker/Interactive/PortActions.pm b/lib/App/Netdisco/Backend/Worker/Interactive/PortActions.pm deleted file mode 100644 index 767f71ad..00000000 --- a/lib/App/Netdisco/Backend/Worker/Interactive/PortActions.pm +++ /dev/null @@ -1,159 +0,0 @@ -package App::Netdisco::Backend::Worker::Interactive::PortActions; - -use App::Netdisco::Util::Port ':all'; -use App::Netdisco::Transport::SNMP; -use App::Netdisco::Util::Device 'get_device'; -use App::Netdisco::Backend::Util ':all'; - -use Role::Tiny; -use namespace::clean; - -sub portname { - my ($self, $job) = @_; - return _set_port_generic($job, 'alias', 'name'); -} - -sub portcontrol { - my ($self, $job) = @_; - - my $port = get_port($job->device, $job->port) - or return job_error(sprintf "Unknown port name [%s] on device [%s]", - $job->port, $job->device); - - my $reconfig_check = port_reconfig_check($port); - return job_error("Cannot alter port: $reconfig_check") - if $reconfig_check; - - # 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 @stat = _set_port_generic($job, 'up_admin'); - return @stat if $stat[0] ne 'done'; - $job->subaction('up'); - return _set_port_generic($job, 'up_admin'); - } - else { - return _set_port_generic($job, 'up_admin'); - } -} - -sub vlan { - my ($self, $job) = @_; - - my $port = get_port($job->device, $job->port) - or return job_error(sprintf "Unknown port name [%s] on device [%s]", - $job->port, $job->device); - - my $port_reconfig_check = port_reconfig_check($port); - return job_error("Cannot alter port: $port_reconfig_check") - if $port_reconfig_check; - - my $vlan_reconfig_check = vlan_reconfig_check($port); - return job_error("Cannot alter vlan: $vlan_reconfig_check") - if $vlan_reconfig_check; - - my @stat = _set_port_generic($job, 'pvid'); # for Cisco trunk - return @stat if $stat[0] eq 'done'; - return _set_port_generic($job, 'vlan'); -} - -sub _set_port_generic { - my ($job, $slot, $column) = @_; - $column ||= $slot; - - my $device = get_device($job->device); - my $ip = $device->ip; - my $pn = $job->port; - my $data = $job->subaction; - - my $port = get_port($ip, $pn) - or return job_error("Unknown port name [$pn] on device [$ip]"); - - if ($device->vendor ne 'netdisco') { - # snmp connect using rw community - my $info = App::Netdisco::Transport::SNMP->writer_for($ip) - or return job_defer("Failed to connect to device [$ip] to control port"); - - my $iid = get_iid($info, $port) - or return job_error("Failed to get port ID for [$pn] from [$ip]"); - - my $method = 'set_i_'. $slot; - my $rv = $info->$method($data, $iid); - - if (!defined $rv) { - return job_error(sprintf 'Failed to set [%s] %s to [%s] on [%s]: %s', - $pn, $slot, $data, $ip, ($info->error || '')); - } - - # confirm the set happened - $info->clear_cache; - my $check_method = 'i_'. $slot; - my $state = ($info->$check_method($iid) || ''); - if (ref {} ne ref $state or $state->{$iid} ne $data) { - return job_error("Verify of [$pn] $slot failed on [$ip]"); - } - } - - # update netdisco DB - $port->update({$column => $data}); - - return job_done("Updated [$pn] $slot status on [$ip] to [$data]"); -} - -sub power { - my ($self, $job) = @_; - - my $port = get_port($job->device, $job->port) - or return job_error(sprintf "Unknown port name [%s] on device [%s]", - $job->port, $job->device); - - return job_error("No PoE service on port [%s] on device [%s]") - unless $port->power; - - my $reconfig_check = port_reconfig_check($port); - return job_error("Cannot alter port: $reconfig_check") - if $reconfig_check; - - my $device = get_device($job->device); - my $ip = $device->ip; - my $pn = $job->port; - - # munge data - (my $data = $job->subaction) =~ s/-\w+//; # remove -other - $data = 'true' if $data =~ m/^(on|yes|up)$/; - $data = 'false' if $data =~ m/^(off|no|down)$/; - - # snmp connect using rw community - my $info = App::Netdisco::Transport::SNMP->writer_for($ip) - or return job_defer("Failed to connect to device [$ip] to control power"); - - my $powerid = get_powerid($info, $port) - or return job_error("Failed to get power ID for [$pn] from [$ip]"); - - my $rv = $info->set_peth_port_admin($data, $powerid); - - if (!defined $rv) { - return job_error(sprintf 'Failed to set [%s] power to [%s] on [%s]: %s', - $pn, $data, $ip, ($info->error || '')); - } - - # confirm the set happened - $info->clear_cache; - my $state = ($info->peth_port_admin($powerid) || ''); - if (ref {} ne ref $state or $state->{$powerid} ne $data) { - return job_error("Verify of [$pn] power failed on [$ip]"); - } - - # update netdisco DB - $port->power->update({ - admin => $data, - status => ($data eq 'false' ? 'disabled' : 'searching'), - }); - - return job_done("Updated [$pn] power status on [$ip] to [$data]"); -} - -1; diff --git a/lib/App/Netdisco/Worker/Plugin/Contact.pm b/lib/App/Netdisco/Worker/Plugin/Contact.pm new file mode 100644 index 00000000..4095f181 --- /dev/null +++ b/lib/App/Netdisco/Worker/Plugin/Contact.pm @@ -0,0 +1,38 @@ +package App::Netdisco::Worker::Plugin::Contact; + +use Dancer ':syntax'; +use App::Netdisco::Worker::Plugin; +use aliased 'App::Netdisco::Worker::Status'; + +use App::Netdisco::Transport::SNMP; + +register_worker({ stage => 'check' }, sub { + my ($job, $workerconf) = @_; + my ($device, $data) = map {$job->$_} qw/device extra/; + return Status->error('Missing device (-d).') if !defined $device; + + # 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 contact"); + + my $rv = $snmp->set_contact($data); + + if (!defined $rv) { + return Status->error( + "failed to set contact on $device: ". ($snmp->error || '')); + } + + # confirm the set happened + $snmp->clear_cache; + my $new_data = ($snmp->contact || ''); + if ($new_data ne $data) { + return Status->error("verify of contact failed on $device: $new_data"); + } + + # update netdisco DB + $device->update({contact => $data}); + + return Status->done("Updated contact on $device to [$data]"); +}); + +true; diff --git a/lib/App/Netdisco/Worker/Plugin/Location.pm b/lib/App/Netdisco/Worker/Plugin/Location.pm new file mode 100644 index 00000000..c3868b38 --- /dev/null +++ b/lib/App/Netdisco/Worker/Plugin/Location.pm @@ -0,0 +1,38 @@ +package App::Netdisco::Worker::Plugin::Location; + +use Dancer ':syntax'; +use App::Netdisco::Worker::Plugin; +use aliased 'App::Netdisco::Worker::Status'; + +use App::Netdisco::Transport::SNMP; + +register_worker({ stage => 'check' }, sub { + my ($job, $workerconf) = @_; + my ($device, $data) = map {$job->$_} qw/device extra/; + return Status->error('Missing device (-d).') if !defined $device; + + # 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 location"); + + my $rv = $snmp->set_location($data); + + if (!defined $rv) { + return Status->error( + "failed to set location on $device: ". ($snmp->error || '')); + } + + # confirm the set happened + $snmp->clear_cache; + my $new_data = ($snmp->location || ''); + if ($new_data ne $data) { + return Status->error("verify of location failed on $device: $new_data"); + } + + # update netdisco DB + $device->update({location => $data}); + + return Status->done("Updated location on $device to [$data]"); +}); + +true; diff --git a/lib/App/Netdisco/Worker/Plugin/PortControl.pm b/lib/App/Netdisco/Worker/Plugin/PortControl.pm new file mode 100644 index 00000000..0efb0c1a --- /dev/null +++ b/lib/App/Netdisco/Worker/Plugin/PortControl.pm @@ -0,0 +1,69 @@ +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) = @_; + my ($device, $pn) = map {$job->$_} qw/device port/; + return Status->error('Missing device (-d).') if !defined $device; + return Status->error('Missing port (-p).') if !defined $pn; + return Status->error('Missing status (-e).') if !defined $job->subaction; + + my $port = get_port($device, $pn) + or return Status->error("Unknown port name [$pn] on device $device"); + + my $port_reconfig_check = port_reconfig_check($port); + return Status->error("Cannot alter port: $port_reconfig_check") + if $port_reconfig_check; + + # 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; diff --git a/lib/App/Netdisco/Worker/Plugin/PortName.pm b/lib/App/Netdisco/Worker/Plugin/PortName.pm new file mode 100644 index 00000000..b21301ba --- /dev/null +++ b/lib/App/Netdisco/Worker/Plugin/PortName.pm @@ -0,0 +1,47 @@ +package App::Netdisco::Worker::Plugin::PortName; + +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) = @_; + my ($device, $pn, $data) = map {$job->$_} qw/device port extra/; + return Status->error('Missing device (-d).') if !defined $device; + return Status->error('Missing port (-p).') if !defined $pn; + return Status->error('Missing name (-e).') if !defined $data; + + # 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 alias"); + + my $port = get_port($device, $pn) + or return Status->error("Unknown port name [$pn] on device $device"); + + my $iid = get_iid($snmp, $port) + or return Status->error("Failed to get port ID for [$pn] from $device"); + + my $rv = $snmp->set_i_alias($data, $iid); + + if (!defined $rv) { + return Status->error(sprintf 'Failed to set [%s] alias to [%s] on $device: %s', + $pn, $data, ($snmp->error || '')); + } + + # confirm the set happened + $snmp->clear_cache; + my $state = ($snmp->i_alias($iid) || ''); + if (ref {} ne ref $state or $state->{$iid} ne $data) { + return Status->error("Verify of [$pn] alias failed on $device"); + } + + # update netdisco DB + $port->update({name => $data}); + + return Status->done("Updated [$pn] alias on [$device] to [$data]"); +}); + +true; diff --git a/lib/App/Netdisco/Worker/Plugin/Power.pm b/lib/App/Netdisco/Worker/Plugin/Power.pm new file mode 100644 index 00000000..0e854226 --- /dev/null +++ b/lib/App/Netdisco/Worker/Plugin/Power.pm @@ -0,0 +1,62 @@ +package App::Netdisco::Worker::Plugin::Power; + +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) = @_; + my ($device, $pn) = map {$job->$_} qw/device port/; + return Status->error('Missing device (-d).') if !defined $device; + return Status->error('Missing port (-p).') if !defined $pn; + return Status->error('Missing status (-e).') if !defined $job->subaction; + + my $port = get_port($device, $pn) + or return Status->error("Unknown port name [$pn] on device $device"); + + return Status->error("No PoE service on port [$pn] on device $device") + unless $port->power; + + my $port_reconfig_check = port_reconfig_check($port); + return Status->error("Cannot alter port: $port_reconfig_check") + if $port_reconfig_check; + + # munge data + (my $data = $job->subaction) =~ s/-\w+//; # remove -other + $data = 'true' if $data =~ m/^(on|yes|up)$/; + $data = 'false' if $data =~ m/^(off|no|down)$/; + + # 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 vlan"); + + my $powerid = get_powerid($snmp, $port) + or return Status->error("failed to get power ID for [$pn] from $device"); + + my $rv = $snmp->set_peth_port_admin($data, $powerid); + + if (!defined $rv) { + return Status->error(sprintf 'failed to set [%s] power to [%s] on [%s]: %s', + $pn, $data, $device, ($snmp->error || '')); + } + + # confirm the set happened + $snmp->clear_cache; + my $state = ($snmp->peth_port_admin($powerid) || ''); + if (ref {} ne ref $state or $state->{$powerid} ne $data) { + return Status->error("Verify of [$pn] power failed on $device"); + } + + # update netdisco DB + $port->power->update({ + admin => $data, + status => ($data eq 'false' ? 'disabled' : 'searching'), + }); + + return Status->done("Updated [$pn] power status on $device to [$data]"); +}); + +true; diff --git a/lib/App/Netdisco/Worker/Plugin/Vlan.pm b/lib/App/Netdisco/Worker/Plugin/Vlan.pm new file mode 100644 index 00000000..6c69f0b2 --- /dev/null +++ b/lib/App/Netdisco/Worker/Plugin/Vlan.pm @@ -0,0 +1,99 @@ +package App::Netdisco::Worker::Plugin::Vlan; + +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) = @_; + my ($device, $pn, $data) = map {$job->$_} qw/device port extra/; + return Status->error('Missing device (-d).') if !defined $device; + return Status->error('Missing port (-p).') if !defined $pn; + return Status->error('Missing vlan (-e).') if !defined $data; + + my $port = get_port($device, $pn) + or return Status->error("Unknown port name [$pn] on device $device"); + + my $port_reconfig_check = port_reconfig_check($port); + return Status->error("Cannot alter port: $port_reconfig_check") + if $port_reconfig_check; + + my $vlan_reconfig_check = vlan_reconfig_check($port); + return Status->error("Cannot alter vlan: $vlan_reconfig_check") + if $vlan_reconfig_check; + + return Status->done("Check phase for update [$pn] vlan $data done."); +}); + +register_worker({ stage => 'main' }, sub { + my ($job, $workerconf) = @_; + my ($device, $pn, $data) = map {$job->$_} qw/device port extra/; + + # 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 pvid"); + + my $port = get_port($device, $pn) + or return Status->error("Unknown port name [$pn] on device $device"); + + my $iid = get_iid($snmp, $port) + or return Status->error("Failed to get port ID for [$pn] from $device"); + + my $rv = $snmp->set_i_pvid($data, $iid); + + if (!defined $rv) { + return Status->error(sprintf 'Failed to set [%s] pvid to [%s] on $device: %s', + $pn, $data, ($snmp->error || '')); + } + + # confirm the set happened + $snmp->clear_cache; + my $state = ($snmp->i_pvid($iid) || ''); + if (ref {} ne ref $state or $state->{$iid} ne $data) { + return Status->error("Verify of [$pn] pvid failed on $device"); + } + + # update netdisco DB + $port->update({pvid => $data}); + + return Status->done("Updated [$pn] pvid on [$device] to [$data]"); +}); + +register_worker({ stage => 'main' }, sub { + my ($job, $workerconf) = @_; + my ($device, $pn, $data) = map {$job->$_} qw/device port extra/; + + # 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 vlan"); + + my $port = get_port($device, $pn) + or return Status->error("Unknown port name [$pn] on device $device"); + + my $iid = get_iid($snmp, $port) + or return Status->error("Failed to get port ID for [$pn] from $device"); + + my $rv = $snmp->set_i_vlan($data, $iid); + + if (!defined $rv) { + return Status->error(sprintf 'Failed to set [%s] vlan to [%s] on $device: %s', + $pn, $data, ($snmp->error || '')); + } + + # confirm the set happened + $snmp->clear_cache; + my $state = ($snmp->i_vlan($iid) || ''); + if (ref {} ne ref $state or $state->{$iid} ne $data) { + return Status->error("Verify of [$pn] vlan failed on $device"); + } + + # update netdisco DB + $port->update({vlan => $data}); + + return Status->done("Updated [$pn] vlan on [$device] to [$data]"); +}); + +true;