Debian Codegraph @

src/network/stonesoft/snmp/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 network::stonesoft::snmp::mode::storage;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27
28 my $oid_fwMountPointName = '.1.3.6.1.4.1.1369.5.2.1.11.3.1.3';
29 my $oid_fwPartitionSize = '.1.3.6.1.4.1.1369.5.2.1.11.3.1.4'; # in kB
30 my $oid_fwPartitionUsed = '.1.3.6.1.4.1.1369.5.2.1.11.3.1.5'; # in kB
31
32 sub new {
33 my ($class, %options) = @_;
34 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
35 bless $self, $class;
36
37 $options{options}->add_options(arguments => {
38 'warning:s' => { name => 'warning' },
39 'critical:s' => { name => 'critical' },
40 'units:s' => { name => 'units', default => '%' },
41 'free' => { name => 'free' },
42 'storage:s' => { name => 'storage' },
43 'regexp' => { name => 'use_regexp' },
44 'regexp-isensitive' => { name => 'use_regexpi' }, # compatibility
45 'regexp-insensitive' => { name => 'use_regexpi' }
46 });
47
48 $self->{storage_id_selected} = [];
49
50 return $self;
51 }
52
53 sub check_options {
54 my ($self, %options) = @_;
55 $self->SUPER::init(%options);
56
57 if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) {
58 $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'.");
59 $self->{output}->option_exit();
60 }
61 if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) {
62 $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'.");
63 $self->{output}->option_exit();
64 }
65 }
66
67 sub run {
68 my ($self, %options) = @_;
69 $self->{snmp} = $options{snmp};
70
71 $self->manage_selection();
72 my $num_disk_check = 0;
73 foreach (sort @{$self->{storage_id_selected}}) {
74 my $name_storage = $self->get_display_value(id => $_);
75 my $total_size = ($self->{results}->{$oid_fwPartitionSize}->{$oid_fwPartitionSize . "." . $_}) * 1024;
76 if ($total_size == 0) {
77 $self->{output}->output_add(long_msg => sprintf("Skipping partition '%s' (total size is 0)", $name_storage));
78 next;
79 }
80
81 $num_disk_check++;
82 my $total_used = ($self->{results}->{$oid_fwPartitionUsed}->{$oid_fwPartitionUsed . "." . $_}) * 1024;
83 my $total_free = $total_size - $total_used;
84 my $prct_used = $total_used * 100 / $total_size;
85 my $prct_free = 100 - $prct_used;
86
87 my ($exit, $threshold_value);
88
89 $threshold_value = $total_used;
90 $threshold_value = $total_free if (defined($self->{option_results}->{free}));
91 if ($self->{option_results}->{units} eq '%') {
92 $threshold_value = $prct_used;
93 $threshold_value = $prct_free if (defined($self->{option_results}->{free}));
94 }
95 $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]);
96
97 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $total_size);
98 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $total_used);
99 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => ($total_size - $total_used));
100
101 $self->{output}->output_add(
102 long_msg => sprintf(
103 "Partition '%s' Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", $name_storage,
104 $total_size_value . " " . $total_size_unit,
105 $total_used_value . " " . $total_used_unit, $prct_used,
106 $total_free_value . " " . $total_free_unit, $prct_free
107 )
108 );
109 if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1) || (defined($self->{option_results}->{storage}) && !defined($self->{option_results}->{use_regexp}))) {
110 $self->{output}->output_add(
111 severity => $exit,
112 short_msg => sprintf(
113 "Partition '%s' Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", $name_storage,
114 $total_size_value . " " . $total_size_unit,
115 $total_used_value . " " . $total_used_unit, $prct_used,
116 $total_free_value . " " . $total_free_unit, $prct_free
117 )
118 );
119 }
120
121 my $label = 'used';
122 my $value_perf = $total_used;
123 if (defined($self->{option_results}->{free})) {
124 $label = 'free';
125 $value_perf = $total_free;
126 }
127 my $extra_label = '';
128 $extra_label = '_' . $name_storage if (!defined($self->{option_results}->{storage}) || defined($self->{option_results}->{use_regexp}));
129 my %total_options = ();
130 if ($self->{option_results}->{units} eq '%') {
131 $total_options{total} = $total_size;
132 $total_options{cast_int} = 1;
133 }
134
135 $self->{output}->perfdata_add(
136 label => $label . $extra_label,
137 unit => 'B',
138 value => $value_perf,
139 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning', %total_options),
140 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical', %total_options),
141 min => 0, max => $total_size
142 );
143 }
144
145 if (!defined($self->{option_results}->{storage}) || defined($self->{option_results}->{use_regexp})) {
146 $self->{output}->output_add(
147 severity => 'OK',
148 short_msg => 'All partitions are ok.'
149 );
150 } elsif ($num_disk_check == 0) {
151 $self->{output}->output_add(
152 severity => 'OK',
153 short_msg => 'No usage for partition.'
154 );
155 }
156
157 $self->{output}->display();
158 $self->{output}->exit();
159 }
160
161 sub manage_selection {
162 my ($self, %options) = @_;
163
164 $self->{results} = $self->{snmp}->get_multiple_table(
165 oids => [
166 { oid => $oid_fwMountPointName },
167 { oid => $oid_fwPartitionSize },
168 { oid => $oid_fwPartitionUsed }
169 ],
170 nothing_quit => 1
171 );
172
173 foreach my $key ($self->{snmp}->oid_lex_sort(keys %{$self->{results}->{$oid_fwMountPointName}})) {
174 $key =~ /\.([0-9]+)$/;
175 my $instance = $1;
176 my $filter_name = $self->{output}->decode($self->{results}->{$oid_fwMountPointName}->{$key});
177 next if (!defined($filter_name));
178 if (!defined($self->{option_results}->{storage})) {
179 push @{$self->{storage_id_selected}}, $instance;
180 next;
181 }
182 if (defined($self->{option_results}->{use_regexp}) && defined($self->{option_results}->{use_regexpi}) && $filter_name =~ /$self->{option_results}->{storage}/i) {
183 push @{$self->{storage_id_selected}}, $instance;
184 }
185 if (defined($self->{option_results}->{use_regexp}) && !defined($self->{option_results}->{use_regexpi}) && $filter_name =~ /$self->{option_results}->{storage}/) {
186 push @{$self->{storage_id_selected}}, $instance;
187 }
188 if (!defined($self->{option_results}->{use_regexp}) && !defined($self->{option_results}->{use_regexpi}) && $filter_name eq $self->{option_results}->{storage}) {
189 push @{$self->{storage_id_selected}}, $instance;
190 }
191 }
192
193 if (scalar(@{$self->{storage_id_selected}}) <= 0) {
194 if (defined($self->{option_results}->{storage})) {
195 $self->{output}->add_option_msg(short_msg => "No storage found for name '" . $self->{option_results}->{storage} . "'.");
196 } else {
197 $self->{output}->add_option_msg(short_msg => "No storage found.");
198 }
199 $self->{output}->option_exit();
200 }
201 }
202
203 sub get_display_value {
204 my ($self, %options) = @_;
205 my $value = $self->{results}->{$oid_fwMountPointName}->{$oid_fwMountPointName . '.' . $options{id}};
206
207 return $value;
208 }
209
210 1;
211
212 __END__
213
214 =head1 MODE
215
216 Check usage on partitions.
217
218 =over 8
219
220 =item B<--warning>
221
222 Warning threshold.
223
224 =item B<--critical>
225
226 Critical threshold.
227
228 =item B<--units>
229
230 Units of thresholds (default: '%') ('%', 'B').
231
232 =item B<--free>
233
234 Thresholds are on free space left.
235
236 =item B<--storage>
237
238 Set the storage name (empty means 'check all storage').
239
240 =item B<--regexp>
241
242 Allows to use regexp to filter storage.
243
244 =item B<--regexp-insensitive>
245
246 Allows to use regexp non case-sensitive (with --regexp).
247
248 =back
249
250 =cut