Debian Codegraph @

src/storage/emc/unisphere/restapi/mode/storageresources.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::emc::unisphere::restapi::mode::storageresources;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use storage::emc::unisphere::restapi::mode::components::resources qw($health_status);
28 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
29
30 sub custom_status_output {
31 my ($self, %options) = @_;
32
33 return 'status : ' . $self->{result_values}->{status};
34 }
35
36 sub custom_usage_output {
37 my ($self, %options) = @_;
38
39 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total_space});
40 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used_space});
41 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free_space});
42 return sprintf(
43 'space usage total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
44 $total_size_value . " " . $total_size_unit,
45 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used_space},
46 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free_space}
47 );
48 }
49
50 sub custom_allocated_output {
51 my ($self, %options) = @_;
52
53 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total_space});
54 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used_alloc});
55 $self->{result_values}->{free_alloc} = 0 if ($self->{result_values}->{free_alloc} < 0);
56 $self->{result_values}->{prct_free_alloc} = 0 if ($self->{result_values}->{prct_free_alloc} < 0);
57 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free_alloc});
58 return sprintf(
59 'allocated usage total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
60 $total_size_value . " " . $total_size_unit,
61 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used_alloc},
62 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free_alloc}
63 );
64 }
65
66 sub set_counters {
67 my ($self, %options) = @_;
68
69 $self->{maps_counters_type} = [
70 { name => 'sr', type => 1, cb_prefix_output => 'prefix_sr_output', message_multiple => 'All storage resources are ok', skipped_code => { -10 => 1 } }
71 ];
72
73 $self->{maps_counters}->{sr} = [
74 {
75 label => 'status',
76 type => 2,
77 unknown_default => '%{health_status} =~ /unknown/i',
78 warning_default => '%{health_status} =~ /ok_but|degraded|minor/i',
79 critical_default => '%{health_status} =~ /major|critical|non_recoverable/i',
80 set => {
81 key_values => [ { name => 'status' }, { name => 'display' } ],
82 closure_custom_output => $self->can('custom_status_output'),
83 closure_custom_perfdata => sub { return 0; },
84 closure_custom_threshold_check => \&catalog_status_threshold_ng
85 }
86 },
87 { label => 'usage', nlabel => 'storageresource.space.usage.bytes', set => {
88 key_values => [ { name => 'used_space' }, { name => 'free_space' }, { name => 'prct_used_space' }, { name => 'prct_free_space' }, { name => 'total_space' }, { name => 'display' }, ],
89 closure_custom_output => $self->can('custom_usage_output'),
90 perfdatas => [
91 { template => '%d', min => 0, max => 'total_space',
92 unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'display' }
93 ]
94 }
95 },
96 { label => 'usage-free', nlabel => 'storageresource.space.free.bytes', display_ok => 0, set => {
97 key_values => [ { name => 'free_space' }, { name => 'used_space' }, { name => 'prct_used_space' }, { name => 'prct_free_space' }, { name => 'total_space' }, { name => 'display' }, ],
98 closure_custom_output => $self->can('custom_usage_output'),
99 perfdatas => [
100 { template => '%d', min => 0, max => 'total_space',
101 unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'display' }
102 ]
103 }
104 },
105 { label => 'usage-prct', nlabel => 'storageresource.space.usage.percentage', display_ok => 0, set => {
106 key_values => [ { name => 'prct_used_space' }, { name => 'display' } ],
107 output_template => 'used : %.2f %%',
108 perfdatas => [
109 { value => 'prct_used_space', template => '%.2f', min => 0, max => 100,
110 unit => '%', label_extra_instance => 1, instance_use => 'display' }
111 ]
112 }
113 },
114 { label => 'allocated', nlabel => 'storageresource.allocated.usage.bytes', display_ok => 0, set => {
115 key_values => [ { name => 'used_alloc' }, { name => 'free_alloc' }, { name => 'prct_used_alloc' }, { name => 'prct_free_alloc' }, { name => 'total_space' }, { name => 'display' }, ],
116 closure_custom_output => $self->can('custom_allocated_output'),
117 perfdatas => [
118 { template => '%d', min => 0, max => 'total_space',
119 unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'display' }
120 ]
121 }
122 },
123 { label => 'allocated-prct', display_ok => 0, nlabel => 'storageresource.allocated.usage.percentage', set => {
124 key_values => [ { name => 'prct_used_alloc' }, { name => 'display' } ],
125 output_template => 'allocated used : %.2f %%',
126 perfdatas => [
127 { template => '%.2f', min => 0, max => 100,
128 unit => '%', label_extra_instance => 1, instance_use => 'display' }
129 ]
130 }
131 }
132 ];
133 }
134
135 sub new {
136 my ($class, %options) = @_;
137 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
138 bless $self, $class;
139
140 $options{options}->add_options(arguments => {
141 'filter-name:s' => { name => 'filter_name' }
142 });
143
144 return $self;
145 }
146
147 sub prefix_sr_output {
148 my ($self, %options) = @_;
149
150 return "Storage resource '" . $options{instance_value}->{display} . "' ";
151 }
152
153 sub manage_selection {
154 my ($self, %options) = @_;
155
156 my $results = $options{custom}->request_api(url_path => '/api/types/storageResource/instances?fields=name,health,sizeUsed,sizeAllocated,sizeTotal');
157
158 $self->{sr} = {};
159 foreach (@{$results->{entries}}) {
160 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
161 $_->{content}->{name} !~ /$self->{option_results}->{filter_name}/) {
162 $self->{output}->output_add(long_msg => "skipping storage resource '" . $_->{content}->{name} . "': no matching filter.", debug => 1);
163 next;
164 }
165
166 $self->{sr}->{$_->{content}->{id}} = {
167 display => $_->{content}->{name},
168 status => $health_status->{ $_->{content}->{health}->{value} },
169 total_space => $_->{content}->{sizeTotal},
170 };
171
172 if (defined($_->{content}->{sizeUsed})) {
173 $self->{sr}->{$_->{content}->{id}}->{used_space} = $_->{content}->{sizeUsed};
174 $self->{sr}->{$_->{content}->{id}}->{free_space} = $_->{content}->{sizeTotal} - $_->{content}->{sizeUsed};
175 $self->{sr}->{$_->{content}->{id}}->{prct_used_space} = $_->{content}->{sizeUsed} * 100 / $_->{content}->{sizeTotal};
176 $self->{sr}->{$_->{content}->{id}}->{prct_free_space} = 100 - ($_->{content}->{sizeUsed} * 100 / $_->{content}->{sizeTotal});
177 }
178 if (defined($_->{content}->{sizeAllocated})) {
179 $self->{sr}->{$_->{content}->{id}}->{used_alloc} = $_->{content}->{sizeAllocated};
180 $self->{sr}->{$_->{content}->{id}}->{free_alloc} = $_->{content}->{sizeTotal} - $_->{content}->{sizeAllocated};
181 $self->{sr}->{$_->{content}->{id}}->{prct_used_alloc} = $_->{content}->{sizeAllocated} * 100 / $_->{content}->{sizeTotal};
182 $self->{sr}->{$_->{content}->{id}}->{prct_free_alloc} = 100 - ($_->{content}->{sizeAllocated} * 100 / $_->{content}->{sizeTotal});
183 }
184 }
185
186 if (scalar(keys %{$self->{sr}}) <= 0) {
187 $self->{output}->add_option_msg(short_msg => "No storage resource found");
188 $self->{output}->option_exit();
189 }
190 }
191
192 1;
193
194 __END__
195
196 =head1 MODE
197
198 Check storage resources.
199
200 =over 8
201
202 =item B<--filter-counters>
203
204 Only display some counters (regexp can be used).
205 Example: --filter-counters='^usage$'
206
207 =item B<--filter-name>
208
209 Filter name (can be a regexp).
210
211 =item B<--unknown-status>
212
213 Define the conditions to match for the status to be UNKNOWN (default: '%{status} =~ /unknown/i').
214 You can use the following variables: %{status}, %{display}
215
216 =item B<--warning-status>
217
218 Define the conditions to match for the status to be WARNING (default: '%{status} =~ /ok_but|degraded|minor/i').
219 You can use the following variables: %{status}, %{display}
220
221 =item B<--critical-status>
222
223 Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /major|critical|non_recoverable/i').
224 You can use the following variables: %{status}, %{display}
225
226 =item B<--warning-*> B<--critical-*>
227
228 Thresholds.
229 Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%),
230 'allocated', 'allocated-prct'.
231
232 =back
233
234 =cut