Debian Codegraph @

src/storage/hp/primera/restapi/mode/diskusage.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::hp::primera::restapi::mode::diskusage;
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 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
32 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
33 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
34 return sprintf(
35 "Used: %s of %s (%.2f%%) Free: %s (%.2f%%)",
36 $total_used_value . " " . $total_used_unit,
37 $total_size_value . " " . $total_size_unit,
38 $self->{result_values}->{prct_used},
39 $total_free_value . " " . $total_free_unit,
40 $self->{result_values}->{prct_free}
41 );
42 }
43
44 sub custom_global_total_usage_output {
45 my ($self, %options) = @_;
46
47 my ($used_human, $used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
48 my ($total_human, $total_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
49 my $msg = "Total Used: $used_human $used_unit / $total_human $total_unit" ;
50
51 return $msg;
52 }
53
54 sub custom_global_total_free_output {
55 my ($self, %options) = @_;
56
57 my ($free_human, $free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
58 my $msg = "Total Free: $free_human $free_unit" ;
59
60 return $msg;
61 }
62
63
64 sub set_counters {
65 my ($self, %options) = @_;
66
67 $self->{maps_counters_type} = [
68 { name => 'global', type => 0 },
69 { name => 'disk', type => 1, cb_prefix_output => 'prefix_disk_output', message_multiple => 'All disks are ok' },
70 ];
71 $self->{maps_counters}->{global} = [
72 {
73 label => 'total-usage',
74 nlabel => 'disks.total.space.usage.bytes',
75 set => {
76 key_values => [ { name => 'used' }, { name => 'total' } ],
77 closure_custom_output => $self->can('custom_global_total_usage_output'),
78 perfdatas => [
79 { template => '%s', min => 0, max => 'total' }
80 ]
81 }
82 },
83 {
84 label => 'total-usage-prct',
85 nlabel => 'disks.total.space.usage.percent',
86 set => {
87 key_values => [ { name => 'used_prct' }],
88 output_template => 'Total percentage used: %.2f %%',
89 perfdatas => [
90 { template => '%s', uom => '%', min => 0, max => 100 }
91 ]
92 }
93 },
94 {
95 label => 'total-free',
96 nlabel => 'disks.total.space.free.bytes',
97 set => {
98 key_values => [ { name => 'free' }, { name => 'total' } ],
99 closure_custom_output => $self->can('custom_global_total_free_output'),
100 perfdatas => [
101 { template => '%s', min => 0, max => 'total' }
102 ]
103 }
104 }
105 ];
106 $self->{maps_counters}->{disk} = [
107 { label => 'usage', nlabel => 'disk.space.usage.bytes', set => {
108 key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'id' }, { name => 'position' }, { name => 'manufacturer' }, { name => 'model' }, { name => 'serial' } ],
109 closure_custom_output => $self->can('custom_usage_output'),
110 perfdatas => [
111 { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'id' }
112 ]
113 }
114 },
115 { label => 'usage-free', display_ok => 0, nlabel => 'disk.space.free.bytes', set => {
116 key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'id' }, { name => 'position' }, { name => 'manufacturer' }, { name => 'model' }, { name => 'serial' } ],
117 closure_custom_output => $self->can('custom_usage_output'),
118 perfdatas => [
119 { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'id' }
120 ]
121 }
122 },
123 { label => 'usage-prct', display_ok => 0, nlabel => 'disk.space.usage.percentage', set => {
124 key_values => [ { name => 'prct_used' }, { name => 'id' }, { name => 'position' }, { name => 'manufacturer' }, { name => 'model' }, { name => 'serial' } ],
125 output_template => 'Used : %.2f %%',
126 perfdatas => [
127 { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'id' }
128 ]
129 }
130 }
131 ];
132 }
133
134 sub new {
135 my ($class, %options) = @_;
136 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
137 bless $self, $class;
138
139 $options{options}->add_options(arguments => {
140 'filter-id:s' => { name => 'filter_id' },
141 'filter-manufacturer:s' => { name => 'filter_manufacturer' },
142 'filter-model:s' => { name => 'filter_model' },
143 'filter-serial:s' => { name => 'filter_serial' },
144 'filter-position:s' => { name => 'filter_position' },
145 });
146
147 return $self;
148 }
149
150 sub prefix_disk_output {
151 my ($self, %options) = @_;
152
153 #return "Disk '" . $options{instance_value}->{display} . "' ";
154 return sprintf(
155 "Disk #%s (%s/%s, serial: %s) located %s has ",
156 $options{instance_value}->{id},
157 $options{instance_value}->{manufacturer},
158 $options{instance_value}->{model},
159 $options{instance_value}->{serial},
160 $options{instance_value}->{position}
161 );
162 }
163
164 sub manage_selection {
165 my ($self, %options) = @_;
166
167 my $response = $options{custom}->request_api(
168 endpoint => '/api/v1/disks'
169 );
170 my $disks = $response->{members};
171
172 $self->{global} = {
173 total => 0,
174 free => 0,
175 used => 0,
176 used_prct => 0
177 };
178 $self->{disk} = {};
179
180 for my $disk (@{$disks}) {
181
182 my $disk_intro = "disk #" . $disk->{id} . " (" . $disk->{manufacturer} . "/" . $disk->{model}
183 . ", serial: " . $disk->{serialNumber} . ") located '" . $disk->{position};
184
185 # skip if filtered by id
186 if (defined($self->{option_results}->{filter_id})
187 and $self->{option_results}->{filter_id} ne ''
188 and $disk->{id} !~ /$self->{option_results}->{filter_id}/) {
189 $self->{output}->output_add(long_msg => "Skipping $disk_intro because the id does not match the filter.", debug => 1);
190 next;
191 }
192 # skip if filtered by manufacturer
193 if (defined($self->{option_results}->{filter_manufacturer})
194 and $self->{option_results}->{filter_manufacturer} ne ''
195 and $disk->{manufacturer} !~ /$self->{option_results}->{filter_manufacturer}/) {
196 $self->{output}->output_add(long_msg => "Skipping $disk_intro because the manufacturer does not match the filter.", debug => 1);
197 next;
198 }
199 # skip if filtered by model
200 if (defined($self->{option_results}->{filter_model})
201 and $self->{option_results}->{filter_model} ne ''
202 and $disk->{model} !~ /$self->{option_results}->{filter_model}/) {
203 $self->{output}->output_add(long_msg => "Skipping $disk_intro because the model does not match the filter.", debug => 1);
204 next;
205 }
206 # skip if filtered by position
207 if (defined($self->{option_results}->{filter_position})
208 and $self->{option_results}->{filter_position} ne ''
209 and $disk->{position} !~ /$self->{option_results}->{filter_position}/) {
210 $self->{output}->output_add(long_msg => "Skipping $disk_intro because the position does not match the filter.", debug => 1);
211 next;
212 }
213 # skip if filtered by serial
214 if (defined($self->{option_results}->{filter_serial})
215 and $self->{option_results}->{filter_serial} ne ''
216 and $disk->{serial} !~ /$self->{option_results}->{filter_serial}/) {
217 $self->{output}->output_add(long_msg => "Skipping $disk_intro because the serial does not match the filter.", debug => 1);
218 next;
219 }
220
221 my $total = $disk->{totalSizeMiB} * 1024 * 1024;
222 my $free = $disk->{freeSizeMiB} * 1024 * 1024;
223 my $used = $total - $free;
224
225 $self->{global}->{total} = $self->{global}->{total} + $total;
226 $self->{global}->{free} = $self->{global}->{free} + $free;
227 $self->{global}->{used} = $self->{global}->{used} + $used;
228
229 $self->{disk}->{$disk->{id}} = {
230 id => $disk->{id},
231 total => $total,
232 used => $used,
233 free => $free,
234 prct_used => $used * 100 / $total,
235 prct_free => $free * 100 / $total,
236 manufacturer => $disk->{manufacturer},
237 model => $disk->{model},
238 serial => $disk->{serialNumber},
239 position => $disk->{position}
240 };
241 }
242
243 $self->{global}->{used_prct} = $self->{global}->{used} * 100 / $self->{global}->{total} if ($self->{global}->{total} > 0);
244
245 if (scalar(keys %{$self->{disk}}) <= 0) {
246 $self->{output}->add_option_msg(short_msg => "No disk found.");
247 $self->{output}->option_exit();
248 }
249 }
250
251 1;
252
253 __END__
254
255 =head1 MODE
256
257 Check disk usage.
258
259 =over 8
260
261 =item B<--filter-counters>
262
263 Define which counters (filtered by regular expression) should be monitored.
264 Example: --filter-counters='^usage$'
265
266 =item B<--filter-id>
267
268 Define which disks should be monitored based on their IDs.
269 This option will be treated as a regular expression.
270
271 =item B<--filter-manufacturer>
272
273 Define which volumes should be monitored based on the disk manufacturer.
274 This option will be treated as a regular expression.
275
276 =item B<--filter-model>
277
278 Define which volumes should be monitored based on the disk model.
279 This option will be treated as a regular expression.
280
281 =item B<--filter-serial>
282
283 Define which volumes should be monitored based on the disk serial number.
284 This option will be treated as a regular expression.
285
286 =item B<--filter-position>
287
288 Define which volumes should be monitored based on the disk position.
289 The position is composed of 3 integers, separated by colons:
290 - Cage number where the physical disk is in.
291 - Magazine number where the physical disk is in.
292 - For DC4 cages, disk position within the magazine. For non-DC4 cages, 0.
293 Example: 7:5:0
294 This option will be treated as a regular expression.
295
296 =item B<--warning-*> B<--critical-*>
297
298 Thresholds for disk usage metrics. * may be replaced with:
299 - For individual disks: 'usage' (B), 'usage-free' (B), 'usage-prct' (%).
300 - For global statistics: 'total-usage' (B), 'total-free' (B), 'total-usage-prct' (%).
301
302 =back
303
304 =cut