| 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::snmp::mode::storage; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
|
| 28 |
sub custom_usage_perfdata { |
| 29 |
my ($self, %options) = @_; |
| 30 |
|
| 31 |
my $label = 'used'; |
| 32 |
my $value_perf = $self->{result_values}->{used}; |
| 33 |
if (defined($self->{instance_mode}->{option_results}->{free})) { |
| 34 |
$label = 'free'; |
| 35 |
$value_perf = $self->{result_values}->{free}; |
| 36 |
} |
| 37 |
|
| 38 |
my %total_options = (); |
| 39 |
if ($self->{instance_mode}->{option_results}->{units} eq '%') { |
| 40 |
$total_options{total} = $self->{result_values}->{total}; |
| 41 |
$total_options{cast_int} = 1; |
| 42 |
} |
| 43 |
|
| 44 |
$self->{output}->perfdata_add( |
| 45 |
label => $label, unit => 'B', |
| 46 |
instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef, |
| 47 |
value => $value_perf, |
| 48 |
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options), |
| 49 |
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options), |
| 50 |
min => 0, max => $self->{result_values}->{total} |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
sub custom_usage_threshold { |
| 55 |
my ($self, %options) = @_; |
| 56 |
|
| 57 |
my ($exit, $threshold_value); |
| 58 |
$threshold_value = $self->{result_values}->{used}; |
| 59 |
$threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free})); |
| 60 |
if ($self->{instance_mode}->{option_results}->{units} eq '%') { |
| 61 |
$threshold_value = $self->{result_values}->{prct_used}; |
| 62 |
$threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free})); |
| 63 |
} |
| 64 |
$exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } ]); |
| 65 |
return $exit; |
| 66 |
} |
| 67 |
|
| 68 |
sub custom_usage_output { |
| 69 |
my ($self, %options) = @_; |
| 70 |
|
| 71 |
my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}); |
| 72 |
my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}); |
| 73 |
my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); |
| 74 |
my $msg = sprintf("Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", |
| 75 |
$total_size_value . " " . $total_size_unit, |
| 76 |
$total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, |
| 77 |
$total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}); |
| 78 |
return $msg; |
| 79 |
} |
| 80 |
|
| 81 |
sub custom_usage_calc { |
| 82 |
my ($self, %options) = @_; |
| 83 |
|
| 84 |
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; |
| 85 |
$self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'}; |
| 86 |
$self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'}; |
| 87 |
$self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used}; |
| 88 |
$self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total}; |
| 89 |
$self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used}; |
| 90 |
|
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
sub set_counters { |
| 95 |
my ($self, %options) = @_; |
| 96 |
|
| 97 |
$self->{maps_counters_type} = [ |
| 98 |
{ name => 'storage', type => 1, cb_prefix_output => 'prefix_storage_output', message_multiple => 'All storages are ok' } |
| 99 |
]; |
| 100 |
|
| 101 |
$self->{maps_counters}->{storage} = [ |
| 102 |
{ label => 'usage', set => { |
| 103 |
key_values => [ { name => 'display' }, { name => 'used' }, { name => 'total' } ], |
| 104 |
closure_custom_calc => $self->can('custom_usage_calc'), |
| 105 |
closure_custom_output => $self->can('custom_usage_output'), |
| 106 |
closure_custom_perfdata => $self->can('custom_usage_perfdata'), |
| 107 |
closure_custom_threshold_check => $self->can('custom_usage_threshold'), |
| 108 |
} |
| 109 |
}, |
| 110 |
]; |
| 111 |
} |
| 112 |
|
| 113 |
sub new { |
| 114 |
my ($class, %options) = @_; |
| 115 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 116 |
bless $self, $class; |
| 117 |
|
| 118 |
$options{options}->add_options(arguments => { |
| 119 |
"filter-name:s" => { name => 'filter_name' }, |
| 120 |
"units:s" => { name => 'units', default => '%' }, |
| 121 |
"free" => { name => 'free' }, |
| 122 |
}); |
| 123 |
|
| 124 |
return $self; |
| 125 |
} |
| 126 |
|
| 127 |
sub prefix_storage_output { |
| 128 |
my ($self, %options) = @_; |
| 129 |
|
| 130 |
return "Storage '" . $options{instance_value}->{display} . "' "; |
| 131 |
} |
| 132 |
|
| 133 |
my $mapping = { |
| 134 |
fileSystemName => { oid => '.1.3.6.1.4.1.11.2.3.1.2.2.1.3' }, |
| 135 |
fileSystemBlock => { oid => '.1.3.6.1.4.1.11.2.3.1.2.2.1.4' }, |
| 136 |
fileSystemBfree => { oid => '.1.3.6.1.4.1.11.2.3.1.2.2.1.5' }, |
| 137 |
fileSystemBavail => { oid => '.1.3.6.1.4.1.11.2.3.1.2.2.1.6' }, |
| 138 |
fileSystemBsize => { oid => '.1.3.6.1.4.1.11.2.3.1.2.2.1.7' }, |
| 139 |
fileSystemDir => { oid => '.1.3.6.1.4.1.11.2.3.1.2.2.1.10' }, |
| 140 |
}; |
| 141 |
my $oid_fileSystemEntry = '.1.3.6.1.4.1.11.2.3.1.2.2.1'; |
| 142 |
|
| 143 |
sub manage_selection { |
| 144 |
my ($self, %options) = @_; |
| 145 |
|
| 146 |
$self->{storage} = {}; |
| 147 |
$self->{results} = $options{snmp}->get_table(oid => $oid_fileSystemEntry, |
| 148 |
nothing_quit => 1); |
| 149 |
foreach my $oid (keys %{$self->{results}}) { |
| 150 |
next if ($oid !~ /^$mapping->{fileSystemDir}->{oid}\.(.*)$/); |
| 151 |
my $instance = $1; |
| 152 |
my $result = $options{snmp}->map_instance(mapping => $mapping, results => $self->{results}, instance => $instance); |
| 153 |
if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && |
| 154 |
$result->{fileSystemDir} !~ /$self->{option_results}->{filter_name}/) { |
| 155 |
$self->{output}->output_add(long_msg => "skipping '" . $result->{fileSystemDir} . "': no matching filter.", debug => 1); |
| 156 |
next; |
| 157 |
} |
| 158 |
|
| 159 |
$self->{storage}->{$instance} = { display => $result->{fileSystemDir}, |
| 160 |
total => $result->{fileSystemBlock} * $result->{fileSystemBsize}, |
| 161 |
used => $result->{fileSystemBlock} * $result->{fileSystemBsize} - $result->{fileSystemBavail} * $result->{fileSystemBsize} }; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
if (scalar(keys %{$self->{storage}}) <= 0) { |
| 166 |
$self->{output}->add_option_msg(short_msg => "No storage found."); |
| 167 |
$self->{output}->option_exit(); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
1; |
| 172 |
|
| 173 |
__END__ |
| 174 |
|
| 175 |
=head1 MODE |
| 176 |
|
| 177 |
Check storages. |
| 178 |
|
| 179 |
=over 8 |
| 180 |
|
| 181 |
=item B<--filter-name> |
| 182 |
|
| 183 |
Filter path name (can be a regexp). |
| 184 |
|
| 185 |
=item B<--warning-*> |
| 186 |
|
| 187 |
Warning threshold. |
| 188 |
Can be: 'usage'. |
| 189 |
|
| 190 |
=item B<--critical-*> |
| 191 |
|
| 192 |
Critical threshold. |
| 193 |
Can be: 'usage'. |
| 194 |
|
| 195 |
=item B<--units> |
| 196 |
|
| 197 |
Units of thresholds (default: '%') ('%', 'B'). |
| 198 |
|
| 199 |
=item B<--free> |
| 200 |
|
| 201 |
Thresholds are on free space left. |
| 202 |
|
| 203 |
=back |
| 204 |
|
| 205 |
=cut |