centreon-plugins/snmp_standard/mode/memory.pm

299 lines
11 KiB
Perl
Raw Normal View History

2013-12-13 16:14:12 +01:00
#
2021-02-08 09:55:50 +01:00
# Copyright 2021 Centreon (http://www.centreon.com/)
2015-07-21 11:51:02 +02:00
#
# 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.
#
2013-12-13 16:14:12 +01:00
package snmp_standard::mode::memory;
2013-12-13 16:14:12 +01:00
2019-03-19 15:52:23 +01:00
use base qw(centreon::plugins::templates::counter);
2013-12-13 16:14:12 +01:00
use strict;
use warnings;
2019-03-19 15:52:23 +01:00
sub custom_usage_output {
2013-12-13 16:14:12 +01:00
my ($self, %options) = @_;
2019-03-19 15:52:23 +01:00
2020-04-02 09:52:25 +02:00
return sprintf(
'Ram Total: %s %s Used (-buffers/cache): %s %s (%.2f%%) Free: %s %s (%.2f%%)',
2020-05-13 14:18:28 +02:00
$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}
2020-04-02 09:52:25 +02:00
);
2019-03-19 15:52:23 +01:00
}
sub custom_swap_output {
my ($self, %options) = @_;
2013-12-13 16:14:12 +01:00
2020-04-02 09:52:25 +02:00
return sprintf(
'Swap Total: %s %s Used: %s %s (%.2f%%) Free: %s %s (%.2f%%)',
2020-05-13 14:18:28 +02:00
$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}
2019-11-14 10:36:54 +01:00
);
2019-03-19 15:52:23 +01:00
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
2019-06-13 14:26:36 +02:00
{ name => 'ram', type => 0, skipped_code => { -10 => 1 } },
2020-06-16 14:21:40 +02:00
{ name => 'swap', type => 0, message_separator => ' - ', skipped_code => { -10 => 1 } }
2019-03-19 15:52:23 +01:00
];
2020-04-02 09:52:25 +02:00
2019-03-19 15:52:23 +01:00
$self->{maps_counters}->{ram} = [
{ label => 'usage', nlabel => 'memory.usage.bytes', set => {
key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
2019-03-19 15:52:23 +01:00
closure_custom_output => $self->can('custom_usage_output'),
perfdatas => [
2020-06-16 14:21:40 +02:00
{ label => 'used', template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1 }
2020-04-02 09:52:25 +02:00
]
}
},
{ label => 'usage-free', display_ok => 0, nlabel => 'memory.free.bytes', set => {
key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
closure_custom_output => $self->can('custom_usage_output'),
perfdatas => [
2020-06-16 14:21:40 +02:00
{ label => 'free', template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1 }
2020-04-02 09:52:25 +02:00
]
}
},
{ label => 'usage-prct', display_ok => 0, nlabel => 'memory.usage.percentage', set => {
2020-10-06 09:05:50 +02:00
key_values => [ { name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'prct_free' }, { name => 'total' } ],
closure_custom_output => $self->can('custom_usage_output'),
perfdatas => [
2020-06-16 14:21:40 +02:00
{ label => 'used_prct', template => '%.2f', min => 0, max => 100, unit => '%' }
2020-04-02 09:52:25 +02:00
]
2019-03-19 15:52:23 +01:00
}
},
{ label => 'buffer', nlabel => 'memory.buffer.bytes', set => {
2019-03-19 15:52:23 +01:00
key_values => [ { name => 'memBuffer' } ],
output_template => 'Buffer: %s %s',
output_change_bytes => 1,
perfdatas => [
2020-06-16 14:21:40 +02:00
{ label => 'buffer', template => '%d', min => 0, unit => 'B' }
2020-04-02 09:52:25 +02:00
]
2019-03-19 15:52:23 +01:00
}
},
{ label => 'cached', nlabel => 'memory.cached.bytes', set => {
2019-03-19 15:52:23 +01:00
key_values => [ { name => 'memCached' } ],
output_template => 'Cached: %s %s',
output_change_bytes => 1,
perfdatas => [
2020-06-16 14:21:40 +02:00
{ label => 'cached', template => '%d', min => 0, unit => 'B' }
2020-04-02 09:52:25 +02:00
]
2019-03-19 15:52:23 +01:00
}
},
{ label => 'shared', nlabel => 'memory.shared.bytes', set => {
2019-03-19 15:52:23 +01:00
key_values => [ { name => 'memShared' } ],
output_template => 'Shared: %s %s',
output_change_bytes => 1,
perfdatas => [
2020-06-16 14:21:40 +02:00
{ label => 'shared', template => '%d', min => 0, unit => 'B' }
2020-04-02 09:52:25 +02:00
]
2019-03-19 15:52:23 +01:00
}
2020-06-16 14:21:40 +02:00
}
2019-03-19 15:52:23 +01:00
];
$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 => [
2020-06-16 14:21:40 +02:00
{ label => 'swap', template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1 }
2020-04-02 09:52:25 +02:00
]
}
},
{ 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' } ],
2019-03-19 15:52:23 +01:00
closure_custom_output => $self->can('custom_swap_output'),
perfdatas => [
2020-06-16 14:21:40 +02:00
{ label => 'swap_free', template => '%d', min => 0, max => 'total',
2020-04-02 09:52:25 +02:00
unit => 'B', cast_int => 1 }
]
}
},
{ label => 'swap-prct', display_ok => 0, nlabel => 'swap.usage.percentage', set => {
key_values => [ { name => 'prct_used' } ],
2019-10-29 13:49:37 +01:00
output_template => 'Swap Used : %.2f %%',
perfdatas => [
2020-06-16 14:21:40 +02:00
{ label => 'swap_prct', template => '%.2f', min => 0, max => 100, unit => '%' }
2020-04-02 09:52:25 +02:00
]
2019-03-19 15:52:23 +01:00
}
2020-04-02 09:52:25 +02:00
}
2019-03-19 15:52:23 +01:00
];
}
2013-12-13 16:14:12 +01:00
2019-03-19 15:52:23 +01:00
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
2020-04-02 09:52:25 +02:00
2019-03-19 15:52:23 +01:00
$options{options}->add_options(arguments => {
2020-10-06 09:23:48 +02:00
'units:s' => { name => 'units', default => '%' },
2020-08-06 14:13:27 +02:00
'free' => { name => 'free' },
'swap' => { name => 'check_swap' },
'patch-redhat' => { name => 'patch_redhat' },
'redhat' => { name => 'redhat' }, # for legacy (do nothing)
'autodetect-redhat' => { name => 'autodetect_redhat' } # for legacy (do nothing)
2019-03-19 15:52:23 +01:00
});
2020-04-02 09:52:25 +02:00
2019-03-19 15:52:23 +01:00
return $self;
}
2014-04-14 15:35:11 +02:00
sub check_options {
my ($self, %options) = @_;
# Compatibility
2019-08-27 13:26:32 +02:00
$self->compat_threshold_counter(%options,
compat => {
th => [ ['usage', { free => 'usage-free', prct => 'usage-prct'} ], [ 'memory-usage-bytes', { free => 'memory-free-bytes', prct => 'memory-usage-percentage' } ] ],
units => $options{option_results}->{units}, free => $options{option_results}->{free}
}
);
$self->compat_threshold_counter(%options,
compat => {
th => [ ['swap', { free => 'swap-free', prct => 'swap-prct' } ], [ 'swap-usage-bytes', { free => 'swap-free-bytes', prct => 'swap-usage-percentage' } ] ],
units => $options{option_results}->{units}, free => $options{option_results}->{free}
}
);
$self->SUPER::check_options(%options);
}
2019-03-19 15:52:23 +01:00
my $mapping = {
memTotalSwap => { oid => '.1.3.6.1.4.1.2021.4.3' },
memAvailSwap => { oid => '.1.3.6.1.4.1.2021.4.4' },
memTotalReal => { oid => '.1.3.6.1.4.1.2021.4.5' },
memAvailReal => { oid => '.1.3.6.1.4.1.2021.4.6' },
memTotalFree => { oid => '.1.3.6.1.4.1.2021.4.11' },
2019-09-20 11:04:02 +02:00
memShared => { oid => '.1.3.6.1.4.1.2021.4.13' },
memBuffer => { oid => '.1.3.6.1.4.1.2021.4.14' },
2020-06-16 14:21:40 +02:00
memCached => { oid => '.1.3.6.1.4.1.2021.4.15' }
2019-03-19 15:52:23 +01:00
};
sub memory_calc {
2019-03-19 15:52:23 +01:00
my ($self, %options) = @_;
my $available = ($options{result}->{memAvailReal}) ? $options{result}->{memAvailReal} * 1024 : 0;
my $total = ($options{result}->{memTotalReal}) ? $options{result}->{memTotalReal} * 1024 : 0;
my $buffer = ($options{result}->{memBuffer}) ? $options{result}->{memBuffer} * 1024 : 0;
my $cached = ($options{result}->{memCached}) ? $options{result}->{memCached} * 1024 : 0;
my ($used, $free, $prct_used, $prct_free) = (0, 0, 0, 0);
2020-08-06 14:13:27 +02:00
# rhel patch introduced: net-snmp-5.7.2-43.el7 (https://bugzilla.redhat.com/show_bug.cgi?id=1250060)
# rhel patch reverted: net-snmp-5.7.2-47.el7 (https://bugzilla.redhat.com/show_bug.cgi?id=1779609)
if ($total != 0) {
2020-08-06 14:13:27 +02:00
$used = (defined($self->{option_results}->{patch_redhat})) ? $total - $available : $total - $available - $buffer - $cached;
$free = (defined($self->{option_results}->{patch_redhat})) ? $available : $total - $used;
$prct_used = $used * 100 / $total;
$prct_free = 100 - $prct_used;
}
2020-06-16 14:21:40 +02:00
2019-03-19 15:52:23 +01:00
$self->{ram} = {
total => $total,
used => $used,
free => $free,
prct_used => $prct_used,
prct_free => $prct_free,
memShared => ($options{result}->{memShared}) ? $options{result}->{memShared} * 1024 : 0,
memBuffer => $buffer,
2020-06-16 14:21:40 +02:00
memCached => $cached
};
}
sub swap_calc {
my ($self, %options) = @_;
my $free = ($options{result}->{memAvailSwap}) ? $options{result}->{memAvailSwap} * 1024 : 0;
my $total = ($options{result}->{memTotalSwap}) ? $options{result}->{memTotalSwap} * 1024 : 0;
my ($used, $prct_used, $prct_free) = (0, 0, 0, 0);
if ($total != 0) {
$used = $total - $free;
$prct_used = $used * 100 / $total;
$prct_free = 100 - $prct_used;
}
$self->{swap} = {
total => $total,
used => $used,
free => $free,
prct_used => $prct_used,
2020-06-16 14:21:40 +02:00
prct_free => $prct_free
2019-03-19 15:52:23 +01:00
};
}
sub manage_selection {
my ($self, %options) = @_;
2019-09-20 11:04:02 +02:00
my $results = $options{snmp}->get_leef(
2020-06-16 14:21:40 +02:00
oids => [ map($_->{oid} . '.0', values(%$mapping)) ],
nothing_quit => 1
2019-09-20 11:04:02 +02:00
);
my $result = $options{snmp}->map_instance(mapping => $mapping, results => $results, instance => 0);
$self->memory_calc(result => $result);
2019-03-19 15:52:23 +01:00
if (defined($self->{option_results}->{check_swap})) {
$self->swap_calc(result => $result);
2014-04-14 15:35:11 +02:00
}
2013-12-13 16:14:12 +01:00
}
1;
__END__
=head1 MODE
2019-03-19 15:52:23 +01:00
Check memory usage (UCD-SNMP-MIB).
2013-12-13 16:14:12 +01:00
=over 8
2019-03-19 15:52:23 +01:00
=item B<--units>
2013-12-13 16:14:12 +01:00
Units of thresholds (Default: '%') ('%', 'absolute') (Deprecated. Please use new counters directly)
2013-12-13 16:14:12 +01:00
2019-03-19 15:52:23 +01:00
=item B<--free>
2013-12-13 16:14:12 +01:00
Thresholds are on free space left (Deprecated. Please use new counters directly)
2013-12-13 16:14:12 +01:00
2014-09-30 16:42:03 +02:00
=item B<--swap>
2014-04-14 15:35:11 +02:00
Check swap also.
=item B<--warning-*> B<--critical-*>
2014-06-23 15:05:42 +02:00
Thresholds.
Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%),
'swap' (B), 'swap-free' (B), 'swap-prct' (%),
'buffer' (B), 'cached' (B), 'shared' (B).
2014-06-23 15:05:42 +02:00
2020-08-06 14:13:27 +02:00
=item B<--patch-redhat>
If using RedHat distribution with net-snmp >= 5.7.2-43 and net-snmp < 5.7.2-47. But you should update net-snmp!!!!
This version: used = memTotalReal - memAvailReal // free = memAvailReal
Others versions: used = memTotalReal - memAvailReal - memBuffer - memCached // free = total - used
2013-12-13 16:14:12 +01:00
=back
=cut