Debian Codegraph @

src/storage/qnap/snmp/mode/volumes.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::volumes;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
28
29 sub custom_status_output {
30 my ($self, %options) = @_;
31
32 return sprintf(
33 "status: %s",
34 $self->{result_values}->{status}
35 );
36 }
37
38 sub custom_space_usage_output {
39 my ($self, %options) = @_;
40
41 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
42 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
43 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
44 return sprintf(
45 "space usage total: %s used: %s (%.2f%%) free: %s (%.2f%%)",
46 $total_size_value . " " . $total_size_unit,
47 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
48 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
49 );
50 }
51
52 sub volume_long_output {
53 my ($self, %options) = @_;
54
55 return sprintf(
56 "checking volume '%s'",
57 $options{instance_value}->{name}
58 );
59 }
60
61 sub prefix_volume_output {
62 my ($self, %options) = @_;
63
64 return sprintf(
65 "volume '%s' ",
66 $options{instance_value}->{name}
67 );
68 }
69
70 sub set_counters {
71 my ($self, %options) = @_;
72
73 $self->{maps_counters_type} = [
74 { name => 'volumes', type => 3, cb_prefix_output => 'prefix_volume_output', cb_long_output => 'volume_long_output',
75 indent_long_output => ' ', message_multiple => 'All volumes are ok',
76 group => [
77 { name => 'status', type => 0, skipped_code => { -10 => 1 } },
78 { name => 'space', type => 0, skipped_code => { -10 => 1 } }
79 ]
80 }
81 ];
82
83 $self->{maps_counters}->{status} = [
84 {
85 label => 'volume-status',
86 type => 2,
87 warning_default => '%{status} =~ /degraded|warning/i',
88 critical_default => '%{status} =~ /critical/i',
89 set => {
90 key_values => [
91 { name => 'status' }, { name => 'name' }
92 ],
93 closure_custom_output => $self->can('custom_status_output'),
94 closure_custom_perfdata => sub { return 0; },
95 closure_custom_threshold_check => \&catalog_status_threshold_ng
96 }
97 }
98 ];
99
100 $self->{maps_counters}->{space} = [
101 { label => 'space-usage', nlabel => 'volume.space.usage.bytes', set => {
102 key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'name' } ],
103 closure_custom_output => $self->can('custom_space_usage_output'),
104 perfdatas => [
105 { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'name' }
106 ]
107 }
108 },
109 { label => 'space-usage-free', display_ok => 0, nlabel => 'volume.space.free.bytes', set => {
110 key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'name' } ],
111 closure_custom_output => $self->can('custom_space_usage_output'),
112 perfdatas => [
113 { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'name' }
114 ]
115 }
116 },
117 { label => 'space-usage-prct', display_ok => 0, nlabel => 'volume.space.usage.percentage', set => {
118 key_values => [ { name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'prct_free' }, { name => 'total' }, { name => 'name' } ],
119 closure_custom_output => $self->can('custom_space_usage_output'),
120 perfdatas => [
121 { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'name' }
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-name:s' => { name => 'filter_name' },
135 'force-counters-legacy' => { name => 'force_counters_legacy' }
136 });
137
138 return $self;
139 }
140
141 sub convert_bytes {
142 my ($self, %options) = @_;
143 my $multiple = defined($options{network}) ? 1000 : 1024;
144 my %units = (K => 1, M => 2, G => 3, T => 4);
145
146 if ($options{value} !~ /^\s*([0-9\.\,]+)\s*(.)/) {
147 $self->{output}->output_add(
148 severity => 'UNKNOWN',
149 output => "Cannot convert value '" . $options{value} . "'"
150 );
151 $self->{output}->display();
152 $self->{output}->exit();
153 }
154 my ($bytes, $unit) = ($1, uc($2));
155
156 for (my $i = 0; $i < $units{$unit}; $i++) {
157 $bytes *= $multiple;
158 }
159
160 return $bytes;
161 }
162
163 my $mapping = {
164 legacy => {
165 name => { oid => '.1.3.6.1.4.1.24681.1.2.17.1.2' }, # sysVolumeDescr
166 total => { oid => '.1.3.6.1.4.1.24681.1.2.17.1.4' }, # sysVolumeTotalSize
167 free => { oid => '.1.3.6.1.4.1.24681.1.2.17.1.5' }, # sysVolumeFreeSize
168 status => { oid => '.1.3.6.1.4.1.24681.1.2.17.1.6' } # sysVolumeStatus
169 },
170 ex => {
171 total => { oid => '.1.3.6.1.4.1.24681.1.4.1.1.1.2.3.2.1.3' }, # volumeCapacity
172 free => { oid => '.1.3.6.1.4.1.24681.1.4.1.1.1.2.3.2.1.4' }, # volumeFreeSize
173 status => { oid => '.1.3.6.1.4.1.24681.1.4.1.1.1.2.3.2.1.5' }, # volumeStatus
174 name => { oid => '.1.3.6.1.4.1.24681.1.4.1.1.1.2.3.2.1.8' } # volumeName
175 },
176 qts => {
177 total => { oid => '.1.3.6.1.4.1.55062.1.10.9.1.3' }, # volumeCapacity
178 free => { oid => '.1.3.6.1.4.1.55062.1.10.9.1.4' }, # volumeFreeSize
179 status => { oid => '.1.3.6.1.4.1.55062.1.10.9.1.5' }, # volumeStatus
180 name => { oid => '.1.3.6.1.4.1.55062.1.10.9.1.8' } # volumeName
181 }
182 };
183
184 sub check_volumes {
185 my ($self, %options) = @_;
186
187 return 0 if (scalar(keys %{$options{snmp_result}}) <= 0);
188
189 foreach (keys %{$options{snmp_result}}) {
190 next if (! /^$mapping->{ $options{type} }->{name}->{oid}\.(.*)$/);
191 my $instance = $1;
192 my $result = $options{snmp}->map_instance(mapping => $mapping->{ $options{type} }, results => $options{snmp_result}, instance => $instance);
193
194 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
195 $result->{name} !~ /$self->{option_results}->{filter_name}/) {
196 $self->{output}->output_add(long_msg => "skipping volume $result->{name}", debug => 1);
197 next;
198 }
199
200 if (defined($options{convert})) {
201 $result->{total} = $self->convert_bytes(value => $result->{total});
202 $result->{free} = $self->convert_bytes(value => $result->{free});
203 }
204
205 $self->{volumes}->{$instance} = {
206 name => $result->{name},
207 status => {
208 name => $result->{name},
209 status => $result->{status}
210 }
211 };
212 if (defined($result->{total}) && $result->{total} > 0) {
213 $self->{volumes}->{$instance}->{space} = {
214 name => $result->{name},
215 total => $result->{total},
216 free => $result->{free},
217 used => $result->{total} - $result->{free},
218 prct_free => $result->{free} * 100 / $result->{total},
219 prct_used => ($result->{total} - $result->{free}) * 100 / $result->{total}
220 };
221 }
222 }
223
224 return 1;
225 }
226
227 sub manage_selection {
228 my ($self, %options) = @_;
229
230 $self->{volumes} = {};
231
232 my $snmp_result;
233 if (!defined($self->{option_results}->{force_counters_legacy})) {
234 $snmp_result = $options{snmp}->get_table(
235 oid => '.1.3.6.1.4.1.55062.1.10.9' # volumeTable
236 );
237 return if ($self->check_volumes(snmp => $options{snmp}, type => 'qts', snmp_result => $snmp_result));
238
239 $snmp_result = $options{snmp}->get_table(
240 oid => '.1.3.6.1.4.1.24681.1.4.1.1.1.2.3.2' # volumeTable
241 );
242 return if ($self->check_volumes(snmp => $options{snmp}, type => 'ex', snmp_result => $snmp_result));
243 }
244
245 $snmp_result = $options{snmp}->get_table(
246 oid => '.1.3.6.1.4.1.24681.1.2.17', # systemVolumeTable
247 nothing_quit => 1
248 );
249 $self->check_volumes(snmp => $options{snmp}, type => 'legacy', snmp_result => $snmp_result, convert => 1);
250 }
251
252 1;
253
254 __END__
255
256 =head1 MODE
257
258 Check volumes.
259
260 =over 8
261
262 =item B<--filter-name>
263
264 Filter volumes by name (can be a regexp).
265
266 =item B<--force-counters-legacy>
267
268 Force to use legacy counters. Should be used when EX/QTS counters are buggy.
269
270 =item B<--unknown-volume-status>
271
272 Define the conditions to match for the status to be UNKNOWN.
273 You can use the following variables: %{status}, %{name}
274
275 =item B<--warning-volume-status>
276
277 Define the conditions to match for the status to be WARNING (default: '%{status} =~ /degraded|warning/i').
278 You can use the following variables: %{status}, %{name}
279
280 =item B<--critical-volume-status>
281
282 Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /critical/i').
283 You can use the following variables: %{status}, %{name}
284
285 =item B<--warning-*> B<--critical-*>
286
287 Thresholds.
288 Can be: 'space-usage-prct', 'space-usage', 'space-usage-free'.
289
290 =back
291
292 =cut
293