Debian Codegraph @

src/storage/qnap/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 storage::qnap::snmp::mode::memory;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27
28 sub custom_ram_usage_output {
29 my ($self, %options) = @_;
30
31 return sprintf(
32 'Memory 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 => 'ram', type => 0, skipped_code => { -10 => 1 } }
46 ];
47
48 $self->{maps_counters}->{ram} = [
49 { label => 'memory-usage', nlabel => 'memory.usage.bytes', set => {
50 key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
51 closure_custom_output => $self->can('custom_ram_usage_output'),
52 perfdatas => [
53 { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1 }
54 ]
55 }
56 },
57 { label => 'memory-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' } ],
59 closure_custom_output => $self->can('custom_ram_usage_output'),
60 perfdatas => [
61 { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1 }
62 ]
63 }
64 },
65 { label => 'memory-usage-prct', display_ok => 0, nlabel => 'memory.usage.percentage', set => {
66 key_values => [ { name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'prct_free' }, { name => 'total' } ],
67 closure_custom_output => $self->can('custom_ram_usage_output'),
68 perfdatas => [
69 { template => '%.2f', min => 0, max => 100, unit => '%' }
70 ]
71 }
72 }
73 ];
74 }
75
76 sub new {
77 my ($class, %options) = @_;
78 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
79 bless $self, $class;
80
81 $options{options}->add_options(arguments => {
82 'force-counters-legacy' => { name => 'force_counters_legacy' }
83 });
84
85 return $self;
86 }
87
88 sub convert_bytes {
89 my ($self, %options) = @_;
90 my $multiple = defined($options{network}) ? 1000 : 1024;
91 my %units = (K => 1, M => 2, G => 3, T => 4);
92
93 if ($options{value} !~ /^\s*([0-9\.\,]+)\s*(.)/) {
94 $self->{output}->output_add(
95 severity => 'UNKNOWN',
96 output => "Cannot convert value '" . $options{value} . "'"
97 );
98 $self->{output}->display();
99 $self->{output}->exit();
100 }
101 my ($bytes, $unit) = ($1, uc($2));
102
103 for (my $i = 0; $i < $units{$unit}; $i++) {
104 $bytes *= $multiple;
105 }
106
107 return $bytes;
108 }
109
110 # From QTS GUI (4.5.4.1741) :
111 # Used memory is "total usable memory" minus "available memory".
112 # (Available memory is the sum of free, buffer, cache, and other reclaimable memory.)
113 # Cache memory used by file systems and processes are not included in "used memory".
114
115 my $mapping = {
116 legacy => {
117 ram_total => { oid => '.1.3.6.1.4.1.24681.1.2.2' }, # systemTotalMem
118 ram_free => { oid => '.1.3.6.1.4.1.24681.1.2.3' } # systemFreeMem
119 },
120 ex => {
121 ram_total => { oid => '.1.3.6.1.4.1.24681.1.3.2' }, # systemTotalMemEX
122 ram_free => { oid => '.1.3.6.1.4.1.24681.1.3.3' } # systemFreeMemEX
123 },
124 es => {
125 ram_total => { oid => '.1.3.6.1.4.1.24681.2.2.2' }, # es-SystemTotalMem
126 ram_free => { oid => '.1.3.6.1.4.1.24681.2.2.3' } # es-SystemFreeMem
127 },
128 qts => {
129 ram_total => { oid => '.1.3.6.1.4.1.55062.1.12.13' }, # systemTotalMem
130 ram_free => { oid => '.1.3.6.1.4.1.55062.1.12.15' } # systemAvailableMem
131 }
132 };
133
134 sub check_memory {
135 my ($self, %options) = @_;
136
137 return 0 if (defined($self->{ram}->{total}));
138
139 my $result = $options{snmp}->map_instance(mapping => $mapping->{ $options{type} }, results => $options{snmp_result}, instance => 0);
140 return 0 if (!defined($result->{ram_free}));
141
142 if (defined($options{convert})) {
143 $result->{ram_total} = $self->convert_bytes(value => $result->{ram_total});
144 $result->{ram_free} = $self->convert_bytes(value => $result->{ram_free});
145 }
146
147 if (defined($result->{ram_total}) && $result->{ram_total} > 0) {
148 $self->{ram} = {
149 total => $result->{ram_total},
150 used => $result->{ram_total} - $result->{ram_free},
151 free => $result->{ram_free},
152 prct_used => 100 - ($result->{ram_free} * 100 / $result->{ram_total}),
153 prct_free => $result->{ram_free} * 100 / $result->{ram_total}
154 };
155 }
156 }
157
158 sub manage_selection {
159 my ($self, %options) = @_;
160
161 my $snmp_result = $options{snmp}->get_leef(
162 oids => [
163 map(
164 $_->{oid} . '.0',
165 values(%{$mapping->{legacy}}),
166 values(%{$mapping->{ex}}),
167 values(%{$mapping->{es}}),
168 values(%{$mapping->{qts}})
169 )
170 ],
171 nothing_quit => 1
172 );
173
174 if (!defined($self->{option_results}->{force_counters_legacy})) {
175 $self->check_memory(snmp => $options{snmp}, type => 'qts', snmp_result => $snmp_result);
176 $self->check_memory(snmp => $options{snmp}, type => 'ex', snmp_result => $snmp_result);
177 $self->check_memory(snmp => $options{snmp}, type => 'es', snmp_result => $snmp_result, convert => 1);
178 }
179 $self->check_memory(snmp => $options{snmp}, type => 'legacy', snmp_result => $snmp_result, convert => 1);
180 }
181
182 1;
183
184 __END__
185
186 =head1 MODE
187
188 Check memory.
189
190 =over 8
191
192 =item B<--force-counters-legacy>
193
194 Force to use legacy counters. Should be used when EX/ES/QTS counters are buggy.
195
196 =item B<--warning-*> B<--critical-*>
197
198 Thresholds.
199 Can be: 'memory-usage' (B), 'memory-usage-free' (B), 'memory-usage-prct' (%).
200
201 =back
202
203 =cut