| 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 storage::qnap::snmp::mode::pools; |
| 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 sprintf( |
| 33 |
"status: %s", |
| 34 |
$self->{result_values}->{status} |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
sub custom_space_usage_output { |
| 39 |
my ($self, %options) = @_; |
| 40 |
|
| 41 |
my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}); |
| 42 |
my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}); |
| 43 |
my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); |
| 44 |
return sprintf( |
| 45 |
"space usage total: %s used: %s (%.2f%%) free: %s (%.2f%%)", |
| 46 |
$total_size_value . " " . $total_size_unit, |
| 47 |
$total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, |
| 48 |
$total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free} |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
sub pool_long_output { |
| 53 |
my ($self, %options) = @_; |
| 54 |
|
| 55 |
return sprintf( |
| 56 |
"checking pool '%s'", |
| 57 |
$options{instance_value}->{name} |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
sub prefix_pool_output { |
| 62 |
my ($self, %options) = @_; |
| 63 |
|
| 64 |
return sprintf( |
| 65 |
"pool '%s' ", |
| 66 |
$options{instance_value}->{name} |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
sub set_counters { |
| 71 |
my ($self, %options) = @_; |
| 72 |
|
| 73 |
$self->{maps_counters_type} = [ |
| 74 |
{ name => 'pools', type => 3, cb_prefix_output => 'prefix_pool_output', cb_long_output => 'pool_long_output', |
| 75 |
indent_long_output => ' ', message_multiple => 'All pools are ok', |
| 76 |
group => [ |
| 77 |
{ name => 'status', type => 0, skipped_code => { -10 => 1 } }, |
| 78 |
{ name => 'space', type => 0, skipped_code => { -10 => 1 } } |
| 79 |
] |
| 80 |
} |
| 81 |
]; |
| 82 |
|
| 83 |
$self->{maps_counters}->{status} = [ |
| 84 |
{ |
| 85 |
label => 'pool-status', |
| 86 |
type => 2, |
| 87 |
warning_default => '%{status} =~ /degraded|warning/i', |
| 88 |
critical_default => '%{status} =~ /error|critical/i', |
| 89 |
set => { |
| 90 |
key_values => [ |
| 91 |
{ name => 'status' }, { name => 'name' } |
| 92 |
], |
| 93 |
closure_custom_output => $self->can('custom_status_output'), |
| 94 |
closure_custom_perfdata => sub { return 0; }, |
| 95 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 96 |
} |
| 97 |
} |
| 98 |
]; |
| 99 |
|
| 100 |
$self->{maps_counters}->{space} = [ |
| 101 |
{ label => 'space-usage', nlabel => 'pool.space.usage.bytes', set => { |
| 102 |
key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'name' } ], |
| 103 |
closure_custom_output => $self->can('custom_space_usage_output'), |
| 104 |
perfdatas => [ |
| 105 |
{ template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'name' } |
| 106 |
] |
| 107 |
} |
| 108 |
}, |
| 109 |
{ label => 'space-usage-free', display_ok => 0, nlabel => 'pool.space.free.bytes', set => { |
| 110 |
key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'name' } ], |
| 111 |
closure_custom_output => $self->can('custom_space_usage_output'), |
| 112 |
perfdatas => [ |
| 113 |
{ template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'name' } |
| 114 |
] |
| 115 |
} |
| 116 |
}, |
| 117 |
{ label => 'space-usage-prct', display_ok => 0, nlabel => 'pool.space.usage.percentage', set => { |
| 118 |
key_values => [ { name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'prct_free' }, { name => 'total' }, { name => 'name' } ], |
| 119 |
closure_custom_output => $self->can('custom_space_usage_output'), |
| 120 |
perfdatas => [ |
| 121 |
{ template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'name' } |
| 122 |
] |
| 123 |
} |
| 124 |
} |
| 125 |
]; |
| 126 |
} |
| 127 |
|
| 128 |
sub new { |
| 129 |
my ($class, %options) = @_; |
| 130 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 131 |
bless $self, $class; |
| 132 |
|
| 133 |
$options{options}->add_options(arguments => { |
| 134 |
'filter-name:s' => { name => 'filter_name' } |
| 135 |
}); |
| 136 |
|
| 137 |
return $self; |
| 138 |
} |
| 139 |
|
| 140 |
sub convert_bytes { |
| 141 |
my ($self, %options) = @_; |
| 142 |
my $multiple = defined($options{network}) ? 1000 : 1024; |
| 143 |
my %units = (K => 1, M => 2, G => 3, T => 4); |
| 144 |
|
| 145 |
if ($options{value} !~ /^\s*([0-9\.\,]+)\s*(.)/) { |
| 146 |
$self->{output}->output_add( |
| 147 |
severity => 'UNKNOWN', |
| 148 |
output => "Cannot convert value '" . $options{value} . "'" |
| 149 |
); |
| 150 |
$self->{output}->display(); |
| 151 |
$self->{output}->exit(); |
| 152 |
} |
| 153 |
my ($bytes, $unit) = ($1, uc($2)); |
| 154 |
|
| 155 |
for (my $i = 0; $i < $units{$unit}; $i++) { |
| 156 |
$bytes *= $multiple; |
| 157 |
} |
| 158 |
|
| 159 |
return $bytes; |
| 160 |
} |
| 161 |
|
| 162 |
my $map_status = { |
| 163 |
0 => 'ready', |
| 164 |
-1 => 'warning', |
| 165 |
-2 => 'notReady', |
| 166 |
-3 => 'error' |
| 167 |
}; |
| 168 |
|
| 169 |
my $mapping = { |
| 170 |
ex => { |
| 171 |
name => { oid => '.1.3.6.1.4.1.24681.1.4.1.1.1.2.2.2.1.2' }, # poolID |
| 172 |
total => { oid => '.1.3.6.1.4.1.24681.1.4.1.1.1.2.2.2.1.3' }, # poolCapacity |
| 173 |
free => { oid => '.1.3.6.1.4.1.24681.1.4.1.1.1.2.2.2.1.4' }, # poolFreeSize |
| 174 |
status => { oid => '.1.3.6.1.4.1.24681.1.4.1.1.1.2.2.2.1.5', map => $map_status } # poolStatus |
| 175 |
}, |
| 176 |
es => { |
| 177 |
name => { oid => '.1.3.6.1.4.1.24681.2.2.23.1.2' }, # es-SysPoolID |
| 178 |
total => { oid => '.1.3.6.1.4.1.24681.2.2.23.1.3' }, # es-SysPoolCapacity |
| 179 |
free => { oid => '.1.3.6.1.4.1.24681.2.2.23.1.4' }, # es-SysPoolFreeSize |
| 180 |
status => { oid => '.1.3.6.1.4.1.24681.2.2.23.1.5' } # es-SysPoolStatus |
| 181 |
} |
| 182 |
}; |
| 183 |
|
| 184 |
sub check_pools { |
| 185 |
my ($self, %options) = @_; |
| 186 |
|
| 187 |
return 0 if (scalar(keys %{$options{snmp_result}}) <= 0); |
| 188 |
|
| 189 |
foreach (keys %{$options{snmp_result}}) { |
| 190 |
next if (! /^$mapping->{ $options{type} }->{name}->{oid}\.(.*)$/); |
| 191 |
my $instance = $1; |
| 192 |
my $result = $options{snmp}->map_instance(mapping => $mapping->{ $options{type} }, results => $options{snmp_result}, instance => $instance); |
| 193 |
|
| 194 |
if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && |
| 195 |
$result->{name} !~ /$self->{option_results}->{filter_name}/) { |
| 196 |
$self->{output}->output_add(long_msg => "skipping pool $result->{name}", debug => 1); |
| 197 |
next; |
| 198 |
} |
| 199 |
|
| 200 |
if (defined($options{convert})) { |
| 201 |
$result->{total} = $self->convert_bytes(value => $result->{total}); |
| 202 |
$result->{free} = $self->convert_bytes(value => $result->{free}); |
| 203 |
} |
| 204 |
|
| 205 |
$self->{pools}->{$instance} = { |
| 206 |
name => $result->{name}, |
| 207 |
status => { |
| 208 |
name => $result->{name}, |
| 209 |
status => $result->{status} |
| 210 |
} |
| 211 |
}; |
| 212 |
if (defined($result->{total}) && $result->{total} > 0) { |
| 213 |
$self->{pools}->{$instance}->{space} = { |
| 214 |
name => $result->{name}, |
| 215 |
total => $result->{total}, |
| 216 |
free => $result->{free}, |
| 217 |
used => $result->{total} - $result->{free}, |
| 218 |
prct_free => $result->{free} * 100 / $result->{total}, |
| 219 |
prct_used => ($result->{total} - $result->{free}) * 100 / $result->{total} |
| 220 |
}; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
return 1; |
| 225 |
} |
| 226 |
|
| 227 |
sub manage_selection { |
| 228 |
my ($self, %options) = @_; |
| 229 |
|
| 230 |
$self->{pools} = {}; |
| 231 |
|
| 232 |
my $snmp_result = $options{snmp}->get_table( |
| 233 |
oid => '.1.3.6.1.4.1.24681.1.4.1.1.1.2.2.2' # poolTable |
| 234 |
); |
| 235 |
return if ($self->check_pools(snmp => $options{snmp}, type => 'ex', snmp_result => $snmp_result)); |
| 236 |
|
| 237 |
$snmp_result = $options{snmp}->get_table( |
| 238 |
oid => '.1.3.6.1.4.1.24681.2.2.23', # es-SystemPoolTable |
| 239 |
nothing_quit => 1 |
| 240 |
); |
| 241 |
$self->check_pools(snmp => $options{snmp}, type => 'es', snmp_result => $snmp_result, convert => 1); |
| 242 |
} |
| 243 |
|
| 244 |
1; |
| 245 |
|
| 246 |
__END__ |
| 247 |
|
| 248 |
=head1 MODE |
| 249 |
|
| 250 |
Check pools. |
| 251 |
|
| 252 |
=over 8 |
| 253 |
|
| 254 |
=item B<--filter-name> |
| 255 |
|
| 256 |
Filter pools by name (can be a regexp). |
| 257 |
|
| 258 |
=item B<--unknown-pool-status> |
| 259 |
|
| 260 |
Define the conditions to match for the status to be UNKNOWN. |
| 261 |
You can use the following variables: %{status}, %{name} |
| 262 |
|
| 263 |
=item B<--warning-pool-status> |
| 264 |
|
| 265 |
Define the conditions to match for the status to be WARNING (default: '%{status} =~ /degraded|warning/i'). |
| 266 |
You can use the following variables: %{status}, %{name} |
| 267 |
|
| 268 |
=item B<--critical-pool-status> |
| 269 |
|
| 270 |
Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /error|critical/i'). |
| 271 |
You can use the following variables: %{status}, %{name} |
| 272 |
|
| 273 |
=item B<--warning-*> B<--critical-*> |
| 274 |
|
| 275 |
Thresholds. |
| 276 |
Can be: 'space-usage-prct', 'space-usage', 'space-usage-free'. |
| 277 |
|
| 278 |
=back |
| 279 |
|
| 280 |
=cut |
| 281 |
|