From dbd10f093d51f32140bfaf861b1ec6a2ffe1a238 Mon Sep 17 00:00:00 2001 From: thibaults-centreon Date: Wed, 23 Oct 2019 13:39:51 +0200 Subject: [PATCH] enh(plugin): refacto/migrate os/linux/local/mode/swap.pm --- os/linux/local/mode/swap.pm | 117 ++++++++++++++++++++++++------------ 1 file changed, 80 insertions(+), 37 deletions(-) diff --git a/os/linux/local/mode/swap.pm b/os/linux/local/mode/swap.pm index aed97d2ce..5ba9113f6 100644 --- a/os/linux/local/mode/swap.pm +++ b/os/linux/local/mode/swap.pm @@ -20,12 +20,68 @@ package os::linux::local::mode::swap; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; use centreon::plugins::misc; +sub custom_swap_output { + my ($self, %options) = @_; + + my $output = sprintf( + 'Swap Total: %s %s Used: %s %s (%.2f%%) Free: %s %s (%.2f%%)', + $self->{perfdata}->change_bytes(value => $self->{result_values}->{total_absolute}), + $self->{perfdata}->change_bytes(value => $self->{result_values}->{used_absolute}), + $self->{result_values}->{prct_used_absolute}, + $self->{perfdata}->change_bytes(value => $self->{result_values}->{free_absolute}), + $self->{result_values}->{prct_free_absolute}, + ); + return $output; +} + + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'swap', type => 0, skipped_code => { -10 => 1 } }, + ]; + + $self->{maps_counters}->{swap} = [ + { label => 'swap-used', nlabel => 'swap.usage.bytes', set => { + key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ], + closure_custom_output => $self->can('custom_swap_output'), + threshold_use => 'used_absolute', + perfdatas => [ + { label => 'swap_used', value => 'used_absolute', template => '%d', min => 0, max => 'total_absolute', unit => 'B', cast_int => 1 }, + ], + }, + }, + { label => 'swap-free', display_ok => 0, nlabel => 'swap.free.bytes', set => { + key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ], + closure_custom_output => $self->can('custom_swap_output'), + threshold_use => 'free_absolute', + }, + }, + { label => 'swap-prct-used', display_ok => 0, nlabel => 'swap.usage.percentage', set => { + key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ], + closure_custom_output => $self->can('custom_swap_output'), + threshold_use => 'prct_used_absolute', + perfdatas => [ + { label => 'swap_prct', value => 'prct_used_absolute', template => '%.2f', min => 0, max => 100, unit => '%' }, + ], + }, + }, + { label => 'swap-prct-free', display_ok => 0, nlabel => 'swap.free.percentage', set => { + key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ], + closure_custom_output => $self->can('custom_swap_output'), + threshold_use => 'prct_free_absolute', + }, + }, + ]; +} + sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); @@ -43,8 +99,6 @@ sub new { "command:s" => { name => 'command', default => 'cat' }, "command-path:s" => { name => 'command_path' }, "command-options:s" => { name => 'command_options', default => '/proc/meminfo 2>&1' }, - "warning:s" => { name => 'warning', }, - "critical:s" => { name => 'critical', }, "no-swap:s" => { name => 'no_swap' }, }); $self->{no_swap} = 'critical'; @@ -53,26 +107,20 @@ 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); + if (defined($self->{option_results}->{no_swap}) && $self->{option_results}->{no_swap} ne '') { if ($self->{output}->is_litteral_status(status => $self->{option_results}->{no_swap}) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong --no-swap status '" . $self->{option_results}->{no_swap} . "'."); - $self->{output}->option_exit(); + $self->{output}->add_option_msg(short_msg => "Wrong --no-swap status '" . $self->{option_results}->{no_swap} . "'."); + $self->{output}->option_exit(); } - $self->{no_swap} = $self->{option_results}->{no_swap}; + $self->{no_swap} = $self->{option_results}->{no_swap}; } + } -sub run { +sub manage_selection { my ($self, %options) = @_; my $stdout = centreon::plugins::misc::execute(output => $self->{output}, @@ -92,9 +140,10 @@ sub run { } if (!defined($total_size) || !defined($swap_free)) { - $self->{output}->add_option_msg(short_msg => "Some informations missing."); + $self->{output}->add_option_msg(short_msg => "Some information missing."); $self->{output}->option_exit(); } + if ($total_size == 0) { $self->{output}->output_add(severity => $self->{no_swap}, short_msg => 'No active swap.'); @@ -105,26 +154,20 @@ sub run { my $swap_used = $total_size - $swap_free; my $prct_used = $swap_used * 100 / $total_size; - my $exit = $self->{perfdata}->threshold_check(value => $prct_used, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); - + my $prct_free = 100 - $prct_used; my ($total_value, $total_unit) = $self->{perfdata}->change_bytes(value => $total_size); my ($swap_used_value, $swap_used_unit) = $self->{perfdata}->change_bytes(value => $swap_used); my ($swap_free_value, $swap_free_unit) = $self->{perfdata}->change_bytes(value => ($total_size - $swap_used)); - $self->{output}->output_add(severity => $exit, - short_msg => sprintf("Swap Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", - $total_value . " " . $total_unit, - $swap_used_value . " " . $swap_used_unit, $prct_used, - $swap_free_value . " " . $swap_free_unit, (100 - $prct_used))); - - $self->{output}->perfdata_add(label => "used", unit => 'B', - value => $swap_used, - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning', total => $total_size), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical', total => $total_size), - min => 0, max => $total_size); - - $self->{output}->display(); - $self->{output}->exit(); + + $self->{swap} = { + used => $swap_used, + free => $swap_free, + prct_used => $prct_used, + prct_free => $prct_free, + total => $total_size + }; + } 1; @@ -137,13 +180,13 @@ Check swap memory (need '/proc/meminfo' file). =over 8 -=item B<--warning> +=item B<--warning-swap-*> -Threshold warning in percent. +Warning threshold, can be used (in Bytes), free (in Bytes), prct-used (Percentage), prct-free (Percentage). -=item B<--critical> +=item B<--critical-swap-*> -Threshold critical in percent. +Critical threshold, , can be used (in Bytes), free (in Bytes), prct-used (Percentage), prct-free (Percentage). =item B<--no-swap>