use new ssh backend (#2632)

This commit is contained in:
qgarnier 2021-03-03 16:39:26 +01:00 committed by GitHub
parent b85c8e8709
commit 120f8c5455
3 changed files with 91 additions and 188 deletions

View File

@ -31,21 +31,15 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options); my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => $options{options}->add_options(arguments => {
{ 'hmc-command:s' => { name => 'hmc_command', default => 'lssvcevents' },
"hostname:s" => { name => 'hostname' }, 'retention:s' => { name => 'retention' },
"timeout:s" => { name => 'timeout', default => 30 }, 'minutes' => { name => 'minutes' },
"sudo" => { name => 'sudo' }, 'filter-status:s' => { name => 'filter_status', default => 'open' },
"ssh-option:s@" => { name => 'ssh_option' }, 'filter-problem-nums:s' => { name => 'filter_problem_nums' },
"ssh-path:s" => { name => 'ssh_path' }, 'filter-system:s' => { name => 'filter_system' }
"ssh-command:s" => { name => 'ssh_command', default => 'ssh' }, });
"hmc-command:s" => { name => 'hmc_command', default => 'lssvcevents' },
"retention:s" => { name => 'retention' },
"minutes" => { name => 'minutes' },
"filter-status:s" => { name => 'filter_status', default => 'open' },
"filter-problem-nums:s" => { name => 'filter_problem_nums' },
"filter-system:s" => { name => 'filter_system' },
});
return $self; return $self;
} }
@ -53,20 +47,16 @@ sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->SUPER::init(%options); $self->SUPER::init(%options);
if (!defined($self->{option_results}->{hostname})) {
$self->{output}->add_option_msg(short_msg => "Need to specify a hostname.");
$self->{output}->option_exit();
}
if (defined($self->{option_results}->{retention}) && $self->{option_results}->{retention} !~ /^\d+$/) { if (defined($self->{option_results}->{retention}) && $self->{option_results}->{retention} !~ /^\d+$/) {
$self->{output}->add_option_msg(short_msg => "Need to specify an integer as retention."); $self->{output}->add_option_msg(short_msg => 'Need to specify an integer as retention.');
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
} }
sub build_command { sub build_command {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{hmc_command} = $self->{option_results}->{hmc_command} . " -t hardware -F first_time~sys_name~text "; $self->{hmc_command} = $self->{option_results}->{hmc_command} . ' -t hardware -F first_time~sys_name~text ';
if (defined($self->{option_results}->{retention}) && $self->{option_results}->{retention} ne '') { if (defined($self->{option_results}->{retention}) && $self->{option_results}->{retention} ne '') {
if (defined($self->{option_results}->{minutes})) { if (defined($self->{option_results}->{minutes})) {
$self->{hmc_command} .= ' -i ' . $self->{option_results}->{retention}; $self->{hmc_command} .= ' -i ' . $self->{option_results}->{retention};
@ -92,37 +82,37 @@ sub run {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->build_command(); $self->build_command();
$self->{option_results}->{remote} = 1; my ($stdout) = $options{custom}->execute_command(
my $stdout = centreon::plugins::misc::execute(output => $self->{output}, command => $self->{hmc_command}
options => $self->{option_results}, );
sudo => $self->{option_results}->{sudo},
command => $self->{hmc_command},
command_path => $self->{option_results}->{command_path},
command_options => $self->{option_results}->{command_options});
###### ######
# Command treatment # Command treatment
###### ######
my @long_msg = split("\n", $stdout); my @long_msg = split("\n", $stdout);
if (defined($self->{option_results}->{retention}) and defined($self->{option_results}->{minutes})) { if (defined($self->{option_results}->{retention}) and defined($self->{option_results}->{minutes})) {
$self->{output}->output_add(long_msg => $stdout); $self->{output}->output_add(
$self->{output}->output_add(severity => 'OK', severity => 'OK',
short_msg => "No Problems on system since " . $self->{option_results}->{retention} . " minutes."); short_msg => "No problems on system since " . $self->{option_results}->{retention} . " minutes."
);
} elsif (defined($self->{option_results}->{retention})) { } elsif (defined($self->{option_results}->{retention})) {
$self->{output}->output_add(long_msg => $stdout); $self->{output}->output_add(
$self->{output}->output_add(severity => 'OK', severity => 'OK',
short_msg => "No Problems on system since " . $self->{option_results}->{retention} . " days."); short_msg => "No Problems on system since " . $self->{option_results}->{retention} . " days."
);
} else { } else {
$self->{output}->output_add(long_msg => $stdout); $self->{output}->output_add(
$self->{output}->output_add(severity => 'OK', severity => 'OK',
short_msg => "No Problems on system."); short_msg => "No problems on system."
);
} }
foreach my $line (@long_msg){ foreach my $line (@long_msg){
if ($line =~ /^(.*)~(.*)~(.*)$/) { if ($line =~ /^(.*)~(.*)~(.*)$/) {
$self->{output}->output_add(severity => 'CRITICAL', $self->{output}->output_add(
short_msg => "[$1][$2] $3"); severity => 'CRITICAL',
short_msg => "[$1][$2] $3"
);
} }
} }
@ -140,26 +130,6 @@ Check Hardware for system managed by HMC.
=over 8 =over 8
=item B<--hostname>
Hostname to query.
=item B<--hostname>
Hostname to query.
=item B<--ssh-option>
Specify multiple options like the user (example: --ssh-option='-l=centreon-engine' --ssh-option='-p=52').
=item B<--ssh-path>
Specify ssh command path (default: none)
=item B<--ssh-command>
Specify ssh command (default: 'ssh').
=item B<--command-hmc> =item B<--command-hmc>
Hmc command to list events (default: lssvcevents). Hmc command to list events (default: lssvcevents).

View File

@ -24,89 +24,12 @@ use base qw(centreon::plugins::templates::counter);
use strict; use strict;
use warnings; use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
sub custom_status_output { sub custom_status_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my $msg = 'led state : ' . $self->{result_values}->{ledstate}; return 'led state : ' . $self->{result_values}->{ledstate};
return $msg;
}
sub custom_status_calc {
my ($self, %options) = @_;
$self->{result_values}->{ledstate} = $options{new_datas}->{$self->{instance} . '_ledstate'};
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
return 0;
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'physical', type => 1, cb_prefix_output => 'prefix_physical_output', message_multiple => 'All physical status are ok' },
{ name => 'virtuallpar', type => 1, cb_prefix_output => 'prefix_virtuallpar_output', message_multiple => 'All virtual partition status are ok' }
];
$self->{maps_counters}->{physical} = [
{ label => 'physical-status', threshold => 0, set => {
key_values => [ { name => 'ledstate' }, { name => 'display' } ],
closure_custom_calc => $self->can('custom_status_calc'),
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold,
}
},
];
$self->{maps_counters}->{virtuallpar} = [
{ label => 'virtuallpar-status', threshold => 0, set => {
key_values => [ { name => 'ledstate' }, { name => 'display' } ],
closure_custom_calc => $self->can('custom_status_calc'),
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold,
}
},
];
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments =>
{
"filter-name:s" => { name => 'filter_name' },
"warning-physical-status:s" => { name => 'warning_physical_status', default => '' },
"critical-physical-status:s" => { name => 'critical_physical_status', default => '%{ledstate} =~ /on/' },
"warning-virtuallpar-status:s" => { name => 'warning_virtuallpar_status', default => '' },
"critical-virtuallpar-status:s" => { name => 'critical_virtuallpar_status', default => '%{ledstate} =~ /on/' },
"hostname:s" => { name => 'hostname' },
"ssh-option:s@" => { name => 'ssh_option' },
"ssh-path:s" => { name => 'ssh_path' },
"ssh-command:s" => { name => 'ssh_command', default => 'ssh' },
"timeout:s" => { name => 'timeout', default => 30 },
"command:s" => { name => 'command' },
"command-path:s" => { name => 'command_path' },
"command-options:s" => { name => 'command_options' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
if (defined($self->{option_results}->{hostname}) && $self->{option_results}->{hostname} ne '') {
$self->{option_results}->{remote} = 1;
}
$self->{default_hmc_cmd} = 'while read system ; do echo "system: $system"; echo -n " phys led: "; lsled -r sa -m "$system" -t "phys" ; while read lpar; do echo -n " lpar [$lpar] led: "; lsled -m "$system" -r sa -t virtuallpar --filter "lpar_names=$lpar" -F state ; done < <(lssyscfg -m "$system" -r lpar -F name) ; done < <(lssyscfg -r sys -F "name")
';
$self->change_macros(macros => ['warning_physical_status', 'critical_physical_status', 'warning_virtuallpar_status', 'critical_virtuallpar_status']);
} }
sub prefix_physical_output { sub prefix_physical_output {
@ -121,21 +44,61 @@ sub prefix_virtuallpar_output {
return "Virtual partition '" . $options{instance_value}->{display} . "' "; return "Virtual partition '" . $options{instance_value}->{display} . "' ";
} }
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'physical', type => 1, cb_prefix_output => 'prefix_physical_output', message_multiple => 'All physical status are ok' },
{ name => 'virtuallpar', type => 1, cb_prefix_output => 'prefix_virtuallpar_output', message_multiple => 'All virtual partition status are ok' }
];
$self->{maps_counters}->{physical} = [
{ label => 'physical-status', type => 2, critical_default => '%{ledstate} =~ /on/', set => {
key_values => [ { name => 'ledstate' }, { name => 'display' } ],
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
}
];
$self->{maps_counters}->{virtuallpar} = [
{ label => 'virtuallpar-status', type => 2, critical_default => '%{ledstate} =~ /on/', set => {
key_values => [ { name => 'ledstate' }, { name => 'display' } ],
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
}
];
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments => {
'filter-name:s' => { name => 'filter_name' }
});
return $self;
}
sub manage_selection { sub manage_selection {
my ($self, %options) = @_; my ($self, %options) = @_;
#system: Server-8203-E4A-SN06DF9A5 #system: Server-8203-E4A-SN06DF9A5
# phys led: state=on # phys led: state=on
# lpar [LPAR1] led: off # lpar [LPAR1] led: off
my $content = centreon::plugins::misc::execute(output => $self->{output}, my ($content) = $options{custom}->execute_command(
options => $self->{option_results}, command => 'while read system ; do echo "system: $system"; echo -n " phys led: "; lsled -r sa -m "$system" -t "phys" ; while read lpar; do echo -n " lpar [$lpar] led: "; lsled -m "$system" -r sa -t virtuallpar --filter "lpar_names=$lpar" -F state ; done < <(lssyscfg -m "$system" -r lpar -F name) ; done < <(lssyscfg -r sys -F "name")
command => defined($self->{option_results}->{command}) && $self->{option_results}->{command} ne '' ? $self->{option_results}->{command} : $self->{default_hmc_cmd}, ',
command_path => $self->{option_results}->{command_path}, command_options => '2>&1'
command_options => defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '' ? $self->{option_results}->{command_options} : undef); );
$self->{physical} = {}; $self->{physical} = {};
$self->{virtuallpar} = {}; $self->{virtuallpar} = {};
while ($content =~ /^system:\s+(.*?)\n(.*?)(?:system:|\Z)/msg) { while ($content =~ /^system:\s+(.*?)\n(.*?)(?:system:|\Z)/msg) {
my ($system_name, $subcontent) = ($1, $2); my ($system_name, $subcontent) = ($1, $2);
if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
@ -143,7 +106,7 @@ sub manage_selection {
$self->{output}->output_add(long_msg => "skipping '" . $system_name . "': no matching filter.", debug => 1); $self->{output}->output_add(long_msg => "skipping '" . $system_name . "': no matching filter.", debug => 1);
next; next;
} }
$subcontent =~ /phys\s+led:\s+state=(\S+)/; $subcontent =~ /phys\s+led:\s+state=(\S+)/;
my $system_ledstate = $1; my $system_ledstate = $1;
while ($subcontent =~ /lpar\s+\[(.*?)\]\s+led:\s+(\S+)/msg) { while ($subcontent =~ /lpar\s+\[(.*?)\]\s+led:\s+(\S+)/msg) {
@ -154,15 +117,15 @@ sub manage_selection {
$self->{output}->output_add(long_msg => "skipping '" . $lpar_name . "': no matching filter.", debug => 1); $self->{output}->output_add(long_msg => "skipping '" . $lpar_name . "': no matching filter.", debug => 1);
next; next;
} }
$self->{virtuallpar}->{$lpar_name} = { display => $lpar_name, ledstate => $lpar_ledstate }; $self->{virtuallpar}->{$lpar_name} = { display => $lpar_name, ledstate => $lpar_ledstate };
} }
$self->{physical}->{$system_name} = { display => $system_name, ledstate => $system_ledstate }; $self->{physical}->{$system_name} = { display => $system_name, ledstate => $system_ledstate };
} }
if (scalar(keys %{$self->{physical}}) <= 0) { if (scalar(keys %{$self->{physical}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No managed system found."); $self->{output}->add_option_msg(short_msg => 'No managed system found.');
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
} }
@ -207,38 +170,6 @@ Can used special variables like: %{ledstate}, %{display}
Set critical threshold (Default: '%{ledstate} =~ /on/'). Set critical threshold (Default: '%{ledstate} =~ /on/').
Can used special variables like: %{ledstate}, %{display} Can used special variables like: %{ledstate}, %{display}
=item B<--hostname>
Hostname to query.
=item B<--ssh-option>
Specify multiple options like the user (example: --ssh-option='-l=centreon-engine' --ssh-option='-p=52').
=item B<--ssh-path>
Specify ssh command path (default: none)
=item B<--ssh-command>
Specify ssh command (default: 'ssh'). Useful to use 'plink'.
=item B<--timeout>
Timeout in seconds for the command (Default: 30).
=item B<--command>
Command to get information. Used it you have output in a file.
=item B<--command-path>
Command path.
=item B<--command-options>
Command options.
=back =back
=cut =cut

View File

@ -22,7 +22,7 @@ package hardware::server::ibm::hmc::ssh::plugin;
use strict; use strict;
use warnings; use warnings;
use base qw(centreon::plugins::script_simple); use base qw(centreon::plugins::script_custom);
sub new { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
@ -30,10 +30,12 @@ sub new {
bless $self, $class; bless $self, $class;
$self->{version} = '0.1'; $self->{version} = '0.1';
%{$self->{modes}} = ( $self->{modes} = {
'hardware-errors' => 'hardware::server::ibm::hmc::ssh::mode::hardwareerrors', 'hardware-errors' => 'hardware::server::ibm::hmc::ssh::mode::hardwareerrors',
'led-status' => 'hardware::server::ibm::hmc::ssh::mode::ledstatus', 'led-status' => 'hardware::server::ibm::hmc::ssh::mode::ledstatus'
); };
$self->{custom_modes}->{cli} = 'centreon::plugins::script_custom::cli';
return $self; return $self;
} }