Debian Codegraph @

src/os/aix/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::aix::local::mode::storage;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27
28 sub custom_usage_output {
29 my ($self, %options) = @_;
30
31 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total_space});
32 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used_space});
33 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free_space});
34 return sprintf(
35 'space usage total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
36 $total_size_value . " " . $total_size_unit,
37 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used_space},
38 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free_space}
39 );
40 }
41
42 sub prefix_storage_output {
43 my ($self, %options) = @_;
44
45 return "Storage '" . $options{instance_value}->{display} . "' ";
46 }
47
48 sub set_counters {
49 my ($self, %options) = @_;
50
51 $self->{maps_counters_type} = [
52 { name => 'storages', type => 1, cb_prefix_output => 'prefix_storage_output', message_multiple => 'All storages are ok', skipped_code => { -10 => 1 } }
53 ];
54
55 $self->{maps_counters}->{storages} = [
56 { label => 'usage', nlabel => 'storage.space.usage.bytes', set => {
57 key_values => [
58 { name => 'used_space' }, { name => 'free_space' },
59 { name => 'prct_used_space' }, { name => 'prct_free_space' },
60 { name => 'total_space' }
61 ],
62 closure_custom_output => $self->can('custom_usage_output'),
63 perfdatas => [
64 { template => '%d', min => 0, max => 'total_space',
65 unit => 'B', cast_int => 1, label_extra_instance => 1 }
66 ]
67 }
68 },
69 { label => 'usage-free', nlabel => 'storage.space.free.bytes', display_ok => 0, set => {
70 key_values => [
71 { name => 'free_space' }, { name => 'used_space' },
72 { name => 'prct_used_space' }, { name => 'prct_free_space' },
73 { name => 'total_space' }
74 ],
75 closure_custom_output => $self->can('custom_usage_output'),
76 perfdatas => [
77 { value => 'free_space', template => '%d', min => 0, max => 'total_space',
78 unit => 'B', cast_int => 1, label_extra_instance => 1 }
79 ]
80 }
81 },
82 { label => 'usage-prct', nlabel => 'storageresource.space.usage.percentage', display_ok => 0, set => {
83 key_values => [ { name => 'prct_used_space' } ],
84 output_template => 'used: %.2f %%',
85 perfdatas => [
86 { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 }
87 ]
88 }
89 }
90 ];
91 }
92
93 sub new {
94 my ($class, %options) = @_;
95 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
96 bless $self, $class;
97
98 $options{options}->add_options(arguments => {
99 'filter-fs:s' => { name => 'filter_fs' },
100 'filter-mount:s' => { name => 'filter_mount' },
101 'space-reservation:s' => { name => 'space_reservation' }
102 });
103
104 return $self;
105 }
106
107 sub manage_selection {
108 my ($self, %options) = @_;
109
110 my ($stdout) = $options{custom}->execute_command(
111 command => 'df',
112 command_options => '-P -k 2>&1'
113 );
114
115 $self->{storages} = {};
116 my @lines = split(/\n/, $stdout);
117 # Header not needed
118 shift @lines;
119 foreach my $line (@lines) {
120 next if ($line !~ /^(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(.*)/);
121 my ($fs, $size, $used, $available, $percent, $mount) = ($1, $2, $3, $4, $5, $6);
122
123 next if (defined($self->{option_results}->{filter_fs}) && $self->{option_results}->{filter_fs} ne '' &&
124 $fs !~ /$self->{option_results}->{filter_fs}/);
125 next if (defined($self->{option_results}->{filter_mount}) && $self->{option_results}->{filter_mount} ne '' &&
126 $mount !~ /$self->{option_results}->{filter_mount}/);
127
128 next if ($size == 0);
129
130 $size = $size * 1024;
131 my $reserved_value = 0;
132 if (defined($self->{option_results}->{space_reservation})) {
133 $reserved_value = $self->{option_results}->{space_reservation} * $size / 100;
134 }
135 my $used_space = $used * 1024;
136 my $free_space = $size - $used_space - $reserved_value;
137 my $prct_used_space = $used_space * 100 / ($size - $reserved_value);
138 my $prct_free_space = 100 - $prct_used_space;
139 # limit to 100. Better output.
140 if ($prct_used_space > 100) {
141 $free_space = 0;
142 $prct_used_space = 100;
143 $prct_free_space = 0;
144 }
145
146 $self->{storages}->{$mount} = {
147 display => $mount,
148 total_space => $size,
149 used_space => $used_space,
150 free_space => $free_space,
151 prct_used_space => $prct_used_space,
152 prct_free_space => $prct_free_space
153 };
154 }
155
156 if (scalar(keys %{$self->{storages}}) <= 0) {
157 $self->{output}->add_option_msg(short_msg => "No storage found.");
158 $self->{output}->option_exit();
159 }
160 }
161
162 1;
163
164 __END__
165
166 =head1 MODE
167
168 Check storage usages.
169 Command used: df -P -k 2>&1
170
171 =over 8
172
173 =item B<--filter-fs>
174
175 Filter filesystem (regexp can be used).
176
177 =item B<--filter-mount>
178
179 Filter mountpoint (regexp can be used).
180
181 =item B<--space-reservation>
182
183 Some filesystem has space reserved (like ext4 for root).
184 The value is in percent of total (default: none).
185
186 =item B<--warning-*> B<--critical-*>
187
188 Thresholds.
189 Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%).
190
191 =back
192
193 =cut