move arpnip and arpwalk to worker plugins

This commit is contained in:
Oliver Gorwits
2017-09-09 16:44:32 +01:00
parent 16a79463cb
commit c6ebb7cf07
7 changed files with 157 additions and 209 deletions

View File

@@ -0,0 +1,31 @@
package App::Netdisco::Worker::Plugin::Arpnip;
use Dancer ':syntax';
use App::Netdisco::Worker::Plugin;
use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Util::Device 'is_arpnipable_now';
use App::Netdisco::Transport::SNMP ();
register_worker({ primary => true }, sub {
my ($job, $workerconf) = @_;
my $device = $job->device;
return Status->error('arpnip failed: unable to interpret device param')
unless defined $device;
my $host = $device->ip;
return Status->done("arpnip skipped: $host not yet discovered")
unless $device->in_storage;
return Status->defer("arpnip skipped: $host is pseudo-device")
if $device->vendor and $device->vendor eq 'netdisco';
return Status->defer("arpnip deferred: $host is not arpnipable")
unless is_arpnipable_now($device);
return Status->done('Arpnip is able to run.');
});
true;

View File

@@ -0,0 +1,119 @@
package App::Netdisco::Worker::Plugin::Arpnip::Nodes;
use Dancer ':syntax';
use App::Netdisco::Worker::Plugin;
use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Util::Node 'check_mac';
use App::Netdisco::Util::FastResolver 'hostnames_resolve_async';
use Dancer::Plugin::DBIC 'schema';
use Time::HiRes 'gettimeofday';
use NetAddr::MAC ();
register_worker({ primary => true, driver => 'snmp' }, sub {
my ($job, $workerconf) = @_;
my $device = $job->device;
my $snmp = App::Netdisco::Transport::SNMP->reader_for($device)
or return Status->defer("arpnip failed: could not SNMP connect to $host");
return Status->defer("Skipped arpnip for device $host without layer 3 capability")
unless $snmp->has_layer(3);
# get v4 arp table
my $v4 = get_arps($device, $snmp->at_paddr, $snmp->at_netaddr);
# get v6 neighbor cache
my $v6 = get_arps($device, $snmp->ipv6_n2p_mac, $snmp->ipv6_n2p_addr);
# would be possible just to use now() on updated records, but by using this
# same value for them all, we _can_ if we want add a job at the end to
# select and do something with the updated set (no reason to yet, though)
my $now = 'to_timestamp('. (join '.', gettimeofday) .')';
# update node_ip with ARP and Neighbor Cache entries
store_arp(\%$_, $now) for @$v4;
debug sprintf ' [%s] arpnip - processed %s ARP Cache entries',
$device->ip, scalar @$v4;
store_arp(\%$_, $now) for @$v6;
debug sprintf ' [%s] arpnip - processed %s IPv6 Neighbor Cache entries',
$device->ip, scalar @$v6;
$device->update({last_arpnip => \$now});
return Status->done('Ended arpnip for '. $device->ip);
});
# get an arp table (v4 or v6)
sub get_arps {
my ($device, $paddr, $netaddr) = @_;
my @arps = ();
while (my ($arp, $node) = each %$paddr) {
my $ip = $netaddr->{$arp};
next unless defined $ip;
next unless check_mac($device, $node);
push @arps, {
node => $node,
ip => $ip,
dns => undef,
};
}
debug sprintf ' resolving %d ARP entries with max %d outstanding requests',
scalar @arps, $ENV{'PERL_ANYEVENT_MAX_OUTSTANDING_DNS'};
my $resolved_ips = hostnames_resolve_async(\@arps);
return $resolved_ips;
}
=head2 store_arp( \%host, $now? )
Stores a new entry to the C<node_ip> table with the given MAC, IP (v4 or v6)
and DNS host name. Host details are provided in a Hash ref:
{
ip => '192.0.2.1',
node => '00:11:22:33:44:55',
dns => 'myhost.example.com',
}
The C<dns> entry is optional. The update will mark old entries for this IP as
no longer C<active>.
Optionally a literal string can be passed in the second argument for the
C<time_last> timestamp, otherwise the current timestamp (C<now()>) is used.
=cut
sub store_arp {
my ($hash_ref, $now) = @_;
$now ||= 'now()';
my $ip = $hash_ref->{'ip'};
my $mac = NetAddr::MAC->new($hash_ref->{'node'});
my $name = $hash_ref->{'dns'};
return if !defined $mac or $mac->errstr;
schema('netdisco')->txn_do(sub {
my $current = schema('netdisco')->resultset('NodeIp')
->search(
{ ip => $ip, -bool => 'active'},
{ columns => [qw/mac ip/] })->update({active => \'false'});
schema('netdisco')->resultset('NodeIp')
->update_or_create(
{
mac => $mac->as_ieee,
ip => $ip,
dns => $name,
active => \'true',
time_last => \$now,
},
{
key => 'primary',
for => 'update',
});
});
}
true;

View File

@@ -0,0 +1,75 @@
package App::Netdisco::Worker::Plugin::Arpnip::Subnets;
use Dancer ':syntax';
use App::Netdisco::Worker::Plugin;
use aliased 'App::Netdisco::Worker::Status';
use App::Netdisco::Util::Permission 'check_acl_no';
use Dancer::Plugin::DBIC 'schema';
use NetAddr::IP::Lite ':lower';
use Time::HiRes 'gettimeofday';
register_worker({ primary => false, driver => 'snmp' }, sub {
my ($job, $workerconf) = @_;
my $device = $job->device;
my $snmp = App::Netdisco::Transport::SNMP->reader_for($device)
or return Status->defer("arpnip failed: could not SNMP connect to $host");
return Status->defer("Skipped arpnip for device $host without layer 3 capability")
unless $snmp->has_layer(3);
# get directly connected networks
my @subnets = gather_subnets($device, $snmp);
# TODO: IPv6 subnets
my $now = 'to_timestamp('. (join '.', gettimeofday) .')';
store_subnet($_, $now) for @subnets;
debug sprintf ' [%s] arpnip - processed %s Subnet entries',
$device->ip, scalar @subnets;
return Status->done('Ended arpnip for '. $device->ip);
});
# gathers device subnets
sub gather_subnets {
my ($device, $snmp) = @_;
my @subnets = ();
my $ip_netmask = $snmp->ip_netmask;
foreach my $entry (keys %$ip_netmask) {
my $ip = NetAddr::IP::Lite->new($entry);
my $addr = $ip->addr;
next if $addr eq '0.0.0.0';
next if check_acl_no($ip, 'group:__LOCAL_ADDRESSES__');
next if setting('ignore_private_nets') and $ip->is_rfc1918;
my $netmask = $ip_netmask->{$addr};
next if $netmask eq '255.255.255.255' or $netmask eq '0.0.0.0';
my $cidr = NetAddr::IP::Lite->new($addr, $netmask)->network->cidr;
debug sprintf ' [%s] arpnip - found subnet %s', $device->ip, $cidr;
push @subnets, $cidr;
}
return @subnets;
}
# update subnets with new networks
sub store_subnet {
my ($subnet, $now) = @_;
schema('netdisco')->txn_do(sub {
schema('netdisco')->resultset('Subnet')->update_or_create(
{
net => $subnet,
last_discover => \$now,
},
{ for => 'update' });
});
}
true;

View File

@@ -0,0 +1,31 @@
package App::Netdisco::Worker::Plugin::Arpwalk;
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('arpnip');
my @devices = schema('netdisco')->resultset('Device')->search({
-or => [ 'vendor' => undef, 'vendor' => { '!=' => 'netdisco' }],
})->has_layer('3')->get_column('ip')->all;
my @filtered_devices = grep {!exists $queued{$_}} @devices;
jq_insert([
map {{
device => $_,
action => 'arpnip',
username => $job->username,
userip => $job->userip,
}} (@filtered_devices)
]);
return Status->done('Queued arpnip job for all devices');
});
true;

View File

@@ -19,7 +19,7 @@ register_worker({ primary => true }, sub {
return Status->done("macsuck skipped: $host not yet discovered")
unless $device->in_storage;
return Status->done("macsuck skipped: $host is pseudo-device")
return Status->defer("macsuck skipped: $host is pseudo-device")
if $device->vendor and $device->vendor eq 'netdisco';
return Status->defer("macsuck deferred: $host is not macsuckable")