enh(cloud::azure::custom::api): Add option to choose cost-metric (#5648)

Refs: CTOR-711
This commit is contained in:
scresto 2025-07-01 14:27:01 +02:00 committed by GitHub
parent 7ef086e281
commit 860959c392
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 7 deletions

View File

@ -28,7 +28,6 @@ sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
my $self = {}; my $self = {};
bless $self, $class; bless $self, $class;
$self->{perfdata} = centreon::plugins::perfdata->new(output => $options{output}); $self->{perfdata} = centreon::plugins::perfdata->new(output => $options{output});
%{$self->{option_results}} = (); %{$self->{option_results}} = ();
@ -45,7 +44,6 @@ sub new {
sub init { sub init {
my ($self, %options) = @_; my ($self, %options) = @_;
# options{default} = { mode_xxx => { option_name => option_value }, } # options{default} = { mode_xxx => { option_name => option_value }, }
%{$self->{option_results}} = %{$options{option_results}}; %{$self->{option_results}} = %{$options{option_results}};
# Manage default value # Manage default value
return if (!defined($options{default})); return if (!defined($options{default}));

View File

@ -930,7 +930,7 @@ sub azure_get_usagedetails_set_url {
my $url = $self->{management_endpoint} . "/subscriptions/" . $self->{subscription}; my $url = $self->{management_endpoint} . "/subscriptions/" . $self->{subscription};
$url .= "/resourceGroups/" . $options{resource_group} if (defined($options{resource_group}) && $options{resource_group} ne ''); $url .= "/resourceGroups/" . $options{resource_group} if (defined($options{resource_group}) && $options{resource_group} ne '');
$url .= "/providers/Microsoft.Consumption/usageDetails?\$filter=properties%2FusageStart ge %27" . $options{usage_start} . "%27 and properties%2FusageEnd le %27" . $options{usage_end} . "%27"; $url .= "/providers/Microsoft.Consumption/usageDetails?\$filter=properties%2FusageStart ge %27" . $options{usage_start} . "%27 and properties%2FusageEnd le %27" . $options{usage_end} . "%27";
$url .= "&metric=actualcost"; $url .= "&metric=". ($options{cost_metric} || 'actualcost');
$url .= "&api-version=" . $self->{api_version}; $url .= "&api-version=" . $self->{api_version};
return $url; return $url;
} }
@ -1261,7 +1261,7 @@ The application needs the 'Monitoring Reader' role (see https://docs.microsoft.c
This custom mode is using the 'OAuth 2.0 Client Credentials Grant Flow' This custom mode is using the 'OAuth 2.0 Client Credentials Grant Flow'
For further informations, visit https://docs.microsoft.com/en-us/azure/active-directory/develop/v1-oauth2-client-creds-grant-flow For further information, visit https://docs.microsoft.com/en-us/azure/active-directory/develop/v1-oauth2-client-creds-grant-flow
=over 8 =over 8
@ -1295,7 +1295,7 @@ Set timeframe in seconds (i.e. 3600 to check last hour).
=item B<--interval> =item B<--interval>
Set interval of the metric query (can be : PT1M, PT5M, PT15M, PT30M, PT1H, PT6H, PT12H, PT24H). Set interval of the metric query (can be : C<PT1M>, C<PT5M>, C<PT15M>, C<PT30M>, C<PT1H>, C<PT6H>, C<PT12H>, C<PT24H>).
=item B<--aggregation> =item B<--aggregation>

View File

@ -113,6 +113,7 @@ sub new {
"lookup-days:s" => { name => 'lookup_days', default => 30 }, "lookup-days:s" => { name => 'lookup_days', default => 30 },
"units:s" => { name => 'units', default => '%' }, "units:s" => { name => 'units', default => '%' },
"timeout:s" => { name => 'timeout', default => '60' }, "timeout:s" => { name => 'timeout', default => '60' },
"cost-metric:s" => { name => "cost_metric", default => 'actualcost' }
}); });
return $self; return $self;
@ -127,6 +128,12 @@ sub check_options {
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
$self->{option_results}->{cost_metric} = lc($self->{option_results}->{cost_metric});
if ($self->{option_results}->{cost_metric} !~ /^(actualcost|amortized)$/) {
$self->{output}->add_option_msg(short_msg => "Invalid cost metric '" . $self->{option_results}->{cost_metric} . "'. Valid values are 'actualcost' or 'amortized'.");
$self->{output}->option_exit();
}
$self->{lookup_days} = $self->{option_results}->{lookup_days}; $self->{lookup_days} = $self->{option_results}->{lookup_days};
} }
@ -143,7 +150,8 @@ sub manage_selection {
my $costs = $options{custom}->azure_get_usagedetails( my $costs = $options{custom}->azure_get_usagedetails(
resource_group => $self->{option_results}->{resource_group}, resource_group => $self->{option_results}->{resource_group},
usage_start => $usage_start, usage_start => $usage_start,
usage_end => $usage_end usage_end => $usage_end,
cost_metric => $self->{option_results}->{cost_metric},
); );
my $cost = 0; my $cost = 0;
@ -183,7 +191,7 @@ perl centreon_plugins.pl --plugin=cloud::azure::management::costs::plugin --cust
You should NOT execute the plugin for a given subscription/resource group more than once a day otherwise, You should NOT execute the plugin for a given subscription/resource group more than once a day otherwise,
you might reach the Azure API calls limit if you have many. you might reach the Azure API calls limit if you have many.
For subscription with large ressource with usagedetail consumption that might requite many API calls, For subscription with large resources and detailed usage consumption that might require many API calls,
you may have to increase timeout. you may have to increase timeout.
=over 8 =over 8
@ -209,8 +217,13 @@ Set warning threshold for cost).
Define the conditions to match for the status to be CRITICAL. Define the conditions to match for the status to be CRITICAL.
=item B<--units> =item B<--units>
Unit of thresholds (default: '%') ('%', 'count'). Unit of thresholds (default: '%') ('%', 'count').
=item B<--cost-metric>
Choose the cost metric to use (default: C<actualcost>) (C<actualcost>, C<amortized>).
=back =back
=cut =cut

View File

@ -203,6 +203,7 @@ NTLMv2
NTP NTP
NVOS NVOS
--oid --oid
OAuth
OID OID
--oid-display --oid-display
--oid-extra-display --oid-extra-display