This commit is contained in:
qgarnier 2021-02-19 14:51:59 +01:00 committed by GitHub
parent add91961dd
commit e1876cb7b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 161 additions and 71 deletions

View File

@ -20,38 +20,143 @@
package os::linux::local::mode::memory;
use base qw(centreon::plugins::mode);
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
sub custom_memory_output {
my ($self, %options) = @_;
return sprintf(
'Ram total: %s %s used (-%s): %s %s (%.2f%%) free: %s %s (%.2f%%)',
$self->{perfdata}->change_bytes(value => $self->{result_values}->{total}),
$self->{result_values}->{used_desc},
$self->{perfdata}->change_bytes(value => $self->{result_values}->{used}),
$self->{result_values}->{prct_used},
$self->{perfdata}->change_bytes(value => $self->{result_values}->{free}),
$self->{result_values}->{prct_free}
);
}
sub custom_swap_output {
my ($self, %options) = @_;
return sprintf(
'Swap total: %s %s used: %s %s (%.2f%%) free: %s %s (%.2f%%)',
$self->{perfdata}->change_bytes(value => $self->{result_values}->{total}),
$self->{perfdata}->change_bytes(value => $self->{result_values}->{used}),
$self->{result_values}->{prct_used},
$self->{perfdata}->change_bytes(value => $self->{result_values}->{free}),
$self->{result_values}->{prct_free}
);
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'memory', type => 0, skipped_code => { -10 => 1 } },
{ name => 'swap', type => 0, skipped_code => { -10 => 1 } }
];
$self->{maps_counters}->{memory} = [
{ label => 'memory-usage', nlabel => 'memory.usage.bytes', set => {
key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'used_desc' } ],
closure_custom_output => $self->can('custom_memory_output'),
perfdatas => [
{ template => '%d', min => 0, max => 'total', unit => 'B' }
]
}
},
{ label => 'memory-usage-free', nlabel => 'memory.free.bytes', display_ok => 0, set => {
key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'used_desc' } ],
closure_custom_output => $self->can('custom_memory_output'),
perfdatas => [
{ template => '%d', min => 0, max => 'total', unit => 'B' }
]
}
},
{ label => 'memory-usage-prct', nlabel => 'memory.usage.percentage', display_ok => 0, set => {
key_values => [ { name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'prct_free' }, { name => 'total' }, { name => 'used_desc' } ],
closure_custom_output => $self->can('custom_memory_output'),
perfdatas => [
{ template => '%.2f', min => 0, max => 100, unit => '%' }
]
}
},
{ label => 'buffer', nlabel => 'memory.buffer.bytes', set => {
key_values => [ { name => 'buffer' } ],
output_template => 'buffer: %s %s',
output_change_bytes => 1,
perfdatas => [
{ template => '%d', min => 0, unit => 'B' }
]
}
},
{ label => 'cached', nlabel => 'memory.cached.bytes', set => {
key_values => [ { name => 'cached' } ],
output_template => 'cached: %s %s',
output_change_bytes => 1,
perfdatas => [
{ template => '%d', min => 0, unit => 'B' }
]
}
},
{ label => 'slab', nlabel => 'memory.slab.bytes', set => {
key_values => [ { name => 'slab' } ],
output_template => 'slab: %s %s',
output_change_bytes => 1,
perfdatas => [
{ template => '%d', min => 0, unit => 'B' }
]
}
},
];
$self->{maps_counters}->{swap} = [
{ label => 'swap', 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'),
perfdatas => [
{ label => 'swap', template => '%d', min => 0, max => 'total', 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'),
perfdatas => [
{ label => 'swap_free', template => '%d', min => 0, max => 'total',
unit => 'B', cast_int => 1 }
]
}
},
{ label => 'swap-prct', display_ok => 0, nlabel => 'swap.usage.percentage', set => {
key_values => [ { name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'prct_free' }, { name => 'total' } ],
closure_custom_output => $self->can('custom_swap_output'),
perfdatas => [
{ label => 'swap_prct', template => '%.2f', min => 0, max => 100, unit => '%' }
]
}
}
];
}
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 => {
'warning:s' => { name => 'warning' },
'critical:s' => { name => 'critical' }
$options{options}->add_options(arguments => {
'swap' => { name => 'check_swap' },
'warning:s' => { name => 'warning', redirect => 'warning-memory-usage-percentage' },
'critical:s' => { name => 'critical', redirect => 'critical-memory-usage-percentage' }
});
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 check_rhel_version {
my ($self, %options) = @_;
@ -60,7 +165,7 @@ sub check_rhel_version {
$self->{rhel_71} = 1 if ($1 >= 8 || ($1 == 7 && $2 >= 1));
}
sub run {
sub manage_selection {
my ($self, %options) = @_;
my ($stdout) = $options{custom}->execute_command(
@ -71,7 +176,7 @@ sub run {
# Buffer can be missing. In Openvz container for example.
my $buffer_used = 0;
my ($cached_used, $free, $total_size, $slab_used);
my ($cached_used, $free, $total_size, $slab_used, $swap_total, $swap_free);
foreach (split(/\n/, $stdout)) {
if (/^MemTotal:\s+(\d+)/i) {
$total_size = $1 * 1024;
@ -83,6 +188,10 @@ sub run {
$slab_used = $1 * 1024;
} elsif (/^MemFree:\s+(\d+)/i) {
$free = $1 * 1024;
} elsif (/^SwapTotal:\s+(\d+)/i) {
$swap_total = $1 * 1024;
} elsif (/^SwapFree:\s+(\d+)/i) {
$swap_free = $1 * 1024;
}
}
@ -95,58 +204,36 @@ sub run {
my $physical_used = $total_size - $free;
my $nobuf_used = $physical_used - $buffer_used - $cached_used;
my ($slab_value, $slab_unit);
($slab_value, $slab_unit) = $self->{perfdata}->change_bytes(value => $slab_used) if (defined($slab_used));
if ($self->{rhel_71} == 1) {
$nobuf_used -= $slab_used;
$nobuf_used -= $slab_used if (defined($slab_used));
}
my $prct_used = $nobuf_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 $used_desc = 'buffers/cache';
$used_desc .= '/slab' if ($self->{rhel_71} == 1 && defined($slab_used));
my ($nobuf_value, $nobuf_unit) = $self->{perfdata}->change_bytes(value => $nobuf_used);
my ($buffer_value, $buffer_unit) = $self->{perfdata}->change_bytes(value => $buffer_used);
my ($cached_value, $cached_unit) = $self->{perfdata}->change_bytes(value => $cached_used);
$self->{memory} = {
total => $total_size,
used => $nobuf_used,
free => $total_size - $nobuf_used,
prct_used => $nobuf_used * 100 / $total_size,
prct_free => 100 - ($nobuf_used * 100 / $total_size),
used_desc => $used_desc,
$self->{output}->output_add(
severity => $exit,
short_msg => sprintf(
'Ram used (-buffers/cache%s) %s (%.2f%%), Buffer: %s, Cached: %s%s',
($self->{rhel_71} == 1 && defined($slab_used)) ? '/slab' : '',
$nobuf_value . " " . $nobuf_unit,
$prct_used,
$buffer_value . " " . $buffer_unit,
$cached_value . " " . $cached_unit,
(defined($slab_used)) ? ', Slab: ' . $slab_value . ' ' . $slab_unit : '',
)
);
buffer => $buffer_used,
cache => $cached_used,
slab => $slab_used
};
$self->{output}->perfdata_add(
label => 'cached', unit => 'B',
value => $cached_used,
min => 0
);
$self->{output}->perfdata_add(
label => 'buffer', unit => 'B',
value => $buffer_used,
min => 0
);
$self->{output}->perfdata_add(
label => 'used', unit => 'B',
value => $nobuf_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}->perfdata_add(
label => 'slab', unit => 'B',
value => $slab_used,
min => 0
) if (defined($slab_used));
$self->{output}->display();
$self->{output}->exit();
if (defined($self->{option_results}->{check_swap}) &&
defined($swap_total) && $swap_total > 0) {
$self->{swap} = {
total => $swap_total,
used => $swap_total - $swap_free,
free => $swap_free,
prct_used => 100 - ($swap_free * 100 / $swap_total),
prct_free => ($swap_free * 100 / $swap_total)
};
}
}
1;
@ -161,13 +248,16 @@ Command used: cat /proc/meminfo /etc/redhat-release 2>&1
=over 8
=item B<--warning>
=item B<--swap>
Threshold warning in percent.
Check swap also.
=item B<--critical>
=item B<--warning-*> B<--critical-*>
Threshold critical in percent.
Thresholds.
Can be: 'memory-usage' (B), 'memory-usage-free' (B), 'memory-usage-prct' (%),
'swap' (B), 'swap-free' (B), 'swap-prct' (%),
'buffer' (B), 'cached' (B), 'slab' (B).
=back