diff --git a/bin/netdisco-do b/bin/netdisco-do index e0d68bb2..44694006 100755 --- a/bin/netdisco-do +++ b/bin/netdisco-do @@ -35,17 +35,18 @@ BEGIN { # for netdisco app config use App::Netdisco; -use App::Netdisco::Backend::Job; use Dancer qw/:moose :script/; info "App::Netdisco version $App::Netdisco::VERSION loaded."; -use NetAddr::IP qw/:rfc3021 :lower/; -use App::Netdisco::Util::Device 'get_device'; - use Try::Tiny; use Pod::Usage; use Scalar::Util 'blessed'; +use NetAddr::IP qw/:rfc3021 :lower/; + +use App::Netdisco::Backend::Job; +use App::Netdisco::Util::Device 'get_device'; + use Getopt::Long; Getopt::Long::Configure ("bundling"); @@ -89,90 +90,11 @@ unless ($action) { ); } -# create worker (placeholder object for the role methods) +# create worker (placeholder object for the action runner) { package MyWorker; - use Moo; - use Module::Load (); - use Data::Printer (); - use Scalar::Util 'blessed'; - use NetAddr::IP qw/:rfc3021 :lower/; - use Dancer ':script'; - - use App::Netdisco::Transport::SNMP; - use App::Netdisco::Util::Device - qw/get_device delete_device renumber_device/; - with 'App::Netdisco::Worker::Runner'; - - eval { Module::Load::load 'App::Netdisco::Util::Graph' }; - sub graph { - App::Netdisco::Util::Graph::graph(); - return ('done', 'Generated graph data.'); - } - - use App::Netdisco::Util::NodeMonitor (); - sub monitor { - App::Netdisco::Util::NodeMonitor::monitor(); - return ('done', 'Generated monitor data.'); - } - - use App::Netdisco::Util::Statistics (); - sub stats { - App::Netdisco::Util::Statistics::update_stats(); - return ('done', 'Updated statistics.'); - } - - sub show { - my ($self, $job) = @_; - my ($device, $port, $extra) = map {$job->$_} qw/device port extra/; - return ('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 ('done', sprintf "Showed %s response from %s.", $extra, $device->ip); - } - - sub delete { - my ($self, $job) = @_; - my ($device, $port, $extra) = map {$job->$_} qw/device port extra/; - return ('error', 'Missing device (-d).') if !defined $device; - - $port = ($port ? 1 : 0); - delete_device($device, $port, $extra); - return ('done', sprintf "Deleted device %s.", $device->ip); - } - - sub renumber { - my ($self, $job) = @_; - my ($device, $port, $extra) = map {$job->$_} qw/device port extra/; - return ('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 ('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 ('error', sprintf "Already know new device as: %s.", $new_dev->ip); - } - - renumber_device($device, $new_ip); - return ('done', sprintf 'Renumbered device %s to %s (%s).', - $device->ip, $new_ip, ($device->dns || '')); - } } my $worker = MyWorker->new(); diff --git a/lib/App/Netdisco/Worker/Plugin/Delete.pm b/lib/App/Netdisco/Worker/Plugin/Delete.pm new file mode 100644 index 00000000..b2f857de --- /dev/null +++ b/lib/App/Netdisco/Worker/Plugin/Delete.pm @@ -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; diff --git a/lib/App/Netdisco/Worker/Plugin/Graph.pm b/lib/App/Netdisco/Worker/Plugin/Graph.pm new file mode 100644 index 00000000..4d677df9 --- /dev/null +++ b/lib/App/Netdisco/Worker/Plugin/Graph.pm @@ -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; diff --git a/lib/App/Netdisco/Worker/Plugin/Monitor.pm b/lib/App/Netdisco/Worker/Plugin/Monitor.pm new file mode 100644 index 00000000..ddf5de29 --- /dev/null +++ b/lib/App/Netdisco/Worker/Plugin/Monitor.pm @@ -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; diff --git a/lib/App/Netdisco/Worker/Plugin/Renumber.pm b/lib/App/Netdisco/Worker/Plugin/Renumber.pm new file mode 100644 index 00000000..5e3f9007 --- /dev/null +++ b/lib/App/Netdisco/Worker/Plugin/Renumber.pm @@ -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; diff --git a/lib/App/Netdisco/Worker/Plugin/Show.pm b/lib/App/Netdisco/Worker/Plugin/Show.pm new file mode 100644 index 00000000..0b1d3e7f --- /dev/null +++ b/lib/App/Netdisco/Worker/Plugin/Show.pm @@ -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; diff --git a/lib/App/Netdisco/Worker/Plugin/Stats.pm b/lib/App/Netdisco/Worker/Plugin/Stats.pm new file mode 100644 index 00000000..3dbc4eae --- /dev/null +++ b/lib/App/Netdisco/Worker/Plugin/Stats.pm @@ -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; diff --git a/share/config.yml b/share/config.yml index 160c2d92..30f259f2 100644 --- a/share/config.yml +++ b/share/config.yml @@ -279,6 +279,13 @@ worker_plugins: - Arpnip::Nodes::RFC - Arpnip::Subnets::RFC - NetBIOS::Nbtstat::RFC + - Graph + - Stats + - Monitor + - Delete + - Show + - Psql + - Renumber # --------------- # GraphViz Export