Debian Codegraph @

src/os/hpux/local/mode/storage.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 os::hpux::local::mode::storage;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::misc;
28
29 sub custom_usage_perfdata {
30 my ($self, %options) = @_;
31
32 my $label = 'used';
33 my $value_perf = $self->{result_values}->{used};
34 if (defined($self->{instance_mode}->{option_results}->{free})) {
35 $label = 'free';
36 $value_perf = $self->{result_values}->{free};
37 }
38
39 my %total_options = ();
40 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
41 $total_options{total} = $self->{result_values}->{total};
42 $total_options{cast_int} = 1;
43 }
44
45 $self->{output}->perfdata_add(
46 label => $label,
47 unit => 'B',
48 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
49 value => $value_perf,
50 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
51 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
52 min => 0, max => $self->{result_values}->{total}
53 );
54 }
55
56 sub custom_usage_threshold {
57 my ($self, %options) = @_;
58
59 my ($exit, $threshold_value);
60 $threshold_value = $self->{result_values}->{used};
61 $threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free}));
62 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
63 $threshold_value = $self->{result_values}->{prct_used};
64 $threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free}));
65 }
66 $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } ]);
67 return $exit;
68 }
69
70 sub custom_usage_output {
71 my ($self, %options) = @_;
72
73 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
74 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
75 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
76 return sprintf(
77 "Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
78 $total_size_value . " " . $total_size_unit,
79 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
80 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
81 );
82 }
83
84 sub custom_usage_calc {
85 my ($self, %options) = @_;
86
87 if ($options{new_datas}->{$self->{instance} . '_total'} == 0) {
88 $self->{error_msg} = "total size is 0";
89 return -2;
90 }
91 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
92 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
93 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'};
94 $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
95 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
96 $self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used};
97
98 return 0;
99 }
100
101 sub prefix_disks_output {
102 my ($self, %options) = @_;
103
104 return "Storage '" . $options{instance_value}->{display} . "' ";
105 }
106
107 sub set_counters {
108 my ($self, %options) = @_;
109
110 $self->{maps_counters_type} = [
111 { name => 'disks', type => 1, cb_prefix_output => 'prefix_disks_output', message_multiple => 'All storages are ok' }
112 ];
113
114 $self->{maps_counters}->{disks} = [
115 { label => 'usage', set => {
116 key_values => [ { name => 'display' }, { name => 'used' }, { name => 'total' } ],
117 closure_custom_calc => $self->can('custom_usage_calc'),
118 closure_custom_output => $self->can('custom_usage_output'),
119 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
120 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
121 }
122 },
123 ];
124 }
125
126 sub new {
127 my ($class, %options) = @_;
128 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
129 bless $self, $class;
130
131 $options{options}->add_options(arguments => {
132 'filter-fs:s' => { name => 'filter_fs', },
133 'units:s' => { name => 'units', default => '%' },
134 'free' => { name => 'free' },
135 'name:s' => { name => 'name' },
136 'regexp' => { name => 'use_regexp' },
137 'regexp-isensitive' => { name => 'use_regexpi' }, # compatibility
138 'regexp-insensitive' => { name => 'use_regexpi' },
139 'space-reservation:s' => { name => 'space_reservation' }
140 });
141
142 return $self;
143 }
144
145 sub manage_selection {
146 my ($self, %options) = @_;
147
148 my ($stdout, $exit_code) = $options{custom}->execute_command(
149 command => 'bdf',
150 command_options => '2>&1',
151 no_quit => 1
152 );
153
154 $self->{disks} = {};
155 my @lines = split /\n/, $stdout;
156 # Header not needed
157 shift @lines;
158 while (my $line = shift @lines) {
159 # When the line is too long, the FS name is printed on a separated line
160 if ($line !~ /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) {
161 $line .= " " . shift(@lines);
162 }
163 next if ($line !~ /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/);
164 my ($fs, $size, $used, $available, $percent, $mount) = ($1, $2, $3, $4, $5, $6);
165
166 next if (defined($self->{option_results}->{filter_fs}) && $self->{option_results}->{filter_fs} ne '' &&
167 $fs !~ /$self->{option_results}->{filter_fs}/);
168
169 next if (defined($self->{option_results}->{name}) && defined($self->{option_results}->{use_regexp}) && defined($self->{option_results}->{use_regexpi})
170 && $mount !~ /$self->{option_results}->{name}/i);
171 next if (defined($self->{option_results}->{name}) && defined($self->{option_results}->{use_regexp}) && !defined($self->{option_results}->{use_regexpi})
172 && $mount !~ /$self->{option_results}->{name}/);
173 next if (defined($self->{option_results}->{name}) && !defined($self->{option_results}->{use_regexp}) && !defined($self->{option_results}->{use_regexpi})
174 && $mount ne $self->{option_results}->{name});
175
176 $size *= 1024;
177 if (defined($self->{option_results}->{space_reservation})) {
178 $size = int($size - ($self->{option_results}->{space_reservation} * $size / 100));
179 }
180 $self->{disks}->{$mount} = { display => $mount, fs => $fs, total => $size, used => $used * 1024 };
181 }
182
183 if (scalar(keys %{$self->{disks}}) <= 0) {
184 if ($exit_code != 0) {
185 $self->{output}->output_add(long_msg => "command output:" . $stdout);
186 }
187 $self->{output}->add_option_msg(short_msg => "No storage found (filters or command issue)");
188 $self->{output}->option_exit();
189 }
190 }
191
192 1;
193
194 __END__
195
196 =head1 MODE
197
198 Check storage usages.
199
200 Command used: bdf 2>&1
201
202 =over 8
203
204 =item B<--warning-usage>
205
206 Warning threshold.
207
208 =item B<--critical-usage>
209
210 Critical threshold.
211
212 =item B<--units>
213
214 Units of thresholds (default: '%') ('%', 'B').
215
216 =item B<--free>
217
218 Thresholds are on free space left.
219
220 =item B<--name>
221
222 Set the storage mount point (empty means 'check all storages')
223
224 =item B<--regexp>
225
226 Allows to use regexp to filter storage mount point (with option --name).
227
228 =item B<--regexp-insensitive>
229
230 Allows to use regexp non case-sensitive (with --regexp).
231
232 =item B<--filter-fs>
233
234 Filter filesystem (regexp can be used).
235
236 =item B<--space-reservation>
237
238 Some filesystem has space reserved (like ext4 for root).
239 The value is in percent of total (default: none).
240
241 =back
242
243 =cut