move nbtstat and nbtwalk to worker plugins
This commit is contained in:
21
lib/App/Netdisco/Worker/Plugin/Nbtstat.pm
Normal file
21
lib/App/Netdisco/Worker/Plugin/Nbtstat.pm
Normal file
@@ -0,0 +1,21 @@
|
||||
package App::Netdisco::Worker::Plugin::Nbtstat;
|
||||
|
||||
use Dancer ':syntax';
|
||||
use App::Netdisco::Worker::Plugin;
|
||||
use aliased 'App::Netdisco::Worker::Status';
|
||||
|
||||
use App::Netdisco::Util::Device 'is_macsuckable';
|
||||
|
||||
register_worker({ primary => true }, sub {
|
||||
my ($job, $workerconf) = @_;
|
||||
|
||||
return Status->error('nbtstat failed: unable to interpret device param')
|
||||
if !defined $job->device;
|
||||
|
||||
return Status->defer(sprintf 'nbtstat deferred: %s is not macsuckable', $job->device->ip)
|
||||
unless is_macsuckable($job->device);
|
||||
|
||||
return Status->done('Nbtstat is able to run.');
|
||||
});
|
||||
|
||||
true;
|
||||
50
lib/App/Netdisco/Worker/Plugin/Nbtstat/Core.pm
Normal file
50
lib/App/Netdisco/Worker/Plugin/Nbtstat/Core.pm
Normal file
@@ -0,0 +1,50 @@
|
||||
package App::Netdisco::Worker::Plugin::Nbtstat::Core;
|
||||
|
||||
use Dancer ':syntax';
|
||||
use App::Netdisco::Worker::Plugin;
|
||||
use aliased 'App::Netdisco::Worker::Status';
|
||||
|
||||
use App::Netdisco::Util::Nbtstat qw/nbtstat_resolve_async store_nbt/;
|
||||
use App::Netdisco::Util::Node 'is_nbtstatable';
|
||||
use Dancer::Plugin::DBIC 'schema';
|
||||
use Time::HiRes 'gettimeofday';
|
||||
|
||||
register_worker({ primary => true }, sub {
|
||||
my ($job, $workerconf) = @_;
|
||||
my $host = $job->device->ip;
|
||||
|
||||
# get list of nodes on device
|
||||
my $interval = (setting('nbtstat_max_age') || 7) . ' day';
|
||||
my $rs = schema('netdisco')->resultset('NodeIp')->search({
|
||||
-bool => 'me.active',
|
||||
-bool => 'nodes.active',
|
||||
'nodes.switch' => $host,
|
||||
'me.time_last' => \[ '>= now() - ?::interval', $interval ],
|
||||
},{
|
||||
join => 'nodes',
|
||||
columns => 'ip',
|
||||
distinct => 1,
|
||||
})->ip_version(4);
|
||||
|
||||
my @ips = map {+{'ip' => $_}}
|
||||
grep { is_nbtstatable( $_ ) }
|
||||
$rs->get_column('ip')->all;
|
||||
|
||||
# Unless we have IPs don't bother
|
||||
if (scalar @ips) {
|
||||
my $now = 'to_timestamp('. (join '.', gettimeofday) .')';
|
||||
my $resolved_nodes = nbtstat_resolve_async(\@ips);
|
||||
|
||||
# update node_nbt with status entries
|
||||
foreach my $result (@$resolved_nodes) {
|
||||
if (defined $result->{'nbname'}) {
|
||||
store_nbt($result, $now);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Status->done("Ended nbtstat for $host");
|
||||
});
|
||||
|
||||
true;
|
||||
|
||||
31
lib/App/Netdisco/Worker/Plugin/Nbtwalk.pm
Normal file
31
lib/App/Netdisco/Worker/Plugin/Nbtwalk.pm
Normal file
@@ -0,0 +1,31 @@
|
||||
package App::Netdisco::Worker::Plugin::Nbtwalk;
|
||||
|
||||
use Dancer ':syntax';
|
||||
use App::Netdisco::Worker::Plugin;
|
||||
use aliased 'App::Netdisco::Worker::Status';
|
||||
|
||||
use App::Netdisco::JobQueue qw/jq_queued jq_insert/;
|
||||
use Dancer::Plugin::DBIC 'schema';
|
||||
|
||||
register_worker({ primary => true }, sub {
|
||||
my ($job, $workerconf) = @_;
|
||||
|
||||
my %queued = map {$_ => 1} jq_queued('nbtstat');
|
||||
my @devices = schema('netdisco')->resultset('Device')->search({
|
||||
-or => [ 'vendor' => undef, 'vendor' => { '!=' => 'netdisco' }],
|
||||
})->has_layer('2')->get_column('ip')->all;
|
||||
my @filtered_devices = grep {!exists $queued{$_}} @devices;
|
||||
|
||||
jq_insert([
|
||||
map {{
|
||||
device => $_,
|
||||
action => 'nbtstat',
|
||||
username => $job->username,
|
||||
userip => $job->userip,
|
||||
}} (@filtered_devices)
|
||||
]);
|
||||
|
||||
return Status->done('Queued nbtstat job for all devices');
|
||||
});
|
||||
|
||||
true;
|
||||
19
lib/App/Netdisco/Worker/Plugin/Test.pm
Normal file
19
lib/App/Netdisco/Worker/Plugin/Test.pm
Normal file
@@ -0,0 +1,19 @@
|
||||
package App::Netdisco::Worker::Plugin::Test;
|
||||
|
||||
use Dancer ':syntax';
|
||||
use App::Netdisco::Worker::Plugin;
|
||||
use aliased 'App::Netdisco::Worker::Status';
|
||||
|
||||
register_worker({ primary => true }, sub {
|
||||
my ($job, $workerconf) = @_;
|
||||
debug 'Test (primary) ran successfully.';
|
||||
return Status->done('Test (primary) ran successfully.');
|
||||
});
|
||||
|
||||
register_worker({ primary => false }, sub {
|
||||
my ($job, $workerconf) = @_;
|
||||
debug 'Test ran successfully.';
|
||||
return Status->done('Test ran successfully.');
|
||||
});
|
||||
|
||||
true;
|
||||
Reference in New Issue
Block a user