Debian Codegraph @

src/storage/hp/primera/restapi/mode/diskstatus.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::diskstatus;
22
23 use base qw(centreon::plugins::templates::counter);
24 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
25
26 use strict;
27 use warnings;
28
29 my %map_state = (
30 1 => 'normal',
31 2 => 'degraded',
32 3 => 'new',
33 4 => 'failed',
34 99 => 'unknown'
35 );
36
37 sub custom_status_output {
38 my ($self, %options) = @_;
39
40 return sprintf(
41 "Disk #%s (%s/%s, serial: %s) located %s is %s",
42 $self->{result_values}->{id},
43 $self->{result_values}->{manufacturer},
44 $self->{result_values}->{model},
45 $self->{result_values}->{serial},
46 $self->{result_values}->{position},
47 $self->{result_values}->{status}
48 );
49 }
50
51 sub prefix_global_output {
52 my ($self, %options) = @_;
53
54 return 'Disks ';
55 }
56
57 sub set_counters {
58 my ($self, %options) = @_;
59
60 $self->{maps_counters_type} = [
61 { name => 'global', cb_prefix_output => 'prefix_global_output', type => 0 },
62 { name => 'disks', type => 1, message_multiple => 'All disks are ok' }
63 ];
64
65 $self->{maps_counters}->{global} = [
66 { label => 'disks-total', nlabel => 'disks.total.count', set => {
67 key_values => [ { name => 'total' } ],
68 output_template => 'total: %s',
69 perfdatas => [
70 { template => '%s', min => 0 }
71 ]
72 }
73 },
74 { label => 'disks-normal', nlabel => 'disks.normal.count', set => {
75 key_values => [ { name => 'normal' }, { name => 'total' } ],
76 output_template => 'normal: %s',
77 perfdatas => [
78 { template => '%s', min => 0, max => 'total' }
79 ]
80 }
81 },
82 { label => 'disks-degraded', nlabel => 'disks.degraded.count', set => {
83 key_values => [ { name => 'degraded' }, { name => 'total' } ],
84 output_template => 'degraded: %s',
85 perfdatas => [
86 { template => '%s', min => 0, max => 'total' }
87 ]
88 }
89 },
90 { label => 'disks-new', nlabel => 'disks.new.count', set => {
91 key_values => [ { name => 'new' }, { name => 'total' } ],
92 output_template => 'new: %s',
93 perfdatas => [
94 { template => '%s', min => 0, max => 'total' }
95 ]
96 }
97 },
98 { label => 'disks-failed', nlabel => 'disks.failed.count', set => {
99 key_values => [ { name => 'failed' }, { name => 'total' } ],
100 output_template => 'failed: %s',
101 perfdatas => [
102 { template => '%s', min => 0, max => 'total' }
103 ]
104 }
105 },
106 { label => 'disks-unknown', nlabel => 'disks.unknown.count', set => {
107 key_values => [ { name => 'unknown' }, { name => 'total' } ],
108 output_template => 'unknown: %s',
109 perfdatas => [
110 { template => '%s', min => 0, max => 'total' }
111 ]
112 }
113 }
114 ];
115 $self->{maps_counters}->{disks} = [
116 {
117 label => 'status',
118 type => 2,
119 warning_default => '%{status} =~ /^(new|degraded|unknown)$/',
120 critical_default => '%{status} =~ /failed/',
121 unknown_default => '%{status} =~ /NOT_DOCUMENTED$/',
122 set => {
123 key_values => [ { name => 'status' }, { name => 'id' }, { name => 'manufacturer' }, { name => 'model' }, { name => 'serial' }, { name => 'position' } ],
124 closure_custom_output => $self->can('custom_status_output'),
125 closure_custom_perfdata => sub { return 0; },
126 closure_custom_threshold_check => \&catalog_status_threshold_ng
127 }
128 }
129 ];
130 }
131
132 sub new {
133 my ($class, %options) = @_;
134 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
135 bless $self, $class;
136
137 $options{options}->add_options(arguments => {
138 'filter-id:s' => { name => 'filter_id' },
139 'filter-manufacturer:s' => { name => 'filter_manufacturer' },
140 'filter-model:s' => { name => 'filter_model' },
141 'filter-position:s' => { name => 'filter_position' },
142 'filter-serial:s' => { name => 'filter_serial' }
143 });
144
145 return $self;
146 }
147
148 sub manage_selection {
149 my ($self, %options) = @_;
150
151 my $api_response = $options{custom}->request_api(
152 endpoint => '/api/v1/disks'
153 );
154
155 my $disks = $api_response->{members};
156
157 $self->{global} = {
158 total => 0,
159 normal => 0,
160 degraded => 0,
161 new => 0,
162 failed => 0,
163 unknown => 0
164 };
165
166
167
168 for my $disk (@{$disks}) {
169
170 my $disk_intro = "disk #" . $disk->{id} . " (" . $disk->{manufacturer} . "/" . $disk->{model}
171 . ", serial: " . $disk->{serialNumber} . ") located '" . $disk->{position};
172 # skip if filtered by id
173 if (defined($self->{option_results}->{filter_id})
174 and $self->{option_results}->{filter_id} ne ''
175 and $disk->{id} !~ /$self->{option_results}->{filter_id}/) {
176 $self->{output}->output_add(long_msg => "Skipping $disk_intro because the id does not match the filter.", debug => 1);
177 next;
178 }
179 # skip if filtered by manufacturer
180 if (defined($self->{option_results}->{filter_manufacturer})
181 and $self->{option_results}->{filter_manufacturer} ne ''
182 and $disk->{manufacturer} !~ /$self->{option_results}->{filter_manufacturer}/) {
183 $self->{output}->output_add(long_msg => "Skipping $disk_intro because the manufacturer does not match the filter.", debug => 1);
184 next;
185 }
186 # skip if filtered by model
187 if (defined($self->{option_results}->{filter_model})
188 and $self->{option_results}->{filter_model} ne ''
189 and $disk->{model} !~ /$self->{option_results}->{filter_model}/) {
190 $self->{output}->output_add(long_msg => "Skipping $disk_intro because the model does not match the filter.", debug => 1);
191 next;
192 }
193 # skip if filtered by position
194 if (defined($self->{option_results}->{filter_position})
195 and $self->{option_results}->{filter_position} ne ''
196 and $disk->{position} !~ /$self->{option_results}->{filter_position}/) {
197 $self->{output}->output_add(long_msg => "Skipping $disk_intro because the position does not match the filter.", debug => 1);
198 next;
199 }
200 # skip if filtered by serial
201 if (defined($self->{option_results}->{filter_serial})
202 and $self->{option_results}->{filter_serial} ne ''
203 and $disk->{serial} !~ /$self->{option_results}->{filter_serial}/) {
204 $self->{output}->output_add(long_msg => "Skipping $disk_intro because the serial does not match the filter.", debug => 1);
205 next;
206 }
207
208 my $state = defined($map_state{$disk->{state}}) ? $map_state{$disk->{state}} : 'NOT_DOCUMENTED';
209
210 # increment adequate global counters
211 $self->{global}->{total} = $self->{global}->{total} + 1;
212 $self->{global}->{$state} = $self->{global}->{$state} + 1;
213
214 # add the instance
215 $self->{disks}->{ $disk->{id} } = {
216 status => $state,
217 position => $disk->{position},
218 id => $disk->{id},
219 manufacturer => $disk->{manufacturer},
220 model => $disk->{model},
221 serial => $disk->{serialNumber}
222 };
223 }
224 }
225
226 1;
227
228 __END__
229
230 =head1 MODE
231
232 Monitor the states of the physical disks.
233
234 =over 8
235
236 =item B<--filter-id>
237
238 Define which disks should be monitored based on their IDs.
239 This option will be treated as a regular expression.
240
241 =item B<--filter-manufacturer>
242
243 Define which volumes should be monitored based on the disk manufacturer.
244 This option will be treated as a regular expression.
245
246 =item B<--filter-model>
247
248 Define which volumes should be monitored based on the disk model.
249 This option will be treated as a regular expression.
250
251 =item B<--filter-serial>
252
253 Define which volumes should be monitored based on the disk serial number.
254 This option will be treated as a regular expression.
255
256 =item B<--filter-position>
257
258 Define which volumes should be monitored based on the disk position.
259 The position is composed of 3 integers, separated by colons:
260 - Cage number where the physical disk is in.
261 - Magazine number where the physical disk is in.
262 - For DC4 cages, disk position within the magazine. For non-DC4 cages, 0.
263 Example: 7:5:0
264 This option will be treated as a regular expression.
265
266 =item B<--warning-status>
267
268 Define the condition to match for the returned status to be WARNING.
269 Default: '%{status} =~ /^(new|degraded|unknown)$/'
270
271 =item B<--critical-status>
272
273 Define the condition to match for the returned status to be CRITICAL.
274 Default: '%{status} =~ /failed/'
275
276 =item B<--unknown-status>
277
278 Define the condition to match for the returned status to be UNKNOWN.
279 Default: '%{status} =~ /NOT_DOCUMENTED$/'
280
281 =item B<--warning-*> B<--critical-*>
282
283 Thresholds. '*' may stand for 'disks-total', 'disks-normal', 'disks-degraded', 'disks-new',
284 'disks-failed', 'disks-unknown'.
285
286 =back
287
288 =cut