diff --git a/centreon-plugins/os/aix/local/custom/cli.pm b/centreon-plugins/os/aix/local/custom/cli.pm new file mode 100644 index 000000000..53da4590e --- /dev/null +++ b/centreon-plugins/os/aix/local/custom/cli.pm @@ -0,0 +1,170 @@ +# +# 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 os::aix::local::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', default => 45 }, + '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->{mode} = $options{mode}; + $self->{ssh} = centreon::plugins::ssh->new(%options); + + return $self; +} + +sub set_options { + my ($self, %options) = @_; + + $self->{option_results} = $options{option_results}; +} + +sub set_defaults { + my ($self, %options) = @_; + + foreach (keys %{$options{default}}) { + if ($_ eq $self->{mode}) { + for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) { + foreach my $opt (keys %{$options{default}->{$_}[$i]}) { + if (!defined($self->{option_results}->{$opt}[$i])) { + $self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt}; + } + } + } + } + } +} + +sub check_options { + my ($self, %options) = @_; + + if (defined($self->{option_results}->{hostname}) && $self->{option_results}->{hostname} ne '') { + $self->{ssh}->check_options(option_results => $self->{option_results}); + } + + return 0; +} + +sub execute_command { + my ($self, %options) = @_; + + 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 => $self->{option_results}->{command_path}, + command_options => defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '' ? $self->{option_results}->{command_options} : $options{command_options}, + timeout => $self->{option_results}->{timeout}, + no_quit => $options{no_quit} + ); + } else { + ($stdout, $exit_code) = centreon::plugins::misc::execute( + output => $self->{output}, + sudo => $self->{option_results}->{sudo}, + options => { timeout => $self->{option_results}->{timeout} }, + command => defined($self->{option_results}->{command}) && $self->{option_results}->{command} ne '' ? $self->{option_results}->{command} : $options{command}, + command_path => $self->{option_results}->{command_path}, + command_options => defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '' ? $self->{option_results}->{command_options} : $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). + +=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/aix/local/mode/cmdreturn.pm b/centreon-plugins/os/aix/local/mode/cmdreturn.pm index cf218e037..a47d18430 100644 --- a/centreon-plugins/os/aix/local/mode/cmdreturn.pm +++ b/centreon-plugins/os/aix/local/mode/cmdreturn.pm @@ -24,41 +24,33 @@ 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); + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); 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' }, - 'command-path:s' => { name => 'command_path' }, - 'command-options:s' => { name => 'command_options' }, - 'manage-returns:s' => { name => 'manage_returns', default => '' }, - 'separator:s' => { name => 'separator', default => '#' }, + $options{options}->add_options(arguments => { + 'exec-command:s' => { name => 'exec_command' }, + 'exec-command-path:s' => { name => 'exec_command_path' }, + 'exec-command-options:s' => { name => 'exec_command_options' }, + 'manage-returns:s' => { name => 'manage_returns', default => '' }, + 'separator:s' => { name => 'separator', default => '#' } }); - $self->{manage_returns} = {}; return $self; } sub check_options { my ($self, %options) = @_; $self->SUPER::init(%options); - - if (!defined($self->{option_results}->{command})) { - $self->{output}->add_option_msg(short_msg => "Need to specify command option."); + + if (!defined($self->{option_results}->{exec_command})) { + $self->{output}->add_option_msg(short_msg => "Need to specify exec-command option."); $self->{output}->option_exit(); } - + + $self->{manage_returns} = {}; foreach my $entry (split(/$self->{option_results}->{separator}/, $self->{option_results}->{manage_returns})) { next if (!($entry =~ /(.*?),(.*?),(.*)/)); next if (!$self->{output}->is_litteral_status(status => $2)); @@ -77,15 +69,13 @@ sub check_options { sub run { 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 => $self->{option_results}->{exec_command}, + command_path => $self->{option_results}->{exec_command_path}, + command_options => $self->{option_results}->{exec_command_options}, no_quit => 1 ); + my $long_msg = $stdout; $long_msg =~ s/\|/~/mg; $self->{output}->output_add(long_msg => $long_msg); @@ -109,11 +99,11 @@ sub run { if (defined($exit_code)) { $self->{output}->perfdata_add( - label => 'code', + nlabel => 'command.exit.code.count', value => $exit_code ); } - + $self->{output}->display(); $self->{output}->exit(); } @@ -137,44 +127,16 @@ Example: 0,OK,File xxx exist#1,CRITICAL,File xxx not exist#,UNKNOWN,Command prob Set the separator used in --manage-returns (default : #) -=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> +=item B<--exec-command> Command to test (Default: none). You can use 'sh' to use '&&' or '||'. -=item B<--command-path> +=item B<--exec-command-path> Command path (Default: none). -=item B<--command-options> +=item B<--exec-command-options> Command options (Default: none). diff --git a/centreon-plugins/os/aix/local/mode/errpt.pm b/centreon-plugins/os/aix/local/mode/errpt.pm index 8c4fc21c8..5cc37e041 100644 --- a/centreon-plugins/os/aix/local/mode/errpt.pm +++ b/centreon-plugins/os/aix/local/mode/errpt.pm @@ -24,24 +24,13 @@ 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 => 'errpt' }, - 'command-path:s' => { name => 'command_path' }, - 'command-options:s' => { name => 'command_options', default => '' }, 'error-type:s' => { name => 'error_type' }, 'error-class:s' => { name => 'error_class' }, 'error-id:s' => { name => 'error_id' }, @@ -51,16 +40,16 @@ sub new { 'filter-resource:s' => { name => 'filter_resource' }, 'filter-id:s' => { name => 'filter_id' }, 'exclude-id:s' => { name => 'exclude_id' }, - 'format-date' => { name => 'format_date' }, + 'format-date' => { name => 'format_date' } }); - $self->{result} = {}; + return $self; } sub check_options { my ($self, %options) = @_; $self->SUPER::init(%options); - + if (defined($self->{option_results}->{exclude_id}) && defined($self->{option_results}->{error_id})) { $self->{output}->add_option_msg(short_msg => "Please use --error-id OR --exclude-id, these options are mutually exclusives"); $self->{output}->option_exit(); @@ -69,8 +58,8 @@ sub check_options { sub manage_selection { my ($self, %options) = @_; - my $extra_options = ''; + my $extra_options = ''; if (defined($self->{option_results}->{error_type})){ $extra_options .= ' -T '.$self->{option_results}->{error_type}; } @@ -88,7 +77,7 @@ sub manage_selection { if (defined($self->{option_results}->{timezone})){ $ENV{TZ} = $self->{option_results}->{timezone}; } - my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($retention); + my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($retention); $year = $year - 100; if (length($sec) == 1){ $sec = '0' . $sec; @@ -109,17 +98,13 @@ sub manage_selection { $retention = $mon . $mday . $hour . $min . $year; $extra_options .= ' -s '.$retention; } - - $extra_options .= $self->{option_results}->{command_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}, + my ($stdout) = $options{custom}->execute_command( + command => 'errpt', command_options => $extra_options ); + + my $results = {}; my @lines = split /\n/, $stdout; # Header not needed shift @lines; @@ -127,50 +112,65 @@ sub manage_selection { next if ($line !~ /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)/); my ($identifier, $timestamp, $resource_name, $description) = ($1, $2, $5, $6); - $self->{result}->{ $timestamp . '~' . $identifier . '~' . $resource_name } = { description => $description }; + $results->{ $timestamp . '~' . $identifier . '~' . $resource_name } = { description => $description }; } + + return $results; } sub run { my ($self, %options) = @_; + my $extra_message = ''; - if (defined($self->{option_results}->{retention})) { $extra_message = ' since ' . $self->{option_results}->{retention} . ' seconds'; } - - $self->manage_selection(); + + my $results = $self->manage_selection(custom => $options{custom}); $self->{output}->output_add( severity => 'OK', short_msg => sprintf("No error found%s.", $extra_message) ); my $total_error = 0; - foreach my $errpt_error (sort(keys %{$self->{result}})) { + foreach my $errpt_error (sort(keys %$results)) { my @split_error = split ('~', $errpt_error); my $timestamp = $split_error[0]; my $identifier = $split_error[1]; my $resource_name = $split_error[2]; - my $description = $self->{result}->{$errpt_error}->{description}; - + my $description = $results->{$errpt_error}->{description}; + next if (defined($self->{option_results}->{filter_resource}) && $self->{option_results}->{filter_resource} ne '' && - $resource_name !~ /$self->{option_results}->{filter_resource}/); + $resource_name !~ /$self->{option_results}->{filter_resource}/); next if (defined($self->{option_results}->{filter_id}) && $self->{option_results}->{filter_id} ne '' && - $identifier !~ /$self->{option_results}->{filter_id}/); + $identifier !~ /$self->{option_results}->{filter_id}/); my $output_date = $split_error[0]; if (defined($self->{option_results}->{format_date})) { my ($month, $day, $hour, $minute, $year) = unpack("(A2)*", $output_date); $output_date = sprintf("20%s/%s/%s %s:%s", $year, $month, $day, $hour, $minute); } - + $total_error++; if (defined($description)) { - $self->{output}->output_add(long_msg => sprintf("Error '%s' Date: %s ResourceName: %s Description: %s", $identifier, - $output_date, $resource_name, $description)); + $self->{output}->output_add( + long_msg => sprintf( + "Error '%s' Date: %s ResourceName: %s Description: %s", + $identifier, + $output_date, + $resource_name, + $description + ) + ); } else { - $self->{output}->output_add(long_msg => sprintf("Error '%s' Date: %s ResourceName: %s", $identifier, - $output_date, $resource_name)); + $self->{output}->output_add( + long_msg => sprintf( + "Error '%s' Date: %s ResourceName: %s", + $identifier, + $output_date, + $resource_name + ) + ); } } @@ -192,46 +192,10 @@ __END__ =head1 MODE Check errpt messages. +Command used: 'errpt' with dynamic options =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: 'errpt'). -Can be changed if you have output in a file. - -=item B<--command-path> - -Command path (Default: none). - =item B<--error-type> Filter error type separated by a coma (INFO, PEND, PERF, PERM, TEMP, UNKN). diff --git a/centreon-plugins/os/aix/local/mode/inodes.pm b/centreon-plugins/os/aix/local/mode/inodes.pm index 41edea786..94f789789 100644 --- a/centreon-plugins/os/aix/local/mode/inodes.pm +++ b/centreon-plugins/os/aix/local/mode/inodes.pm @@ -24,54 +24,40 @@ use base qw(centreon::plugins::templates::counter); use strict; use warnings; -use centreon::plugins::misc; sub set_counters { my ($self, %options) = @_; - + $self->{maps_counters_type} = [ { name => 'inodes', type => 1, cb_prefix_output => 'prefix_inodes_output', message_multiple => 'All inode partitions are ok' } ]; - + $self->{maps_counters}->{inodes} = [ - { label => 'usage', set => { - key_values => [ { name => 'used' }, { name => 'display' } ], - output_template => 'Used: %s %%', + { label => 'usage', nlabel => 'storage.inodes.usage.percentage', set => { + key_values => [ { name => 'used' } ], + output_template => 'used: %s %%', perfdatas => [ - { label => 'used', value => 'used', template => '%d', - unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' }, - ], + { template => '%.2f', unit => '%', min => 0, max => 100, label_extra_instance => 1 } + ] } - }, + } ]; } 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); + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); 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 => 'df' }, - "command-path:s" => { name => 'command_path' }, - "command-options:s" => { name => 'command_options', default => '-i -v 2>&1' }, - "filter-fs:s" => { name => 'filter_fs', }, - "name:s" => { name => 'name' }, - "regexp" => { name => 'use_regexp' }, - "regexp-isensitive" => { name => 'use_regexpi' }, + 'filter-fs:s' => { name => 'filter_fs' }, + 'filter-mount:s' => { name => 'filter_mount' } }); return $self; @@ -80,15 +66,11 @@ sub new { 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}, - no_quit => 1 + my ($stdout, $exit_code) = $options{custom}->execute_command( + command => 'df', + command_options => '-i -v 2>&1' ); + $self->{inodes} = {}; my @lines = split /\n/, $stdout; # Header not needed @@ -100,29 +82,21 @@ sub manage_selection { # #Filesystem 512-blocks Free %Used Iused %Iused Mounted on #/dev/hd0 19368 9976 48% 4714 5% / - + next if ($line !~ /^(\S+)/); my $fs = $1; next if ($line !~ /(\d+)%\s+([^%]*?)$/); my ($ipercent, $mount) = ($1, $2); - + 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}); - + $fs !~ /$self->{option_results}->{filter_fs}/); + next if (defined($self->{option_results}->{filter_mount}) && $self->{option_results}->{filter_mount} ne '' && + $mount !~ /$self->{option_results}->{filter_mount}/); + $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); - } $self->{output}->add_option_msg(short_msg => "No storage found (filters or command issue)"); $self->{output}->option_exit(); } @@ -135,49 +109,17 @@ __END__ =head1 MODE Check inodes usage on partitions. +Command used: df -i -v 2>&1 =over 8 -=item B<--remote> +=item B<--filter-fs> -Execute command remotely in 'ssh'. +Filter filesystem (regexp can be used). -=item B<--hostname> +=item B<--filter-mount> -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: 'df'). -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 -v 2>&1'). +Filter mountpoint (regexp can be used). =item B<--warning-usage> @@ -187,22 +129,6 @@ Threshold warning in percent. Threshold critical in percent. -=item B<--name> - -Set the storage mount point (empty means 'check all storages') - -=item B<--regexp> - -Allows to use regexp to filter storage mount point (with option --name). - -=item B<--regexp-isensitive> - -Allows to use regexp non case-sensitive (with --regexp). - -=item B<--filter-fs> - -Filter filesystem (regexp can be used). - =back =cut diff --git a/centreon-plugins/os/aix/local/mode/liststorages.pm b/centreon-plugins/os/aix/local/mode/liststorages.pm index 1b3cf2297..7cc38d32c 100644 --- a/centreon-plugins/os/aix/local/mode/liststorages.pm +++ b/centreon-plugins/os/aix/local/mode/liststorages.pm @@ -24,29 +24,17 @@ 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 => 'df' }, - "command-path:s" => { name => 'command_path' }, - "command-options:s" => { name => 'command_options', default => '-P -k 2>&1' }, - "filter-fs:s" => { name => 'filter_fs', }, - "filter-mount:s" => { name => 'filter_mount', }, - }); - $self->{result} = {}; + $options{options}->add_options(arguments => { + 'filter-fs:s' => { name => 'filter_fs' }, + 'filter-mount:s' => { name => 'filter_mount' } + }); + return $self; } @@ -58,12 +46,12 @@ sub check_options { 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 => 'df', + command_options => '-P -k 2>&1' + ); + + my $results = {}; my @lines = split /\n/, $stdout; # Header not needed shift @lines; @@ -73,29 +61,39 @@ sub manage_selection { if (defined($self->{option_results}->{filter_fs}) && $self->{option_results}->{filter_fs} ne '' && $fs !~ /$self->{option_results}->{filter_fs}/) { - $self->{output}->output_add(long_msg => "Skipping storage '" . $mount . "': no matching filter fs"); + $self->{output}->output_add(long_msg => "skipping storage '" . $mount . "': no matching filter fs", debug => 1); next; } if (defined($self->{option_results}->{filter_mount}) && $self->{option_results}->{filter_mount} ne '' && $mount !~ /$self->{option_results}->{filter_mount}/) { - $self->{output}->output_add(long_msg => "Skipping storage '" . $mount . "': no matching filter mount"); + $self->{output}->output_add(long_msg => "skipping storage '" . $mount . "': no matching filter mount", debug => 1); next; } - $self->{result}->{$mount} = {fs => $fs}; + $results->{$mount} = { fs => $fs }; } + + return $results; } sub run { my ($self, %options) = @_; - $self->manage_selection(); - foreach my $name (sort(keys %{$self->{result}})) { - $self->{output}->output_add(long_msg => "'" . $name . "' [fs = " . $self->{result}->{$name}->{fs} . ']'); + my $results = $self->manage_selection(custom => $options{custom}); + foreach my $name (sort(keys %$results)) { + $self->{output}->output_add( + long_msg => sprintf( + "'%s' [fs = %s]", + $name, + $results->{$name}->{fs} + ) + ); } - $self->{output}->output_add(severity => 'OK', - short_msg => 'List storages:'); + $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(); } @@ -109,11 +107,12 @@ sub disco_format { sub disco_show { my ($self, %options) = @_; - $self->manage_selection(); - foreach my $name (sort(keys %{$self->{result}})) { - $self->{output}->add_disco_entry(name => $name, - fs => $self->{result}->{$name}->{fs}, - ); + 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}, + ); } } @@ -124,50 +123,10 @@ __END__ =head1 MODE List storages. +Command used: df -P -k 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: 'df'). -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: '-P -k 2>&1'). - =item B<--filter-fs> Filter filesystem (regexp can be used). diff --git a/centreon-plugins/os/aix/local/mode/lvsync.pm b/centreon-plugins/os/aix/local/mode/lvsync.pm index 32f91f7ee..d53d9df6d 100644 --- a/centreon-plugins/os/aix/local/mode/lvsync.pm +++ b/centreon-plugins/os/aix/local/mode/lvsync.pm @@ -20,53 +20,89 @@ package os::aix::local::mode::lvsync; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; -use centreon::plugins::misc; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); + +sub custom_status_output { + my ($self, %options) = @_; + + return sprintf( + 'state: %s [lp: %s pp: %s pv: %s]', + $self->{result_values}->{state}, + $self->{result_values}->{lp}, + $self->{result_values}->{pp}, + $self->{result_values}->{pv} + ); +} + +sub prefix_lv_output { + my ($self, %options) = @_; + + return sprintf( + "Logical volume '%s' [mount point: %s] ", + $options{instance_value}->{lv}, + $options{instance_value}->{mount} + ); +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'lvs', type => 1, cb_prefix_output => 'prefix_lv_output', message_multiple => 'All logical volumes are ok', skipped_code => { -10 => 1 } } + ]; + + $self->{maps_counters}->{lvs} = [ + { label => 'status', threshold => 0, set => { + key_values => [ + { name => 'state' }, { name => 'mount' }, + { name => 'lv' }, { name => 'pp' }, + { name => 'pv' }, { name => 'lp' }, + { name => 'type' } + ], + 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 => - { - "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 => 'lsvg' }, - "command-path:s" => { name => 'command_path' }, - "command-options:s" => { name => 'command_options', default => '-o | lsvg -i -l 2>&1' }, - "filter-state:s" => { name => 'filter_state', default => 'stale' }, - "filter-type:s" => { name => 'filter_type', }, - "name:s" => { name => 'name' }, - "regexp" => { name => 'use_regexp' }, - "regexp-isensitive" => { name => 'use_regexpi' }, - }); + $options{options}->add_options(arguments => { + 'filter-type:s' => { name => 'filter_type' }, + 'unknown-status:s' => { name => 'unknown_status', default => '' }, + 'warning-status:s' => { name => 'warning_status', default => '' }, + 'critical-status:s' => { name => 'critical_status', default => '%{state} =~ /stale/i' }, + }); + $self->{result} = {}; return $self; } sub check_options { my ($self, %options) = @_; - $self->SUPER::init(%options); + $self->SUPER::check_options(%options); + + $self->change_macros(macros => ['warning_status', 'critical_status', 'unknown_status']); } 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 => 'lsvg', + command_options => '-o | lsvg -i -l 2>&1' + ); + + $self->{lvs} = {}; my @lines = split /\n/, $stdout; # Header not needed shift @lines; @@ -75,62 +111,27 @@ sub manage_selection { next if ($line !~ /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)/); my ($lv, $type, $lp, $pp, $pv, $lvstate, $mount) = ($1, $2, $3, $4, $5, $6, $7); - next if (defined($self->{option_results}->{filter_state}) && $self->{option_results}->{filter_state} ne '' && - $lvstate !~ /$self->{option_results}->{filter_state}/); next if (defined($self->{option_results}->{filter_type}) && $self->{option_results}->{filter_type} ne '' && - $type !~ /$self->{option_results}->{filter_type}/); - - 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}); + $type !~ /$self->{option_results}->{filter_type}/); + next if (defined($self->{option_results}->{filter_mount}) && $self->{option_results}->{filter_mount} ne '' && + $mount !~ /$self->{option_results}->{filter_mount}/); - $self->{result}->{$mount} = {lv => $lv, type => $type, lp => $lp, pp => $pp, pv => $pv, lvstate => $lvstate}; + $self->{lvs}->{$mount} = { + lv => $lv, + mount => $mount, + type => $type, + lp => $lp, + pp => $pp, + pv => $pv, + state => $lvstate + }; } } - -} -sub run { - my ($self, %options) = @_; - - $self->manage_selection(); - - if (scalar(keys %{$self->{result}}) <= 0) { - $self->{output}->output_add(long_msg => 'All LV are ok.'); - $self->{output}->output_add(severity => 'OK', - short_msg => 'All LV are ok.'); - } else { - my $num_disk_check = 0; - foreach my $name (sort(keys %{$self->{result}})) { - $num_disk_check++; - my $lv = $self->{result}->{$name}->{lv}; - my $type = $self->{result}->{$name}->{type}; - my $lp = $self->{result}->{$name}->{lp}; - my $pp = $self->{result}->{$name}->{pp}; - my $pv = $self->{result}->{$name}->{pv}; - my $lvstate = $self->{result}->{$name}->{lvstate}; - my $mount = $name; - - $self->{output}->output_add(long_msg => sprintf("LV '%s' MountPoint: '%s' State: '%s' [LP: %s PP: %s PV: %s]", $lv, - $mount, $lvstate, - $lp, $pp, $pv)); - $self->{output}->output_add(severity => 'critical', - short_msg => sprintf("LV '%s' MountPoint: '%s' State: '%s' [LP: %s PP: %s PV: %s]", $lv, - $mount, $lvstate, - $lp, $pp, $pv)); - } - - if ($num_disk_check == 0) { - $self->{output}->add_option_msg(short_msg => "No lv checked."); - $self->{output}->option_exit(); - } + if (scalar(keys %{$self->{lvs}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No logical volumes found."); + $self->{output}->option_exit(); } - - $self->{output}->display(); - $self->{output}->exit(); } 1; @@ -140,70 +141,33 @@ __END__ =head1 MODE Check vg mirroring. +Command used: lsvg -o | lsvg -i -l 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: 'lsvg'). -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: '-o | lsvg -i -l 2>&1'). - -=item B<--name> - -Set the storage mount point (empty means 'check all storages') - -=item B<--regexp> - -Allows to use regexp to filter storage mount point (with option --name). - -=item B<--regexp-isensitive> - -Allows to use regexp non case-sensitive (with --regexp). - -=item B<--filter-state> - -Filter filesystem state (Default: stale) (regexp can be used). - =item B<--filter-type> Filter filesystem type (regexp can be used). +=item B<--filter-mount> + +Filter storage mount point (regexp can be used). + +=item B<--unknown-status> + +Set unknown threshold for status. +Can used special variables like: %{state}, %{lv}, %{mount}, %{type}. + +=item B<--warning-status> + +Set warning threshold for status. +Can used special variables like: %{state}, %{lv}, %{mount}, %{type}. + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{state} =~ /stale/i'). +Can used special variables like: %{state}, %{lv}, %{mount}, %{type}. + =back =cut diff --git a/centreon-plugins/os/aix/local/mode/process.pm b/centreon-plugins/os/aix/local/mode/process.pm index 8bab5d2a1..3c2d4e904 100644 --- a/centreon-plugins/os/aix/local/mode/process.pm +++ b/centreon-plugins/os/aix/local/mode/process.pm @@ -24,19 +24,18 @@ use base qw(centreon::plugins::templates::counter); use strict; use warnings; -use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_calc); -use centreon::plugins::misc; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); sub custom_status_output { my ($self, %options) = @_; - my $msg = sprintf('Process [command => %s] [arg: %s] [state: %s] [elapsed => %s]', + return sprintf( + 'Process [command => %s] [arg: %s] [state: %s] [elapsed => %s]', $self->{result_values}->{cmd}, $self->{result_values}->{args}, $self->{result_values}->{state}, $self->{result_values}->{elapsed} ); - return $msg; } sub set_counters { @@ -63,9 +62,10 @@ sub set_counters { $self->{maps_counters}->{alarm} = [ { label => 'status', threshold => 0, set => { - key_values => [ { name => 'ppid' }, { name => 'state' }, - { name => 'elapsed' }, { name => 'cmd' }, { name => 'args' } ], - closure_custom_calc => \&catalog_status_calc, + key_values => [ + { name => 'ppid' }, { name => 'state' }, + { name => 'elapsed' }, { name => 'cmd' }, { name => 'args' } + ], closure_custom_output => $self->can('custom_status_output'), closure_custom_perfdata => sub { return 0; }, closure_custom_threshold_check => \&catalog_status_threshold, @@ -80,22 +80,12 @@ sub new { 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 => 'ps' }, - 'command-path:s' => { name => 'command_path' }, - 'command-options:s' => { name => 'command_options', default => '-e -o state -o ===%t===%p===%P=== -o comm:50 -o ===%a 2>&1' }, 'filter-command:s' => { name => 'filter_command' }, 'filter-arg:s' => { name => 'filter_arg' }, 'filter-state:s' => { name => 'filter_state' }, 'filter-ppid:s' => { name => 'filter_ppid' }, 'warning-status:s' => { name => 'warning_status', default => '' }, - 'critical-status:s' => { name => 'critical_status', default => '' }, + 'critical-status:s' => { name => 'critical_status', default => '' } }); return $self; @@ -147,13 +137,9 @@ sub get_time_seconds { 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 => 'ps', + command_options => '-e -o state -o ===%t===%p===%P=== -o comm:50 -o ===%a 2>&1' ); $self->{alarms}->{global} = { alarm => {} }; @@ -194,51 +180,10 @@ __END__ =head1 MODE Check AIX processes. -Can filter on commands, arguments and states. +Command used: ps -e -o state -o ===%t===%p===%P=== -o comm:50 -o ===%a 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: 'ps'). -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: '-e -o state -o ===%t===%p===%P=== -o comm:50 -o ===%a 2>&1'). - =item B<--filter-command> Filter process commands (regexp can be used). @@ -269,7 +214,8 @@ Can used special variables like: %{ppid}, %{state}, %{elapsed}, %{cmd}, %{args} =item B<--warning-*> B<--critical-*> -Thresholds. Can be: 'total'. +Thresholds. +Can be: 'total'. =back diff --git a/centreon-plugins/os/aix/local/mode/storage.pm b/centreon-plugins/os/aix/local/mode/storage.pm index 614b35d9c..cb716e796 100644 --- a/centreon-plugins/os/aix/local/mode/storage.pm +++ b/centreon-plugins/os/aix/local/mode/storage.pm @@ -20,173 +20,144 @@ package os::aix::local::mode::storage; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; -use centreon::plugins::misc; + +sub custom_usage_output { + my ($self, %options) = @_; + + my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total_space}); + my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used_space}); + my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free_space}); + return sprintf( + 'space 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_space}, + $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free_space} + ); +} + +sub prefix_storage_output { + my ($self, %options) = @_; + + return "Storage '" . $options{instance_value}->{display} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'storages', type => 1, cb_prefix_output => 'prefix_storage_output', message_multiple => 'All storages are ok', skipped_code => { -10 => 1 } } + ]; + + $self->{maps_counters}->{storages} = [ + { label => 'usage', nlabel => 'storage.space.usage.bytes', set => { + key_values => [ + { name => 'used_space' }, { name => 'free_space' }, + { name => 'prct_used_space' }, { name => 'prct_free_space' }, + { name => 'total_space' } + ], + closure_custom_output => $self->can('custom_usage_output'), + perfdatas => [ + { template => '%d', min => 0, max => 'total_space', + unit => 'B', cast_int => 1, label_extra_instance => 1 } + ] + } + }, + { label => 'usage-free', nlabel => 'storage.space.free.bytes', display_ok => 0, set => { + key_values => [ + { name => 'free_space' }, { name => 'used_space' }, + { name => 'prct_used_space' }, { name => 'prct_free_space' }, + { name => 'total_space' } + ], + closure_custom_output => $self->can('custom_usage_output'), + perfdatas => [ + { value => 'free_space', template => '%d', min => 0, max => 'total_space', + unit => 'B', cast_int => 1, label_extra_instance => 1 } + ] + } + }, + { label => 'usage-prct', nlabel => 'storageresource.space.usage.percentage', display_ok => 0, set => { + key_values => [ { name => 'prct_used_space' } ], + output_template => 'used: %.2f %%', + perfdatas => [ + { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 } + ] + } + } + ]; +} sub new { my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %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' }, - "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 => 'df' }, - "command-path:s" => { name => 'command_path' }, - "command-options:s" => { name => 'command_options', default => '-P -k 2>&1' }, - "filter-fs:s" => { name => 'filter_fs', }, - "warning:s" => { name => 'warning' }, - "critical:s" => { name => 'critical' }, - "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; -} + $options{options}->add_options(arguments => { + 'filter-fs:s' => { name => 'filter_fs' }, + 'filter-mount:s' => { name => 'filter_mount' }, + 'space-reservation:s' => { name => 'space_reservation' } + }); -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); - $self->{output}->option_exit(); - } + return $self; } 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 => 'df', + command_options => '-P -k 2>&1' + ); + + $self->{storages} = {}; my @lines = split /\n/, $stdout; # Header not needed shift @lines; foreach my $line (@lines) { next if ($line !~ /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)/); my ($fs, $size, $used, $available, $percent, $mount) = ($1, $2, $3, $4, $5, $6); - + 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}); + $fs !~ /$self->{option_results}->{filter_fs}/); + next if (defined($self->{option_results}->{filter_mount}) && $self->{option_results}->{filter_mount} ne '' && + $mount !~ /$self->{option_results}->{filter_mount}/); - next if ($size !~ m/^\d+$/ || $used !~ m/^\d+$/ || $available !~ m/^\d+$/); + next if ($size !~ /^\d+$/ || $used !~ /^\d+$/ || $available !~ /^\d+$/); + next if ($size == 0); - $self->{result}->{$mount} = {fs => $fs, total => $size, used => $used, free => $available}; - } - - if (scalar(keys %{$self->{result}}) <= 0) { - if (defined($self->{option_results}->{name})) { - $self->{output}->add_option_msg(short_msg => "No storage found for mount point '" . $self->{option_results}->{name} . "'."); - } else { - $self->{output}->add_option_msg(short_msg => "No storage found."); - } - $self->{output}->option_exit(); - } -} - -sub run { - my ($self, %options) = @_; - - $self->manage_selection(); - if (!defined($self->{option_results}->{name}) || defined($self->{option_results}->{use_regexp})) { - $self->{output}->output_add(severity => 'OK', - short_msg => 'All storages are ok.'); - } - - my $num_disk_check = 0; - foreach my $name (sort(keys %{$self->{result}})) { - my $total_size = $self->{result}->{$name}->{total} * 1024; + $size = $size * 1024; + my $reserved_value = 0; if (defined($self->{option_results}->{space_reservation})) { - $total_size = $total_size - ($self->{option_results}->{space_reservation} * $total_size / 100); + $reserved_value = $self->{option_results}->{space_reservation} * $size / 100; } - next if ($total_size == 0); - - $num_disk_check++; - my $total_used = $self->{result}->{$name}->{used} * 1024; - my $total_free = $self->{result}->{$name}->{free} * 1024; - my $prct_used = $total_used * 100 / $total_size; - my $prct_free = 100 - $prct_used; - - my ($exit, $threshold_value); - - $threshold_value = $total_used; - $threshold_value = $total_free if (defined($self->{option_results}->{free})); - if ($self->{option_results}->{units} eq '%') { - $threshold_value = $prct_used; - $threshold_value = $prct_free if (defined($self->{option_results}->{free})); - } - $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); - - my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $total_size); - my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $total_used); - my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => ($total_size - $total_used)); - - $self->{output}->output_add(long_msg => sprintf("Storage '%s' Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", $name, - $total_size_value . " " . $total_size_unit, - $total_used_value . " " . $total_used_unit, $prct_used, - $total_free_value . " " . $total_free_unit, $prct_free)); - if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1) || (defined($self->{option_results}->{name}) && !defined($self->{option_results}->{use_regexp}))) { - $self->{output}->output_add(severity => $exit, - short_msg => sprintf("Storage '%s' Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", $name, - $total_size_value . " " . $total_size_unit, - $total_used_value . " " . $total_used_unit, $prct_used, - $total_free_value . " " . $total_free_unit, $prct_free)); - } - - my $label = 'used'; - my $value_perf = $total_used; - if (defined($self->{option_results}->{free})) { - $label = 'free'; - $value_perf = $total_free; + my $used_space = $used * 1024; + my $free_space = $size - $used_space - $reserved_value; + my $prct_used_space = $used_space * 100 / ($size - $reserved_value); + my $prct_free_space = 100 - $prct_used_space; + # limit to 100. Better output. + if ($prct_used_space > 100) { + $free_space = 0; + $prct_used_space = 100; + $prct_free_space = 0; } - my $extra_label = ''; - $extra_label = '_' . $name if (!defined($self->{option_results}->{name}) || defined($self->{option_results}->{use_regexp})); - my %total_options = (); - if ($self->{option_results}->{units} eq '%') { - $total_options{total} = $total_size; - $total_options{cast_int} = 1; - } - $self->{output}->perfdata_add(label => $label . $extra_label, unit => 'B', - value => $value_perf, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning', %total_options), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical', %total_options), - min => 0, max => $total_size); + + $self->{storages}->{$mount} = { + display => $mount, + total_space => $size, + used_space => $used_space, + free_space => $free_space, + prct_used_space => $prct_used_space, + prct_free_space => $prct_free_space + }; } - if ($num_disk_check == 0) { - $self->{output}->add_option_msg(short_msg => "No storage checked."); + if (scalar(keys %{$self->{storages}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No storage found."); $self->{output}->option_exit(); } - - $self->{output}->display(); - $self->{output}->exit(); } 1; @@ -196,87 +167,28 @@ __END__ =head1 MODE Check storage usages. +Command used: df -P -k 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: 'df'). -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: '-P -k 2>&1'). - -=item B<--warning> - -Threshold warning. - -=item B<--critical> - -Threshold critical. - -=item B<--units> - -Units of thresholds (Default: '%') ('%', 'B'). - -=item B<--free> - -Thresholds are on free space left. - -=item B<--name> - -Set the storage mount point (empty means 'check all storages') - -=item B<--regexp> - -Allows to use regexp to filter storage mount point (with option --name). - -=item B<--regexp-isensitive> - -Allows to use regexp non case-sensitive (with --regexp). - =item B<--filter-fs> Filter filesystem (regexp can be used). +=item B<--filter-mount> + +Filter mountpoint (regexp can be used). + =item B<--space-reservation> Some filesystem has space reserved (like ext4 for root). The value is in percent of total (Default: none). +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%). + =back =cut diff --git a/centreon-plugins/os/aix/local/plugin.pm b/centreon-plugins/os/aix/local/plugin.pm index 99c04c002..e259bf00c 100644 --- a/centreon-plugins/os/aix/local/plugin.pm +++ b/centreon-plugins/os/aix/local/plugin.pm @@ -22,7 +22,7 @@ package os::aix::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,15 +30,17 @@ sub new { bless $self, $class; $self->{version} = '0.1'; - %{$self->{modes}} = ( + $self->{modes} = { 'cmd-return' => 'os::aix::local::mode::cmdreturn', 'errpt' => 'os::aix::local::mode::errpt', 'inodes' => 'os::aix::local::mode::inodes', 'list-storages' => 'os::aix::local::mode::liststorages', 'lvsync' => 'os::aix::local::mode::lvsync', 'process' => 'os::aix::local::mode::process', - 'storage' => 'os::aix::local::mode::storage', - ); + 'storage' => 'os::aix::local::mode::storage' + }; + + $self->{custom_modes}->{cli} = 'os::aix::local::custom::cli'; return $self; }