diff --git a/centreon/plugins/values.pm b/centreon/plugins/values.pm index 473689ced..4ec4f1909 100644 --- a/centreon/plugins/values.pm +++ b/centreon/plugins/values.pm @@ -59,10 +59,12 @@ sub new { $self->{output_absolute_unit} = ''; $self->{output_per_second_unit} = ''; + $self->{output_error_template} = $self->{label} . ': %s'; + $self->{threshold_use} = undef; $self->{threshold_warn} = undef; $self->{threshold_crit} = undef; - + $self->{per_second} = 0; $self->{last_timestamp} = undef; @@ -76,12 +78,12 @@ sub init { my $warn = defined($self->{threshold_warn}) ? $self->{threshold_warn} : 'warning-' . $self->{label}; my $crit = defined($self->{threshold_crit}) ? $self->{threshold_crit} : 'critical-' . $self->{label}; - if (($self->{perfdata}->threshold_validate(label => $warn, value => $self->{option_results}->{$warn})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong $warn threshold '" . $self->{option_results}->{$warn} . "'."); + if (($self->{perfdata}->threshold_validate(label => $warn, value => $options{option_results}->{$warn})) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong $warn threshold '" . $options{option_results}->{$warn} . "'."); $self->{output}->option_exit(); } - if (($self->{perfdata}->threshold_validate(label => $crit, value => $self->{option_results}->{$crit})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong $crit threshold '" . $self->{option_results}->{$crit} . "'."); + if (($self->{perfdata}->threshold_validate(label => $crit, value => $options{option_results}->{$crit})) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong $crit threshold '" . $options{option_results}->{$crit} . "'."); $self->{output}->option_exit(); } } @@ -101,16 +103,16 @@ sub calc { foreach my $value (@{$self->{key_values}}) { if (defined($value->{diff}) && $value->{diff} == 1) { if ($self->{per_second} == 1) { - $self->{result_values}->{$value->{name} . '_per_second'} = $options{new_datas}->{$self->{instance} . '_' . $value->{name}} - $options{old_datas}->{$self->{instance} . '_' . $value->{name}}) / $options{delta_time}; + $self->{result_values}->{$value->{name} . '_per_second'} = ($options{new_datas}->{$self->{instance} . '_' . $value->{name}} - $options{old_datas}->{$self->{instance} . '_' . $value->{name}}) / $options{delta_time}; } - $self->{result_values}->{$value->{name} . '_absolute'} = $options{new_datas}->{$self->{instance} . '_' . $value->{name}} - $options{old_datas}->{$self->{instance} . '_' . $value->{name}}), unit => $value->{unit}; + $self->{result_values}->{$value->{name} . '_absolute'} = $options{new_datas}->{$self->{instance} . '_' . $value->{name}} - $options{old_datas}->{$self->{instance} . '_' . $value->{name}}; } else { # absolute one. nothing to do - $self->{result_values}->{$value->{name} . '_absolute'} = $options{new_datas}->{$self->{instance} . '_' . . $value->{name}}; + $self->{result_values}->{$value->{name} . '_absolute'} = $options{new_datas}->{$self->{instance} . '_' . $value->{name}}; } } - return 1; + return 0; } sub threshold_check { @@ -120,6 +122,9 @@ sub threshold_check { return &{$self->{closure_custom_threshold}}($self); } + my $warn = defined($self->{threshold_warn}) ? $self->{threshold_warn} : 'warning-' . $self->{label}; + my $crit = defined($self->{threshold_crit}) ? $self->{threshold_crit} : 'critical-' . $self->{label}; + my $first = ${${$self->{key_values}}[0]}{name}; my $value; @@ -132,15 +137,21 @@ sub threshold_check { $value = $self->{result_values}->{$self->{threshold_use}}; } - return $self->{perfdata}->threshold_check(value => $value, threshold => [ { label => 'warning-' . $self->{label}, 'exit_litteral' => 'critical' }, - { label => 'critical-' . $self->{label}, 'exit_litteral' => 'warning' }]); + return $self->{perfdata}->threshold_check(value => $value, threshold => [ { label => $crit, 'exit_litteral' => 'critical' }, + { label => $warn, 'exit_litteral' => 'warning' }]); +} + +sub output_error { + my ($self, %options) = @_; + + return sprintf($self->{output_error_template}, $self->{error_msg}); } sub output { my ($self, %options) = @_; if (defined($self->{closure_custom_output})) { - return &{$self->{closure_custom_output}}($self); + return $self->{closure_custom_output}->($self); } my $first = ${${$self->{key_values}}[0]}{name}; my ($value, $unit) = ($self->{result_values}->{$first . '_absolute'}, $self->{result_values}->{output_absolute_unit}); @@ -173,8 +184,9 @@ sub perfdata { $template = $perf->{template} if (defined($perf->{template})); $label = $perf->{label} if (defined($perf->{label})); - $extra_label .= '_' . $self->{instance} if ($perf->{label_extra_instance} == 1 && !defined($options{no_extra_instance})); - $self->{output}->perfdata_add(label => $perf->{label} . $extra_label, unit => $perf->{unit}, + $extra_label .= '_' . $self->{instance} if ($perf->{label_extra_instance} == 1 && + (!defined($options{extra_instance}) || $options{extra_instance} != 0)); + $self->{output}->perfdata_add(label => $label . $extra_label, unit => $perf->{unit}, value => sprintf($template, $self->{result_values}->{$perf->{value}}), warning => $self->{perfdata}->get_perfdata_for_output(label => $warn), critical => $self->{perfdata}->get_perfdata_for_output(label => $crit), @@ -185,14 +197,16 @@ sub perfdata { sub execute { my ($self, %options) = @_; my $old_datas = {}; - + + $self->{result_values} = {}, + $self->{error_msg} = undef; my $quit = 0; my $per_second = 0; $options{new_datas} = {} if (!defined($options{new_datas})); foreach my $value (@{$self->{key_values}}) { if (defined($value->{diff}) && $value->{diff} == 1) { - $options{new_datas}->{$self->{instance} . '_' . $value} = $options{values}->{$value->{name}}; + $options{new_datas}->{$self->{instance} . '_' . $value->{name}} = $options{values}->{$value->{name}}; $old_datas->{$self->{instance} . '_' . $value->{name}} = $self->{statefile}->get(name => $self->{instance} . '_' . $value->{name}); if (!defined($old_datas->{$self->{instance} . '_' . $value->{name}})) { $quit = 1; @@ -202,17 +216,23 @@ sub execute { $old_datas->{$self->{instance} . '_' . $value->{name}} = 0; } } else { - $options{new_datas}->{$self->{instance} . '_' . $value} = $options{values}->{$value->{name}}; + $options{new_datas}->{$self->{instance} . '_' . $value->{name}} = $options{values}->{$value->{name}}; } } - return undef if ($quit == 1); + if ($quit == 1) { + $self->{error_msg} = "Buffer creation"; + return -1; + } if ($self->{per_second} == 1) { if (!defined($self->{last_timestamp})) { $self->{last_timestamp} = $self->{statefile}->get(name => 'last_timestamp'); } - return undef if (!defined($self->{last_timestamp})); + if (!defined($self->{last_timestamp})) { + $self->{error_msg} = "Buffer creation"; + return -1; + } } my $delta_time; @@ -224,7 +244,7 @@ sub execute { } if (defined($self->{closure_custom_calc})) { - return &{$self->{closure_custom_cacl}}($self, old_datas => $old_datas, new_datas => $options{new_datas}, delta_time => $delta_time ); + return $self->{closure_custom_calc}->($self, old_datas => $old_datas, new_datas => $options{new_datas}, delta_time => $delta_time); } return $self->calc(old_datas => $old_datas, new_datas => $options{new_datas}, delta_time => $delta_time); } diff --git a/storage/hp/p2000/xmlapi/mode/volumesstats.pm b/storage/hp/p2000/xmlapi/mode/volumesstats.pm index 8dfc40805..d453667e1 100644 --- a/storage/hp/p2000/xmlapi/mode/volumesstats.pm +++ b/storage/hp/p2000/xmlapi/mode/volumesstats.pm @@ -49,7 +49,7 @@ my $maps_counters = { { name => 'data-read-numeric', diff => 1 }, ], per_second => 1, - output_template => 'Read I/O : %s %s/s', + output_template => 'Read I/O: %s %s/s', output_change_bytes => 1, perfdatas => [ { value => 'data-read-numeric_absolute', @@ -63,7 +63,7 @@ my $maps_counters = { { name => 'data-written-numeric', diff => 1 }, ], per_second => 1, - output_template => 'Write I/O : %s %s/s', + output_template => 'Write I/O: %s %s/s', output_change_bytes => 1, perfdatas => [ { value => 'data-written-numeric_absolute', @@ -71,12 +71,42 @@ my $maps_counters = { ], } }, + 'write-cache-hits' => { class => 'centreon::plugins::values', obj => undef, + set => { + key_values => [ + { name => 'write-cache-hits', diff => 1 }, + { name => 'write-cache-misses', diff => 1 }, + ], + closure_custom_calc => \&custom_write_cache_calc, + output_template => 'Write Cache Hits: %.2f %%', + output_use => 'write-cache-hits_prct', threshold_use => 'write-cache-hits_prct', + perfdatas => [ + { value => 'write-cache-hits_prct', template => '%.2f', + unit => '%', min => 0, max => 100, label_extra_instance => 1 }, + ], + } + }, + 'read-cache-hits' => { class => 'centreon::plugins::values', obj => undef, + set => { + key_values => [ + { name => 'read-cache-hits', diff => 1 }, + { name => 'read-cache-misses', diff => 1 }, + ], + closure_custom_calc => \&custom_read_cache_calc, + output_template => 'Read Cache Hits: %.2f %%', + output_use => 'read-cache-hits_prct', threshold_use => 'read-cache-hits_prct', + perfdatas => [ + { value => 'read-cache-hits_prct', template => '%.2f', + unit => '%', min => 0, max => 100, label_extra_instance => 1 }, + ], + } + }, iops => { class => 'centreon::plugins::values', obj => undef, set => { key_values => [ { name => 'iops' }, ], - output_template => 'IOPs : %s', + output_template => 'IOPs: %s', perfdatas => [ { value => 'iops_absolute', unit => 'iops', min => 0, label_extra_instance => 1 }, @@ -85,6 +115,36 @@ my $maps_counters = { }, }; +sub custom_write_cache_calc { + my ($self, %options) = @_; + my $diff_hits = ($options{new_datas}->{$self->{instance} . '_write-cache-hits'} - $options{old_datas}->{$self->{instance} . '_write-cache-hits'}); + my $total = $diff_hits + + ($options{new_datas}->{$self->{instance} . '_write-cache-misses'} - $options{old_datas}->{$self->{instance} . '_write-cache-misses'}); + + if ($total == 0) { + $self->{error_msg} = "skipped"; + return -2; + } + + $self->{result_values}->{'write-cache-hits_prct'} = $diff_hits * 100 / $total; + return 0; +} + +sub custom_read_cache_calc { + my ($self, %options) = @_; + my $diff_hits = ($options{new_datas}->{$self->{instance} . '_read-cache-hits'} - $options{old_datas}->{$self->{instance} . '_read-cache-hits'}); + my $total = $diff_hits + + ($options{new_datas}->{$self->{instance} . '_read-cache-misses'} - $options{old_datas}->{$self->{instance} . '_read-cache-misses'}); + + if ($total == 0) { + $self->{error_msg} = "skipped"; + return -2; + } + + $self->{result_values}->{'read-cache-hits_prct'} = $diff_hits * 100 / $total; + return 0; +} + sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); @@ -108,10 +168,9 @@ sub new { $maps_counters->{$_}->{obj} = $class->new(statefile => $self->{statefile_value}, output => $self->{output}, perfdata => $self->{perfdata}, label => $_); + $maps_counters->{$_}->{obj}->set(%{$maps_counters->{$_}->{set}}); } - - return $self; } @@ -120,12 +179,7 @@ sub check_options { $self->SUPER::init(%options); foreach (keys %{$maps_counters}) { - foreach my $name (keys %{$maps_counters->{$_}->{thresholds}}) { - if (($self->{perfdata}->threshold_validate(label => $maps_counters->{$_}->{thresholds}->{$name}->{label}, value => $self->{option_results}->{$name})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong " . $maps_counters->{$_}->{thresholds}->{$name}->{label} . " threshold '" . $self->{option_results}->{$name} . "'."); - $self->{output}->option_exit(); - } - } + $maps_counters->{$_}->{obj}->init(option_results => $self->{option_results}); } $self->{statefile_value}->check_options(%options); @@ -138,7 +192,7 @@ sub manage_selection { base_type => 'volume-statistics', key => 'volume-name', properties_name => '^data-read-numeric|data-written-numeric|write-cache-hits|write-cache-misses|read-cache-hits|read-cache-misses|iops$'); - foreach my $name (keys %{$self->{results}}) { + foreach my $name (sort keys %{$self->{results}}) { # Get all without a name if (!defined($self->{option_results}->{name})) { push @{$self->{volume_name_selected}}, $name; @@ -168,7 +222,12 @@ sub run { $self->{p2000}->login(); $self->manage_selection(); - if (!defined($self->{option_results}->{name}) || defined($self->{option_results}->{use_regexp})) { + my $multiple = 1; + if (scalar(@{$self->{volume_name_selected}}) == 1) { + $multiple = 0; + } + + if ($multiple == 1) { $self->{output}->output_add(severity => 'OK', short_msg => 'All volumes statistics are ok.'); } @@ -179,15 +238,16 @@ sub run { foreach my $name (sort @{$self->{volume_name_selected}}) { my ($short_msg, $long_msg) = ('', ''); my @exits; - foreach (keys %{$maps_counters}) { - $maps_counters->{$_}->{obj}->set(instance => $name, - %{$maps_counters->{$_}->{set}}, - ); + foreach (sort keys %{$maps_counters}) { + $maps_counters->{$_}->{obj}->set(instance => $name); my ($value_check) = $maps_counters->{$_}->{obj}->execute(values => $self->{results}->{$name}, new_datas => $self->{new_datas}); - next if (!defined($value_check)); + if ($value_check != 0) { + $long_msg .= ' ' . $maps_counters->{$_}->{obj}->output_error(); + next; + } my $exit2 = $maps_counters->{$_}->{obj}->threshold_check(); push @exits, $exit2; @@ -198,7 +258,7 @@ sub run { $short_msg .= ' ' . $output; } - $maps_counters->{$_}->{obj}->perfdata(); + $maps_counters->{$_}->{obj}->perfdata(extra_instance => $multiple); } $self->{output}->output_add(long_msg => "Volume '$name':$long_msg"); @@ -208,6 +268,10 @@ sub run { short_msg => "Volume '$name':$short_msg" ); } + + if ($multiple == 0) { + $self->{output}->output_add(short_msg => "Volume '$name':$long_msg"); + } } $self->{statefile_value}->write(data => $self->{new_datas}); @@ -228,12 +292,12 @@ Check volume statistics. =item B<--warning-*> Threshold warning. -Can be: 'read', 'write', . +Can be: 'read', 'write', 'iops', 'write-cache-hits', 'read-cache-hits'. =item B<--critical-*> Threshold critical. -Can be: 'read', 'write', . +Can be: 'read', 'write', 'iops', 'write-cache-hits', 'read-cache-hits'. =item B<--name>