move ssh code into Transport, part one

This commit is contained in:
Oliver Gorwits
2019-03-11 18:31:26 +00:00
parent 9bccde604b
commit d0216b0ebb

View File

@@ -9,16 +9,13 @@ use App::Netdisco::Util::Node qw/check_mac store_arp/;
use App::Netdisco::Util::FastResolver 'hostnames_resolve_async'; use App::Netdisco::Util::FastResolver 'hostnames_resolve_async';
use Dancer::Plugin::DBIC 'schema'; use Dancer::Plugin::DBIC 'schema';
use Time::HiRes 'gettimeofday'; use Time::HiRes 'gettimeofday';
use Module::Load ();
use Net::OpenSSH;
use Try::Tiny;
register_worker({ phase => 'main', driver => 'snmp' }, sub { register_worker({ phase => 'main', driver => 'snmp' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my $device = $job->device; my $device = $job->device;
my $snmp = App::Netdisco::Transport::SNMP->reader_for($device) my $snmp = App::Netdisco::Transport::SNMP->reader_for($device)
or return Status->defer("arpnip snmp failed: could not SNMP connect to $device"); or return Status->defer("arpnip failed: could not SNMP connect to $device");
# get v4 arp table # get v4 arp table
my $v4 = get_arps_snmp($device, $snmp->at_paddr, $snmp->at_netaddr); my $v4 = get_arps_snmp($device, $snmp->at_paddr, $snmp->at_netaddr);
@@ -32,15 +29,15 @@ register_worker({ phase => 'main', driver => 'snmp' }, sub {
# update node_ip with ARP and Neighbor Cache entries # update node_ip with ARP and Neighbor Cache entries
store_arp(\%$_, $now) for @$v4; store_arp(\%$_, $now) for @$v4;
debug sprintf ' [%s] arpnip snmp - processed %s ARP Cache entries', debug sprintf ' [%s] arpnip - processed %s ARP Cache entries',
$device->ip, scalar @$v4; $device->ip, scalar @$v4;
store_arp(\%$_, $now) for @$v6; store_arp(\%$_, $now) for @$v6;
debug sprintf ' [%s] arpnip snmp - processed %s IPv6 Neighbor Cache entries', debug sprintf ' [%s] arpnip - processed %s IPv6 Neighbor Cache entries',
$device->ip, scalar @$v6; $device->ip, scalar @$v6;
$device->update({last_arpnip => \$now}); $device->update({last_arpnip => \$now});
return Status->done("Ended arpnip snmp for $device"); return Status->done("Ended arpnip for $device");
}); });
# get an arp table (v4 or v6) # get an arp table (v4 or v6)
@@ -70,74 +67,41 @@ register_worker({ phase => 'main', driver => 'cli' }, sub {
my ($job, $workerconf) = @_; my ($job, $workerconf) = @_;
my $device = $job->device; my $device = $job->device;
my ($ssh, $selected_auth) = App::Netdisco::Transport::CLI->session_for($device->ip, "sshcollector"); my $cli = App::Netdisco::Transport::CLI->session_for($device)
or return Status->defer("arpnip failed: could not SSH connect to $device");
if (get_arps_cli($device, $ssh, $selected_auth)){ # should be both v4 and v6
my $now = 'to_timestamp('. (join '.', gettimeofday) .')'; my $arps = get_arps_cli($device, $cli->arpnip);
$device->update({last_arpnip => \$now});
my $endmsg = "Ended arpnip cli for $device";
if ($selected_auth->{'snmp_arpnip_also'}){ # update node_ip with ARP and Neighbor Cache entries
$endmsg .= ", now running arpnip due to snmp_arpnip_also"; my $now = 'to_timestamp('. (join '.', gettimeofday) .')';
info sprintf " [%s] arpnip cli - $endmsg", $device->ip; store_arp(\%$_, $now) for @$arps;
return Status->info($endmsg); debug sprintf ' [%s] arpnip - processed %s ARP / IPv6 Neighbor Cache entries',
}else{ $device->ip, scalar @$arps;
info sprintf " [%s] arpnip cli - $endmsg", $device->ip;
return Status->done($endmsg);
}
}else{
Status->defer("arpnip cli failed");
}
$device->update({last_arpnip => \$now});
return Status->done("Ended arpnip for $device");
}); });
sub get_arps_cli { sub get_arps_cli {
my ($device, $ssh, $selected_auth) = @_; my ($device, $entries) = @_;
my @arps = ();
$entries ||= [];
foreach my $entry (@$entries) {
unless ($ssh){ next unless check_mac( $entry->{mac} );
my $msg = "could not connect to $device with SSH, deferring job"; push @arps, {
warning sprintf " [%s] arpnip cli - %s", $device->ip, $msg; node => $entry->{mac},
return undef; ip => $entry->{ip},
dns => $entry->{dns},
};
} }
my $class = "App::Netdisco::SSHCollector::Platform::".$selected_auth->{platform}; debug sprintf ' resolving %d ARP entries with max %d outstanding requests',
debug sprintf " [%s] arpnip cli - delegating to platform module %s", $device->ip, $class; scalar @arps, $ENV{'PERL_ANYEVENT_MAX_OUTSTANDING_DNS'};
my $resolved_ips = hostnames_resolve_async(\@arps);
my $load_failed = 0; return $resolved_ips;
try {
Module::Load::load $class;
} catch {
warning sprintf " [%s] arpnip cli - failed to load %s: %s", $device->ip, $class, substr($_, 0, 50)."...";
$load_failed = 1;
};
return undef if $load_failed;
my $platform_class = $class->new();
my $arpentries = [ $platform_class->arpnip($device->ip, $ssh, $selected_auth) ];
if (not scalar @$arpentries) {
warning sprintf " [%s] WARNING: no entries received from device", $device->ip;
}
hostnames_resolve_async($arpentries);
foreach my $arpentry ( @$arpentries ) {
# skip broadcast/vrrp/hsrp and other weirdos
next unless check_mac( $arpentry->{mac} );
debug sprintf ' [%s] arpnip cli - stored entry: %s / %s / %s',
$device->ip, $arpentry->{mac}, $arpentry->{ip},
$arpentry->{dns} if defined $arpentry->{dns};
store_arp({
node => $arpentry->{mac},
ip => $arpentry->{ip},
dns => $arpentry->{dns},
});
}
return 1;
} }
true; true;