| 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 cloud::azure::common::storageaccount::filesharequota; |
| 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 $value_perf = $self->{result_values}->{used_space}; |
| 32 |
|
| 33 |
my %total_options = (); |
| 34 |
if ($self->{instance_mode}->{option_results}->{units} eq '%') { |
| 35 |
$total_options{total} = $self->{result_values}->{total_capacity}; |
| 36 |
$total_options{cast_int} = 1; |
| 37 |
} |
| 38 |
|
| 39 |
$self->{output}->perfdata_add( |
| 40 |
nlabel => 'fileshare.capacity.usage.bytes', |
| 41 |
unit => 'B', |
| 42 |
value => $value_perf, |
| 43 |
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options), |
| 44 |
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options), |
| 45 |
min => 0, max => $self->{result_values}->{total_capacity} |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
sub custom_usage_threshold { |
| 50 |
my ($self, %options) = @_; |
| 51 |
|
| 52 |
my ($exit, $threshold_value); |
| 53 |
$threshold_value = $self->{result_values}->{used_space}; |
| 54 |
if ($self->{instance_mode}->{option_results}->{units} eq '%') { |
| 55 |
$threshold_value = $self->{result_values}->{prct_used}; |
| 56 |
} |
| 57 |
$exit = $self->{perfdata}->threshold_check( |
| 58 |
value => $threshold_value, |
| 59 |
threshold => [ |
| 60 |
{ label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, |
| 61 |
{ label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } |
| 62 |
] |
| 63 |
); |
| 64 |
return $exit; |
| 65 |
} |
| 66 |
|
| 67 |
sub custom_usage_output { |
| 68 |
my ($self, %options) = @_; |
| 69 |
|
| 70 |
my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total_capacity} ); |
| 71 |
my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used_space} ); |
| 72 |
my $msg = sprintf( |
| 73 |
"Fileshare '%s' from storage account '%s' used capacity: %s (%.2f%%), total size %s", |
| 74 |
$self->{result_values}->{fileshare}, |
| 75 |
$self->{result_values}->{storageaccount}, |
| 76 |
$total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, |
| 77 |
$total_size_value . " " . $total_size_unit |
| 78 |
); |
| 79 |
return $msg; |
| 80 |
} |
| 81 |
|
| 82 |
sub custom_usage_calc { |
| 83 |
my ($self, %options) = @_; |
| 84 |
|
| 85 |
$self->{result_values}->{storageaccount} = $options{new_datas}->{fileshare_storage_account}; |
| 86 |
$self->{result_values}->{fileshare} = $options{new_datas}->{fileshare_fileshare}; |
| 87 |
|
| 88 |
$self->{result_values}->{used_space} = $options{new_datas}->{fileshare_used_space}; |
| 89 |
$self->{result_values}->{total_capacity} = $options{new_datas}->{fileshare_total_capacity} * ( 1024 ** 3); |
| 90 |
$self->{result_values}->{prct_used} = ($self->{result_values}->{total_capacity} > 0) ? $self->{result_values}->{used_space} * 100 / $self->{result_values}->{total_capacity} : 0; |
| 91 |
|
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
sub set_counters { |
| 96 |
my ($self, %options) = @_; |
| 97 |
|
| 98 |
$self->{maps_counters_type} = [ |
| 99 |
{ name => 'fileshare', type => 0 } |
| 100 |
]; |
| 101 |
|
| 102 |
$self->{maps_counters}->{fileshare} = [ |
| 103 |
{ label => 'capacity-usage', set => { |
| 104 |
key_values => [ { name => 'used_space' }, { name => 'total_capacity' }, { name => 'storage_account' }, { name => 'fileshare' } ], |
| 105 |
closure_custom_calc => $self->can('custom_usage_calc'), |
| 106 |
closure_custom_output => $self->can('custom_usage_output'), |
| 107 |
closure_custom_perfdata => $self->can('custom_usage_perfdata'), |
| 108 |
closure_custom_threshold_check => $self->can('custom_usage_threshold') |
| 109 |
} |
| 110 |
} |
| 111 |
] |
| 112 |
} |
| 113 |
|
| 114 |
sub new { |
| 115 |
my ($class, %options) = @_; |
| 116 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 117 |
bless $self, $class; |
| 118 |
|
| 119 |
$options{options}->add_options(arguments => { |
| 120 |
'resource-group:s' => { name => 'resource_group' }, |
| 121 |
'storage-account:s' => { name => 'storage_account' }, |
| 122 |
'units:s' => { name => 'units', default => '%' }, |
| 123 |
'fileshare:s' => { name => 'fileshare' } |
| 124 |
}); |
| 125 |
|
| 126 |
return $self; |
| 127 |
} |
| 128 |
|
| 129 |
sub check_options { |
| 130 |
my ($self, %options) = @_; |
| 131 |
$self->SUPER::check_options(%options); |
| 132 |
|
| 133 |
$self->{api_version} = '2021-09-01'; |
| 134 |
|
| 135 |
if (!defined($self->{option_results}->{resource_group}) || $self->{option_results}->{resource_group} eq '') { |
| 136 |
$self->{output}->add_option_msg(short_msg => "Need to specify --resource-group <name>."); |
| 137 |
$self->{output}->option_exit(); |
| 138 |
} |
| 139 |
if (!defined($self->{option_results}->{storage_account}) || $self->{option_results}->{storage_account} eq '') { |
| 140 |
$self->{output}->add_option_msg(short_msg => "Need to specify --storage-account <name>."); |
| 141 |
$self->{output}->option_exit(); |
| 142 |
} |
| 143 |
if (!defined($self->{option_results}->{fileshare}) || $self->{option_results}->{fileshare} eq '') { |
| 144 |
$self->{output}->add_option_msg(short_msg => "Need to specify --fileshare <name>"); |
| 145 |
$self->{output}->option_exit(); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
sub manage_selection { |
| 150 |
my ($self, %options) = @_; |
| 151 |
|
| 152 |
my $results; |
| 153 |
$results = $options{custom}->azure_get_file_share_stats( |
| 154 |
resource_group => $self->{option_results}->{resource_group}, storage_account => $self->{option_results}->{storage_account}, |
| 155 |
fileshare => $self->{option_results}->{fileshare}, api_version => $self->{api_version} |
| 156 |
); |
| 157 |
|
| 158 |
$self->{fileshare} = { |
| 159 |
storage_account => $self->{option_results}->{storage_account}, |
| 160 |
fileshare => $self->{option_results}->{fileshare}, |
| 161 |
total_capacity => $results->{properties}->{shareQuota}, |
| 162 |
used_space => $results->{properties}->{shareUsageBytes} |
| 163 |
}; |
| 164 |
|
| 165 |
if (scalar(keys %{$self->{fileshare}}) <= 0) { |
| 166 |
$self->{output}->add_option_msg(short_msg => "No entry found."); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
1; |
| 171 |
|
| 172 |
__END__ |
| 173 |
|
| 174 |
=head1 MODE |
| 175 |
|
| 176 |
Check storage account fileshare capacity usage. |
| 177 |
|
| 178 |
Example: |
| 179 |
|
| 180 |
perl centreon_plugins.pl --plugin=cloud::azure::storage::storageaccount::plugin --mode=file-share-quota --custommode=api |
| 181 |
--client-id='XXX' --subscription='XXX' --tenant='XXX' --client-secret='XXX' --resource-group=MYRESOURCEGROUP1 |
| 182 |
--storage-account=MYSTORAGEACCOUNT1 --fileshare=FILESHARE1 |
| 183 |
|
| 184 |
=over 8 |
| 185 |
|
| 186 |
=item B<--units> |
| 187 |
|
| 188 |
Units of thresholds. Can be : '%', 'B' |
| 189 |
|
| 190 |
Default: '%' |
| 191 |
|
| 192 |
=item B<--resource-group> |
| 193 |
|
| 194 |
Set resource group from which depends the storage account (required). |
| 195 |
|
| 196 |
=item B<--storage-account> |
| 197 |
|
| 198 |
Set storage account from which the fileshare to monitor is from (required). |
| 199 |
|
| 200 |
=item B<--fileshare> |
| 201 |
|
| 202 |
Set fileshare to monitor (required). |
| 203 |
|
| 204 |
=item B<--warning-capacity-usage> |
| 205 |
|
| 206 |
Warning threshold for fileshare capacity usage. |
| 207 |
|
| 208 |
Threshold is in percentage by default. |
| 209 |
To specify it in Bytes please set the units option '--units=B'. |
| 210 |
|
| 211 |
=item B<--critical-capacity-usage> |
| 212 |
|
| 213 |
Critical threshold for fileshare capacity usage. |
| 214 |
|
| 215 |
Threshold is in percentage by default. |
| 216 |
To specify it in Bytes please set the units option '--units=B'. |
| 217 |
|
| 218 |
=back |
| 219 |
|
| 220 |
=cut |