wip: libssh backend

This commit is contained in:
garnier-quentin 2020-02-19 14:07:24 +01:00
parent 8fbc67bff3
commit 17a713a267
2 changed files with 21 additions and 27 deletions

View File

@ -76,6 +76,7 @@ sub connect {
return if ($self->{connected} == 1); return if ($self->{connected} == 1);
$self->{ssh}->options(host => $options{hostname});
if ($self->{ssh}->connect(SkipKeyProblem => $self->{ssh_strict_connect}) != $self->{constant_cb}->(name => 'SSH_OK')) { if ($self->{ssh}->connect(SkipKeyProblem => $self->{ssh_strict_connect}) != $self->{constant_cb}->(name => 'SSH_OK')) {
$self->{output}->add_option_msg(short_msg => 'connect issue: ' . $self->{ssh}->error()); $self->{output}->add_option_msg(short_msg => 'connect issue: ' . $self->{ssh}->error());
$self->{output}->option_exit(); $self->{output}->option_exit();
@ -84,14 +85,15 @@ sub connect {
if ($self->{ssh}->auth_publickey_auto() != $self->{constant_cb}->(name => 'SSH_AUTH_SUCCESS')) { if ($self->{ssh}->auth_publickey_auto() != $self->{constant_cb}->(name => 'SSH_AUTH_SUCCESS')) {
if (defined($self->{ssh_username}) && $self->{ssh_username} ne '' && if (defined($self->{ssh_username}) && $self->{ssh_username} ne '' &&
defined($self->{ssh_password}) && $self->{ssh_password} ne '' && defined($self->{ssh_password}) && $self->{ssh_password} ne '' &&
$self->{ssh}->auth_password(password => $self->{ssh_password}) != $self->{constant_cb}->(name => 'SSH_AUTH_SUCCESS')) { $self->{ssh}->auth_password(password => $self->{ssh_password}) == $self->{constant_cb}->(name => 'SSH_AUTH_SUCCESS')) {
my $msg_error = $self->{ssh}->error(GetErrorSession => 1); $self->{connected} = 1;
$self->{output}->add_option_msg(short_msg => sprintf("auth issue: %s", defined($msg_error) && $msg_error ne '' ? $msg_error : 'pubkey issue')); return ;
$self->{output}->option_exit();
} }
}
$self->{connected} = 1; my $msg_error = $self->{ssh}->error(GetErrorSession => 1);
$self->{output}->add_option_msg(short_msg => sprintf("auth issue: %s", defined($msg_error) && $msg_error ne '' ? $msg_error : 'pubkey issue'));
$self->{output}->option_exit();
}
} }
sub execute { sub execute {
@ -100,7 +102,8 @@ sub execute {
if (defined($options{timeout}) && $options{timeout} =~ /(\d+)/) { if (defined($options{timeout}) && $options{timeout} =~ /(\d+)/) {
$self->{ssh}->options(timeout => $options{timeout}); $self->{ssh}->options(timeout => $options{timeout});
} }
$self->connect();
$self->connect(hostname => $options{hostname});
my $cmd = ''; my $cmd = '';
$cmd = 'sudo ' if (defined($options{sudo})); $cmd = 'sudo ' if (defined($options{sudo}));
@ -108,43 +111,34 @@ sub execute {
$cmd .= $options{command} . ' ' if (defined($options{command})); $cmd .= $options{command} . ' ' if (defined($options{command}));
$cmd .= $options{command_options} if (defined($options{command_options})); $cmd .= $options{command_options} if (defined($options{command_options}));
my $ret; # ssh_pipe useless ? maybe.
if (defined($options{ssh_pipe})) { my $ret = $self->{ssh}->execute_simple(
$ret = $self->{ssh}->execute_simple( cmd => $cmd,
input_data => $cmd, timeout => $options{timeout},
timeout => $options{timeout}, timeout_nodata => $options{timeout}
timeout_nodata => $options{timeout} );
);
} else { $self->{output}->output_add(long_msg => $ret->{stdout}, debug => 1) if (defined($ret->{stdout}));
$ret = $self->{ssh}->execute_simple( $self->{output}->output_add(long_msg => $ret->{stderr}, debug => 1) if (defined($ret->{stderr}));
cmd => $cmd,
timeout => $options{timeout},
timeout_nodata => $options{timeout}
);
}
my ($content, $exit_code); my ($content, $exit_code);
if ($ret->{exit} == $self->{constant_cb}->(name => 'SSH_OK')) { if ($ret->{exit} == $self->{constant_cb}->(name => 'SSH_OK')) {
$content = $ret->{stdout}; $content = $ret->{stdout};
$exit_code = $ret->{exit_code}; $exit_code = $ret->{exit_code};
} elsif ($ret->{exit} == $self->{constant_cb}->(name => 'SSH_AGAIN')) { # AGAIN means timeout } elsif ($ret->{exit} == $self->{constant_cb}->(name => 'SSH_AGAIN')) { # AGAIN means timeout
$self->{output}->output_add(long_msg => $ret->{stdout}, debug => 1);
$self->{output}->output_add(long_msg => $ret->{stderr}, debug => 1);
$self->{output}->add_option_msg(short_msg => sprintf('command execution timeout')); $self->{output}->add_option_msg(short_msg => sprintf('command execution timeout'));
$self->{output}->option_exit(); $self->{output}->option_exit();
} else { } else {
$self->{output}->add_option_msg(short_msg => $self->{output}->add_option_msg(short_msg =>
sprintf( sprintf(
'command execution error: %s', 'command execution error: %s',
$ret->{session}->error(GetErrorSession => 1) $self->{ssh}->error(GetErrorSession => 1)
) )
); );
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
if ($exit_code != 0) { if ($exit_code != 0) {
$self->{output}->output_add(long_msg => $ret->{stdout}, debug => 1);
$self->{output}->output_add(long_msg => $ret->{stderr}, debug => 1);
$self->{output}->add_option_msg(short_msg => sprintf('command execution error [exit code: %s]', $exit_code)); $self->{output}->add_option_msg(short_msg => sprintf('command execution error [exit code: %s]', $exit_code));
$self->{output}->option_exit(); $self->{output}->option_exit();
} }

View File

@ -22,7 +22,7 @@ package centreon::plugins::backend::ssh::libsshconstants;
use strict; use strict;
use warnings; use warnings;
use Libssh::Session qw(:constants); use Libssh::Session qw(:all);
sub get_constant_value { sub get_constant_value {
my (%options) = @_; my (%options) = @_;