From d036c373f07d1075a0d856dc307ea608627575e5 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Wed, 15 Mar 2017 11:02:24 +0100 Subject: [PATCH] + Fix #191 --- os/linux/local/mode/inodes.pm | 115 +++++++---------- os/linux/local/mode/storage.pm | 230 +++++++++++++++++---------------- 2 files changed, 167 insertions(+), 178 deletions(-) diff --git a/os/linux/local/mode/inodes.pm b/os/linux/local/mode/inodes.pm index 042929bc4..21a905365 100644 --- a/os/linux/local/mode/inodes.pm +++ b/os/linux/local/mode/inodes.pm @@ -20,12 +20,38 @@ package os::linux::local::mode::inodes; -use base qw(centreon::plugins::mode); +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 %%', + perfdatas => [ + { label => 'used', value => 'used_absolute', template => '%d', + unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display_absolute' }, + ], + } + }, + ]; +} + +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); @@ -46,8 +72,6 @@ sub new { "command-options:s" => { name => 'command_options', default => '-P -i -T 2>&1' }, "filter-type:s" => { name => 'filter_type', }, "filter-fs:s" => { name => 'filter_fs', }, - "warning:s" => { name => 'warning' }, - "critical:s" => { name => 'critical' }, "name:s" => { name => 'name' }, "regexp" => { name => 'use_regexp' }, "regexp-isensitive" => { name => 'use_regexpi' }, @@ -56,33 +80,22 @@ sub new { return $self; } -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(); - } -} - 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, $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 + ); + $self->{inodes} = {}; 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+(\S+)\s+(.*)/); + next if ($line !~ /^(\S+)\s+(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(.*)/); my ($fs, $type, $size, $used, $available, $percent, $mount) = ($1, $2, $3, $4, $5, $6, $7); next if (defined($self->{option_results}->{filter_fs}) && $self->{option_results}->{filter_fs} ne '' && @@ -98,56 +111,18 @@ sub manage_selection { && $mount ne $self->{option_results}->{name}); $percent =~ s/%//g; - $self->{result}->{$mount} = {fs => $fs, type => $type, total => $size, percent => $percent}; + $self->{inodes}->{$mount} = { display => $mount, fs => $fs, type => $type, total => $size, used => $percent }; } - 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."); + 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(); } } -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 inode partitions are ok.'); - } - - foreach my $name (sort(keys %{$self->{result}})) { - my $prct_used = $self->{result}->{$name}->{percent}; - my $prct_free = 100 - $prct_used; - - my $exit = $self->{perfdata}->threshold_check(value => $prct_used, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); - - $self->{output}->output_add(long_msg => sprintf("Inodes partition '%s' Used: %s %% Free: %s %%", - $name, $prct_used, $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("Inodes partition '%s' Used: %s %% Free: %s %%", - $name, $prct_used, $prct_free)); - } - - my $label = 'used'; - my $extra_label = ''; - $extra_label = '_' . $name if (!defined($self->{option_results}->{name}) || defined($self->{option_results}->{use_regexp})); - $self->{output}->perfdata_add(label => $label . $extra_label, unit => '%', - value => $prct_used, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), - min => 0, max => 100); - } - - $self->{output}->display(); - $self->{output}->exit(); -} - 1; __END__ @@ -199,11 +174,11 @@ Command path (Default: none). Command options (Default: '-P -i -T 2>&1'). -=item B<--warning> +=item B<--warning-usage> Threshold warning in percent. -=item B<--critical> +=item B<--critical-usage> Threshold critical in percent. diff --git a/os/linux/local/mode/storage.pm b/os/linux/local/mode/storage.pm index e7d2b2b5c..fefff5d98 100644 --- a/os/linux/local/mode/storage.pm +++ b/os/linux/local/mode/storage.pm @@ -20,12 +20,107 @@ package os::linux::local::mode::storage; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; use centreon::plugins::misc; +my $instance_mode; + +sub custom_usage_perfdata { + my ($self, %options) = @_; + + my $label = 'used'; + my $value_perf = $self->{result_values}->{used}; + if (defined($instance_mode->{option_results}->{free})) { + $label = 'free'; + $value_perf = $self->{result_values}->{free}; + } + my $extra_label = ''; + $extra_label = '_' . $self->{result_values}->{display} if (!defined($options{extra_instance}) || $options{extra_instance} != 0); + my %total_options = (); + if ($instance_mode->{option_results}->{units} eq '%') { + $total_options{total} = $self->{result_values}->{total}; + $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-' . $self->{label}, %total_options), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{label}, %total_options), + min => 0, max => $self->{result_values}->{total}); +} + +sub custom_usage_threshold { + my ($self, %options) = @_; + + my ($exit, $threshold_value); + $threshold_value = $self->{result_values}->{used}; + $threshold_value = $self->{result_values}->{free} if (defined($instance_mode->{option_results}->{free})); + if ($instance_mode->{option_results}->{units} eq '%') { + $threshold_value = $self->{result_values}->{prct_used}; + $threshold_value = $self->{result_values}->{prct_free} if (defined($instance_mode->{option_results}->{free})); + } + $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{label}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{label}, exit_litteral => 'warning' } ]); + return $exit; +} + +sub custom_usage_output { + my ($self, %options) = @_; + + 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; +} + +sub custom_usage_calc { + my ($self, %options) = @_; + + if ($options{new_datas}->{$self->{instance} . '_total'} == 0) { + $self->{error_msg} = "total size is 0"; + return -2; + } + $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; + $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'}; + $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'}; + $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used}; + $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total}; + $self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used}; + + return 0; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'disks', type => 1, cb_prefix_output => 'prefix_disks_output', message_multiple => 'All storages are ok' } + ]; + + $self->{maps_counters}->{disks} = [ + { label => 'usage', set => { + key_values => [ { name => 'display' }, { name => 'used' }, { name => 'total' } ], + closure_custom_calc => $self->can('custom_usage_calc'), + closure_custom_output => $self->can('custom_usage_output'), + closure_custom_perfdata => $self->can('custom_usage_perfdata'), + closure_custom_threshold_check => $self->can('custom_usage_threshold'), + } + }, + ]; +} + +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); @@ -46,8 +141,6 @@ sub new { "command-options:s" => { name => 'command_options', default => '-P -k -T 2>&1' }, "filter-type:s" => { name => 'filter_type', }, "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' }, @@ -61,31 +154,27 @@ sub new { 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(); - } + $self->SUPER::check_options(%options); + + $instance_mode = $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, $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 + ); + $self->{disks} = {}; 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+(\S+)\s+(.*)/); + next if ($line !~ /^(\S+)\s+(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(.*)/); my ($fs, $type, $size, $used, $available, $percent, $mount) = ($1, $2, $3, $4, $5, $6, $7); next if (defined($self->{option_results}->{filter_fs}) && $self->{option_results}->{filter_fs} ne '' && @@ -100,95 +189,20 @@ sub manage_selection { 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}); - $self->{result}->{$mount} = {fs => $fs, type => $type, 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 *= 1024; if (defined($self->{option_results}->{space_reservation})) { - $total_size = $total_size - ($self->{option_results}->{space_reservation} * $total_size / 100); + $size = int($size - ($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 $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); - } - - if ($num_disk_check == 0) { - $self->{output}->add_option_msg(short_msg => "No storage checked."); - $self->{output}->option_exit(); + $self->{disks}->{$mount} = { display => $mount, fs => $fs, type => $type, total => $size, used => $used * 1024 }; } - $self->{output}->display(); - $self->{output}->exit(); + if (scalar(keys %{$self->{disks}}) <= 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(); + } } 1; @@ -242,11 +256,11 @@ Command path (Default: none). Command options (Default: '-P -k -T 2>&1'). -=item B<--warning> +=item B<--warning-usage> Threshold warning. -=item B<--critical> +=item B<--critical-usage> Threshold critical.