« blib/lib/App/Netdisco/Worker/Plugin/Noop.pm
↑ Summary
blib/lib/App/Netdisco/Worker/Plugin/PortName.pm »
| 1 | package App::Netdisco::Worker::Plugin::PingSweep; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use App::Netdisco::Worker::Plugin; | ||
| 5 | use aliased 'App::Netdisco::Worker::Status'; | ||
| 6 | |||
| 7 | use App::Netdisco::JobQueue 'jq_insert'; | ||
| 8 | |||
| 9 | use Time::HiRes; | ||
| 10 | use Sys::SigAction 'timeout_call'; | ||
| 11 | use Net::Ping; | ||
| 12 | use Net::Ping::External; | ||
| 13 | use Proc::ProcessTable; | ||
| 14 | use NetAddr::IP qw/:rfc3021 :lower/; | ||
| 15 | |||
| 16 | register_worker({ phase => 'main' }, sub { | ||
| 17 | my ($job, $workerconf) = @_; | ||
| 18 | my $targets = $job->device | ||
| 19 | or return Status->error('missing parameter -d/device with IP prefix'); | ||
| 20 | |||
| 21 | my $timeout = $job->extra || '0.1'; | ||
| 22 | |||
| 23 | my $net = NetAddr::IP->new($targets); | ||
| 24 | if (!$net or $net->num == 0 or $net->addr eq '0.0.0.0') { | ||
| 25 | return Status->error( | ||
| 26 | sprintf 'unable to understand as host, IP, or prefix: %s', $targets) | ||
| 27 | } | ||
| 28 | |||
| 29 | my $job_count = 0; | ||
| 30 | my $ping = Net::Ping->new({proto => 'external'}); | ||
| 31 | |||
| 32 | my $pinger = sub { | ||
| 33 | my $host = shift; | ||
| 34 | $ping->ping($host); | ||
| 35 | debug sprintf 'pinged %s successfully', $host; | ||
| 36 | }; | ||
| 37 | |||
| 38 | # this is needed to allow bash/shell subprocess to exit | ||
| 39 | $SIG{CHLD} = 'IGNORE'; | ||
| 40 | # debug sprintf 'I am PID %s', $$; | ||
| 41 | |||
| 42 | ADDRESS: foreach my $idx (0 .. $net->num()) { | ||
| 43 | my $addr = $net->nth($idx) or next; | ||
| 44 | my $host = $addr->addr; | ||
| 45 | |||
| 46 | if (timeout_call($timeout, $pinger, $host)) { | ||
| 47 | debug sprintf 'pinged %s and timed out', $host; | ||
| 48 | |||
| 49 | # this is a bit gross, but needed because Net::Ping::External doesn't | ||
| 50 | # provide any cleanup/management or access to child PID | ||
| 51 | my $t = Proc::ProcessTable->new; | ||
| 52 | foreach my $p ( @{$t->table} ) { | ||
| 53 | if ($p->ppid() and $p->ppid() == $$) { | ||
| 54 | my $pid = $p->pid(); | ||
| 55 | |||
| 56 | foreach my $c ( @{$t->table} ) { | ||
| 57 | if ($c->ppid() and $c->ppid() == $pid) { | ||
| 58 | # debug sprintf 'killing fork %s (%s) of %s', $c->pid(), $c->cmndline(), $p->pid(); | ||
| 59 | kill 1, $c->pid(); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | # debug sprintf 'killing fork %s (%s) of %s', $p->pid(), $p->cmndline(), $$; | ||
| 64 | kill 1, $p->pid(); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | next ADDRESS; | ||
| 69 | } | ||
| 70 | |||
| 71 | jq_insert([{ | ||
| 72 | action => 'discover', | ||
| 73 | device => $host, | ||
| 74 | username => ($ENV{USER} || 'netdisco-do'), | ||
| 75 | }]); | ||
| 76 | |||
| 77 | ++$job_count; | ||
| 78 | } | ||
| 79 | |||
| 80 | return Status->done(sprintf | ||
| 81 | 'Finished ping sweep: queued %s jobs from %s hosts', $job_count, $net->num()); | ||
| 82 | }); | ||
| 83 | |||
| 84 | true; |