| 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::hp::storeonce::3::restapi::mode::clusterusage; |
| 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}->{health}; |
| 33 |
} |
| 34 |
|
| 35 |
sub custom_usage_perfdata { |
| 36 |
my ($self, %options) = @_; |
| 37 |
|
| 38 |
my $nlabel = 'cluster.space.usage.bytes'; |
| 39 |
my $value_perf = $self->{result_values}->{used}; |
| 40 |
if (defined($self->{instance_mode}->{option_results}->{free})) { |
| 41 |
$nlabel = 'cluster.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( |
| 73 |
value => $threshold_value, |
| 74 |
threshold => [ |
| 75 |
{ label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, |
| 76 |
{ label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } |
| 77 |
] |
| 78 |
); |
| 79 |
return $exit; |
| 80 |
} |
| 81 |
|
| 82 |
sub custom_usage_output { |
| 83 |
my ($self, %options) = @_; |
| 84 |
|
| 85 |
my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}); |
| 86 |
my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}); |
| 87 |
my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); |
| 88 |
return sprintf( |
| 89 |
"Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", |
| 90 |
$total_size_value . " " . $total_size_unit, |
| 91 |
$total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, |
| 92 |
$total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free} |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
sub custom_usage_calc { |
| 97 |
my ($self, %options) = @_; |
| 98 |
|
| 99 |
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; |
| 100 |
$self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'}; |
| 101 |
$self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'}; |
| 102 |
$self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used}; |
| 103 |
$self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total}; |
| 104 |
$self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used}; |
| 105 |
|
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
sub prefix_cluster_output { |
| 110 |
my ($self, %options) = @_; |
| 111 |
|
| 112 |
return "Cluster '" . $options{instance_value}->{display} . "' "; |
| 113 |
} |
| 114 |
|
| 115 |
sub set_counters { |
| 116 |
my ($self, %options) = @_; |
| 117 |
|
| 118 |
$self->{maps_counters_type} = [ |
| 119 |
{ name => 'cluster', type => 1, cb_prefix_output => 'prefix_cluster_output', message_multiple => 'All clusters are ok' } |
| 120 |
]; |
| 121 |
|
| 122 |
$self->{maps_counters}->{cluster} = [ |
| 123 |
{ |
| 124 |
label => 'status', |
| 125 |
type => 2, |
| 126 |
warning_default => '%{health} =~ /warning/', |
| 127 |
critical_default => '%{health} =~ /critical/', |
| 128 |
set => { |
| 129 |
key_values => [ { name => 'health' }, { name => 'display' } ], |
| 130 |
closure_custom_output => $self->can('custom_status_output'), |
| 131 |
closure_custom_perfdata => sub { return 0; }, |
| 132 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 133 |
} |
| 134 |
}, |
| 135 |
{ label => 'usage', set => { |
| 136 |
key_values => [ { name => 'display' }, { name => 'used' }, { name => 'total' } ], |
| 137 |
closure_custom_calc => $self->can('custom_usage_calc'), |
| 138 |
closure_custom_output => $self->can('custom_usage_output'), |
| 139 |
closure_custom_perfdata => $self->can('custom_usage_perfdata'), |
| 140 |
closure_custom_threshold_check => $self->can('custom_usage_threshold') |
| 141 |
} |
| 142 |
}, |
| 143 |
{ label => 'dedup', nlabel => 'cluster.deduplication.ratio.count', set => { |
| 144 |
key_values => [ { name => 'dedup' }, { name => 'display' } ], |
| 145 |
output_template => 'Dedup Ratio: %.2f', |
| 146 |
perfdatas => [ |
| 147 |
{ template => '%.2f', min => 0, label_extra_instance => 1, instance_use => 'display' } |
| 148 |
] |
| 149 |
} |
| 150 |
} |
| 151 |
]; |
| 152 |
} |
| 153 |
|
| 154 |
sub new { |
| 155 |
my ($class, %options) = @_; |
| 156 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 157 |
bless $self, $class; |
| 158 |
|
| 159 |
$options{options}->add_options(arguments => { |
| 160 |
'filter-name:s' => { name => 'filter_name' }, |
| 161 |
'units:s' => { name => 'units', default => '%' }, |
| 162 |
'free' => { name => 'free' } |
| 163 |
}); |
| 164 |
|
| 165 |
return $self; |
| 166 |
} |
| 167 |
|
| 168 |
my %mapping_health_level = ( |
| 169 |
0 => 'unknown', |
| 170 |
1 => 'ok', |
| 171 |
2 => 'information', |
| 172 |
3 => 'warning', |
| 173 |
4 => 'critical' |
| 174 |
); |
| 175 |
|
| 176 |
sub manage_selection { |
| 177 |
my ($self, %options) = @_; |
| 178 |
|
| 179 |
$self->{cluster} = {}; |
| 180 |
my $result = $options{custom}->get(path => '/cluster', ForceArray => ['cluster']); |
| 181 |
if (defined($result->{cluster})) { |
| 182 |
foreach my $entry (@{$result->{cluster}}) { |
| 183 |
if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && |
| 184 |
$entry->{properties}->{applianceName} !~ /$self->{option_results}->{filter_name}/) { |
| 185 |
$self->{output}->output_add(long_msg => "skipping '" . $entry->{properties}->{applianceName} . "': no matching filter.", debug => 1); |
| 186 |
next; |
| 187 |
} |
| 188 |
|
| 189 |
my $total = defined($entry->{properties}->{capacity}) ? $entry->{properties}->{capacity} * 1024 * 1024 * 1024 : $entry->{properties}->{localCapacityBytes}; |
| 190 |
my $used = $total - (defined($entry->{properties}->{freeSpace}) ? $entry->{properties}->{freeSpace} * 1024 * 1024 * 1024 : $entry->{properties}->{localFreeBytes}); |
| 191 |
|
| 192 |
$self->{cluster}->{$entry->{properties}->{serialNumber}} = { |
| 193 |
display => $entry->{properties}->{applianceName}, |
| 194 |
health => $mapping_health_level{$entry->{properties}->{healthLevel}}, |
| 195 |
total => $total, |
| 196 |
used => $used, |
| 197 |
dedup => $entry->{properties}->{dedupeRatio} |
| 198 |
}; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
if (scalar(keys %{$self->{cluster}}) <= 0) { |
| 203 |
$self->{output}->add_option_msg(short_msg => "No cluster found."); |
| 204 |
$self->{output}->option_exit(); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
1; |
| 209 |
|
| 210 |
__END__ |
| 211 |
|
| 212 |
=head1 MODE |
| 213 |
|
| 214 |
Check cluster usages. |
| 215 |
|
| 216 |
=over 8 |
| 217 |
|
| 218 |
=item B<--filter-counters> |
| 219 |
|
| 220 |
Only display some counters (regexp can be used). |
| 221 |
Example: --filter-counters='^status$' |
| 222 |
|
| 223 |
=item B<--filter-name> |
| 224 |
|
| 225 |
Filter cluster name (can be a regexp). |
| 226 |
|
| 227 |
=item B<--warning-status> |
| 228 |
|
| 229 |
Define the conditions to match for the status to be WARNING (default: '%{health} =~ /warning/'). |
| 230 |
You can use the following variables: %{health}, %{display} |
| 231 |
|
| 232 |
=item B<--critical-status> |
| 233 |
|
| 234 |
Define the conditions to match for the status to be CRITICAL (default: '%{health} =~ /critical/'). |
| 235 |
You can use the following variables: %{health}, %{display} |
| 236 |
|
| 237 |
=item B<--warning-*> |
| 238 |
|
| 239 |
Warning threshold. |
| 240 |
Can be: 'usage', 'dedup'. |
| 241 |
|
| 242 |
=item B<--critical-*> |
| 243 |
|
| 244 |
Critical threshold. |
| 245 |
Can be: 'usage', 'dedup'. |
| 246 |
|
| 247 |
=item B<--units> |
| 248 |
|
| 249 |
Units of thresholds (default: '%') ('%', 'B'). |
| 250 |
|
| 251 |
=item B<--free> |
| 252 |
|
| 253 |
Thresholds are on free space left. |
| 254 |
|
| 255 |
=back |
| 256 |
|
| 257 |
=cut |