move all netdisco-do action to worker plugins

This commit is contained in:
Oliver Gorwits
2017-09-06 21:23:54 +01:00
parent 707fc82b99
commit e7508a9eca
8 changed files with 140 additions and 84 deletions

View File

@@ -0,0 +1,19 @@
package App::Netdisco::Worker::Plugin::Delete;
use Dancer ':syntax';
use App::Netdisco::Worker::Plugin;
use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Util::Device 'delete_device';
register_worker({ primary => true }, sub {
my ($job, $workerconf) = @_;
my ($device, $port, $extra) = map {$job->$_} qw/device port extra/;
return Status->error('Missing device (-d).') if !defined $device;
$port = ($port ? 1 : 0);
delete_device($device, $port, $extra);
return Status->done(sprintf "Deleted device %s.", $device->ip);
});
true;

View File

@@ -0,0 +1,15 @@
package App::Netdisco::Worker::Plugin::Graph;
use Dancer ':syntax';
use App::Netdisco::Worker::Plugin;
use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Util::Graph ();
register_worker({ primary => true }, sub {
my ($job, $workerconf) = @_;
App::Netdisco::Util::Graph::graph();
return Status->done('Generated graph data.');
});
true;

View File

@@ -0,0 +1,15 @@
package App::Netdisco::Worker::Plugin::Monitor;
use Dancer ':syntax';
use App::Netdisco::Worker::Plugin;
use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Util::NodeMonitor ();
register_worker({ primary => true }, sub {
my ($job, $workerconf) = @_;
App::Netdisco::Util::NodeMonitor::monitor();
return Status->done('Generated monitor data.');
});
true;

View File

@@ -0,0 +1,31 @@
package App::Netdisco::Worker::Plugin::Renumber;
use Dancer ':syntax';
use App::Netdisco::Worker::Plugin;
use aliased 'App::Netdisco::Worker::Status';
use NetAddr::IP qw/:rfc3021 :lower/;
use App::Netdisco::Util::Device qw/get_device renumber_device/;
register_worker({ primary => true }, sub {
my ($job, $workerconf) = @_;
my ($device, $port, $extra) = map {$job->$_} qw/device port extra/;
return Status->error('Missing device (-d).') if !defined $device;
my $old_ip = $device->ip;
my $new_ip = NetAddr::IP->new($extra);
unless ($new_ip and $new_ip->addr ne '0.0.0.0') {
return Status->error("Bad host or IP: ".($extra || '0.0.0.0'));
}
my $new_dev = get_device($new_ip->addr);
if ($new_dev and $new_dev->in_storage and ($new_dev->ip ne $device->ip)) {
return Status->error(sprintf "Already know new device as: %s.", $new_dev->ip);
}
renumber_device($device, $new_ip);
return Status->done(sprintf 'Renumbered device %s to %s (%s).',
$device->ip, $new_ip, ($device->dns || ''));
});
true;

View File

@@ -0,0 +1,32 @@
package App::Netdisco::Worker::Plugin::Show;
use Dancer ':syntax';
use App::Netdisco::Worker::Plugin;
use aliased 'App::Netdisco::Worker::Status';
use Data::Printer ();
use App::Netdisco::Transport::SNMP;
register_worker({ primary => true }, sub {
my ($job, $workerconf) = @_;
my ($device, $port, $extra) = map {$job->$_} qw/device port extra/;
return Status->error('Missing device (-d).') if !defined $device;
$extra ||= 'interfaces'; my $class = undef;
($class, $extra) = split(/::([^:]+)$/, $extra);
if ($class and $extra) {
$class = 'SNMP::Info::'.$class;
}
else {
$extra = $class;
undef $class;
}
my $i = App::Netdisco::Transport::SNMP->reader_for($device, $class);
Data::Printer::p($i->$extra);
return Status->done(
sprintf "Showed %s response from %s.", $extra, $device->ip);
});
true;

View File

@@ -0,0 +1,15 @@
package App::Netdisco::Worker::Plugin::Stats;
use Dancer ':syntax';
use App::Netdisco::Worker::Plugin;
use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Util::Statistics ();
register_worker({ primary => true }, sub {
my ($job, $workerconf) = @_;
App::Netdisco::Util::Statistics::update_stats();
return Status->done('Updated statistics.');
});
true;