From 4266841878006fb4590991f93a3bcf8de93bb884 Mon Sep 17 00:00:00 2001 From: qgarnier Date: Wed, 1 Jun 2022 13:46:39 +0200 Subject: [PATCH] (plugin) os::hpux::local - use backend ssh + new mode list-storages (#3705) --- centreon-plugins/os/hpux/local/mode/inodes.pm | 106 ++++----------- .../os/hpux/local/mode/liststorages.pm | 126 ++++++++++++++++++ .../os/hpux/local/mode/storage.pm | 107 ++++----------- centreon-plugins/os/hpux/local/plugin.pm | 15 ++- 4 files changed, 188 insertions(+), 166 deletions(-) create mode 100644 centreon-plugins/os/hpux/local/mode/liststorages.pm diff --git a/centreon-plugins/os/hpux/local/mode/inodes.pm b/centreon-plugins/os/hpux/local/mode/inodes.pm index 9404855d9..4c80b3df8 100644 --- a/centreon-plugins/os/hpux/local/mode/inodes.pm +++ b/centreon-plugins/os/hpux/local/mode/inodes.pm @@ -26,6 +26,12 @@ use strict; use warnings; use centreon::plugins::misc; +sub prefix_inodes_output { + my ($self, %options) = @_; + + return "Inodes partition '" . $options{instance_value}->{display} . "' "; +} + sub set_counters { my ($self, %options) = @_; @@ -38,58 +44,37 @@ sub set_counters { key_values => [ { name => 'used' }, { name => 'display' } ], output_template => 'Used: %s %%', perfdatas => [ - { label => 'used', value => 'used', template => '%d', - unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' }, - ], + { label => 'used', template => '%d', unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' } + ] } - }, + } ]; } -sub prefix_inodes_output { - my ($self, %options) = @_; - - return "Inodes partition '" . $options{instance_value}->{display} . "' "; -} - 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 => 'bdf' }, - "command-path:s" => { name => 'command_path' }, - "command-options:s" => { name => 'command_options', default => '-i 2>&1' }, - "filter-fs:s" => { name => 'filter_fs', }, - "name:s" => { name => 'name' }, - "regexp" => { name => 'use_regexp' }, - "regexp-isensitive" => { name => 'use_regexpi' }, - }); - $self->{result} = {}; + $options{options}->add_options(arguments => { + 'filter-fs:s' => { name => 'filter_fs', }, + 'name:s' => { name => 'name' }, + 'regexp' => { name => 'use_regexp' }, + 'regexp-isensitive' => { name => 'use_regexpi' } + }); + return $self; } sub manage_selection { my ($self, %options) = @_; - my ($stdout, $exit_code) = 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, $exit_code) = $options{custom}->execute_command( + command => 'bdf', + command_options => '-i 2>&1', no_quit => 1 ); + $self->{inodes} = {}; my @lines = split /\n/, $stdout; # Header not needed @@ -101,21 +86,21 @@ sub manage_selection { } next if ($line !~ /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/); my ($fs, $size, $used, $available, $percent, $iused, $ifree, $ipercent, $mount) = ($1, $2, $3, $4, $5, $6, $7, $8, $9); - + next if (defined($self->{option_results}->{filter_fs}) && $self->{option_results}->{filter_fs} ne '' && $fs !~ /$self->{option_results}->{filter_fs}/); - + next if (defined($self->{option_results}->{name}) && defined($self->{option_results}->{use_regexp}) && defined($self->{option_results}->{use_regexpi}) && $mount !~ /$self->{option_results}->{name}/i); next if (defined($self->{option_results}->{name}) && defined($self->{option_results}->{use_regexp}) && !defined($self->{option_results}->{use_regexpi}) && $mount !~ /$self->{option_results}->{name}/); next if (defined($self->{option_results}->{name}) && !defined($self->{option_results}->{use_regexp}) && !defined($self->{option_results}->{use_regexpi}) && $mount ne $self->{option_results}->{name}); - + $ipercent =~ s/%//g; $self->{inodes}->{$mount} = { display => $mount, used => $ipercent }; } - + if (scalar(keys %{$self->{inodes}}) <= 0) { if ($exit_code != 0) { $self->{output}->output_add(long_msg => "command output:" . $stdout); @@ -133,49 +118,10 @@ __END__ Check inodes usage on partitions. +Command used: bdf -i 2>&1 + =over 8 -=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: 'bdf'). -Can be changed if you have output in a file. - -=item B<--command-path> - -Command path (Default: none). - -=item B<--command-options> - -Command options (Default: '-i 2>&1'). - =item B<--warning-usage> Threshold warning in percent. diff --git a/centreon-plugins/os/hpux/local/mode/liststorages.pm b/centreon-plugins/os/hpux/local/mode/liststorages.pm new file mode 100644 index 000000000..69c29b121 --- /dev/null +++ b/centreon-plugins/os/hpux/local/mode/liststorages.pm @@ -0,0 +1,126 @@ +# +# Copyright 2022 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::hpux::local::mode::liststorages; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $options{options}->add_options(arguments => {}); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); +} + +sub manage_selection { + my ($self, %options) = @_; + + my ($stdout) = $options{custom}->execute_command( + command => 'bdf', + command_options => '2>&1', + no_quit => 1 + ); + + my $results = {}; + my @lines = split(/\n/, $stdout); + # Header not needed + shift @lines; + while (my $line = shift @lines) { + # When the line is too long, the FS name is printed on a separated line + if ($line !~ /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) { + $line .= " " . shift(@lines); + } + next if ($line !~ /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/); + my ($fs, $size, $mount) = ($1, $2 * 1024, $6); + + $results->{$mount} = { fs => $fs, size => $size }; + } + + return $results; +} + +sub run { + my ($self, %options) = @_; + + my $results = $self->manage_selection(custom => $options{custom}); + foreach my $name (sort(keys %$results)) { + $self->{output}->output_add( + long_msg => sprintf( + '[name: %s][fs: %s][size: %s]', + $name, + $results->{$name}->{fs}, + $results->{$name}->{size} + ) + ); + } + + $self->{output}->output_add( + severity => 'OK', + short_msg => 'List storages:' + ); + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); + $self->{output}->exit(); +} + +sub disco_format { + my ($self, %options) = @_; + + $self->{output}->add_disco_format(elements => ['name', 'fs', 'size']); +} + +sub disco_show { + my ($self, %options) = @_; + + my $results = $self->manage_selection(custom => $options{custom}); + foreach my $name (sort(keys %$results)) { + $self->{output}->add_disco_entry( + name => $name, + fs => $results->{$name}->{fs}, + size => $results->{$name}->{size} + ); + } +} + +1; + +__END__ + +=head1 MODE + +List storages. + +Command used: bdf 2>&1 + +=over 8 + +=back + +=cut diff --git a/centreon-plugins/os/hpux/local/mode/storage.pm b/centreon-plugins/os/hpux/local/mode/storage.pm index 1f4513656..0dba0e810 100644 --- a/centreon-plugins/os/hpux/local/mode/storage.pm +++ b/centreon-plugins/os/hpux/local/mode/storage.pm @@ -43,7 +43,8 @@ sub custom_usage_perfdata { } $self->{output}->perfdata_add( - label => $label, unit => 'B', + label => $label, + unit => 'B', instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef, value => $value_perf, warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options), @@ -72,11 +73,12 @@ sub custom_usage_output { my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}); my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}); my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); - my $msg = sprintf("Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", - $total_size_value . " " . $total_size_unit, - $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, - $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}); - return $msg; + return sprintf( + "Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", + $total_size_value . " " . $total_size_unit, + $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, + $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free} + ); } sub custom_usage_calc { @@ -96,6 +98,12 @@ sub custom_usage_calc { return 0; } +sub prefix_disks_output { + my ($self, %options) = @_; + + return "Storage '" . $options{instance_value}->{display} . "' "; +} + sub set_counters { my ($self, %options) = @_; @@ -115,53 +123,33 @@ sub set_counters { ]; } -sub prefix_disks_output { - my ($self, %options) = @_; - - return "Storage '" . $options{instance_value}->{display} . "' "; -} - 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 => 'bdf' }, - "command-path:s" => { name => 'command_path' }, - "command-options:s" => { name => 'command_options', default => ' 2>&1' }, - "filter-fs:s" => { name => 'filter_fs', }, - "units:s" => { name => 'units', default => '%' }, - "free" => { name => 'free' }, - "name:s" => { name => 'name' }, - "regexp" => { name => 'use_regexp' }, - "regexp-isensitive" => { name => 'use_regexpi' }, - "space-reservation:s" => { name => 'space_reservation' }, + 'filter-fs:s' => { name => 'filter_fs', }, + 'units:s' => { name => 'units', default => '%' }, + 'free' => { name => 'free' }, + 'name:s' => { name => 'name' }, + 'regexp' => { name => 'use_regexp' }, + 'regexp-isensitive' => { name => 'use_regexpi' }, + 'space-reservation:s' => { name => 'space_reservation' } }); - $self->{result} = {}; return $self; } sub manage_selection { my ($self, %options) = @_; - my ($stdout, $exit_code) = 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, $exit_code) = $options{custom}->execute_command( + command => 'bdf', + command_options => '2>&1', no_quit => 1 ); + $self->{disks} = {}; my @lines = split /\n/, $stdout; # Header not needed @@ -169,7 +157,7 @@ sub manage_selection { while (my $line = shift @lines) { # When the line is too long, the FS name is printed on a separated line if ($line !~ /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) { - $line .= " ".shift @lines; + $line .= " " . shift(@lines); } next if ($line !~ /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/); my ($fs, $size, $used, $available, $percent, $mount) = ($1, $2, $3, $4, $5, $6); @@ -208,49 +196,10 @@ __END__ Check storage usages. +Command used: bdf 2>&1 + =over 8 -=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: 'bdf'). -Can be changed if you have output in a file. - -=item B<--command-path> - -Command path (Default: none). - -=item B<--command-options> - -Command options (Default: ' 2>&1'). - =item B<--warning-usage> Threshold warning. diff --git a/centreon-plugins/os/hpux/local/plugin.pm b/centreon-plugins/os/hpux/local/plugin.pm index bf6b2ae08..89ae0aa68 100644 --- a/centreon-plugins/os/hpux/local/plugin.pm +++ b/centreon-plugins/os/hpux/local/plugin.pm @@ -22,19 +22,20 @@ package os::hpux::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) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; - $self->{version} = '0.1'; - %{$self->{modes}} = ( - 'inodes' => 'os::hpux::local::mode::inodes', - 'list-storages' => 'os::hpux::local::mode::liststorages', - 'storage' => 'os::hpux::local::mode::storage', - ); + $self->{modes} = { + 'inodes' => 'os::hpux::local::mode::inodes', + 'list-storages' => 'os::hpux::local::mode::liststorages', + 'storage' => 'os::hpux::local::mode::storage' + }; + + $self->{custom_modes}->{cli} = 'centreon::plugins::script_custom::cli'; return $self; }