Debian Codegraph @

src/storage/hp/storeonce/3/restapi/mode/servicesetusage.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::storeonce::3::restapi::mode::servicesetusage;
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 [replication health: %s] [housekeeping health: %s]',
34 $self->{result_values}->{health}, $self->{result_values}->{replication_health}, $self->{result_values}->{housekeeping_health}
35 );
36 }
37
38 sub custom_usage_perfdata {
39 my ($self, %options) = @_;
40
41 my $nlabel = 'serviceset.space.usage.bytes';
42 my $value_perf = $self->{result_values}->{used};
43 if (defined($self->{instance_mode}->{option_results}->{free})) {
44 $nlabel = 'serviceset.space.free.bytes';
45 $value_perf = $self->{result_values}->{free};
46 }
47
48 my %total_options = ();
49 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
50 $total_options{total} = $self->{result_values}->{total};
51 $total_options{cast_int} = 1;
52 }
53
54 $self->{output}->perfdata_add(
55 nlabel => $nlabel,
56 unit => 'B',
57 instances => $self->{result_values}->{display},
58 value => $value_perf,
59 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
60 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
61 min => 0, max => $self->{result_values}->{total}
62 );
63 }
64
65 sub custom_usage_threshold {
66 my ($self, %options) = @_;
67
68 my ($exit, $threshold_value);
69 $threshold_value = $self->{result_values}->{used};
70 $threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free}));
71 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
72 $threshold_value = $self->{result_values}->{prct_used};
73 $threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free}));
74 }
75 $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } ]);
76 return $exit;
77 }
78
79 sub custom_usage_output {
80 my ($self, %options) = @_;
81
82 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
83 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
84 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
85 return sprintf(
86 "Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
87 $total_size_value . " " . $total_size_unit,
88 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
89 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
90 );
91 }
92
93 sub custom_usage_calc {
94 my ($self, %options) = @_;
95
96 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
97 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
98 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'};
99 $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
100 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
101 $self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used};
102
103 return 0;
104 }
105
106 sub prefix_scs_output {
107 my ($self, %options) = @_;
108
109 return "Service set '" . $options{instance_value}->{display} . "' ";
110 }
111
112 sub set_counters {
113 my ($self, %options) = @_;
114
115 $self->{maps_counters_type} = [
116 { name => 'scs', type => 1, cb_prefix_output => 'prefix_scs_output', message_multiple => 'All service sets are ok' }
117 ];
118
119 $self->{maps_counters}->{scs} = [
120 {
121 label => 'status',
122 type => 2,
123 warning_default => '%{health} =~ /warning/i',
124 critical_default => '%{health} =~ /critical/i',
125 set => {
126 key_values => [ { name => 'health' }, { name => 'replication_health' }, { name => 'housekeeping_health' }, { name => 'display' } ],
127 closure_custom_output => $self->can('custom_status_output'),
128 closure_custom_perfdata => sub { return 0; },
129 closure_custom_threshold_check => \&catalog_status_threshold_ng
130 }
131 },
132 { label => 'usage', set => {
133 key_values => [ { name => 'display' }, { name => 'used' }, { name => 'total' } ],
134 closure_custom_calc => $self->can('custom_usage_calc'),
135 closure_custom_output => $self->can('custom_usage_output'),
136 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
137 closure_custom_threshold_check => $self->can('custom_usage_threshold')
138 }
139 },
140 { label => 'dedup', nlabel => 'serviceset.deduplication.ratio.count', set => {
141 key_values => [ { name => 'dedup' }, { name => 'display' } ],
142 output_template => 'Dedup Ratio: %.2f',
143 perfdatas => [
144 { template => '%.2f', min => 0, label_extra_instance => 1, instance_use => 'display' }
145 ]
146 }
147 }
148 ];
149 }
150
151 sub new {
152 my ($class, %options) = @_;
153 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
154 bless $self, $class;
155
156 $options{options}->add_options(arguments => {
157 "filter-name:s" => { name => 'filter_name' },
158 "warning-status:s" => { name => 'warning_status', default => '%{health} =~ /warning/' },
159 "critical-status:s" => { name => 'critical_status', default => '%{health} =~ /critical/' },
160 "units:s" => { name => 'units', default => '%' },
161 "free" => { name => 'free' }
162 });
163
164 return $self;
165 }
166
167 my %mapping_health_level = (
168 0 => 'unknown',
169 1 => 'ok',
170 2 => 'information',
171 3 => 'warning',
172 4 => 'critical'
173 );
174
175 sub manage_selection {
176 my ($self, %options) = @_;
177
178 $self->{scs} = {};
179 my $result = $options{custom}->get(path => '/cluster/servicesets', ForceArray => ['serviceset']);
180 if (defined($result->{servicesets}->{serviceset})) {
181 foreach my $entry (@{$result->{servicesets}->{serviceset}}) {
182 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
183 $entry->{properties}->{alias} !~ /$self->{option_results}->{filter_name}/) {
184 $self->{output}->output_add(long_msg => "skipping '" . $entry->{properties}->{alias} . "': no matching filter.", debug => 1);
185 next;
186 }
187
188 $self->{scs}->{ $entry->{properties}->{ssid} } = {
189 display => $entry->{properties}->{alias},
190 health => $mapping_health_level{$entry->{properties}->{healthLevel}},
191 housekeeping_health => $mapping_health_level{$entry->{properties}->{housekeepingHealthLevel}},
192 replication_health => $mapping_health_level{$entry->{properties}->{repHealthLevel}},
193 total => $entry->{properties}->{capacityBytes},
194 used => $entry->{properties}->{capacityBytes} - $entry->{properties}->{freeBytes},
195 dedup => $entry->{properties}->{dedupeRatio}
196 };
197 }
198 }
199
200 if (scalar(keys %{$self->{scs}}) <= 0) {
201 $self->{output}->add_option_msg(short_msg => "No service set found.");
202 $self->{output}->option_exit();
203 }
204 }
205
206 1;
207
208 __END__
209
210 =head1 MODE
211
212 Check service set usages.
213
214 =over 8
215
216 =item B<--filter-counters>
217
218 Only display some counters (regexp can be used).
219 Example: --filter-counters='^status$'
220
221 =item B<--filter-name>
222
223 Filter service set name (can be a regexp).
224
225 =item B<--warning-status>
226
227 Define the conditions to match for the status to be WARNING (default: '%{health} =~ /warning/).
228 You can use the following variables: %{health}, %{replication_health}, %{housekeeping_health}, %{display}
229
230 =item B<--critical-status>
231
232 Define the conditions to match for the status to be CRITICAL (default: '%{health} =~ /critical/').
233 You can use the following variables: %{health}, %{replication_health}, %{housekeeping_health}, %{display}
234
235 =item B<--warning-*>
236
237 Warning threshold.
238 Can be: 'usage', 'dedup'.
239
240 =item B<--critical-*>
241
242 Critical threshold.
243 Can be: 'usage', 'dedup'.
244
245 =item B<--units>
246
247 Units of thresholds (default: '%') ('%', 'B').
248
249 =item B<--free>
250
251 Thresholds are on free space left.
252
253 =back
254
255 =cut