| 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 centreon::common::bluearc::snmp::mode::volumeusage; |
| 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 $nlabel = 'volume.space.usage.bytes'; |
| 39 |
my $value_perf = $self->{result_values}->{used}; |
| 40 |
if (defined($self->{instance_mode}->{option_results}->{free})) { |
| 41 |
$nlabel = 'volume.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 => $nlabel, |
| 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, max => $self->{result_values}->{total} |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
sub custom_usage_threshold { |
| 63 |
my ($self, %options) = @_; |
| 64 |
|
| 65 |
my ($exit, $threshold_value); |
| 66 |
$threshold_value = $self->{result_values}->{used}; |
| 67 |
$threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free})); |
| 68 |
if ($self->{instance_mode}->{option_results}->{units} eq '%') { |
| 69 |
$threshold_value = $self->{result_values}->{prct_used}; |
| 70 |
$threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free})); |
| 71 |
} |
| 72 |
$exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } ]); |
| 73 |
return $exit; |
| 74 |
} |
| 75 |
|
| 76 |
sub custom_usage_output { |
| 77 |
my ($self, %options) = @_; |
| 78 |
|
| 79 |
my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}); |
| 80 |
my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}); |
| 81 |
my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); |
| 82 |
return sprintf( |
| 83 |
"usage total: %s used: %s (%.2f%%) free: %s (%.2f%%)", |
| 84 |
$total_size_value . " " . $total_size_unit, |
| 85 |
$total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, |
| 86 |
$total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free} |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
sub custom_usage_calc { |
| 91 |
my ($self, %options) = @_; |
| 92 |
|
| 93 |
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; |
| 94 |
$self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'}; |
| 95 |
$self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'}; |
| 96 |
$self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used}; |
| 97 |
$self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total}; |
| 98 |
$self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used}; |
| 99 |
|
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
sub prefix_volume_output { |
| 104 |
my ($self, %options) = @_; |
| 105 |
|
| 106 |
return "Volume '" . $options{instance_value}->{display} . "' "; |
| 107 |
} |
| 108 |
|
| 109 |
sub set_counters { |
| 110 |
my ($self, %options) = @_; |
| 111 |
|
| 112 |
$self->{maps_counters_type} = [ |
| 113 |
{ name => 'volume', type => 1, cb_prefix_output => 'prefix_volume_output', message_multiple => 'All volumes are ok' } |
| 114 |
]; |
| 115 |
|
| 116 |
$self->{maps_counters}->{volume} = [ |
| 117 |
{ label => 'status', type => 2, warning_default => '%{status} =~ /needsChecking/i', set => { |
| 118 |
key_values => [ { name => 'status' }, { name => 'display' } ], |
| 119 |
closure_custom_output => $self->can('custom_status_output'), |
| 120 |
closure_custom_perfdata => sub { return 0; }, |
| 121 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 122 |
} |
| 123 |
}, |
| 124 |
{ label => 'usage', set => { |
| 125 |
key_values => [ { name => 'display' }, { name => 'used' }, { name => 'total' } ], |
| 126 |
closure_custom_calc => $self->can('custom_usage_calc'), |
| 127 |
closure_custom_output => $self->can('custom_usage_output'), |
| 128 |
closure_custom_perfdata => $self->can('custom_usage_perfdata'), |
| 129 |
closure_custom_threshold_check => $self->can('custom_usage_threshold') |
| 130 |
} |
| 131 |
} |
| 132 |
]; |
| 133 |
} |
| 134 |
|
| 135 |
sub new { |
| 136 |
my ($class, %options) = @_; |
| 137 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 138 |
bless $self, $class; |
| 139 |
|
| 140 |
$options{options}->add_options(arguments => { |
| 141 |
'filter-name:s' => { name => 'filter_name' }, |
| 142 |
'units:s' => { name => 'units', default => '%' }, |
| 143 |
'free' => { name => 'free' } |
| 144 |
}); |
| 145 |
|
| 146 |
return $self; |
| 147 |
} |
| 148 |
|
| 149 |
my %map_volume_status = ( |
| 150 |
1 => 'unformatted', |
| 151 |
2 => 'mounted', |
| 152 |
3 => 'formatted', |
| 153 |
4 => 'needsChecking' |
| 154 |
); |
| 155 |
|
| 156 |
my $mapping = { |
| 157 |
volumeLabel => { oid => '.1.3.6.1.4.1.11096.6.1.1.1.3.5.2.1.3' }, |
| 158 |
volumeStatus => { oid => '.1.3.6.1.4.1.11096.6.1.1.1.3.5.2.1.4', map => \%map_volume_status }, |
| 159 |
volumeCapacity => { oid => '.1.3.6.1.4.1.11096.6.1.1.1.3.5.2.1.5' }, |
| 160 |
volumeFreeCapacity => { oid => '.1.3.6.1.4.1.11096.6.1.1.1.3.5.2.1.6' } |
| 161 |
}; |
| 162 |
my $oid_volumeEntry = '.1.3.6.1.4.1.11096.6.1.1.1.3.5.2.1'; |
| 163 |
|
| 164 |
sub manage_selection { |
| 165 |
my ($self, %options) = @_; |
| 166 |
|
| 167 |
if ($options{snmp}->is_snmpv1()) { |
| 168 |
$self->{output}->add_option_msg(short_msg => "Need to use SNMP v2c or v3."); |
| 169 |
$self->{output}->option_exit(); |
| 170 |
} |
| 171 |
|
| 172 |
$self->{results} = $options{snmp}->get_table( |
| 173 |
oid => $oid_volumeEntry, |
| 174 |
nothing_quit => 1 |
| 175 |
); |
| 176 |
|
| 177 |
$self->{volume} = {}; |
| 178 |
foreach my $oid (keys %{$self->{results}}) { |
| 179 |
next if ($oid !~ /^$mapping->{volumeStatus}->{oid}\.(.*)$/); |
| 180 |
my $instance = $1; |
| 181 |
my $result = $options{snmp}->map_instance(mapping => $mapping, results => $self->{results}, instance => $instance); |
| 182 |
if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && |
| 183 |
$result->{volumeLabel} !~ /$self->{option_results}->{filter_name}/) { |
| 184 |
$self->{output}->output_add(long_msg => "skipping '" . $result->{volumeLabel} . "': no matching filter.", debug => 1); |
| 185 |
next; |
| 186 |
} |
| 187 |
|
| 188 |
$self->{volume}->{$instance} = { |
| 189 |
display => $result->{volumeLabel}, |
| 190 |
status => $result->{volumeStatus}, |
| 191 |
total => $result->{volumeCapacity}, |
| 192 |
used => $result->{volumeCapacity} - $result->{volumeFreeCapacity} |
| 193 |
}; |
| 194 |
} |
| 195 |
|
| 196 |
if (scalar(keys %{$self->{volume}}) <= 0) { |
| 197 |
$self->{output}->add_option_msg(short_msg => "No volume found."); |
| 198 |
$self->{output}->option_exit(); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
1; |
| 203 |
|
| 204 |
__END__ |
| 205 |
|
| 206 |
=head1 MODE |
| 207 |
|
| 208 |
Check volume usages. |
| 209 |
|
| 210 |
=over 8 |
| 211 |
|
| 212 |
=item B<--filter-counters> |
| 213 |
|
| 214 |
Only display some counters (regexp can be used). |
| 215 |
Example: --filter-counters='^status$' |
| 216 |
|
| 217 |
=item B<--filter-name> |
| 218 |
|
| 219 |
Filter volume name (can be a regexp). |
| 220 |
|
| 221 |
=item B<--warning-status> |
| 222 |
|
| 223 |
Define the conditions to match for the status to be WARNING (default: '%{status} =~ /needsChecking/i'). |
| 224 |
You can use the following variables: %{status}, %{display} |
| 225 |
|
| 226 |
=item B<--critical-status> |
| 227 |
|
| 228 |
Define the conditions to match for the status to be CRITICAL (default: -). |
| 229 |
You can use the following variables: %{status}, %{display} |
| 230 |
|
| 231 |
=item B<--warning-*> B<--critical-*> |
| 232 |
|
| 233 |
Thresholds. |
| 234 |
Can be: 'usage'. |
| 235 |
|
| 236 |
=item B<--units> |
| 237 |
|
| 238 |
Units of thresholds (default: '%') ('%', 'B'). |
| 239 |
|
| 240 |
=item B<--free> |
| 241 |
|
| 242 |
Thresholds are on free space left. |
| 243 |
|
| 244 |
=back |
| 245 |
|
| 246 |
=cut |