| 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::management::costs::mode::budgets; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
|
| 28 |
sub custom_cost_perfdata { |
| 29 |
my ($self, %options) = @_; |
| 30 |
|
| 31 |
my %budget_options = (); |
| 32 |
if ($self->{instance_mode}->{option_results}->{units} eq '%') { |
| 33 |
$budget_options{total} = $self->{result_values}->{budget}; |
| 34 |
$budget_options{cast_int} = 1; |
| 35 |
} |
| 36 |
|
| 37 |
$self->{output}->perfdata_add( |
| 38 |
nlabel => 'azure.budget.consumption.currency', |
| 39 |
value => sprintf("%.2f", $self->{result_values}->{cost}), |
| 40 |
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %budget_options), |
| 41 |
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %budget_options), |
| 42 |
min => 0, max => $self->{result_values}->{budget} |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
sub custom_cost_threshold { |
| 47 |
my ($self, %options) = @_; |
| 48 |
|
| 49 |
my $threshold_value = $self->{result_values}->{cost}; |
| 50 |
if ($self->{instance_mode}->{option_results}->{units} eq '%') { |
| 51 |
$threshold_value = $self->{result_values}->{prct_cost}; |
| 52 |
} |
| 53 |
my $exit = $self->{perfdata}->threshold_check( |
| 54 |
value => $threshold_value, |
| 55 |
threshold => [ |
| 56 |
{ label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, |
| 57 |
{ label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } |
| 58 |
] |
| 59 |
); |
| 60 |
return $exit; |
| 61 |
} |
| 62 |
|
| 63 |
sub custom_cost_output { |
| 64 |
my ($self, %options) = @_; |
| 65 |
|
| 66 |
return sprintf( |
| 67 |
"Spent amount is %.2f%s on %d%s of allowed budget (%.2f%% consumption) for the past %d days", |
| 68 |
$self->{result_values}->{cost}, |
| 69 |
$self->{instance_mode}->{currency}, |
| 70 |
$self->{result_values}->{budget}, |
| 71 |
$self->{instance_mode}->{currency}, |
| 72 |
$self->{result_values}->{prct_cost}, |
| 73 |
$self->{instance_mode}->{lookup_days} |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
sub custom_cost_calc { |
| 78 |
my ($self, %options) = @_; |
| 79 |
|
| 80 |
$self->{result_values}->{cost} = $options{new_datas}->{$self->{instance} . '_cost'}; |
| 81 |
$self->{result_values}->{budget} = $options{new_datas}->{$self->{instance} . '_budget'}; |
| 82 |
$self->{result_values}->{prct_cost} = ($self->{result_values}->{budget} != 0) ? $self->{result_values}->{cost} * 100 / $self->{result_values}->{budget} : 0; |
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
sub set_counters { |
| 87 |
my ($self, %options) = @_; |
| 88 |
|
| 89 |
$self->{maps_counters_type} = [ |
| 90 |
{ name => 'cost', type => 0 } |
| 91 |
]; |
| 92 |
|
| 93 |
$self->{maps_counters}->{cost} = [ |
| 94 |
{ label => 'cost', set => { |
| 95 |
key_values => [ { name => 'cost' }, { name => 'budget' } ], |
| 96 |
closure_custom_calc => $self->can('custom_cost_calc'), |
| 97 |
closure_custom_output => $self->can('custom_cost_output'), |
| 98 |
closure_custom_threshold_check => $self->can('custom_cost_threshold'), |
| 99 |
closure_custom_perfdata => $self->can('custom_cost_perfdata') |
| 100 |
} |
| 101 |
}, |
| 102 |
]; |
| 103 |
} |
| 104 |
|
| 105 |
sub new { |
| 106 |
my ($class, %options) = @_; |
| 107 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 108 |
bless $self, $class; |
| 109 |
|
| 110 |
$options{options}->add_options(arguments => { |
| 111 |
"budget-name:s" => { name => 'budget_name' }, |
| 112 |
"resource-group:s" => { name => 'resource_group' }, |
| 113 |
"lookup-days:s" => { name => 'lookup_days', default => 30 }, |
| 114 |
"units:s" => { name => 'units', default => '%' }, |
| 115 |
"timeout:s" => { name => 'timeout', default => '60' }, |
| 116 |
}); |
| 117 |
|
| 118 |
return $self; |
| 119 |
} |
| 120 |
|
| 121 |
sub check_options { |
| 122 |
my ($self, %options) = @_; |
| 123 |
$self->SUPER::check_options(%options); |
| 124 |
|
| 125 |
if (!defined($self->{option_results}->{budget_name}) || $self->{option_results}->{budget_name} eq '') { |
| 126 |
$self->{output}->add_option_msg(short_msg => "Need to specify --budget-name option"); |
| 127 |
$self->{output}->option_exit(); |
| 128 |
} |
| 129 |
|
| 130 |
$self->{lookup_days} = $self->{option_results}->{lookup_days}; |
| 131 |
} |
| 132 |
|
| 133 |
sub manage_selection { |
| 134 |
my ($self, %options) = @_; |
| 135 |
|
| 136 |
my $budget = $options{custom}->azure_get_budget( |
| 137 |
resource_group => $self->{option_results}->{resource_group}, |
| 138 |
budget_name => $self->{option_results}->{budget_name} |
| 139 |
); |
| 140 |
|
| 141 |
my $usage_start = DateTime->now()->add(days => - $self->{option_results}->{lookup_days} + 1); |
| 142 |
my $usage_end = DateTime->now(); |
| 143 |
my $costs = $options{custom}->azure_get_usagedetails( |
| 144 |
resource_group => $self->{option_results}->{resource_group}, |
| 145 |
usage_start => $usage_start, |
| 146 |
usage_end => $usage_end |
| 147 |
); |
| 148 |
|
| 149 |
my $cost = 0; |
| 150 |
for (my $i = 0; $costs->[$i]; $i++) { |
| 151 |
$cost += $costs->[$i]->{properties}->{cost}; |
| 152 |
} |
| 153 |
|
| 154 |
if (!$budget) { |
| 155 |
$self->{output}->add_option_msg(short_msg => "No " . $self->{option_results}->{budget_name} . " found (or missing permissions)"); |
| 156 |
$self->{output}->option_exit(); |
| 157 |
} |
| 158 |
|
| 159 |
if (!$costs || $cost < 0.01) { |
| 160 |
$self->{output}->add_option_msg(short_msg => "Null or < 0.01 " . $budget->{properties}->{currentSpend}->{unit} . " cost found on the specified scope."); |
| 161 |
$self->{output}->option_exit(); |
| 162 |
} |
| 163 |
|
| 164 |
$self->{currency} = $budget->{properties}->{currentSpend}->{unit}; |
| 165 |
$self->{cost} = { |
| 166 |
cost => $cost, |
| 167 |
budget => $budget->{properties}->{amount}, |
| 168 |
}; |
| 169 |
} |
| 170 |
|
| 171 |
1; |
| 172 |
|
| 173 |
__END__ |
| 174 |
|
| 175 |
=head1 MODE |
| 176 |
|
| 177 |
Check cost status. |
| 178 |
|
| 179 |
Example: |
| 180 |
perl centreon_plugins.pl --plugin=cloud::azure::management::costs::plugin --custommode=api --mode=costs |
| 181 |
--resource-group='MYRESOURCEGROUP' --budget-name='MYBUDGET |
| 182 |
|
| 183 |
You should NOT execute the plugin for a given subscription/resource group more than once a day otherwise, |
| 184 |
you might reach the Azure API calls limit if you have many. |
| 185 |
|
| 186 |
For subscription with large ressource with usagedetail consumption that might requite many API calls, |
| 187 |
you may have to increase timeout. |
| 188 |
|
| 189 |
=over 8 |
| 190 |
|
| 191 |
=item B<--budget-name> |
| 192 |
|
| 193 |
Set budget name (required). |
| 194 |
|
| 195 |
=item B<--resource-group> |
| 196 |
|
| 197 |
Set resource group (optional). |
| 198 |
|
| 199 |
=item B<--lookup-days> |
| 200 |
|
| 201 |
Days backward to look up (default: '30'). |
| 202 |
|
| 203 |
=item B<--warning-cost> |
| 204 |
|
| 205 |
Set warning threshold for cost). |
| 206 |
|
| 207 |
=item B<--critical-cost> |
| 208 |
|
| 209 |
Define the conditions to match for the status to be CRITICAL. |
| 210 |
|
| 211 |
=item B<--units> |
| 212 |
Unit of thresholds (default: '%') ('%', 'count'). |
| 213 |
|
| 214 |
=back |
| 215 |
|
| 216 |
=cut |