Debian Codegraph @

src/network/dlink/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 network::dlink::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_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 set_counters {
42 my ($self, %options) = @_;
43
44 $self->{maps_counters_type} = [
45 { name => 'memory', type => 1, cb_prefix_output => 'prefix_memory_output', message_multiple => 'All memory usages are ok', skipped_code => { -10 => 1 } }
46 ];
47
48 $self->{maps_counters}->{memory} = [
49 { label => 'usage', display_ok => 0, nlabel => 'memory.usage.bytes', set => {
50 key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'display' } ],
51 closure_custom_output => $self->can('custom_usage_output'),
52 perfdatas => [
53 { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1 }
54 ]
55 }
56 },
57 { label => 'usage-free', display_ok => 0, nlabel => 'memory.free.bytes', set => {
58 key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'display' } ],
59 closure_custom_output => $self->can('custom_usage_output'),
60 perfdatas => [
61 { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1 }
62 ]
63 }
64 },
65 { label => 'usage-prct', nlabel => 'memory.usage.percentage', set => {
66 key_values => [ { name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'prct_free' }, { name => 'total' }, { name => 'display' } ],
67 closure_custom_output => $self->can('custom_usage_output'),
68 perfdatas => [
69 { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 }
70 ]
71 }
72 }
73 ];
74 }
75
76 sub prefix_memory_output {
77 my ($self, %options) = @_;
78
79 return "Memory '" . $options{instance_value}->{display} . "' ";
80 }
81
82 sub new {
83 my ($class, %options) = @_;
84 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
85 bless $self, $class;
86
87 $options{options}->add_options(arguments => {
88 });
89
90 return $self;
91 }
92
93 my $map_memory_type = {
94 1 => 'dram', 2 => 'flash', 3 => 'nvram'
95 };
96
97 my $mapping_industrial = {
98 total => { oid => '.1.3.6.1.4.1.171.14.5.1.4.1.3' }, # dEntityExtMemUtilTotal (KB)
99 used => { oid => '.1.3.6.1.4.1.171.14.5.1.4.1.4' }, # dEntityExtMemUtilUsed (KB)
100 free => { oid => '.1.3.6.1.4.1.171.14.5.1.4.1.5' } # dEntityExtMemUtilUsed (KB)
101 };
102 my $oid_dEntityExtMemoryUtilEntry = '.1.3.6.1.4.1.171.14.5.1.4.1';
103
104 my $mapping_common = {
105 total => { oid => '.1.3.6.1.4.1.171.17.5.1.4.1.3' }, # esEntityExtMemUtilTotal (KB)
106 used => { oid => '.1.3.6.1.4.1.171.17.5.1.4.1.4' }, # esEntityExtMemUtilUsed (KB)
107 free => { oid => '.1.3.6.1.4.1.171.17.5.1.4.1.5' } # esEntityExtMemUtilFree (KB)
108 };
109 my $oid_esEntityExtMemoryUtilEntry = '.1.3.6.1.4.1.171.17.5.1.4.1';
110
111 sub check_memory {
112 my ($self, %options) = @_;
113
114 return if ($self->{checked_memory} == 1);
115
116 foreach (keys %{$options{snmp_result}}) {
117 next if (! /^$options{mapping}->{total}->{oid}\.(\d+)\.(\d+)$/);
118 my $instance = $1 . '.' . $2;
119 my $display = 'unit' . $1 . $self->{output}->get_instance_perfdata_separator() . $map_memory_type->{$2};
120 my $result = $options{snmp}->map_instance(mapping => $options{mapping}, results => $options{snmp_result}, instance => $instance);
121
122 $self->{checked_memory} = 1;
123 $self->{memory}->{$display} = {
124 display => $display,
125 used => $result->{used} * 1024,
126 free => $result->{free} * 1024,
127 prct_used => ($result->{used} * 1024 * 100) / ($result->{total} * 1024),
128 prct_free => ($result->{free} * 1024 * 100) / ($result->{total} * 1024),
129 total => $result->{total} * 1024
130 };
131 }
132 }
133
134 sub manage_selection {
135 my ($self, %options) = @_;
136
137 my $snmp_result = $options{snmp}->get_multiple_table(
138 oids => [
139 { oid => $oid_dEntityExtMemoryUtilEntry, start => $mapping_industrial->{total}->{oid} },
140 { oid => $oid_esEntityExtMemoryUtilEntry, start => $mapping_common->{total}->{oid} }
141 ],
142 nothing_quit => 1
143 );
144
145 $self->{checked_memory} = 0;
146 $self->check_memory(snmp => $options{snmp}, snmp_result => $snmp_result->{$oid_dEntityExtMemoryUtilEntry}, mapping => $mapping_industrial);
147 $self->check_memory(snmp => $options{snmp}, snmp_result => $snmp_result->{$oid_esEntityExtMemoryUtilEntry}, mapping => $mapping_common);
148 }
149
150 1;
151
152 __END__
153
154 =head1 MODE
155
156 Check memory usages.
157
158 =over 8
159
160 =item B<--warning-*> B<--critical-*>
161
162 Thresholds.
163 Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%).
164
165 =back
166
167 =cut