Debian Codegraph @

src/cloud/azure/management/costs/mode/costsexplorer.pm

raw
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::costsexplorer;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use DateTime;
28
29 sub resource_group_prefix_output {
30 my ($self, %options) = @_;
31
32 return sprintf( "Resource group '%s' ", $options{instance});
33 }
34
35 sub custom_subscription_output {
36 my ($self, %options) = @_;
37
38 return sprintf( "Subscription costs for specified period: %.2f %s", $self->{result_values}->{subscription_cost}, $self->{result_values}->{currency});
39 }
40
41 sub custom_resource_group_output {
42 my ($self, %options) = @_;
43
44 return sprintf( "costs for specified period: %.2f %s", $self->{result_values}->{resource_group_cost}, $self->{result_values}->{currency});
45 }
46
47
48 sub set_counters {
49 my ($self, %options) = @_;
50
51 $self->{maps_counters_type} = [
52 { name => 'global', type => 0, skipped_code => { -10 => 1 } },
53 { name => 'resource_group', type => 1, cb_prefix_output => 'resource_group_prefix_output', message_multiple => 'All resource group costs are OK', skipped_code => { -10 => 1 } },
54 ];
55
56 $self->{maps_counters}->{global} = [
57 { label => 'subscription-costs', nlabel => 'azure.subscription.global.costs', set => {
58 key_values => [ { name => 'subscription_cost' }, { name => 'currency' } ],
59 closure_custom_output => $self->can('custom_subscription_output'),
60 perfdatas => [
61 { template => '%.2f', min => 0 }
62 ]
63 }
64 }
65 ];
66 $self->{maps_counters}->{resource_group} = [
67 { label => 'resource-group-costs', nlabel => 'azure.resourcegroup.costs', set => {
68 key_values => [ { name => 'resource_group_cost' }, { name => 'currency'} ],
69 closure_custom_output => $self->can('custom_resource_group_output'),
70 perfdatas => [
71 { template => '%.2f', min => 0, label_extra_instance => 1 },
72 ]
73 }
74 }
75 ]
76
77 }
78
79 sub new {
80 my ($class, %options) = @_;
81 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
82 bless $self, $class;
83
84 $options{options}->add_options(arguments => {
85 "lookup-days:s" => { name => 'lookup_days', default => 30 },
86 "resource-group:s@" => { name => 'resource_group' },
87 "tags:s@" => { name => 'tags' }
88 });
89
90 return $self;
91 }
92
93 sub check_options {
94 my ($self, %options) = @_;
95 $self->SUPER::check_options(%options);
96
97 if (defined($self->{option_results}->{tags}) && $self->{option_results}->{tags} ne '') {
98 foreach my $tag_pair (@{$self->{option_results}->{tags}}) {
99 my ($key, $value) = split / => /, $tag_pair;
100 next if !defined($key);
101 $value = "" if !defined($value);
102 centreon::plugins::misc::trim($value);
103 push @{$self->{tags}->{ centreon::plugins::misc::trim($key) }}, $value;
104 }
105 }
106 }
107
108 sub create_body_filter {
109 my ($self, %options) = @_;
110
111 my $filter;
112 my $resource_group_filter;
113
114 # group by resource group if we want costs for one or multiple resource groups
115 if (defined($self->{option_results}->{resource_group}) && $self->{option_results}->{resource_group} ne ""){
116 $resource_group_filter->{dimensions}->{name} = "ResourceGroup";
117 $resource_group_filter->{dimensions}->{operator} = "In";
118 $resource_group_filter->{dimensions}->{values} = $self->{option_results}->{resource_group};
119 }
120
121 # if we have more than two elements to filter on (either several tags, or a tag and some resource groups) we need to have a 'and' array containing the elements
122 if ((defined($self->{tags}) && keys %{$self->{tags}} > 0) && (defined($self->{option_results}->{resource_group}) && $self->{option_results}->{resource_group} ne "")
123 || (defined($self->{tags}) && keys %{$self->{tags}} > 1)){
124 foreach my $tag (keys %{$self->{tags}}){
125 push @{$filter->{and}}, my $tags_filter = {
126 tags => {
127 name => $tag,
128 operator => "In",
129 values => $self->{tags}->{$tag}
130 }
131 };
132 }
133 push @{$filter->{and}}, $resource_group_filter if keys %{$resource_group_filter} > 0; # we add a filter for resource group if at least one resource group was specified
134 return $filter;
135 }
136
137 # if we have at least one tag and no resource group we need to set the filter on this tag, without the 'and'
138 if (defined($self->{tags}) && keys %{$self->{tags}} >= 0 && (!defined($self->{option_results}->{resource_group}) || $self->{option_results}->{resource_group} eq "")){
139 foreach my $tag (keys %{$self->{tags}}){
140 $filter = {
141 tags => {
142 name => $tag,
143 operator => "In",
144 values => $self->{tags}->{$tag}
145 }
146 };
147 }
148 return $filter;
149 }
150
151 # if we only have resource group(s) specified to filter on, then we return the filter for resource group(s)
152 $filter = $resource_group_filter;
153 return $filter;
154 }
155
156 sub create_body_payload {
157 my ($self, %options) = @_;
158
159 my $start_date = DateTime->now->subtract( days => $self->{option_results}->{lookup_days} );
160 my $end_date = DateTime->now;
161
162 my $form_post = {
163 "type" => "Usage",
164 "timeframe" => "Custom",
165 "dataset" => {
166 "aggregation" => {
167 "totalCost" => {
168 "name" => "PreTaxCost",
169 "function" => "Sum"
170 }
171 },
172 "granularity" => "daily",
173 }
174 };
175
176 $form_post->{timeperiod}->{from} = $start_date->ymd;
177 $form_post->{timeperiod}->{to} = $end_date->ymd;
178
179 if ((defined($self->{option_results}->{resource_group}) && $self->{option_results}->{resource_group} ne "") ||
180 ((defined($self->{tags}) && keys %{$self->{tags}} > 0))){
181 my $filter;
182 $filter = $self->create_body_filter();
183 $form_post->{dataset}->{filter} = $filter if defined($filter);
184 push @{$form_post->{dataset}->{grouping}}, { "type" => "Dimension", "name" => "ResourceGroup"};
185 }
186 return $form_post;
187 }
188
189
190 sub manage_selection {
191 my ($self, %options) = @_;
192
193 my $raw_form_post = $self->create_body_payload();
194 my $costs = $options{custom}->azure_get_subscription_cost_management(body_post => $raw_form_post);
195
196 my $sum_costs;
197 my $currency;
198
199 if ((!defined($self->{option_results}->{resource_group}) || $self->{option_results}->{resource_group} eq "")
200 && keys %{$self->{tags}} == 0){
201 foreach my $daily_subscription_cost (@{$costs}){
202 $sum_costs += ${$daily_subscription_cost}[0];
203 $currency = ${$daily_subscription_cost}[2];
204 }
205 $self->{global} = {
206 subscription_cost => $sum_costs,
207 currency => $currency
208 };
209 }
210 if (defined($self->{option_results}->{resource_group}) || defined($self->{option_results}->{tags}) && keys %{$self->{tags}} > 0){
211 my $resource_group_total_costs;
212
213 foreach my $daily_resource_group_cost (@{$costs}){
214 my $resource_group = ${$daily_resource_group_cost}[2];
215 $sum_costs += ${$daily_resource_group_cost}[0];
216 $currency = ${$daily_resource_group_cost}[3];
217
218 $resource_group_total_costs->{$resource_group}->{sum} = $sum_costs;
219 $resource_group_total_costs->{$resource_group}->{currency} = $currency;
220 }
221
222 foreach my $resource_group (keys %$resource_group_total_costs){
223 $self->{resource_group}->{$resource_group} = {
224 resource_group_cost => $resource_group_total_costs->{$resource_group}->{sum},
225 currency => $resource_group_total_costs->{$resource_group}->{currency}
226 };
227 }
228 }
229 if (scalar(keys %{$self->{global}}) <= 0 && scalar(keys %{$self->{resource_group}}) <= 0) {
230 $self->{output}->add_option_msg(short_msg => "No entry found.");
231 $self->{output}->option_exit();
232 }
233 }
234
235 1;
236
237 __END__
238
239 =head1 MODE
240
241 Check costs for a subscription or per resource group.
242
243 If you don't specify a resource group or any tags then you will have costs for the whole subscription.
244
245 You can specify resource groups and tags to filter on.
246
247 Example to get costs per subscription for the last 30 days:
248 perl centreon_plugins.pl --plugin=cloud::azure::management::costs::plugin --mode=costs-explorer --custommode=api --client-id='xxx'
249 --client-secret='xxx' --tenant='xxx' --subscription='xxx' --lookup-days=30
250
251 Example to get costs for a resource group:
252 perl centreon_plugins.pl --plugin=cloud::azure::management::costs::plugin --mode=costs-explorer --custommode=api --client-id='xxx'
253 --client-secret='xxx' --tenant='xxx' --subscription='xxx' --lookup-days=30 --resource-group=MYRESOURCEGROUP --tags='Environment => integration'
254
255 =over 8
256
257 =item B<--resource-group>
258
259 Set resource group (optional).
260
261 If you don't, you get costs for the whole subscription.
262
263 You can specify multiple resource groups. You will get results for each one of the resource groups specified.
264 Example: --resource-group=MYRESOURCEGROUP1 --resource-group=MYRESOURCEGROUP2
265
266 =item B<--tags>
267
268 Set tags to filter on (optional).
269
270 You can specify multiple tags. You will get results for the resource groups matching all the tags specified.
271 Example: --tags='Environment => DEV' --tags='managed_by => automation'
272
273 =item B<--lookup-days>
274
275 Days backward to look up (default: '30').
276
277 =item B<--warning-subscription-costs>
278
279 Set warning threshold for subscription costs.
280
281 =item B<--critical-subscription-costs>
282
283 Set critical threshold for subscription costs.
284
285 =item B<--warning-resource-group-costs>
286
287 Set warning threshold for resource groups costs.
288
289 =item B<--critical-resource-group-costs>
290
291 Set critical threshold for resource groups costs.
292
293 =back
294
295 =cut