Debian Codegraph @

src/storage/ibm/storwize/ssh/mode/poolusage.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::ibm::storwize::ssh::mode::poolusage;
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 'status: ' . $self->{result_values}->{status};
33 }
34
35 sub custom_usage_perfdata {
36 my ($self, %options) = @_;
37
38 my $label = 'pool.space.usage.bytes';
39 my $value_perf = $self->{result_values}->{used};
40 if (defined($self->{instance_mode}->{option_results}->{free})) {
41 $label = 'pool.space.free.bytes';
42 $value_perf = $self->{result_values}->{free};
43 }
44
45 my %total_options = ();
46 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
47 $total_options{total} = $self->{result_values}->{total};
48 $total_options{cast_int} = 1;
49 }
50
51 $self->{output}->perfdata_add(
52 nlabel => $label,
53 unit => 'B',
54 instances => $self->{result_values}->{display},
55 value => $value_perf,
56 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
57 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
58 min => 0,
59 max => $self->{result_values}->{total}
60 );
61 }
62
63 sub custom_usage_threshold {
64 my ($self, %options) = @_;
65
66 my ($exit, $threshold_value);
67 $threshold_value = $self->{result_values}->{used};
68 $threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free}));
69 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
70 $threshold_value = $self->{result_values}->{prct_used};
71 $threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free}));
72 }
73 $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } ]);
74 return $exit;
75 }
76
77 sub custom_usage_output {
78 my ($self, %options) = @_;
79
80 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
81 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
82 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
83 return sprintf(
84 'usage total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
85 $total_size_value . " " . $total_size_unit,
86 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
87 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
88 );
89 }
90
91 sub custom_usage_calc {
92 my ($self, %options) = @_;
93
94 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
95 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
96
97 return -10 if ($self->{result_values}->{total} <= 0);
98
99 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'};
100 $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
101 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
102 $self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used};
103
104 return 0;
105 }
106
107 sub prefix_pool_output {
108 my ($self, %options) = @_;
109
110 return "Pool '" . $options{instance_value}->{display} . "' ";
111 }
112
113 sub set_counters {
114 my ($self, %options) = @_;
115
116 $self->{maps_counters_type} = [
117 { name => 'pool', type => 1, cb_prefix_output => 'prefix_pool_output', message_multiple => 'All pools are ok', skipped_code => { -10 => 1 } }
118 ];
119
120 $self->{maps_counters}->{pool} = [
121 {
122 label => 'status',
123 type => 2,
124 warning_default => '%{status} =~ /degraded/i',
125 critical_default => '%{status} =~ /offline/i',
126 set => {
127 key_values => [ { name => 'status' }, { name => 'display' } ],
128 closure_custom_output => $self->can('custom_status_output'),
129 closure_custom_perfdata => sub { return 0; },
130 closure_custom_threshold_check => \&catalog_status_threshold_ng
131 }
132 },
133 { label => 'usage', set => {
134 key_values => [ { name => 'display' }, { name => 'used' }, { name => 'total' } ],
135 closure_custom_calc => $self->can('custom_usage_calc'),
136 closure_custom_output => $self->can('custom_usage_output'),
137 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
138 closure_custom_threshold_check => $self->can('custom_usage_threshold')
139 }
140 }
141 ];
142 }
143
144 sub new {
145 my ($class, %options) = @_;
146 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
147 bless $self, $class;
148
149 $options{options}->add_options(arguments => {
150 'filter-name:s' => { name => 'filter_name' },
151 'units:s' => { name => 'units', default => '%' },
152 'free' => { name => 'free' }
153 });
154
155 return $self;
156 }
157
158 sub manage_selection {
159 my ($self, %options) = @_;
160
161 my ($content) = $options{custom}->execute_command(command => 'lsmdiskgrp -delim : -bytes');
162 $self->{pool} = {};
163 my $result = $options{custom}->get_hasharray(content => $content, delim => ':');
164 foreach (@$result) {
165 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
166 $_->{name} !~ /$self->{option_results}->{filter_name}/) {
167 $self->{output}->output_add(long_msg => "skipping '" . $_->{name} . "': no matching filter.", debug => 1);
168 next;
169 }
170
171 $self->{pool}->{$_->{id}} = {
172 display => $_->{name},
173 status => $_->{status},
174 total => $_->{used_capacity} + $_->{free_capacity},
175 used => $_->{used_capacity}
176 };
177 }
178
179 if (scalar(keys %{$self->{pool}}) <= 0) {
180 $self->{output}->add_option_msg(short_msg => 'No pool found.');
181 $self->{output}->option_exit();
182 }
183 }
184
185 1;
186
187 __END__
188
189 =head1 MODE
190
191 Check pool usages.
192
193 =over 8
194
195 =item B<--filter-counters>
196
197 Only display some counters (regexp can be used).
198 Example: --filter-counters='^status$'
199
200 =item B<--filter-name>
201
202 Filter pool name (can be a regexp).
203
204 =item B<--warning-status>
205
206 Define the conditions to match for the status to be WARNING (default: '%{status} =~ /degraded/i').
207 You can use the following variables: %{status}, %{display}
208
209 =item B<--critical-status>
210
211 Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /offline/i').
212 You can use the following variables: %{status}, %{display}
213
214 =item B<--warning-*> B<--critical-*>
215
216 Thresholds.
217 Can be: 'usage'.
218
219 =item B<--units>
220
221 Units of thresholds (default: '%') ('%', 'B').
222
223 =item B<--free>
224
225 Thresholds are on free space left.
226
227 =back
228
229 =cut