Improve netdisco-sshcollector CLI experience (#460)

* Improve netdisco-sshcollector CLI experience

 * add -d <device>
 * add -O debug option to toggle Net::OpenSSH::debug
 * add -w MCE::Loop max_workers
 * updated POD for all of the above

* Fix -Q documentation

 * Specifying -Q multiple times has no effect, removed part talking about that.

* Emit Net::OpenSSH connection failures as warnings
This commit is contained in:
Christian Ramseyer
2018-12-28 16:58:32 +01:00
committed by Oliver Gorwits
parent f5622929e0
commit b1ca1b968d

View File

@@ -53,10 +53,13 @@ use Pod::Usage 'pod2usage';
use Getopt::Long;
Getopt::Long::Configure ("bundling");
my ($debug, $sqltrace) = (undef, 0);
my ($debug, $sqltrace, $device, $opensshdebug, $workers) = (undef, 0, undef, undef, "auto");
my $result = GetOptions(
'debug|D' => \$debug,
'sqltrace|Q+' => \$sqltrace,
'debug|D' => \$debug,
'sqltrace|Q' => \$sqltrace,
'device|d=s' => \$device,
'opensshdebug|O' => \$opensshdebug,
'workers|w=i' => \$workers,
) or pod2usage(
-msg => 'error: bad options',
-verbose => 0,
@@ -71,10 +74,11 @@ $ENV{DBIC_TRACE} ||= $sqltrace;
# reconfigure logging to force console output
Dancer::Logger->init('console', $CONFIG);
#this may be helpful with SSH issues:
#$Net::OpenSSH::debug = ~0;
if ($opensshdebug){
$Net::OpenSSH::debug = ~0;
}
MCE::Loop::init { chunk_size => 1 };
MCE::Loop::init { chunk_size => 1, max_workers => $workers };
my %stats;
$stats{entry} = 0;
@@ -83,6 +87,11 @@ exit main();
sub main {
my @input = @{ setting('sshcollector') };
if ($device){
@input = grep{ ($_->{hostname} && $_->{hostname} eq $device)
|| ($_->{ip} && $_->{ip} eq $device) } @input;
}
#one-line Fisher-Yates from https://www.perlmonks.org/index.pl?node=Array%20One-Liners
my ($i,$j) = (0);
@input[-$i,$j] = @input[$j,-$i] while $j = rand(@input - $i), ++$i < @input;
@@ -108,7 +117,12 @@ sub main {
],
);
MCE->gather( process($hostlabel, $ssh, $host) );
if ($ssh->error){
warning "WARNING: Couldn't connect to <$hostlabel> - " . $ssh->error;
}else{
MCE->gather( process($hostlabel, $ssh, $host) );
}
}
} \@input;
@@ -173,7 +187,10 @@ full SNMP support
~/bin/localenv cpanm --notest Net::OpenSSH Expect
# run manually, or add to cron:
~/bin/netdisco-sshcollector [-DQ]
~/bin/netdisco-sshcollector [-DQO] [-w <max_workers>]
# limit run to a single device defined in the config
~/bin/netdisco-sshcollector [-DQO] [-w <max_workers>] -d <device>
=head1 DESCRIPTION
@@ -271,10 +288,7 @@ don't support command execution via ssh:
The returned IP and MAC addresses should be in a format that the respective
B<inetaddr> and B<macaddr> datatypes in PostgreSQL can handle.
=head1 DEBUG LEVELS
The flags "C<-DQ>" can be specified, multiple times, and enable the following
items in order:
=head1 COMMAND LINE OPTIONS
=over 4
@@ -284,7 +298,21 @@ Netdisco debug log level
=item C<-Q>
L<DBIx::Class> trace enabled
L<DBIx::Class> trace enabled.
=item C<-O>
L<Net::OpenSSH> trace enabled
=item C<-w>
Set maximum parallel workers for L<MCE::Loop>. The default is B<auto>.
=item C<-d device>
Only run for a single device. Takes an IP or hostname, must exactly match the value
in the config file.
=back