| 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::datafactory::factories::mode::factoryusage; |
| 22 |
|
| 23 |
use base qw(cloud::azure::custom::mode); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
|
| 28 |
sub get_metrics_mapping { |
| 29 |
my ($self, %options) = @_; |
| 30 |
|
| 31 |
my $metrics_mapping = { |
| 32 |
'factory_percentage_usage' => { |
| 33 |
'output' => 'Factory usage', |
| 34 |
'label' => 'factory-percentage-usage', |
| 35 |
'nlabel' => 'azdatafactory.factoryusage.percentage', |
| 36 |
'unit' => '%', |
| 37 |
'min' => '0', |
| 38 |
'max' => '100' |
| 39 |
}, |
| 40 |
'resource_percentage_usage' => { |
| 41 |
'output' => 'Resource usage', |
| 42 |
'label' => 'resource-percentage-usage', |
| 43 |
'nlabel' => 'azdatafactory.factoryusage.resource.percentage', |
| 44 |
'unit' => '%', |
| 45 |
'min' => '0', |
| 46 |
'max' => '100' |
| 47 |
}, |
| 48 |
'FactorySizeInGbUnits' => { |
| 49 |
'output' => 'Factory size', |
| 50 |
'label' => 'factory-size', |
| 51 |
'nlabel' => 'azdatafactory.factoryusage.size.bytes', |
| 52 |
'unit' => 'B', |
| 53 |
'min' => '0', |
| 54 |
'change_bytes' => '2' |
| 55 |
}, |
| 56 |
'ResourceCount' => { |
| 57 |
'output' => 'Resource count', |
| 58 |
'label' => 'resource-count', |
| 59 |
'nlabel' => 'azdatafactory.factoryusage.resource.count', |
| 60 |
'unit' => '', |
| 61 |
'min' => '0' |
| 62 |
} |
| 63 |
}; |
| 64 |
|
| 65 |
return $metrics_mapping; |
| 66 |
} |
| 67 |
|
| 68 |
sub new { |
| 69 |
my ($class, %options) = @_; |
| 70 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 71 |
bless $self, $class; |
| 72 |
|
| 73 |
$options{options}->add_options(arguments => { |
| 74 |
'api-version:s' => { name => 'api_version', default => '2018-01-01'}, |
| 75 |
'resource:s' => { name => 'resource' }, |
| 76 |
'resource-group:s' => { name => 'resource_group' } |
| 77 |
}); |
| 78 |
return $self; |
| 79 |
} |
| 80 |
|
| 81 |
sub check_options { |
| 82 |
my ($self, %options) = @_; |
| 83 |
$self->SUPER::check_options(%options); |
| 84 |
|
| 85 |
if (!defined($self->{option_results}->{resource})) { |
| 86 |
$self->{output}->add_option_msg(short_msg => "Need to specify either --resource <name> with --resource-group option or --resource <id>."); |
| 87 |
$self->{output}->option_exit(); |
| 88 |
} |
| 89 |
|
| 90 |
$self->{api_version} = (defined($self->{option_results}->{api_version}) && $self->{option_results}->{api_version} ne "") ? $self->{option_results}->{api_version} : "2018-01-01"; |
| 91 |
|
| 92 |
my $resource = $self->{option_results}->{resource}; |
| 93 |
my $resource_group = defined($self->{option_results}->{resource_group}) ? $self->{option_results}->{resource_group} : ''; |
| 94 |
|
| 95 |
if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.DataFactory\/factories\/(.*)$/) { |
| 96 |
$resource_group = $1; |
| 97 |
$resource = $2; |
| 98 |
} |
| 99 |
|
| 100 |
$self->{az_resource} = $resource; |
| 101 |
$self->{az_resource_group} = $resource_group; |
| 102 |
$self->{az_resource_type} = 'factories'; |
| 103 |
$self->{az_resource_namespace} = 'Microsoft.DataFactory'; |
| 104 |
$self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900; |
| 105 |
$self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT5M"; |
| 106 |
$self->{az_aggregations} = ['Maximum']; |
| 107 |
|
| 108 |
if (defined($self->{option_results}->{aggregation})) { |
| 109 |
$self->{az_aggregations} = []; |
| 110 |
foreach my $stat (@{$self->{option_results}->{aggregation}}) { |
| 111 |
if ($stat ne '') { |
| 112 |
push @{$self->{az_aggregations}}, ucfirst(lc($stat)); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
foreach my $metric ('FactorySizeInGbUnits', 'MaxAllowedFactorySizeInGbUnits', 'ResourceCount', 'MaxAllowedResourceCount') { |
| 118 |
push @{$self->{az_metrics}}, $metric; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
sub manage_selection { |
| 123 |
my ($self, %options) = @_; |
| 124 |
|
| 125 |
my %metric_results; |
| 126 |
my $raw_results; |
| 127 |
|
| 128 |
($metric_results{$self->{az_resource}}, $raw_results) = $options{custom}->azure_get_metrics( |
| 129 |
aggregations => $self->{az_aggregations}, |
| 130 |
dimension => $self->{az_dimension}, |
| 131 |
interval => $self->{az_interval}, |
| 132 |
metrics => $self->{az_metrics}, |
| 133 |
resource => $self->{az_resource}, |
| 134 |
resource_group => $self->{az_resource_group}, |
| 135 |
resource_namespace => $self->{az_resource_namespace}, |
| 136 |
resource_type => $self->{az_resource_type}, |
| 137 |
timeframe => $self->{az_timeframe} |
| 138 |
); |
| 139 |
|
| 140 |
my $datas = 0; |
| 141 |
$self->{metrics}->{$self->{az_resource}}->{display} = $self->{az_resource}; |
| 142 |
foreach my $aggregation (@{$self->{az_aggregations}}) { |
| 143 |
next if (!defined($aggregation) && !defined($self->{option_results}->{zeroed})); |
| 144 |
$self->{metrics}->{$self->{az_resource}}->{statistics}->{lc($aggregation)}->{display} = lc($aggregation); |
| 145 |
$self->{metrics}->{$self->{az_resource}}->{statistics}->{lc($aggregation)}->{timeframe} = $self->{az_timeframe}; |
| 146 |
foreach my $metric (@{$self->{az_metrics}}) { |
| 147 |
my $metric_name = lc($metric); |
| 148 |
$metric_name =~ s/ /_/g; |
| 149 |
next if (!defined($metric_results{$self->{az_resource}}->{$metric_name}->{lc($aggregation)}) && !defined($self->{option_results}->{zeroed})); |
| 150 |
$self->{metrics}->{$self->{az_resource}}->{statistics}->{lc($aggregation)}->{$metric} = |
| 151 |
defined($metric_results{$self->{az_resource}}->{$metric_name}->{lc($aggregation)}) ? |
| 152 |
$metric_results{$self->{az_resource}}->{$metric_name}->{lc($aggregation)} : 0; |
| 153 |
$datas = 1; |
| 154 |
} |
| 155 |
# Compute percentages from metrics |
| 156 |
next if (!defined($self->{metrics}->{$self->{az_resource}}->{statistics}->{lc($aggregation)})); |
| 157 |
my $metricsAggregation = $self->{metrics}->{$self->{az_resource}}->{statistics}->{lc($aggregation)}; |
| 158 |
if (defined($metricsAggregation->{FactorySizeInGbUnits})) { |
| 159 |
$metricsAggregation->{FactorySizeInGbUnits} = $metricsAggregation->{FactorySizeInGbUnits} * 1024 * 1024 * 1024; |
| 160 |
} |
| 161 |
if (defined($metricsAggregation->{MaxAllowedFactorySizeInGbUnits}) || defined($self->{option_results}->{zeroed})) { |
| 162 |
$metricsAggregation->{MaxAllowedFactorySizeInGbUnits} = $metricsAggregation->{MaxAllowedFactorySizeInGbUnits} * 1024 * 1024 * 1024; |
| 163 |
my $max_allowed_factory_size_in_gb_units = $metricsAggregation->{MaxAllowedFactorySizeInGbUnits}; |
| 164 |
if ($max_allowed_factory_size_in_gb_units > 0) { |
| 165 |
$metricsAggregation->{factory_percentage_usage} = ($metricsAggregation->{FactorySizeInGbUnits} / $max_allowed_factory_size_in_gb_units) * 100; |
| 166 |
} else { |
| 167 |
$metricsAggregation->{factory_percentage_usage} = 0; |
| 168 |
} |
| 169 |
$datas = 1; |
| 170 |
} |
| 171 |
if (defined($metricsAggregation->{MaxAllowedResourceCount}) || defined($self->{option_results}->{zeroed})) { |
| 172 |
my $max_allowed_resource_count = $metricsAggregation->{MaxAllowedResourceCount}; |
| 173 |
if ($max_allowed_resource_count > 0) { |
| 174 |
$metricsAggregation->{resource_percentage_usage} = ($metricsAggregation->{ResourceCount} / $max_allowed_resource_count) * 100; |
| 175 |
} else { |
| 176 |
$metricsAggregation->{resource_percentage_usage} = 0; |
| 177 |
} |
| 178 |
$datas = 1; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
if (!$datas) { |
| 183 |
$self->{output}->add_option_msg(short_msg => 'No metrics. Check your options or use --zeroed option to set 0 on undefined values'); |
| 184 |
$self->{output}->option_exit(); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
1; |
| 189 |
|
| 190 |
__END__ |
| 191 |
|
| 192 |
=head1 MODE |
| 193 |
|
| 194 |
Check Azure Data Factory for factory size and resource usage. |
| 195 |
|
| 196 |
Example: |
| 197 |
|
| 198 |
Using resource name: |
| 199 |
|
| 200 |
perl centreon_plugins.pl --plugin=cloud::azure::datafactory::factory::plugin --mode=factoryusage --custommode=api |
| 201 |
--resource=<factory_id> --resource-group=<resourcegroup_id> |
| 202 |
|
| 203 |
Using resource ID: |
| 204 |
|
| 205 |
perl centreon_plugins.pl --plugin=cloud::azure::datafactory::factory::plugin --mode=factoryusage --custommode=api |
| 206 |
--resource='/subscriptions/<subscription_id>/resourceGroups/<resourcegroup_id>/providers/Microsoft.DataFactory/factories/<factory_id>' |
| 207 |
|
| 208 |
|
| 209 |
=over 8 |
| 210 |
|
| 211 |
=item B<--resource> |
| 212 |
|
| 213 |
Set resource name or ID (required). |
| 214 |
|
| 215 |
=item B<--resource-group> |
| 216 |
|
| 217 |
Set resource group (required if resource's name is used). |
| 218 |
|
| 219 |
=item B<--warning-$metric$> |
| 220 |
|
| 221 |
Warning thresholds ($metric$ can be: 'factory-percentage-usage', 'resource-percentage-usage', 'factory-size', 'resource-count'). |
| 222 |
|
| 223 |
=item B<--critical-$metric$> |
| 224 |
|
| 225 |
Critical thresholds ($metric$ can be: 'factory-percentage-usage', 'resource-percentage-usage', 'factory-size', 'resource-count'). |
| 226 |
|
| 227 |
=back |
| 228 |
|
| 229 |
=cut |