break change: counter template for memory standard

This commit is contained in:
garnier-quentin 2019-06-13 14:09:07 +02:00
parent 4340022c1b
commit 8df62a296e
1 changed files with 151 additions and 176 deletions

View File

@ -25,150 +25,30 @@ use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
sub custom_usage_perfdata {
my ($self, %options) = @_;
my %total_options = ();
if ($self->{instance_mode}->{option_results}->{units} eq '%') {
$total_options{total} = $self->{result_values}->{total};
$total_options{cast_int} = 1;
}
if (defined($self->{instance_mode}->{option_results}->{free})) {
$self->{output}->perfdata_add(
label => 'free',
unit => 'B',
value => $self->{result_values}->{free},
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}
);
} else {
$self->{output}->perfdata_add(
label => 'used',
unit => 'B',
value => $self->{result_values}->{used},
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($self->{instance_mode}->{option_results}->{free}));
if ($self->{instance_mode}->{option_results}->{units} eq '%') {
$threshold_value = $self->{result_values}->{prct_used};
$threshold_value = $self->{result_values}->{prct_free} if (defined($self->{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 $msg = sprintf('Ram Total: %s %s Used (-buffers/cache): %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});
my $msg = sprintf("Ram Total: %s %s Used (-buffers/cache): %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 $msg;
}
sub custom_usage_calc {
my ($self, %options) = @_;
$self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_memTotalReal'};
$self->{result_values}->{available} = $options{new_datas}->{$self->{instance} . '_memAvailReal'};
$self->{result_values}->{buffer} = $options{new_datas}->{$self->{instance} . '_memBuffer'};
$self->{result_values}->{cached} = $options{new_datas}->{$self->{instance} . '_memCached'};
if ($self->{result_values}->{total} != 0) {
$self->{result_values}->{physical_used} = $self->{result_values}->{total} - $self->{result_values}->{available};
$self->{result_values}->{used} = $self->{result_values}->{physical_used} - $self->{result_values}->{buffer} - $self->{result_values}->{cached};
$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};
} else {
$self->{result_values}->{used} = 0;
$self->{result_values}->{free} = 0;
$self->{result_values}->{prct_used} = 0;
}
return 0;
}
sub custom_swap_perfdata {
my ($self, %options) = @_;
my %total_options = ();
if ($self->{instance_mode}->{option_results}->{units} eq '%') {
$total_options{total} = $self->{result_values}->{total};
$total_options{cast_int} = 1;
}
$self->{output}->perfdata_add(
label => 'swap',
unit => 'B',
value => $self->{result_values}->{used},
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_swap_threshold {
my ($self, %options) = @_;
my ($exit, $threshold_value);
$threshold_value = $self->{result_values}->{used};
$threshold_value = $self->{result_values}->{available} if (defined($self->{instance_mode}->{option_results}->{free}));
if ($self->{instance_mode}->{option_results}->{units} eq '%') {
$threshold_value = $self->{result_values}->{prct_used};
$threshold_value = $self->{result_values}->{prct_available} if (defined($self->{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_swap_output {
my ($self, %options) = @_;
my $msg = 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}->{available}),
$self->{result_values}->{prct_available});
$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 $msg;
}
sub custom_swap_calc {
my ($self, %options) = @_;
$self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_memTotalSwap'};
$self->{result_values}->{available} = $options{new_datas}->{$self->{instance} . '_memAvailSwap'};
if ($self->{result_values}->{total} != 0) {
$self->{result_values}->{used} = $self->{result_values}->{total} - $self->{result_values}->{available};
$self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
$self->{result_values}->{prct_available} = 100 - $self->{result_values}->{prct_used};
} else {
$self->{result_values}->{used} = 0;
$self->{result_values}->{prct_used} = 0;
$self->{result_values}->{prct_available} = 0;
}
return 0;
}
sub set_counters {
my ($self, %options) = @_;
@ -178,16 +58,34 @@ sub set_counters {
];
$self->{maps_counters}->{ram} = [
{ label => 'usage', set => {
key_values => [ { name => 'memTotalReal' }, { name => 'memAvailReal' }, { name => 'memTotalFree' },
{ name => 'memBuffer' }, { name => 'memCached' } ],
closure_custom_calc => $self->can('custom_usage_calc'),
{ label => 'usage', nlabel => 'memory.usage.bytes', set => {
key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
closure_custom_output => $self->can('custom_usage_output'),
closure_custom_threshold_check => $self->can('custom_usage_threshold'),
closure_custom_perfdata => $self->can('custom_usage_perfdata')
perfdatas => [
{ label => 'used', value => 'used_absolute', template => '%d', min => 0, max => 'total_absolute',
unit => 'B', cast_int => 1 },
],
}
},
{ label => 'buffer', set => {
{ 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 => [
{ label => 'free', value => 'free_absolute', template => '%d', min => 0, max => 'total_absolute',
unit => 'B', cast_int => 1 },
],
}
},
{ label => 'usage-prct', display_ok => 0, nlabel => 'memory.usage.percentage', set => {
key_values => [ { name => 'prct_used' } ],
output_template => 'Used : %.2f %%',
perfdatas => [
{ label => 'used_prct', value => 'prct_used_absolute', template => '%d', min => 0, max => 0,
unit => '%' },
],
}
},
{ label => 'buffer', nlabel => 'memory.buffer.bytes', set => {
key_values => [ { name => 'memBuffer' } ],
output_template => 'Buffer: %s %s',
output_change_bytes => 1,
@ -197,7 +95,7 @@ sub set_counters {
],
}
},
{ label => 'cached', set => {
{ label => 'cached', nlabel => 'memory.cached.bytes', set => {
key_values => [ { name => 'memCached' } ],
output_template => 'Cached: %s %s',
output_change_bytes => 1,
@ -207,7 +105,7 @@ sub set_counters {
],
}
},
{ label => 'shared', set => {
{ label => 'shared', nlabel => 'memory.shared.bytes', set => {
key_values => [ { name => 'memShared' } ],
output_template => 'Shared: %s %s',
output_change_bytes => 1,
@ -219,12 +117,31 @@ sub set_counters {
},
];
$self->{maps_counters}->{swap} = [
{ label => 'swap', set => {
key_values => [ { name => 'memTotalSwap' }, { name => 'memAvailSwap' } ],
closure_custom_calc => $self->can('custom_swap_calc'),
{ 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'),
closure_custom_threshold_check => $self->can('custom_swap_threshold'),
closure_custom_perfdata => $self->can('custom_swap_perfdata')
perfdatas => [
{ label => 'swap', 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'),
perfdatas => [
{ label => 'swap_free', value => 'free_absolute', template => '%d', min => 0, max => 'total_absolute',
unit => 'B', cast_int => 1 },
],
}
},
{ label => 'swap-prct', display_ok => 0, nlabel => 'swap.usage.percentage', set => {
key_values => [ { name => 'prct_used' } ],
output_template => 'Used : %.2f %%',
perfdatas => [
{ label => 'swap_prct', value => 'prct_used_absolute', template => '%d', min => 0, max => 0,
unit => '%' },
],
}
},
];
@ -237,15 +154,38 @@ sub new {
$self->{version} = '1.0';
$options{options}->add_options(arguments => {
"units:s" => { name => 'units', default => '%' },
"free" => { name => 'free' },
"swap" => { name => 'check_swap' },
"no-swap:s" => { name => 'no_swap' }, # legacy
'units:s' => { name => 'units', default => '%' },
'free' => { name => 'free' },
'swap' => { name => 'check_swap' },
});
return $self;
}
sub compat_threshold_counter {
my ($self, %options) = @_;
foreach ('warning', 'critical') {
next if (!defined($options{option_results}->{$_ . '-' . $options{compat}->{th}}) || $options{option_results}->{$_ . '-' . $options{compat}->{th}} eq '');
if (defined($options{compat}->{free})) {
$options{option_results}->{$_ . '-' . $options{compat}->{th} . '-free'} = $options{option_results}->{$_ . '-' . $options{compat}->{th}};
$options{option_results}->{$_ . '-' . $options{compat}->{th}} = undef;
} elsif (defined($options{compat}->{units}) && $options{compat}->{units} eq '%') {
$options{option_results}->{$_ . '-' . $options{compat}->{th} . '-prct'} = $options{option_results}->{$_ . '-' . $options{compat}->{th}};
$options{option_results}->{$_ . '-' . $options{compat}->{th}} = undef;
}
}
}
sub check_options {
my ($self, %options) = @_;
# Compatibility
$self->compat_threshold_counter(%options, compat => { th => 'usage', units => $options{option_results}->{units}, free => $options{option_results}->{free}});
$self->compat_threshold_counter(%options, compat => { th => 'swap', units => $options{option_results}->{units}, free => $options{option_results}->{free}});
$self->SUPER::check_options(%options);
}
my $mapping = {
memTotalSwap => { oid => '.1.3.6.1.4.1.2021.4.3' },
memAvailSwap => { oid => '.1.3.6.1.4.1.2021.4.4' },
@ -259,27 +199,67 @@ my $mapping = {
my $oid_memory = '.1.3.6.1.4.1.2021.4';
sub memory_calc {
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);
if ($total != 0) {
$used = $total - $available - $buffer - $cached;
$free = $total - $used;
$prct_used = $used * 100 / $total;
$prct_free = 100 - $prct_used;
}
$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,
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,
prct_free => $prct_free,
};
}
sub manage_selection {
my ($self, %options) = @_;
my $results = $options{snmp}->get_table(oid => $oid_memory);
my $results = $options{snmp}->get_table(oid => $oid_memory, end => $mapping->{memCached}->{oid});
my $result = $options{snmp}->map_instance(mapping => $mapping, results => $results, instance => 0);
$self->memory_calc(result => $result);
$self->{ram} = {
memTotalReal => ($result->{memTotalReal}) ? $result->{memTotalReal} * 1024 : 0,
memAvailReal => ($result->{memAvailReal}) ? $result->{memAvailReal} * 1024 : 0,
memTotalFree => ($result->{memTotalFree}) ? $result->{memTotalFree} * 1024 : 0,
memShared => ($result->{memShared}) ? $result->{memShared} * 1024 : 0,
memBuffer => ($result->{memBuffer}) ? $result->{memBuffer} * 1024 : 0,
memCached => ($result->{memCached}) ? $result->{memCached} * 1024 : 0,
};
if (defined($self->{option_results}->{check_swap})) {
$self->{swap} = {
memTotalSwap => ($result->{memTotalSwap}) ? $result->{memTotalSwap} * 1024 : 0,
memAvailSwap => ($result->{memAvailSwap}) ? $result->{memAvailSwap} * 1024 : 0,
};
$self->swap_calc(result => $result);
}
}
@ -295,27 +275,22 @@ Check memory usage (UCD-SNMP-MIB).
=item B<--units>
Units of thresholds (Default: '%') ('%', 'absolute').
Units of thresholds (Default: '%') ('%', 'absolute') (Deprecated. Please use new counters directly)
=item B<--free>
Thresholds are on free space left.
Thresholds are on free space left (Deprecated. Please use new counters directly)
=item B<--swap>
Check swap also.
=item B<--warning-*>
=item B<--warning-*> B<--critical-*>
Threshold warning.
Can be: 'usage', 'swap', 'buffer' (absolute),
'cached' (absolute), 'shared' (absolute).
=item B<--critical-*>
Threshold critical.
Can be: 'usage', 'swap', 'buffer' (absolute),
'cached' (absolute), 'shared' (absolute).
Thresholds.
Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%),
'swap' (B), 'swap-free' (B), 'swap-prct' (%),
'buffer' (B), 'cached' (B), 'shared' (B).
=back