enh(linux/local): add ssh check-plugin mode (#2763)
This commit is contained in:
parent
9a8e52feea
commit
9f99d856da
|
@ -153,6 +153,12 @@ sub add_option_msg {
|
|||
$self->output_add(%options);
|
||||
}
|
||||
|
||||
sub set_ignore_label {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->{option_results}->{output_ignore_label} = 1;
|
||||
}
|
||||
|
||||
sub set_status {
|
||||
my ($self, %options) = @_;
|
||||
# $options{exit_litteral} = string litteral exit
|
||||
|
|
|
@ -316,6 +316,7 @@ sub run_instances {
|
|||
my $cb_init_counters = $self->get_callback(method_name => $options{config}->{cb_init_counters});
|
||||
my $display_status_lo = defined($options{display_status_long_output}) && $options{display_status_long_output} == 1 ? 1 : 0;
|
||||
my $display_short = (!defined($options{config}->{display_short}) || $options{config}->{display_short} != 0) ? 1 : 0;
|
||||
my $display_long = (!defined($options{config}->{display_long}) || $options{config}->{display_long} != 0) ? 1 : 0;
|
||||
my $resume = defined($options{resume}) && $options{resume} == 1 ? 1 : 0;
|
||||
my $no_message_multiple = 1;
|
||||
|
||||
|
@ -388,7 +389,8 @@ sub run_instances {
|
|||
my $debug = 0;
|
||||
$debug = 1 if ($display_status_lo == 1 && $self->{output}->is_status(value => $exit, compare => 'OK', litteral => 1));
|
||||
if (scalar @{$self->{maps_counters}->{$options{config}->{name}}} > 0 && $long_msg ne '') {
|
||||
$self->{output}->output_add(long_msg => ($display_status_lo == 1 ? lc($exit) . ': ' : '') . $prefix_output . $long_msg . $suffix_output, debug => $debug);
|
||||
$self->{output}->output_add(long_msg => ($display_status_lo == 1 ? lc($exit) . ': ' : '') . $prefix_output . $long_msg . $suffix_output, debug => $debug)
|
||||
if ($display_long == 1);
|
||||
}
|
||||
if ($resume == 1) {
|
||||
$self->{most_critical_instance} = $self->{output}->get_most_critical(status => [ $self->{most_critical_instance}, $exit ]);
|
||||
|
@ -698,7 +700,7 @@ sub run {
|
|||
$self->run_multiple(config => $entry);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (defined($self->{statefile_value})) {
|
||||
$self->{statefile_value}->write(data => $self->{new_datas});
|
||||
}
|
||||
|
|
|
@ -39,8 +39,9 @@ sub new {
|
|||
$options{output}->option_exit();
|
||||
}
|
||||
|
||||
$self->{mode_name} = $options{mode_name};
|
||||
# discovery-snmp cannot be used at distance.
|
||||
return $self if (defined($options{mode_name}) && $options{mode_name} eq 'discovery-snmp');
|
||||
return $self if (defined($options{mode_name}) && $options{mode_name} =~ /discovery-snmp|check-plugin/);
|
||||
|
||||
if (!defined($options{noptions})) {
|
||||
$options{options}->add_options(arguments => {
|
||||
|
@ -72,6 +73,8 @@ sub set_defaults {}
|
|||
sub check_options {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
return 0 if ($self->{mode_name} =~ /discovery-snmp|check-plugin/);
|
||||
|
||||
if (defined($self->{option_results}->{timeout}) && $self->{option_results}->{timeout} =~ /(\d+)/) {
|
||||
$self->{timeout} = $1;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,217 @@
|
|||
#
|
||||
# Copyright 2021 Centreon (http://www.centreon.com/)
|
||||
#
|
||||
# Centreon is a full-fledged industry-strength solution that meets
|
||||
# the needs in IT infrastructure and application monitoring for
|
||||
# service performance.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
package os::linux::local::mode::checkplugin;
|
||||
|
||||
use base qw(centreon::plugins::templates::counter);
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
|
||||
use Time::HiRes qw(gettimeofday tv_interval);
|
||||
use centreon::plugins::ssh;
|
||||
use centreon::plugins::misc;
|
||||
|
||||
sub custom_status_output {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
return $self->{result_values}->{short_message};
|
||||
}
|
||||
|
||||
sub set_counters {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->{maps_counters_type} = [
|
||||
{ name => 'commands', type => 1, message_separator => ' - ', display_long => 0 }
|
||||
];
|
||||
|
||||
$self->{maps_counters}->{commands} = [
|
||||
{
|
||||
label => 'status', type => 2,
|
||||
unknown_default => '%{exit_code} == 3',
|
||||
warning_default => '%{exit_code} == 1',
|
||||
critical_default => '%{exit_code} == 2',
|
||||
set => {
|
||||
key_values => [
|
||||
{ name => 'short_message' }, { name => 'exit_code' }
|
||||
],
|
||||
closure_custom_output => $self->can('custom_status_output'),
|
||||
closure_custom_perfdata => sub { return 0; },
|
||||
closure_custom_threshold_check => \&catalog_status_threshold_ng
|
||||
}
|
||||
},
|
||||
{ label => 'time', nlabel => 'ssh.response.time.seconds', display_ok => 0, set => {
|
||||
key_values => [ { name => 'time' } ],
|
||||
output_template => 'response time: %.3fs',
|
||||
perfdatas => [
|
||||
{ template => '%.3f', min => 0, unit => 's', label_extra_instance => 1 }
|
||||
]
|
||||
}
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
sub new {
|
||||
my ($class, %options) = @_;
|
||||
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
|
||||
bless $self, $class;
|
||||
|
||||
$options{options}->add_options(arguments => {
|
||||
'hostname:s' => { name => 'hostname' },
|
||||
'timeout:s' => { name => 'timeout' },
|
||||
'command:s@' => { name => 'command' }
|
||||
});
|
||||
|
||||
$self->{ssh} = centreon::plugins::ssh->new(%options);
|
||||
return $self;
|
||||
}
|
||||
|
||||
sub check_options {
|
||||
my ($self, %options) = @_;
|
||||
$self->SUPER::check_options(%options);
|
||||
|
||||
if (!defined($self->{option_results}->{hostname}) || $self->{option_results}->{hostname} eq '') {
|
||||
$self->{output}->add_option_msg(short_msg => 'Set --hostname option');
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
if (!defined($self->{option_results}->{command})) {
|
||||
$self->{output}->add_option_msg(short_msg => 'Need to specify at least one --command option');
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
|
||||
$self->{ssh}->check_options(option_results => $self->{option_results});
|
||||
}
|
||||
|
||||
sub parse_perfdatas {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
while ($options{perfdatas} =~ /(.*?)=([0-9\.]+)([^0-9;]+?)?([0-9.@;]+?)?(?:\s+|\Z)/g) {
|
||||
my ($label, $value, $unit, $extra) = ($1, $2, $3, $4);
|
||||
$label = centreon::plugins::misc::trim($label);
|
||||
$label =~ s/^'//;
|
||||
$label =~ s/'$//;
|
||||
my @extras = split(';', $extra);
|
||||
$self->{output}->perfdata_add(
|
||||
nlabel => $label,
|
||||
unit => $unit,
|
||||
value => $value,
|
||||
warning => $extras[1],
|
||||
critical => $extras[2],
|
||||
min => $extras[3],
|
||||
max => $extras[4]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
sub parse_plugin_output {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
my @lines = split(/\n/, $options{output});
|
||||
my $short = 'no output';
|
||||
my $line = shift(@lines);
|
||||
if (defined($line) && $line =~ /^(.*?)(?:\|(.*)|\Z)/) {
|
||||
$short = $1;
|
||||
if (defined($2)) {
|
||||
$self->parse_perfdatas(perfdatas => $2);
|
||||
}
|
||||
}
|
||||
|
||||
$self->{commands}->{ $options{cmd} }->{short_message} = $short;
|
||||
foreach (@lines) {
|
||||
$self->{output}->output_add(long_msg => $_);
|
||||
}
|
||||
}
|
||||
|
||||
sub manage_selection {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->{output}->set_ignore_label();
|
||||
|
||||
my $timeout = $self->{option_results}->{timeout};
|
||||
$timeout = 45 if (!defined($timeout) || $timeout !~ /\d+/);
|
||||
|
||||
$self->{commands} = {};
|
||||
my $i = 1;
|
||||
foreach my $command (@{$self->{option_results}->{command}}) {
|
||||
my $timing0 = [gettimeofday];
|
||||
my ($stdout, $exit_code) = $self->{ssh}->execute(
|
||||
hostname => $self->{option_results}->{hostname},
|
||||
command => $command,
|
||||
timeout => $timeout,
|
||||
no_quit => 1
|
||||
);
|
||||
my $cmd = 'command' . $i;
|
||||
$self->{commands}->{$cmd} = {
|
||||
time => tv_interval($timing0, [gettimeofday]),
|
||||
exit_code => $exit_code
|
||||
};
|
||||
$self->parse_plugin_output(cmd => $cmd, output => $stdout);
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
__END__
|
||||
|
||||
=head1 MODE
|
||||
|
||||
SSH execution commands in a remote host.
|
||||
|
||||
=over 8
|
||||
|
||||
=item B<--hostname>
|
||||
|
||||
Hostname to query.
|
||||
|
||||
=item B<--timeout>
|
||||
|
||||
Timeout in seconds for the command (Default: 45).
|
||||
|
||||
=item B<--command>
|
||||
|
||||
command to execute on the remote machine
|
||||
|
||||
=item B<--unknown-status>
|
||||
|
||||
Set unknown threshold for status (Default: '%{exit_code} == 3').
|
||||
Can used special variables like: %{short_message}, %{exit_code}
|
||||
|
||||
=item B<--warning-status>
|
||||
|
||||
Set warning threshold for status (Default: '%{exit_code} == 1').
|
||||
Can used special variables like: %{short_message}, %{exit_code}
|
||||
|
||||
=item B<--critical-status>
|
||||
|
||||
Set critical threshold for status (Default: '%{exit_code} == 2').
|
||||
Can used special variables like: %{short_message}, %{exit_code}
|
||||
|
||||
=item B<--warning-time>
|
||||
|
||||
Threshold warning in seconds.
|
||||
|
||||
=item B<--critical-time>
|
||||
|
||||
Threshold critical in seconds.
|
||||
|
||||
=back
|
||||
|
||||
=cut
|
|
@ -31,6 +31,7 @@ sub new {
|
|||
|
||||
$self->{version} = '0.1';
|
||||
$self->{modes} = {
|
||||
'check-plugin' => 'os::linux::local::mode::checkplugin',
|
||||
'cpu' => 'os::linux::local::mode::cpu',
|
||||
'cpu-detailed' => 'os::linux::local::mode::cpudetailed',
|
||||
'cmd-return' => 'os::linux::local::mode::cmdreturn',
|
||||
|
|
Loading…
Reference in New Issue