Debian Codegraph @

src/storage/hp/lefthand/snmp/mode/volumeusage.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::lefthand::snmp::mode::volumeusage;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use Digest::MD5 qw(md5_hex);
28 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
29
30 sub custom_status_output {
31 my ($self, %options) = @_;
32
33 my $msg = 'replication status : ' . $self->{result_values}->{status};
34 return $msg;
35 }
36
37 sub custom_status_calc {
38 my ($self, %options) = @_;
39
40 $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_clusVolumeReplicationStatus'};
41 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
42 return 0;
43 }
44
45 sub custom_usage_perfdata {
46 my ($self, %options) = @_;
47
48 my $label = 'used';
49 my $value_perf = $self->{result_values}->{used};
50 if (defined($self->{instance_mode}->{option_results}->{free})) {
51 $label = 'free';
52 $value_perf = $self->{result_values}->{free};
53 }
54
55 my %total_options = ();
56 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
57 $total_options{total} = $self->{result_values}->{total};
58 $total_options{cast_int} = 1;
59 }
60
61 $self->{output}->perfdata_add(
62 label => $label, unit => 'B',
63 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
64 value => $value_perf,
65 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
66 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
67 min => 0, max => $self->{result_values}->{total}
68 );
69 }
70
71 sub custom_usage_threshold {
72 my ($self, %options) = @_;
73
74 my ($exit, $threshold_value);
75 $threshold_value = $self->{result_values}->{used};
76 $threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free}));
77 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
78 $threshold_value = $self->{result_values}->{prct_used};
79 $threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free}));
80 }
81 $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } ]);
82 return $exit;
83 }
84
85 sub custom_usage_output {
86 my ($self, %options) = @_;
87
88 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
89 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
90 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
91 my $msg = sprintf("Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
92 $total_size_value . " " . $total_size_unit,
93 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
94 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free});
95 return $msg;
96 }
97
98 sub custom_usage_calc {
99 my ($self, %options) = @_;
100
101 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
102 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
103 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'};
104 $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
105 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
106 $self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used};
107
108 return 0;
109 }
110
111 sub set_counters {
112 my ($self, %options) = @_;
113
114 $self->{maps_counters_type} = [
115 { name => 'volume', type => 1, cb_prefix_output => 'prefix_volume_output', message_multiple => 'All volumes are ok' }
116 ];
117
118 $self->{maps_counters}->{volume} = [
119 { label => 'usage', set => {
120 key_values => [ { name => 'display' }, { name => 'used' }, { name => 'total' } ],
121 closure_custom_calc => $self->can('custom_usage_calc'),
122 closure_custom_output => $self->can('custom_usage_output'),
123 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
124 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
125 }
126 },
127 { label => 'read', set => {
128 key_values => [ { name => 'clusVolumeStatsKbytesRead', per_second => 1 }, { name => 'display' } ],
129 output_template => 'Read I/O : %s %s/s', output_error_template => "Read I/O : %s",
130 output_change_bytes => 1,
131 perfdatas => [
132 { label => 'read', template => '%d',
133 unit => 'B/s', min => 0, label_extra_instance => 1, instance_use => 'display' },
134 ],
135 }
136 },
137 { label => 'write', set => {
138 key_values => [ { name => 'clusVolumeStatsKbytesWrite', per_second => 1 }, { name => 'display' } ],
139 output_template => 'Write I/O : %s %s/s', output_error_template => "Write I/O : %s",
140 output_change_bytes => 1,
141 perfdatas => [
142 { label => 'write', template => '%d',
143 unit => 'B/s', min => 0, label_extra_instance => 1, instance_use => 'display' },
144 ],
145 }
146 },
147 { label => 'read-iops', set => {
148 key_values => [ { name => 'clusVolumeStatsIOsRead', per_second => 1 }, { name => 'display' } ],
149 output_template => 'Read IOPs : %.2f', output_error_template => "Read IOPs : %s",
150 perfdatas => [
151 { label => 'read_iops', template => '%.2f',
152 unit => 'iops', min => 0, label_extra_instance => 1, instance_use => 'display' },
153 ],
154 }
155 },
156 { label => 'write-iops', set => {
157 key_values => [ { name => 'clusVolumeStatsIOsWrite', per_second => 1 }, { name => 'display' } ],
158 output_template => 'Write IOPs : %.2f', output_error_template => "Write IOPs : %s",
159 perfdatas => [
160 { label => 'write_iops', template => '%.2f',
161 unit => 'iops', min => 0, label_extra_instance => 1, instance_use => 'display' },
162 ],
163 }
164 },
165 { label => 'read-latency', set => {
166 key_values => [ { name => 'clusVolumeStatsIoLatencyRead', diff => 1 }, { name => 'display' } ],
167 output_template => 'Read Latency : %.2f ms', output_error_template => "Read Latency : %s",
168 perfdatas => [
169 { label => 'read_latency', template => '%.2f',
170 unit => 'ms', min => 0, label_extra_instance => 1, instance_use => 'display' },
171 ],
172 }
173 },
174 { label => 'write-latency', set => {
175 key_values => [ { name => 'clusVolumeStatsIoLatencyWrite', diff => 1 }, { name => 'display' } ],
176 output_template => 'Write Latency : %.2f ms', output_error_template => "Write Latency : %s",
177 perfdatas => [
178 { label => 'write_latency', template => '%.2f',
179 unit => 'ms', min => 0, label_extra_instance => 1, instance_use => 'display' },
180 ],
181 }
182 },
183 { label => 'replication-status', threshold => 0, set => {
184 key_values => [ { name => 'clusVolumeReplicationStatus' }, { name => 'display' } ],
185 closure_custom_calc => $self->can('custom_status_calc'),
186 closure_custom_output => $self->can('custom_status_output'),
187 closure_custom_perfdata => sub { return 0; },
188 closure_custom_threshold_check => \&catalog_status_threshold
189 }
190 }
191 ];
192 }
193
194 sub new {
195 my ($class, %options) = @_;
196 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
197 bless $self, $class;
198
199 $options{options}->add_options(arguments => {
200 'filter-name:s' => { name => 'filter_name' },
201 'warning-replication-status:s' => { name => 'warning_replication_status', default => '' },
202 'critical-replication-status:s' => { name => 'critical_replication_status', default => '%{status} !~ /normal/i' },
203 'units:s' => { name => 'units', default => '%' },
204 'free' => { name => 'free' }
205 });
206
207 return $self;
208 }
209
210 sub check_options {
211 my ($self, %options) = @_;
212 $self->SUPER::check_options(%options);
213
214 $self->change_macros(macros => ['warning_replication_status', 'critical_replication_status']);
215 }
216
217 sub prefix_volume_output {
218 my ($self, %options) = @_;
219
220 return "Volume '" . $options{instance_value}->{display} . "' ";
221 }
222
223 my %map_replication_status = (1 => 'normal', 2 => 'faulty');
224 my $mapping = {
225 clusVolumeName => { oid => '.1.3.6.1.4.1.9804.3.1.1.2.12.97.1.2' },
226 clusVolumeSize => { oid => '.1.3.6.1.4.1.9804.3.1.1.2.12.97.1.5' },
227 clusVolumeReplicationStatus => { oid => '.1.3.6.1.4.1.9804.3.1.1.2.12.97.1.15', map => \%map_replication_status },
228 clusVolumeUsedSpace => { oid => '.1.3.6.1.4.1.9804.3.1.1.2.12.97.1.31' },
229 clusVolumeStatsIOsRead => { oid => '.1.3.6.1.4.1.9804.3.1.1.2.12.97.1.36' },
230 clusVolumeStatsIOsWrite => { oid => '.1.3.6.1.4.1.9804.3.1.1.2.12.97.1.37' },
231 clusVolumeStatsKbytesRead => { oid => '.1.3.6.1.4.1.9804.3.1.1.2.12.97.1.38' },
232 clusVolumeStatsKbytesWrite => { oid => '.1.3.6.1.4.1.9804.3.1.1.2.12.97.1.39' },
233 clusVolumeStatsIoLatencyRead => { oid => '.1.3.6.1.4.1.9804.3.1.1.2.12.97.1.42' },
234 clusVolumeStatsIoLatencyWrite => { oid => '.1.3.6.1.4.1.9804.3.1.1.2.12.97.1.43' },
235 };
236
237 my $oid_clusVolumeEntry = '.1.3.6.1.4.1.9804.3.1.1.2.12.97.1';
238
239 sub manage_selection {
240 my ($self, %options) = @_;
241
242 if ($options{snmp}->is_snmpv1()) {
243 $self->{output}->add_option_msg(short_msg => "Need to use SNMP v2c or v3.");
244 $self->{output}->option_exit();
245 }
246
247 $self->{volume} = {};
248 my $snmp_result = $options{snmp}->get_table(oid => $oid_clusVolumeEntry,
249 nothing_quit => 1);
250
251
252 foreach my $oid (keys %{$snmp_result}) {
253 next if ($oid !~ /^$mapping->{clusVolumeName}->{oid}\.(.*)$/);
254 my $instance = $1;
255 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
256
257 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
258 $result->{clusVolumeName} !~ /$self->{option_results}->{filter_name}/) {
259 $self->{output}->output_add(long_msg => "skipping '" . $result->{clusVolumeName} . "': no matching filter.", debug => 1);
260 next;
261 }
262
263 $result->{clusVolumeStatsKbytesRead} *= 1024;
264 $result->{clusVolumeStatsKbytesWrite} *= 1024;
265 $self->{volume}->{$instance} = {
266 display => $result->{clusVolumeName},
267 total => $result->{clusVolumeSize} * 1024,
268 used => $result->{clusVolumeUsedSpace} * 1024,
269 %$result
270 };
271 }
272
273 if (scalar(keys %{$self->{volume}}) <= 0) {
274 $self->{output}->add_option_msg(short_msg => "No volume found.");
275 $self->{output}->option_exit();
276 }
277
278 $self->{cache_name} = "hp_lefthand_" . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' .
279 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
280 (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all'));
281 }
282
283 1;
284
285 __END__
286
287 =head1 MODE
288
289 Check volumes.
290
291 =over 8
292
293 =item B<--filter-counters>
294
295 Only display some counters (regexp can be used).
296 Example: --filter-counters='^read|write$'
297
298 =item B<--filter-name>
299
300 Filter volume name (can be a regexp).
301
302 =item B<--warning-replication-status>
303
304 Define the conditions to match for the status to be WARNING.
305 You can use the following variables: %{status}, %{display}
306
307 =item B<--critical-replication-status>
308
309 Define the conditions to match for the status to be CRITICAL (default: '%{status} !~ /normal/i').
310 You can use the following variables: %{status}, %{display}
311
312 =item B<--warning-*>
313
314 Warning threshold.
315 Can be: 'read' (b/s), 'write' (b/s), 'read-iops', 'write-iops',
316 'read-latency', 'write-latency', 'usage'.
317
318 =item B<--critical-*>
319
320 Critical threshold.
321 Can be: 'read' (b/s), 'write' (b/s), 'read-iops', 'write-iops',
322 'read-latency', 'write-latency', 'usage'.
323
324 =item B<--units>
325
326 Units of thresholds (default: '%') ('%', 'B').
327
328 =item B<--free>
329
330 Thresholds are on free space left.
331
332 =back
333
334 =cut