| 1 |
# |
| 2 |
# Copyright 2024 Centreon (http://www.centreon.com/) |
| 3 |
# |
| 4 |
# Centreon is a full-fledged industry-strength solution that meets |
| 5 |
# the needs in IT infrastructure and application monitoring for |
| 6 |
# service performance. |
| 7 |
# |
| 8 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
# you may not use this file except in compliance with the License. |
| 10 |
# You may obtain a copy of the License at |
| 11 |
# |
| 12 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
# |
| 14 |
# Unless required by applicable law or agreed to in writing, software |
| 15 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
# See the License for the specific language governing permissions and |
| 18 |
# limitations under the License. |
| 19 |
# |
| 20 |
|
| 21 |
package network::enterasys::snmp::mode::memory; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
|
| 28 |
sub custom_usage_output { |
| 29 |
my ($self, %options) = @_; |
| 30 |
|
| 31 |
return sprintf( |
| 32 |
'total: %s %s used: %s %s (%.2f%%) free: %s %s (%.2f%%)', |
| 33 |
$self->{perfdata}->change_bytes(value => $self->{result_values}->{total}), |
| 34 |
$self->{perfdata}->change_bytes(value => $self->{result_values}->{used}), |
| 35 |
$self->{result_values}->{prct_used}, |
| 36 |
$self->{perfdata}->change_bytes(value => $self->{result_values}->{free}), |
| 37 |
$self->{result_values}->{prct_free} |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
sub prefix_memory_output { |
| 42 |
my ($self, %options) = @_; |
| 43 |
|
| 44 |
return sprintf( |
| 45 |
"Memory '%s' [%s] ", |
| 46 |
$options{instance_value}->{name}, |
| 47 |
$options{instance_value}->{physicalName} |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
sub set_counters { |
| 52 |
my ($self, %options) = @_; |
| 53 |
|
| 54 |
$self->{maps_counters_type} = [ |
| 55 |
{ name => 'memory', type => 1, cb_prefix_output => 'prefix_memory_output', message_multiple => 'All memory usages are ok', skipped_code => { -10 => 1 } } |
| 56 |
]; |
| 57 |
|
| 58 |
$self->{maps_counters}->{memory} = [ |
| 59 |
{ label => 'usage', display_ok => 0, nlabel => 'memory.usage.bytes', set => { |
| 60 |
key_values => [ |
| 61 |
{ name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, |
| 62 |
{ name => 'physicalName' }, { name => 'name'} |
| 63 |
], |
| 64 |
closure_custom_output => $self->can('custom_usage_output'), |
| 65 |
closure_custom_perfdata => sub { |
| 66 |
my ($self, %options) = @_; |
| 67 |
|
| 68 |
$self->{output}->perfdata_add( |
| 69 |
nlabel => $self->{nlabel}, |
| 70 |
unit => 'B', |
| 71 |
instances => [$self->{result_values}->{physicalName}, $self->{result_values}->{name}], |
| 72 |
value => $self->{result_values}->{used}, |
| 73 |
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}), |
| 74 |
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}), |
| 75 |
min => 0, |
| 76 |
max => $self->{result_values}->{total} |
| 77 |
); |
| 78 |
} |
| 79 |
} |
| 80 |
}, |
| 81 |
{ label => 'usage-free', display_ok => 0, nlabel => 'memory.free.bytes', set => { |
| 82 |
key_values => [ |
| 83 |
{ name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, |
| 84 |
{ name => 'physicalName' }, { name => 'name'} |
| 85 |
], |
| 86 |
closure_custom_output => $self->can('custom_usage_output'), |
| 87 |
closure_custom_perfdata => sub { |
| 88 |
my ($self, %options) = @_; |
| 89 |
|
| 90 |
$self->{output}->perfdata_add( |
| 91 |
nlabel => $self->{nlabel}, |
| 92 |
unit => 'B', |
| 93 |
instances => [$self->{result_values}->{physicalName}, $self->{result_values}->{name}], |
| 94 |
value => $self->{result_values}->{free}, |
| 95 |
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}), |
| 96 |
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}), |
| 97 |
min => 0, |
| 98 |
max => $self->{result_values}->{total} |
| 99 |
); |
| 100 |
} |
| 101 |
} |
| 102 |
}, |
| 103 |
{ label => 'usage-prct', nlabel => 'memory.usage.percentage', set => { |
| 104 |
key_values => [ |
| 105 |
{ name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'prct_free' }, { name => 'total' }, |
| 106 |
{ name => 'physicalName' }, { name => 'name'} |
| 107 |
], |
| 108 |
closure_custom_output => $self->can('custom_usage_output'), |
| 109 |
closure_custom_perfdata => sub { |
| 110 |
my ($self, %options) = @_; |
| 111 |
|
| 112 |
$self->{output}->perfdata_add( |
| 113 |
nlabel => $self->{nlabel}, |
| 114 |
unit => '%', |
| 115 |
instances => [$self->{result_values}->{physicalName}, $self->{result_values}->{name}], |
| 116 |
value => sprintf('%.2f', $self->{result_values}->{prct_used}), |
| 117 |
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}), |
| 118 |
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}), |
| 119 |
min => 0, |
| 120 |
max => 100 |
| 121 |
); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
]; |
| 126 |
} |
| 127 |
|
| 128 |
sub new { |
| 129 |
my ($class, %options) = @_; |
| 130 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 131 |
bless $self, $class; |
| 132 |
|
| 133 |
$options{options}->add_options(arguments => { |
| 134 |
'filter-entity-name:s' => { name => 'filter_entity_name' }, |
| 135 |
'filter-memory-name:s' => { name => 'filter_memory_name' } |
| 136 |
}); |
| 137 |
|
| 138 |
return $self; |
| 139 |
} |
| 140 |
|
| 141 |
my $mapping = { |
| 142 |
descr => { oid => '.1.3.6.1.4.1.5624.1.2.49.1.3.1.1.3' }, # etsysResourceStorageDescr (KB) |
| 143 |
total => { oid => '.1.3.6.1.4.1.5624.1.2.49.1.3.1.1.4' }, # etsysResourceStorageSize (KB) |
| 144 |
free => { oid => '.1.3.6.1.4.1.5624.1.2.49.1.3.1.1.5' } # etsysResourceStorageAvailable (KB) |
| 145 |
}; |
| 146 |
my $oid_etsysResourceStorageTable = '.1.3.6.1.4.1.5624.1.2.49.1.3.1'; |
| 147 |
my $oid_entPhysicalName = '.1.3.6.1.2.1.47.1.1.1.1.7'; |
| 148 |
|
| 149 |
sub manage_selection { |
| 150 |
my ($self, %options) = @_; |
| 151 |
|
| 152 |
my $snmp_result = $options{snmp}->get_table( |
| 153 |
oid => $oid_etsysResourceStorageTable, |
| 154 |
nothing_quit => 1 |
| 155 |
); |
| 156 |
|
| 157 |
$self->{memory} = {}; |
| 158 |
my $indexes = {}; |
| 159 |
foreach (keys %$snmp_result) { |
| 160 |
next if (! /^$mapping->{total}->{oid}\.(\d+).(\d+)\.(\d+)$/); |
| 161 |
my ($physicalIndex, $type) = ($1, $2); |
| 162 |
my $instance = $1 . '.' . $2 . '.' . $3; |
| 163 |
|
| 164 |
next if ($type != 2); # 2 = ram |
| 165 |
|
| 166 |
my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance); |
| 167 |
next if ($result->{total} <= 0); |
| 168 |
|
| 169 |
next if (defined($self->{option_results}->{filter_memory_name}) && $self->{option_results}->{filter_memory_name} ne '' && |
| 170 |
$result->{descr} !~ /$self->{option_results}->{filter_memory_name}/); |
| 171 |
|
| 172 |
$options{snmp}->load(oids => [ $oid_entPhysicalName . '.' . $physicalIndex ]) if (!defined($indexes->{$physicalIndex})); |
| 173 |
$indexes->{$physicalIndex} = 1; |
| 174 |
|
| 175 |
$result->{total} *= 1024; |
| 176 |
$result->{free} *= 1024; |
| 177 |
$self->{memory}->{ $result->{descr} } = { |
| 178 |
physicalIndex => $physicalIndex, |
| 179 |
name => $result->{descr}, |
| 180 |
used => $result->{total} - $result->{free}, |
| 181 |
free => $result->{free}, |
| 182 |
prct_used => ($result->{total} - $result->{free}) * 100 / $result->{total}, |
| 183 |
prct_free => $result->{free} * 100 / $result->{total}, |
| 184 |
total => $result->{total} |
| 185 |
}; |
| 186 |
} |
| 187 |
|
| 188 |
if (scalar(keys %$indexes) > 0) { |
| 189 |
$snmp_result = $options{snmp}->get_leef(); |
| 190 |
foreach (keys %{$self->{memory}}) { |
| 191 |
my $entity_name = $snmp_result->{ $oid_entPhysicalName . '.' . $self->{memory}->{$_}->{physicalIndex} }; |
| 192 |
if (defined($self->{option_results}->{filter_entity_name}) && $self->{option_results}->{filter_entity_name} ne '' && |
| 193 |
$entity_name !~ /$self->{option_results}->{filter_entity_name}/) { |
| 194 |
delete $self->{memory}->{$_}; |
| 195 |
next; |
| 196 |
} |
| 197 |
$self->{memory}->{$_}->{physicalName} = $entity_name; |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
1; |
| 203 |
|
| 204 |
__END__ |
| 205 |
|
| 206 |
=head1 MODE |
| 207 |
|
| 208 |
Check memory usages. |
| 209 |
|
| 210 |
=over 8 |
| 211 |
|
| 212 |
=item B<--filter-entity-name> |
| 213 |
|
| 214 |
Filter entity name (can be a regexp). |
| 215 |
|
| 216 |
=item B<--filter-memory-name> |
| 217 |
|
| 218 |
Filter memory name (can be a regexp). |
| 219 |
|
| 220 |
=item B<--warning-*> B<--critical-*> |
| 221 |
|
| 222 |
Thresholds. |
| 223 |
Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%). |
| 224 |
|
| 225 |
=back |
| 226 |
|
| 227 |
=cut |