move arpnip and arpwalk to worker plugins
This commit is contained in:
75
lib/App/Netdisco/Worker/Plugin/Arpnip/Subnets.pm
Normal file
75
lib/App/Netdisco/Worker/Plugin/Arpnip/Subnets.pm
Normal 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;
|
||||
Reference in New Issue
Block a user