From 1fc0b241a81a1710f4d859f3cedecf27b3023b46 Mon Sep 17 00:00:00 2001 From: qgarnier Date: Fri, 4 Sep 2020 14:53:25 +0200 Subject: [PATCH] fix #2176 (#2193) --- .../apps/pacemaker/local/mode/clustat.pm | 125 +++--------- .../apps/pacemaker/local/mode/constraints.pm | 104 +++------- .../apps/pacemaker/local/mode/crm.pm | 150 ++++++--------- .../apps/pacemaker/local/plugin.pm | 10 +- .../centreon/plugins/script_custom/cli.pm | 178 ++++++++++++++++++ centreon-plugins/os/linux/local/custom/cli.pm | 6 +- 6 files changed, 309 insertions(+), 264 deletions(-) create mode 100644 centreon-plugins/centreon/plugins/script_custom/cli.pm diff --git a/centreon-plugins/apps/pacemaker/local/mode/clustat.pm b/centreon-plugins/apps/pacemaker/local/mode/clustat.pm index 446cfa5f1..67201d22e 100644 --- a/centreon-plugins/apps/pacemaker/local/mode/clustat.pm +++ b/centreon-plugins/apps/pacemaker/local/mode/clustat.pm @@ -37,16 +37,7 @@ my %map_node_state = ( sub custom_state_output { my ($self, %options) = @_; - my $msg = sprintf("state is '%s'", $self->{result_values}->{state}); - return $msg; -} - -sub custom_state_calc { - my ($self, %options) = @_; - - $self->{result_values}->{state} = $options{new_datas}->{$self->{instance} . '_state'}; - $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; - return 0; + return sprintf("state is '%s'", $self->{result_values}->{state}); } sub set_counters { @@ -58,24 +49,23 @@ sub set_counters { ]; $self->{maps_counters}->{nodes} = [ - { label => 'node', threshold => 0, set => { + { label => 'node', type => 2, critical_default => '%{state} !~ /up|clean/', set => { key_values => [ { name => 'state' }, { name => 'display' } ], - closure_custom_calc => $self->can('custom_state_calc'), closure_custom_output => $self->can('custom_state_output'), closure_custom_threshold_check => \&catalog_status_threshold, - closure_custom_perfdata => sub { return 0; }, + closure_custom_perfdata => sub { return 0; } } - }, + } ]; + $self->{maps_counters}->{groups} = [ - { label => 'group', threshold => 0, set => { + { label => 'group', type => 2, critical_default => '%{state} !~ /starting|started/', set => { key_values => [ { name => 'state' }, { name => 'display' } ], - closure_custom_calc => $self->can('custom_state_calc'), closure_custom_output => $self->can('custom_state_output'), closure_custom_threshold_check => \&catalog_status_threshold, - closure_custom_perfdata => sub { return 0; }, + closure_custom_perfdata => sub { return 0; } } - }, + } ]; } @@ -96,44 +86,22 @@ sub new { my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; - $options{options}->add_options(arguments => - { - "hostname:s" => { name => 'hostname' }, - "remote" => { name => 'remote' }, - "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 }, - "sudo" => { name => 'sudo' }, - "command:s" => { name => 'command', default => 'clustat' }, - "command-path:s" => { name => 'command_path', default => '/usr/sbin' }, - "command-options:s" => { name => 'command_options', default => ' -x 2>&1' }, - "warning-group:s" => { name => 'warning_group' }, - "critical-group:s" => { name => 'critical_group' }, - "warning-node:s" => { name => 'warning_node' }, - "critical-node:s" => { name => 'critical_node' }, - "filter-node:s" => { name => 'filter_node', default => '%{state} !~ /up|clean/' }, - "filter-groups:s" => { name => 'filter_groups', default => '%{state} !~ /starting|started/' }, - }); + $options{options}->add_options(arguments => { + 'filter-node:s' => { name => 'filter_node' }, + 'filter-groups:s' => { name => 'filter_groups' } + }); + return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::check_options(%options); - - $self->change_macros(macros => ['warning_group', 'critical_group', 'warning_node', 'critical_node']); -} - sub manage_selection { my ($self, %options) = @_; - my $stdout = centreon::plugins::misc::execute(output => $self->{output}, - options => $self->{option_results}, - sudo => $self->{option_results}->{sudo}, - command => $self->{option_results}->{command}, - command_path => $self->{option_results}->{command_path}, - command_options => $self->{option_results}->{command_options}); + my ($stdout) = $options{custom}->execute_command( + command => 'clustat', + command_path => '/usr/sbin', + command_options => '-x 2>&1' + ); my $clustat_hash = XMLin($stdout); @@ -143,8 +111,11 @@ sub manage_selection { $self->{output}->output_add(long_msg => "skipping peer '" . $node . "': no matching filter.", debug => 1); next; } - $self->{nodes}->{$node} = { state => $map_node_state{$clustat_hash->{nodes}->{node}->{$node}->{state}}, - display => $node }; + + $self->{nodes}->{$node} = { + display => $node, + state => $map_node_state{$clustat_hash->{nodes}->{node}->{$node}->{state}} + }; } foreach my $group_name (keys %{$clustat_hash->{groups}->{group}}) { @@ -153,8 +124,11 @@ sub manage_selection { $self->{output}->output_add(long_msg => "skipping peer '" . $group_name . "': no matching filter.", debug => 1); next; } - $self->{groups}->{$group_name} = { state => $clustat_hash->{groups}->{group}->{$group_name}->{state_str}, - display => $group_name }; + + $self->{groups}->{$group_name} = { + display => $group_name, + state => $clustat_hash->{groups}->{group}->{$group_name}->{state_str} + }; } } @@ -168,6 +142,8 @@ __END__ Check Cluster Resource Manager (need 'clustat' command). Should be executed on a cluster node. +Command used: /usr/sbin/clustat -x 2>&1 + =over 8 =item B<--warning-*> @@ -180,47 +156,6 @@ Warning threshold for status. Can be ('group','node') Critical threshold for status. (Default: --critical-node '%{state} !~ /up|clean/' --critical-group '%{state} !~ /started|starting/') -=item B<--remote> - -Execute command remotely in 'ssh'. - -=item B<--hostname> - -Hostname to query (need --remote). - -=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<--sudo> - -Use 'sudo' to execute the command. - -=item B<--command> - -Command to get information (Default: 'crm_mon'). -Can be changed if you have output in a file. - -=item B<--command-path> - -Command path (Default: '/usr/sbin'). - -=item B<--command-options> - -Command options (Default: ' -x 2>&1'). - =back =cut diff --git a/centreon-plugins/apps/pacemaker/local/mode/constraints.pm b/centreon-plugins/apps/pacemaker/local/mode/constraints.pm index 3172ed506..b2cf81029 100644 --- a/centreon-plugins/apps/pacemaker/local/mode/constraints.pm +++ b/centreon-plugins/apps/pacemaker/local/mode/constraints.pm @@ -30,21 +30,11 @@ sub new { my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; - $options{options}->add_options(arguments => - { - "hostname:s" => { name => 'hostname' }, - "remote" => { name => 'remote' }, - "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 }, - "sudo" => { name => 'sudo' }, - "command:s" => { name => 'command', default => 'crm_resource' }, - "command-path:s" => { name => 'command_path', default => '/usr/sbin' }, - "command-options:s" => { name => 'command_options', default => ' --constraints -r' }, - "resource:s" => { name => 'resource' }, - "warning" => { name => 'warning' }, - }); + $options{options}->add_options(arguments => { + 'resource:s' => { name => 'resource' }, + 'warning' => { name => 'warning' } + }); + $self->{threshold} = 'CRITICAL'; return $self; } @@ -54,8 +44,8 @@ sub check_options { $self->SUPER::init(%options); if (!defined($self->{option_results}->{resource}) || $self->{option_results}->{resource} eq '') { - $self->{output}->add_option_msg(short_msg => "Please set the resource name with --resource option"); - $self->{output}->option_exit(); + $self->{output}->add_option_msg(short_msg => "Please set the resource name with --resource option"); + $self->{output}->option_exit(); } $self->{threshold} = 'WARNING' if (defined $self->{option_results}->{warning}); @@ -64,13 +54,17 @@ sub check_options { sub parse_output { my ($self, %options) = @_; - $self->{output}->output_add(severity => 'OK', - short_msg => sprintf("Resource '%s' constraint location is OK", $self->{option_results}->{resource})); + $self->{output}->output_add( + severity => 'OK', + short_msg => sprintf("Resource '%s' constraint location is OK", $self->{option_results}->{resource}) + ); if ($options{output} =~ /Connection to cluster failed\:(.*)/i ) { - $self->{output}->output_add(severity => 'CRITICAL', - short_msg => "Connection to cluster FAILED: $1"); - return ; + $self->{output}->output_add( + severity => 'CRITICAL', + short_msg => "Connection to cluster FAILED: $1" + ); + return ; } my @lines = split /\n/, $options{output}; @@ -81,12 +75,16 @@ sub parse_output { $line =~ /^\s+:\sNode\s([a-zA-Z0-9-_]+)\s+\(score=([a-zA-Z0-9-_]+),\sid=([a-zA-Z0-9-_]+)/; my ($node, $score, $rule) = ($1, $2, $3); if ($score eq '-INFINITY' && $rule =~ /^cli-ban/) { - $self->{output}->output_add(severity => $self->{threshold}, - short_msg => sprintf("Resource '%s' is locked on node '%s' ('%s')", $self->{option_results}->{resource}, $node, $rule)); + $self->{output}->output_add( + severity => $self->{threshold}, + short_msg => sprintf("Resource '%s' is locked on node '%s' ('%s')", $self->{option_results}->{resource}, $node, $rule) + ); } } else { - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => "ERROR: $line"); + $self->{output}->output_add( + severity => 'UNKNOWN', + short_msg => "ERROR: $line" + ); } } } @@ -94,12 +92,11 @@ sub parse_output { sub run { my ($self, %options) = @_; - my $stdout = centreon::plugins::misc::execute(output => $self->{output}, - options => $self->{option_results}, - sudo => $self->{option_results}->{sudo}, - command => $self->{option_results}->{command}, - command_path => $self->{option_results}->{command_path}, - command_options => $self->{option_results}->{command_options}." ".$self->{option_results}->{resource}); + my ($stdout) = $options{custom}->execute_command( + command => 'crm_resource', + command_path => '/usr/sbin', + command_options => '--constraints -r' + ); $self->parse_output(output => $stdout); @@ -116,6 +113,8 @@ __END__ Check that a resource has no location constraint (migrate without unmigrate) Can be executed from any cluster node. +Command used: /usr/sbin/crm_resource --constraints -r + =over 8 =item B<--resource> @@ -126,47 +125,6 @@ Set the resource name you want to check Return a warning instead of a critical -=item B<--remote> - -Execute command remotely in 'ssh'. - -=item B<--hostname> - -Hostname to query (need --remote). - -=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<--sudo> - -Use 'sudo' to execute the command. - -=item B<--command> - -Command to get information (Default: 'crm_resource'). -Can be changed if you have output in a file. - -=item B<--command-path> - -Command path (Default: '/usr/sbin'). - -=item B<--command-options> - -Command options (Default: ' --constraints -r'). - =back =cut diff --git a/centreon-plugins/apps/pacemaker/local/mode/crm.pm b/centreon-plugins/apps/pacemaker/local/mode/crm.pm index 810bdb58a..d8f1a281a 100644 --- a/centreon-plugins/apps/pacemaker/local/mode/crm.pm +++ b/centreon-plugins/apps/pacemaker/local/mode/crm.pm @@ -24,31 +24,20 @@ use base qw(centreon::plugins::mode); use strict; use warnings; -use centreon::plugins::misc; sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; - $options{options}->add_options(arguments => - { - "hostname:s" => { name => 'hostname' }, - "remote" => { name => 'remote' }, - "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 }, - "sudo" => { name => 'sudo' }, - "command:s" => { name => 'command', default => 'crm_mon' }, - "command-path:s" => { name => 'command_path', default => '/usr/sbin' }, - "command-options:s" => { name => 'command_options', default => '-1 -r -f 2>&1' }, - "warning" => { name => 'warning', }, - "standbyignore" => { name => 'standbyignore', }, - "resources:s" => { name => 'resources', }, - "ignore-stopped-clone:s" => { name => 'ignore_stopped_clone', }, - "ignore-failed-actions:s@" => { name => 'ignore_failed_actions', }, - }); + $options{options}->add_options(arguments => { + 'warning' => { name => 'warning' }, + 'standbyignore' => { name => 'standbyignore' }, + 'resources:s' => { name => 'resources' }, + 'ignore-stopped-clone:s' => { name => 'ignore_stopped_clone' }, + 'ignore-failed-actions:s@' => { name => 'ignore_failed_actions' } + }); + $self->{threshold} = 'CRITICAL'; $self->{resources_check} = {}; return $self; @@ -76,38 +65,50 @@ sub parse_output { my ($self, %options) = @_; my @standby; - $self->{output}->output_add(severity => 'OK', - short_msg => "Cluster is OK"); + $self->{output}->output_add( + severity => 'OK', + short_msg => "Cluster is OK" + ); my @lines = split /\n/, $options{crm_out}; foreach my $line (@lines) { if ($line =~ /Connection to cluster failed\:(.*)/i ) { - $self->{output}->output_add(severity => 'CRITICAL', - short_msg => "Connection to cluster FAILED: $1"); + $self->{output}->output_add( + severity => 'CRITICAL', + short_msg => "Connection to cluster FAILED: $1" + ); return ; } elsif ($line =~ /Current DC:/) { if ($line !~ m/partition with quorum$/ ) { - $self->{output}->output_add(severity => 'CRITICAL', - short_msg => "No Quorum"); + $self->{output}->output_add( + severity => 'CRITICAL', + short_msg => "No Quorum" + ); } } elsif ($line =~ /^offline:\s*\[\s*(\S.*?)\s*\]/i) { # Count offline nodes my @offline = split( /\s+/, $1 ); my $numoffline = scalar @offline; - $self->{output}->output_add(severity => $self->{threshold}, - short_msg => "$numoffline Nodes Offline"); + $self->{output}->output_add( + severity => $self->{threshold}, + short_msg => "$numoffline Nodes Offline" + ); } elsif ($line =~ /^node\s+(\S.*):\s*standby/i) { push @standby, $1; } elsif ($line =~ /\s*([0-9a-zA-Z_\-]+)\s+\(\S+\)\:\s+Started\s+([0-9a-zA-Z_\-]+)/) { # Check Resources pos if (defined($self->{resources_check}->{$1}) && $self->{resources_check}->{$1} ne $2) { - $self->{output}->output_add(severity => $self->{threshold}, - short_msg => "Resource '$1' is started on node '$2'"); + $self->{output}->output_add( + severity => $self->{threshold}, + short_msg => "Resource '$1' is started on node '$2'" + ); } $self->{output}->output_add(long_msg => "Resource '$1' is started on node '$2'"); } elsif ($line =~ /\s*([0-9a-zA-Z_\-]+)\s+\(\S+\)\:\s+Stopped/ || $line =~ /\s*([0-9a-zA-Z_\-]+)\s+\(\S+\)\:\s+\(\S+\)\s+Stopped/) { - $self->{output}->output_add(severity => $self->{threshold}, - short_msg => "Resource '$1' is stopped", - long_msg => "Resource '$1' is stopped"); + $self->{output}->output_add( + severity => $self->{threshold}, + short_msg => "Resource '$1' is stopped", + long_msg => "Resource '$1' is stopped" + ); } elsif ($line =~ m/\s*stopped\:\s*\[\s*(.*)\s*\]/i) { # Check Master/Slave stopped my @stopped = (); @@ -118,8 +119,10 @@ sub parse_output { } } if (scalar(@stopped) > 0) { - $self->{output}->output_add(severity => $self->{threshold}, - short_msg => join(' ', @stopped) . " Stopped"); + $self->{output}->output_add( + severity => $self->{threshold}, + short_msg => join(' ', @stopped) . " Stopped" + ); } } elsif ($line =~ /^Failed actions\:/) { # Check Failed Actions @@ -138,35 +141,43 @@ sub parse_output { } } if ($error == 1) { - $self->{output}->output_add(severity => 'CRITICAL', - short_msg => "FAILED actions detected or not cleaned up"); + $self->{output}->output_add( + severity => 'CRITICAL', + short_msg => "FAILED actions detected or not cleaned up" + ); } } elsif ($line =~ /\s*(\S+?)\s+ \(.*\)\:\s+\w+\s+\w+\s+\(unmanaged\)\s+FAILED/) { # Check Unmanaged - $self->{output}->output_add(severity => 'CRITICAL', - short_msg => "$1 unmanaged FAILED"); + $self->{output}->output_add( + severity => 'CRITICAL', + short_msg => "$1 unmanaged FAILED" + ); } elsif ($line =~ /\s*(\S+?):.*(fail-count=\d+)/i) { # Check for resource Fail count - $self->{output}->output_add(severity => 'WARNING', - short_msg => "$1 failure detected, $2"); + $self->{output}->output_add( + severity => 'WARNING', + short_msg => "$1 failure detected, $2" + ); } } if (scalar(@standby) > 0 && !defined($self->{option_results}->{standbyignore})) { - $self->{output}->output_add(severity => $self->{threshold}, - short_msg => join( ', ', @standby ) . " in Standby"); + $self->{output}->output_add( + severity => $self->{threshold}, + short_msg => join( ', ', @standby ) . " in Standby" + ); } } sub run { my ($self, %options) = @_; - my $stdout = centreon::plugins::misc::execute(output => $self->{output}, - options => $self->{option_results}, - sudo => $self->{option_results}->{sudo}, - command => $self->{option_results}->{command}, - command_path => $self->{option_results}->{command_path}, - command_options => $self->{option_results}->{command_options}); + my ($stdout) = $options{custom}->execute_command( + command => 'crm_mon', + command_path => '/usr/sbin', + command_options => '-1 -r -f 2>&1' + ); + $self->parse_output(crm_out => $stdout); $self->{output}->display(); @@ -182,6 +193,8 @@ __END__ Check Cluster Resource Manager (need 'crm_mon' command). Should be executed on a cluster node. +Command used: /usr/sbin/crm_mon -1 -r -f 2>&1 + =over 8 =item B<--warning> @@ -206,47 +219,6 @@ Stopped clone resource on nodes (that match) are skipped. Failed actions errors (that match) are skipped. -=item B<--remote> - -Execute command remotely in 'ssh'. - -=item B<--hostname> - -Hostname to query (need --remote). - -=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<--sudo> - -Use 'sudo' to execute the command. - -=item B<--command> - -Command to get information (Default: 'crm_mon'). -Can be changed if you have output in a file. - -=item B<--command-path> - -Command path (Default: '/usr/sbin'). - -=item B<--command-options> - -Command options (Default: '-1 -r -f 2>&1'). - =back =cut diff --git a/centreon-plugins/apps/pacemaker/local/plugin.pm b/centreon-plugins/apps/pacemaker/local/plugin.pm index 15ae86391..ca4fad64e 100644 --- a/centreon-plugins/apps/pacemaker/local/plugin.pm +++ b/centreon-plugins/apps/pacemaker/local/plugin.pm @@ -22,7 +22,7 @@ package apps::pacemaker::local::plugin; use strict; use warnings; -use base qw(centreon::plugins::script_simple); +use base qw(centreon::plugins::script_custom); sub new { my ($class, %options) = @_; @@ -30,11 +30,13 @@ sub new { bless $self, $class; $self->{version} = '0.1'; - %{$self->{modes}} = ( + $self->{modes} = { 'clustat' => 'apps::pacemaker::local::mode::clustat', 'constraints' => 'apps::pacemaker::local::mode::constraints', - 'crm' => 'apps::pacemaker::local::mode::crm', - ); + 'crm' => 'apps::pacemaker::local::mode::crm' + }; + + $self->{custom_modes}->{cli} = 'centreon::plugins::script_custom::cli'; return $self; } diff --git a/centreon-plugins/centreon/plugins/script_custom/cli.pm b/centreon-plugins/centreon/plugins/script_custom/cli.pm new file mode 100644 index 000000000..d0f44a8fd --- /dev/null +++ b/centreon-plugins/centreon/plugins/script_custom/cli.pm @@ -0,0 +1,178 @@ +# +# Copyright 2020 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 centreon::plugins::script_custom::cli; + +use strict; +use warnings; +use centreon::plugins::ssh; +use centreon::plugins::misc; + +sub new { + my ($class, %options) = @_; + my $self = {}; + bless $self, $class; + + if (!defined($options{output})) { + print "Class Custom: Need to specify 'output' argument.\n"; + exit 3; + } + if (!defined($options{options})) { + $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument."); + $options{output}->option_exit(); + } + + if (!defined($options{noptions})) { + $options{options}->add_options(arguments => { + 'hostname:s' => { name => 'hostname' }, + 'timeout:s' => { name => 'timeout' }, + 'command:s' => { name => 'command' }, + 'command-path:s' => { name => 'command_path' }, + 'command-options:s' => { name => 'command_options' }, + 'sudo:s' => { name => 'sudo' } + }); + } + + $options{options}->add_help(package => __PACKAGE__, sections => 'CLI OPTIONS', once => 1); + + $self->{output} = $options{output}; + $self->{ssh} = centreon::plugins::ssh->new(%options); + + return $self; +} + +sub set_options { + my ($self, %options) = @_; + + $self->{option_results} = $options{option_results}; +} + +sub set_defaults {} + +sub check_options { + my ($self, %options) = @_; + + if (defined($self->{option_results}->{timeout}) && $self->{option_results}->{timeout} =~ /(\d+)/) { + $self->{timeout} = $1; + } + if (defined($self->{option_results}->{hostname}) && $self->{option_results}->{hostname} ne '') { + $self->{ssh}->check_options(option_results => $self->{option_results}); + } + + return 0; +} + +sub get_identifier { + my ($self, %options) = @_; + + my $id = defined($self->{option_results}->{hostname}) ? $self->{option_results}->{hostname} : 'me'; + if (defined($self->{option_results}->{hostname}) && $self->{option_results}->{hostname} ne '') { + $id .= ':' . $self->{ssh}->get_port(); + } + return $id; +} + +sub execute_command { + my ($self, %options) = @_; + + my $timeout = $self->{timeout}; + if (!defined($timeout)) { + $timeout = defined($options{timeout}) ? $options{timeout} : 45; + } + my $command_options = defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '' ? $self->{option_results}->{command_options} : $options{command_options}; + if (defined($options{command_options_suffix})) { + $command_options .= $options{command_options_suffix}; + } + + my ($stdout, $exit_code); + if (defined($self->{option_results}->{hostname}) && $self->{option_results}->{hostname} ne '') { + ($stdout, $exit_code) = $self->{ssh}->execute( + hostname => $self->{option_results}->{hostname}, + sudo => $self->{option_results}->{sudo}, + command => defined($self->{option_results}->{command}) && $self->{option_results}->{command} ne '' ? $self->{option_results}->{command} : $options{command}, + command_path => defined($self->{option_results}->{command_path}) && $self->{option_results}->{command_path} ne '' ? $self->{option_results}->{command_path} : $options{command_path}, + command_options => $command_options, + timeout => $timeout, + no_quit => $options{no_quit} + ); + } else { + ($stdout, $exit_code) = centreon::plugins::misc::execute( + output => $self->{output}, + sudo => $self->{option_results}->{sudo}, + options => { timeout => $timeout }, + command => defined($self->{option_results}->{command}) && $self->{option_results}->{command} ne '' ? $self->{option_results}->{command} : $options{command}, + command_path => defined($self->{option_results}->{command_path}) && $self->{option_results}->{command_path} ne '' ? $self->{option_results}->{command_path} : $options{command_path}, + command_options => $command_options, + no_quit => $options{no_quit} + ); + } + + $self->{output}->output_add(long_msg => "command response: $stdout", debug => 1); + + return ($stdout, $exit_code); +} + +1; + +__END__ + +=head1 NAME + +ssh + +=head1 SYNOPSIS + +my ssh + +=head1 CLI OPTIONS + +=over 8 + +=item B<--hostname> + +Hostname to query. + +=item B<--timeout> + +Timeout in seconds for the command (Default: 45). Default value can be override by the mode. + +=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. + +=item B<--sudo> + +sudo command. + +=back + +=head1 DESCRIPTION + +B. + +=cut diff --git a/centreon-plugins/os/linux/local/custom/cli.pm b/centreon-plugins/os/linux/local/custom/cli.pm index a38361a27..0c5edac96 100644 --- a/centreon-plugins/os/linux/local/custom/cli.pm +++ b/centreon-plugins/os/linux/local/custom/cli.pm @@ -40,7 +40,7 @@ sub new { } # discovery-snmp cannot be used at distance. - return $self if ($options{mode_name} eq 'discovery-snmp'); + return $self if (defined($options{mode_name}) && $options{mode_name} eq 'discovery-snmp'); if (!defined($options{noptions})) { $options{options}->add_options(arguments => { @@ -110,7 +110,7 @@ sub execute_command { hostname => $self->{option_results}->{hostname}, sudo => $self->{option_results}->{sudo}, command => defined($self->{option_results}->{command}) && $self->{option_results}->{command} ne '' ? $self->{option_results}->{command} : $options{command}, - command_path => defined($self->{option_results}->{command}) && $self->{option_results}->{command_path} ne '' ? $self->{option_results}->{command_path} : $options{command_path}, + command_path => defined($self->{option_results}->{command_path}) && $self->{option_results}->{command_path} ne '' ? $self->{option_results}->{command_path} : $options{command_path}, command_options => $command_options, timeout => $timeout, no_quit => $options{no_quit} @@ -121,7 +121,7 @@ sub execute_command { sudo => $self->{option_results}->{sudo}, options => { timeout => $timeout }, command => defined($self->{option_results}->{command}) && $self->{option_results}->{command} ne '' ? $self->{option_results}->{command} : $options{command}, - command_path => defined($self->{option_results}->{command}) && $self->{option_results}->{command_path} ne '' ? $self->{option_results}->{command_path} : $options{command_path}, + command_path => defined($self->{option_results}->{command_path}) && $self->{option_results}->{command_path} ne '' ? $self->{option_results}->{command_path} : $options{command_path}, command_options => $command_options, no_quit => $options{no_quit} );