Debian Codegraph @

src/centreon/common/cisco/standard/snmp/mode/memory.pm

raw
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 centreon::common::cisco::standard::snmp::mode::memory;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27
28 sub custom_usage_perfdata {
29 my ($self, %options) = @_;
30
31 my ($label, $nlabel, $unit, $total) = ('used', $self->{nlabel}, '%', 100);
32 my $value_perf = $self->{result_values}->{prct_used};
33 my %total_options = ();
34 if ($self->{result_values}->{total} != -1) {
35 $nlabel = 'memory.usage.bytes';
36 $unit = 'B';
37 $total = $self->{result_values}->{total};
38 $total_options{total} = $self->{result_values}->{total};
39 $total_options{cast_int} = 1;
40 $value_perf = $self->{result_values}->{used};
41 }
42
43 $self->{output}->perfdata_add(
44 label => $label, unit => $unit,
45 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
46 nlabel => $nlabel,
47 value => $value_perf,
48 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
49 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
50 min => 0, max => $total
51 );
52 }
53
54 sub custom_usage_threshold {
55 my ($self, %options) = @_;
56
57 return $self->{perfdata}->threshold_check(value => $self->{result_values}->{prct_used}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } ]);
58 }
59
60 sub custom_usage_output {
61 my ($self, %options) = @_;
62
63 my $msg;
64 if ($self->{result_values}->{total} != -1) {
65 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
66 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
67 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
68 $msg = sprintf(
69 'Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)',
70 $total_size_value . " " . $total_size_unit,
71 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
72 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
73 );
74 } else {
75 $msg = sprintf("Usage : %.2f %%", $self->{result_values}->{prct_used});
76 }
77 return $msg;
78 }
79
80 sub custom_usage_calc {
81 my ($self, %options) = @_;
82
83 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
84 $self->{result_values}->{prct_used} = $options{new_datas}->{$self->{instance} . '_prct_used'};
85 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
86 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'};
87 if ($self->{result_values}->{total} != -1) {
88 $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
89 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
90 $self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used};
91 }
92
93 return 0;
94 }
95
96 sub prefix_memory_output {
97 my ($self, %options) = @_;
98
99 return "Memory '" . $options{instance_value}->{display} . "' ";
100 }
101
102 sub set_counters {
103 my ($self, %options) = @_;
104
105 $self->{maps_counters_type} = [
106 { name => 'memory', type => 1, cb_prefix_output => 'prefix_memory_output', message_multiple => 'All memories are ok', skipped_code => { -10 => 1 } }
107 ];
108
109 $self->{maps_counters}->{memory} = [
110 { label => 'usage', nlabel => 'memory.usage.percentage', set => {
111 key_values => [ { name => 'display' }, { name => 'used' }, { name => 'total' }, { name => 'prct_used' } ],
112 closure_custom_calc => $self->can('custom_usage_calc'),
113 closure_custom_output => $self->can('custom_usage_output'),
114 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
115 closure_custom_threshold_check => $self->can('custom_usage_threshold')
116 }
117 }
118 ];
119 }
120
121 sub new {
122 my ($class, %options) = @_;
123 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
124 bless $self, $class;
125
126 $options{options}->add_options(arguments => {
127 'filter-pool:s' => { name => 'filter_pool' },
128 'check-order:s' => { name => 'check_order', default => 'enhanced_pool,pool,process,system_ext' }
129 });
130
131 return $self;
132 }
133
134 my $mapping_memory_pool = {
135 ciscoMemoryPoolName => { oid => '.1.3.6.1.4.1.9.9.48.1.1.1.2' },
136 ciscoMemoryPoolUsed => { oid => '.1.3.6.1.4.1.9.9.48.1.1.1.5' }, # in B
137 ciscoMemoryPoolFree => { oid => '.1.3.6.1.4.1.9.9.48.1.1.1.6' } # in B
138 };
139 my $oid_ciscoMemoryPoolEntry = '.1.3.6.1.4.1.9.9.48.1.1.1';
140
141 sub check_memory_pool {
142 my ($self, %options) = @_;
143
144 return if ($self->{checked_memory} == 1);
145
146 my $snmp_result = $self->{snmp}->get_table(
147 oid => $oid_ciscoMemoryPoolEntry,
148 start => $mapping_memory_pool->{ciscoMemoryPoolName}->{oid}, end => $mapping_memory_pool->{ciscoMemoryPoolFree}->{oid}
149 );
150
151 foreach my $oid (keys %{$snmp_result}) {
152 next if ($oid !~ /^$mapping_memory_pool->{ciscoMemoryPoolName}->{oid}\.(.*)$/);
153 my $instance = $1;
154 my $result = $self->{snmp}->map_instance(mapping => $mapping_memory_pool, results => $snmp_result, instance => $instance);
155
156 $self->{checked_memory} = 1;
157 if (defined($self->{option_results}->{filter_pool}) && $self->{option_results}->{filter_pool} ne '' &&
158 $result->{ciscoMemoryPoolName} !~ /$self->{option_results}->{filter_pool}/) {
159 $self->{output}->output_add(long_msg => "skipping '" . $result->{ciscoMemoryPoolName} . "': no matching filter.", debug => 1);
160 next;
161 }
162
163 $self->{memory}->{$instance} = {
164 display => $result->{ciscoMemoryPoolName},
165 total => $result->{ciscoMemoryPoolFree} + $result->{ciscoMemoryPoolUsed},
166 used => $result->{ciscoMemoryPoolUsed},
167 prct_used => -1,
168 };
169 }
170 }
171
172 my $oid_cseSysMemoryUtilization = '.1.3.6.1.4.1.9.9.305.1.1.2';
173
174 sub check_memory_system_ext {
175 my ($self, %options) = @_;
176
177 return if ($self->{checked_memory} == 1);
178
179 my $snmp_result = $self->{snmp}->get_table(
180 oid => $oid_cseSysMemoryUtilization,
181 );
182
183 foreach my $oid (keys %{$snmp_result}) {
184 my $used = $snmp_result->{$oid};
185 next if ($used eq '');
186 my $display = 'system';
187
188 $self->{checked_memory} = 1;
189 if (defined($self->{option_results}->{filter_pool}) && $self->{option_results}->{filter_pool} ne '' &&
190 $display !~ /$self->{option_results}->{filter_pool}/) {
191 $self->{output}->output_add(long_msg => "skipping '" . $display . "': no matching filter.", debug => 1);
192 next;
193 }
194
195 $self->{memory}->{system} = {
196 display => 'system',
197 total => -1,
198 used => -1,
199 prct_used => $used,
200 };
201 }
202 }
203
204 my $mapping_enh_memory_pool = {
205 cempMemPoolName => { oid => '.1.3.6.1.4.1.9.9.221.1.1.1.1.3' },
206 cempMemPoolUsed => { oid => '.1.3.6.1.4.1.9.9.221.1.1.1.1.7' }, # in B
207 cempMemPoolFree => { oid => '.1.3.6.1.4.1.9.9.221.1.1.1.1.8' }, # in B
208 cempMemPoolHCUsed => { oid => '.1.3.6.1.4.1.9.9.221.1.1.1.1.18' }, # in B
209 cempMemPoolHCFree => { oid => '.1.3.6.1.4.1.9.9.221.1.1.1.1.20' }, # in B
210 };
211
212 sub check_memory_enhanced_pool {
213 my ($self, %options) = @_;
214
215 return if ($self->{checked_memory} == 1);
216
217 my $oids = [
218 { oid => $mapping_enh_memory_pool->{cempMemPoolName}->{oid} },
219 { oid => $mapping_enh_memory_pool->{cempMemPoolUsed}->{oid} },
220 { oid => $mapping_enh_memory_pool->{cempMemPoolFree}->{oid} },
221 ];
222 if (!$self->{snmp}->is_snmpv1()) {
223 push @{$oids}, { oid => $mapping_enh_memory_pool->{cempMemPoolHCUsed}->{oid} },
224 { oid => $mapping_enh_memory_pool->{cempMemPoolHCFree}->{oid} };
225 }
226 my $snmp_result = $self->{snmp}->get_multiple_table(
227 oids => $oids,
228 return_type => 1
229 );
230
231 my $physical_array = {};
232 foreach my $oid (keys %{$snmp_result}) {
233 next if ($oid !~ /^$mapping_enh_memory_pool->{cempMemPoolName}->{oid}\.(.*?)\.(.*)$/);
234 my ($physical_index, $mem_index) = ($1, $2);
235 my $result = $self->{snmp}->map_instance(mapping => $mapping_enh_memory_pool, results => $snmp_result, instance => $physical_index . '.' . $mem_index);
236
237 $self->{checked_memory} = 1;
238
239 my $used = defined($result->{cempMemPoolHCUsed}) ? $result->{cempMemPoolHCUsed} : $result->{cempMemPoolUsed};
240 my $free = defined($result->{cempMemPoolHCFree}) ? $result->{cempMemPoolHCFree} : $result->{cempMemPoolFree};
241 if ($used + $free <= 0) {
242 $self->{output}->output_add(long_msg => "skipping '" . $result->{cempMemPoolName} . "': no total.", debug => 1);
243 next;
244 }
245
246 $physical_array->{$physical_index} = 1;
247 $self->{memory}->{$physical_index . '.' . $mem_index} = {
248 display => $result->{cempMemPoolName},
249 total => $used + $free,
250 used => $used,
251 prct_used => -1,
252 physical_index => $physical_index,
253 };
254 }
255
256 if (scalar(keys %$physical_array) > 0) {
257 my $oid_entPhysicalName = '.1.3.6.1.2.1.47.1.1.1.1.7';
258 $self->{snmp}->load(
259 oids => [$oid_entPhysicalName],
260 instances => [keys %$physical_array],
261 instance_regexp => '^(.*)$'
262 );
263 $snmp_result = $self->{snmp}->get_leef();
264 foreach (keys %{$self->{memory}}) {
265 if (defined($snmp_result->{ $oid_entPhysicalName . '.' . $self->{memory}->{$_}->{physical_index} })) {
266 my $display = $snmp_result->{ $oid_entPhysicalName . '.' . $self->{memory}->{$_}->{physical_index} } . '_' . $self->{memory}->{$_}->{display};
267 if (defined($self->{option_results}->{filter_pool}) && $self->{option_results}->{filter_pool} ne '' &&
268 $display !~ /$self->{option_results}->{filter_pool}/) {
269 $self->{output}->output_add(long_msg => "skipping '" . $display . "': no matching filter.", debug => 1);
270 delete $self->{memory}->{$_};
271 next;
272 }
273 $self->{memory}->{$_}->{display} = $display;
274 } else {
275 if (defined($self->{option_results}->{filter_pool}) && $self->{option_results}->{filter_pool} ne '' &&
276 $self->{memory}->{$_}->{display} !~ /$self->{option_results}->{filter_pool}/) {
277 $self->{output}->output_add(long_msg => "skipping '" . $self->{memory}->{$_}->{display} . "': no matching filter.", debug => 1);
278 delete $self->{memory}->{$_};
279 }
280 }
281 }
282 }
283 }
284
285 my $mapping_memory_process = {
286 cpmCPUMemoryUsed => { oid => '.1.3.6.1.4.1.9.9.109.1.1.1.1.12' }, # in KB
287 cpmCPUMemoryFree => { oid => '.1.3.6.1.4.1.9.9.109.1.1.1.1.13' }, # in KB
288 cpmCPUMemoryUsedOvrflw => { oid => '.1.3.6.1.4.1.9.9.109.1.1.1.1.16' }, # in KB
289 cpmCPUMemoryFreeOvrflw => { oid => '.1.3.6.1.4.1.9.9.109.1.1.1.1.18' }, # in KB
290 };
291
292 sub check_memory_process {
293 my ($self, %options) = @_;
294
295 return if ($self->{checked_memory} == 1);
296
297 my $oid_cpmCPUTotalEntry = '.1.3.6.1.4.1.9.9.109.1.1.1.1';
298 my $snmp_result = $self->{snmp}->get_table(
299 oid => $oid_cpmCPUTotalEntry,
300 start => $mapping_memory_process->{cpmCPUMemoryUsed}->{oid},
301 end => $mapping_memory_process->{cpmCPUMemoryFreeOvrflw}->{oid},
302 );
303
304 foreach my $oid (keys %{$snmp_result}) {
305 next if ($oid !~ /^$mapping_memory_process->{cpmCPUMemoryUsed}->{oid}\.(.*)$/);
306 my $instance = $1;
307 my $result = $self->{snmp}->map_instance(mapping => $mapping_memory_process, results => $snmp_result, instance => $instance);
308
309 $self->{checked_memory} = 1;
310
311 my $used = (
312 defined($result->{cpmCPUMemoryUsedOvrflw}) ?
313 ($result->{cpmCPUMemoryUsedOvrflw} << 32) + ($result->{cpmCPUMemoryUsed}) :
314 $result->{cpmCPUMemoryUsed}
315 ) * 1024;
316 my $free = (
317 defined($result->{cpmCPUMemoryFreeOvrflw}) ?
318 ($result->{cpmCPUMemoryFreeOvrflw} << 32) + ($result->{cpmCPUMemoryFree}) :
319 $result->{cpmCPUMemoryFree}
320 ) * 1024;
321 $self->{memory}->{$instance} = {
322 display => $instance,
323 total => $used + $free,
324 used => $used,
325 prct_used => -1,
326 };
327 }
328 }
329
330 sub manage_selection {
331 my ($self, %options) = @_;
332
333 $self->{snmp} = $options{snmp};
334 $self->{checked_memory} = 0;
335 $self->{memory} = {};
336
337 foreach (split /,/, $self->{option_results}->{check_order}) {
338 my $method = $self->can('check_memory_' . $_);
339 if ($method) {
340 $self->$method();
341 }
342 }
343
344 if ($self->{checked_memory} == 0) {
345 $self->{output}->add_option_msg(short_msg => "Cannot find memory informations");
346 $self->{output}->option_exit();
347 }
348 }
349
350 1;
351
352 __END__
353
354 =head1 MODE
355
356 Check memory usage (CISCO-MEMORY-POOL-MIB, CISCO-ENHANCED-MEMPOOL-MIB, CISCO-PROCESS-MIB, CISCO-SYSTEM-EXT-MIB).
357
358 =over 8
359
360 =item B<--warning-usage>
361
362 Warning threshold in percent.
363
364 =item B<--critical-usage>
365
366 Critical threshold in percent.
367
368 =item B<--filter-pool>
369
370 Filter pool to check (can use regexp).
371
372 =item B<--check-order>
373
374 Check memory in standard cisco mib. If you have some issue (wrong memory information in a specific mib), you can change the order
375 (default: 'enhanced_pool,pool,process,system_ext').
376
377 =back
378
379 =cut
380