Debian Codegraph @

src/storage/oracle/zs/restapi/mode/pools.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::oracle::zs::restapi::mode::pools;
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 catalog_status_calc);
28
29 sub custom_status_output {
30 my ($self, %options) = @_;
31
32 my $msg = 'status : ' . $self->{result_values}->{status};
33 return $msg;
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 my $msg = sprintf('space usage total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
43 $total_size_value . " " . $total_size_unit,
44 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used_space},
45 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free_space}
46 );
47 return $msg;
48 }
49
50 sub set_counters {
51 my ($self, %options) = @_;
52
53 $self->{maps_counters_type} = [
54 { name => 'pool', type => 1, cb_prefix_output => 'prefix_pool_output', message_multiple => 'All pools are ok', skipped_code => { -10 => 1 } },
55 ];
56
57 $self->{maps_counters}->{pool} = [
58 { label => 'status', threshold => 0, set => {
59 key_values => [ { name => 'status' }, { name => 'display' } ],
60 closure_custom_calc => \&catalog_status_calc,
61 closure_custom_output => $self->can('custom_status_output'),
62 closure_custom_perfdata => sub { return 0; },
63 closure_custom_threshold_check => \&catalog_status_threshold,
64 }
65 },
66 { label => 'usage', nlabel => 'pool.space.usage.bytes', set => {
67 key_values => [ { name => 'used_space' }, { name => 'free_space' }, { name => 'prct_used_space' }, { name => 'prct_free_space' }, { name => 'total_space' }, { name => 'display' }, ],
68 closure_custom_output => $self->can('custom_usage_output'),
69 perfdatas => [
70 { value => 'used_space', template => '%d', min => 0, max => 'total_space',
71 unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'display' },
72 ],
73 }
74 },
75 { label => 'usage-free', nlabel => 'pool.space.free.bytes', display_ok => 0, set => {
76 key_values => [ { name => 'free_space' }, { name => 'used_space' }, { name => 'prct_used_space' }, { name => 'prct_free_space' }, { name => 'total_space' }, { name => 'display' }, ],
77 closure_custom_output => $self->can('custom_usage_output'),
78 perfdatas => [
79 { value => 'free_space', template => '%d', min => 0, max => 'total_space',
80 unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'display' },
81 ],
82 }
83 },
84 { label => 'usage-prct', nlabel => 'pool.space.usage.percentage', display_ok => 0, set => {
85 key_values => [ { name => 'prct_used_space' }, { name => 'display' } ],
86 output_template => 'used : %.2f %%',
87 perfdatas => [
88 { value => 'prct_used_space', template => '%.2f', min => 0, max => 100,
89 unit => '%', label_extra_instance => 1, instance_use => 'display' },
90 ],
91 }
92 },
93 ];
94 }
95
96 sub new {
97 my ($class, %options) = @_;
98 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
99 bless $self, $class;
100
101 $options{options}->add_options(arguments => {
102 'filter-name:s' => { name => 'filter_name' },
103 'unknown-status:s' => { name => 'unknown_status', default => '' },
104 'warning-status:s' => { name => 'warning_status', default => '' },
105 'critical-status:s' => { name => 'critical_status', default => '%{status} !~ /online|exported/i' },
106 });
107
108 return $self;
109 }
110
111 sub check_options {
112 my ($self, %options) = @_;
113 $self->SUPER::check_options(%options);
114
115 $self->change_macros(macros => ['warning_status', 'critical_status', 'unknown_status']);
116 }
117
118 sub prefix_pool_output {
119 my ($self, %options) = @_;
120
121 return "Pool '" . $options{instance_value}->{display} . "' ";
122 }
123
124 sub manage_selection {
125 my ($self, %options) = @_;
126
127 my $results = $options{custom}->request_api(url_path => '/api/storage/v1/pools');
128
129 $self->{pool} = {};
130 foreach (@{$results->{pools}}) {
131 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
132 $_->{name} !~ /$self->{option_results}->{filter_name}/) {
133 $self->{output}->output_add(long_msg => "skipping pool '" . $_->{name} . "': no matching filter.", debug => 1);
134 next;
135 }
136
137 $self->{pool}->{$_->{name}} = {
138 display => $_->{name},
139 status => $_->{status},
140 total_space => $_->{usage}->{total},
141 used_space => $_->{usage}->{used},
142 free_space => $_->{usage}->{free},
143 prct_used_space =>
144 defined($_->{usage}->{total}) ? ($_->{usage}->{used} * 100 / $_->{usage}->{total}) : undef,
145 prct_free_space =>
146 defined($_->{usage}->{total}) ? ($_->{usage}->{free} * 100 / $_->{usage}->{total}) : undef,
147 };
148 }
149
150 if (scalar(keys %{$self->{pool}}) <= 0) {
151 $self->{output}->add_option_msg(short_msg => "No pool found");
152 $self->{output}->option_exit();
153 }
154 }
155
156 1;
157
158 __END__
159
160 =head1 MODE
161
162 Check pool usages.
163
164 =over 8
165
166 =item B<--filter-counters>
167
168 Only display some counters (regexp can be used).
169 Example: --filter-counters='^usage$'
170
171 =item B<--filter-name>
172
173 Filter pool name (can be a regexp).
174
175 =item B<--unknown-status>
176
177 Define the conditions to match for the status to be UNKNOWN (default: '').
178 You can use the following variables: %{status}, %{display}
179
180 =item B<--warning-status>
181
182 Define the conditions to match for the status to be WARNING (default: '').
183 You can use the following variables: %{status}, %{display}
184
185 =item B<--critical-status>
186
187 Define the conditions to match for the status to be CRITICAL (default: '%{status} !~ /online|exported/i').
188 You can use the following variables: %{status}, %{display}
189
190 =item B<--warning-*> B<--critical-*>
191
192 Thresholds.
193 Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%).
194
195 =back
196
197 =cut