diff --git a/cloud/azure/compute/virtualmachine/mode/cpu.pm b/cloud/azure/compute/virtualmachine/mode/cpu.pm index cc76ac350..2a2aaa14d 100644 --- a/cloud/azure/compute/virtualmachine/mode/cpu.pm +++ b/cloud/azure/compute/virtualmachine/mode/cpu.pm @@ -101,23 +101,12 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'virtualMachines'; $self->{az_resource_namespace} = 'Microsoft.Compute'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Compute\/virtualMachines\/.*$/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900; $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT5M"; - $self->{az_aggregations} = ['Average']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -141,12 +130,16 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; - $resource_name = $1 if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Compute\/virtualMachines\/(.*)$/); + if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Compute\/virtualMachines\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; + } ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource, - resource_group => $self->{az_resource_group}, + resource => $resource_name, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, resource_namespace => $self->{az_resource_namespace}, metrics => $self->{az_metrics}, diff --git a/cloud/azure/compute/virtualmachine/mode/diskio.pm b/cloud/azure/compute/virtualmachine/mode/diskio.pm index bde99f1a3..1fcbb91f8 100644 --- a/cloud/azure/compute/virtualmachine/mode/diskio.pm +++ b/cloud/azure/compute/virtualmachine/mode/diskio.pm @@ -180,24 +180,13 @@ sub check_options { $self->{output}->add_option_msg(short_msg => "Need to specify either --resource with --resource-group option or --resource ."); $self->{output}->option_exit(); } - - $self->{az_resource_group} = ''; + + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'virtualMachines'; $self->{az_resource_namespace} = 'Microsoft.Compute'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Compute\/virtualMachines\/.*$/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900; $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT5M"; - $self->{az_aggregations} = ['Average']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -224,12 +213,16 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; - $resource_name = $1 if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Compute\/.*\/(.*)$/); + if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Compute\/virtualMachines\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; + } ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource, - resource_group => $self->{az_resource_group}, + resource => $resource_name, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, resource_namespace => $self->{az_resource_namespace}, metrics => $self->{az_metrics}, diff --git a/cloud/azure/compute/virtualmachine/mode/listresources.pm b/cloud/azure/compute/virtualmachine/mode/listresources.pm index 441e61072..54805ae0e 100644 --- a/cloud/azure/compute/virtualmachine/mode/listresources.pm +++ b/cloud/azure/compute/virtualmachine/mode/listresources.pm @@ -61,13 +61,25 @@ sub run { foreach my $vm (@{$self->{vms}}) { next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && $vm->{name} !~ /$self->{option_results}->{filter_name}/); + my $computer_name = '-'; + $computer_name = $vm->{properties}->{osProfile}->{computerName} if (defined($vm->{properties}->{osProfile}->{computerName})); + $computer_name = $vm->{osProfile}->{computerName} if (defined($vm->{osProfile}->{computerName})); + my $resource_group = '-'; + $resource_group = $vm->{resourceGroup} if (defined($vm->{resourceGroup})); + $resource_group = $1 if (defined($vm->{id}) && $vm->{id} =~ /resourceGroups\/(.*)\/providers/); $self->{output}->output_add(long_msg => sprintf("[name = %s][computername = %s][resourcegroup = %s]" . "[location = %s][vmid = %s][vmsize = %s][os = %s][state = %s]", - $vm->{name}, (defined($vm->{osProfile}->{computerName})) ? $vm->{osProfile}->{computerName} : "-", - $vm->{resourceGroup}, $vm->{location}, $vm->{vmId}, $vm->{hardwareProfile}->{vmSize}, - $vm->{storageProfile}->{osDisk}->{osType}, $vm->{powerState})); + $vm->{name}, + $computer_name, + $resource_group, + $vm->{location}, + (defined($vm->{properties}->{vmId})) ? $vm->{properties}->{vmId} : $vm->{vmId}, + (defined($vm->{properties}->{hardwareProfile}->{vmSize})) ? $vm->{properties}->{hardwareProfile}->{vmSize} : $vm->{hardwareProfile}->{vmSize}, + (defined($vm->{properties}->{storageProfile}->{osDisk}->{osType})) ? $vm->{properties}->{storageProfile}->{osDisk}->{osType} : $vm->{storageProfile}->{osDisk}->{osType}, + (defined($vm->{powerState})) ? $vm->{powerState} : "-", + )); } - + $self->{output}->output_add(severity => 'OK', short_msg => 'List vitual machines:'); $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); @@ -85,15 +97,21 @@ sub disco_show { $self->manage_selection(%options); foreach my $vm (@{$self->{vms}}) { + my $computer_name = '-'; + $computer_name = $vm->{properties}->{osProfile}->{computerName} if (defined($vm->{properties}->{osProfile}->{computerName})); + $computer_name = $vm->{osProfile}->{computerName} if (defined($vm->{osProfile}->{computerName})); + my $resource_group = '-'; + $resource_group = $vm->{resourceGroup} if (defined($vm->{resourceGroup})); + $resource_group = $1 if (defined($vm->{id}) && $vm->{id} =~ /resourceGroups\/(.*)\/providers/); $self->{output}->add_disco_entry( name => $vm->{name}, - computername => (defined($vm->{osProfile}->{computerName})) ? $vm->{osProfile}->{computerName} : "-", - resourcegroup => $vm->{resourceGroup}, + computername => $computer_name, + resourcegroup => $resource_group, location => $vm->{location}, - vmid => $vm->{vmId}, - vmsize => $vm->{hardwareProfile}->{vmSize}, - os => $vm->{storageProfile}->{osDisk}->{osType}, - state => $vm->{powerState}, + vmid => (defined($vm->{properties}->{vmId})) ? $vm->{properties}->{vmId} : $vm->{vmId}, + vmsize => (defined($vm->{properties}->{hardwareProfile}->{vmSize})) ? $vm->{properties}->{hardwareProfile}->{vmSize} : $vm->{hardwareProfile}->{vmSize}, + os => (defined($vm->{properties}->{storageProfile}->{osDisk}->{osType})) ? $vm->{properties}->{storageProfile}->{osDisk}->{osType} : $vm->{storageProfile}->{osDisk}->{osType}, + state => (defined($vm->{powerState})) ? $vm->{powerState} : "-", ); } } diff --git a/cloud/azure/compute/virtualmachine/mode/network.pm b/cloud/azure/compute/virtualmachine/mode/network.pm index 8e6856ed9..000c14089 100644 --- a/cloud/azure/compute/virtualmachine/mode/network.pm +++ b/cloud/azure/compute/virtualmachine/mode/network.pm @@ -140,23 +140,12 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'virtualMachines'; $self->{az_resource_namespace} = 'Microsoft.Compute'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Compute\/virtualMachines\/.*$/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900; $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT5M"; - $self->{az_aggregations} = ['Total']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -182,12 +171,16 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; - $resource_name = $1 if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Compute\/virtualMachines\/(.*)$/); + if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Compute\/virtualMachines\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; + } ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource, - resource_group => $self->{az_resource_group}, + resource => $resource_name, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, resource_namespace => $self->{az_resource_namespace}, metrics => $self->{az_metrics}, diff --git a/cloud/azure/compute/virtualmachine/mode/vmsizes.pm b/cloud/azure/compute/virtualmachine/mode/vmsizes.pm index 5ebf73fc7..3cbc2f2e8 100644 --- a/cloud/azure/compute/virtualmachine/mode/vmsizes.pm +++ b/cloud/azure/compute/virtualmachine/mode/vmsizes.pm @@ -159,15 +159,18 @@ sub manage_selection { my $vms = $options{custom}->azure_list_vms(resource_group => $self->{option_results}->{resource_group}, show_details => 1); foreach my $vm (@{$vms}) { - next if (defined($self->{option_results}->{running}) && $vm->{powerState} !~ /running/); + next if (defined($self->{option_results}->{running}) && defined($vm->{powerState}) && $vm->{powerState} !~ /running/); if (defined($self->{option_results}->{filter_size}) && $self->{option_results}->{filter_size} ne '' && - $vm->{hardwareProfile}->{vmSize} !~ /$self->{option_results}->{filter_size}/) { - $self->{output}->output_add(long_msg => sprintf("skipping size '%s'", $vm->{hardwareProfile}->{vmSize}), debug => 1); + (defined($vm->{hardwareProfile}->{vmSize}) && $vm->{hardwareProfile}->{vmSize} !~ /$self->{option_results}->{filter_size}/ || + defined($vm->{properties}->{hardwareProfile}->{vmSize}) && $vm->{properties}->{hardwareProfile}->{vmSize} !~ /$self->{option_results}->{filter_size}/)) { + $self->{output}->output_add(long_msg => sprintf("skipping size '%s'", $vm->{hardwareProfile}->{vmSize}), debug => 1) if (defined($vm->{hardwareProfile}->{vmSize})); + $self->{output}->output_add(long_msg => sprintf("skipping size '%s'", $vm->{properties}->{hardwareProfile}->{vmSize}), debug => 1) if (defined($vm->{properties}->{hardwareProfile}->{vmSize})); next; } foreach my $type (keys %vm_types) { next if (!defined($self->{maps_counters}->{$type})); - $self->{$type}->{$vm->{hardwareProfile}->{vmSize}}++ if (map(/$vm->{hardwareProfile}->{vmSize}/, @{$vm_types{$type}})); + $self->{$type}->{$vm->{hardwareProfile}->{vmSize}}++ if (defined($vm->{hardwareProfile}->{vmSize}) && map(/$vm->{hardwareProfile}->{vmSize}/, @{$vm_types{$type}})); + $self->{$type}->{$vm->{properties}->{hardwareProfile}->{vmSize}}++ if (defined($vm->{properties}->{hardwareProfile}->{vmSize}) && map(/$vm->{properties}->{hardwareProfile}->{vmSize}/, @{$vm_types{$type}})); } } @@ -255,7 +258,7 @@ Can be: 'standard-b1s', 'standard-b1ms', 'standard-b2s', 'standard-b2ms', 'stand =item B<--running> -Only check running virtual machines. +Only check running virtual machines (only with az CLI). =back diff --git a/cloud/azure/compute/virtualmachine/mode/vmsstate.pm b/cloud/azure/compute/virtualmachine/mode/vmsstate.pm index c43dd0aaf..635286835 100644 --- a/cloud/azure/compute/virtualmachine/mode/vmsstate.pm +++ b/cloud/azure/compute/virtualmachine/mode/vmsstate.pm @@ -191,7 +191,7 @@ __END__ =head1 MODE -Check virtual machines status. +Check virtual machines status (Only with az CLI). Example: perl centreon_plugins.pl --plugin=cloud::azure::compute::virtualmachine::plugin --custommode=azcli --mode=vms-state diff --git a/cloud/azure/compute/virtualmachine/plugin.pm b/cloud/azure/compute/virtualmachine/plugin.pm index 69d6b3d1f..c0022345a 100644 --- a/cloud/azure/compute/virtualmachine/plugin.pm +++ b/cloud/azure/compute/virtualmachine/plugin.pm @@ -40,9 +40,21 @@ sub new { ); $self->{custom_modes}{azcli} = 'cloud::azure::custom::azcli'; + $self->{custom_modes}{api} = 'cloud::azure::custom::api'; return $self; } +sub init { + my ($self, %options) = @_; + + $self->{options}->add_options(arguments => + { + 'api-version:s' => { name => 'api_version', default => '2018-01-01' }, + }); + + $self->SUPER::init(%options); +} + 1; __END__ diff --git a/cloud/azure/custom/api.pm b/cloud/azure/custom/api.pm new file mode 100644 index 000000000..07b9fa88e --- /dev/null +++ b/cloud/azure/custom/api.pm @@ -0,0 +1,472 @@ +# +# Copyright 2018 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package cloud::azure::custom::api; + +use strict; +use warnings; +use DateTime; +use centreon::plugins::http; +use centreon::plugins::statefile; +use JSON::XS; +use URI::Encode; +use Digest::MD5 qw(md5_hex); + +sub new { + my ($class, %options) = @_; + my $self = {}; + bless $self, $class; + + if (!defined($options{output})) { + print "Class Custom: Need to specify 'output' argument.\n"; + exit 3; + } + if (!defined($options{options})) { + $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument."); + $options{output}->option_exit(); + } + + if (!defined($options{noptions})) { + $options{options}->add_options(arguments => + { + "subscription:s" => { name => 'subscription' }, + "tenant:s" => { name => 'tenant' }, + "client-id:s" => { name => 'client_id' }, + "client-secret:s" => { name => 'client_secret' }, + "login-endpoint:s" => { name => 'login_endpoint' }, + "management-endpoint:s" => { name => 'management_endpoint' }, + "timeframe:s" => { name => 'timeframe' }, + "interval:s" => { name => 'interval' }, + "aggregation:s@" => { name => 'aggregation' }, + "zeroed" => { name => 'zeroed' }, + "timeout:s" => { name => 'timeout' }, + "proxyurl:s" => { name => 'proxyurl' }, + }); + } + $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); + + $self->{output} = $options{output}; + $self->{mode} = $options{mode}; + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + $self->{cache} = centreon::plugins::statefile->new(%options); + + return $self; +} + +sub set_options { + my ($self, %options) = @_; + + $self->{option_results} = $options{option_results}; +} + +sub set_defaults { + my ($self, %options) = @_; + + foreach (keys %{$options{default}}) { + if ($_ eq $self->{mode}) { + for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) { + foreach my $opt (keys %{$options{default}->{$_}[$i]}) { + if (!defined($self->{option_results}->{$opt}[$i])) { + $self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt}; + } + } + } + } + } +} + +sub check_options { + my ($self, %options) = @_; + + if (defined($self->{option_results}->{aggregation})) { + foreach my $aggregation (@{$self->{option_results}->{aggregation}}) { + if ($aggregation !~ /average|maximum|minimum|total/i) { + $self->{output}->add_option_msg(short_msg => "Aggregation '" . $aggregation . "' is not handled"); + $self->{output}->option_exit(); + } + } + } + + $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10; + $self->{proxyurl} = (defined($self->{option_results}->{proxyurl})) ? $self->{option_results}->{proxyurl} : undef; + $self->{ssl_opt} = (defined($self->{option_results}->{ssl_opt})) ? $self->{option_results}->{ssl_opt} : undef; + $self->{timeframe} = (defined($self->{option_results}->{timeframe})) ? $self->{option_results}->{timeframe} : undef; + $self->{step} = (defined($self->{option_results}->{step})) ? $self->{option_results}->{step} : undef; + $self->{subscription} = (defined($self->{option_results}->{subscription})) ? $self->{option_results}->{subscription} : undef; + $self->{tenant} = (defined($self->{option_results}->{tenant})) ? $self->{option_results}->{tenant} : undef; + $self->{client_id} = (defined($self->{option_results}->{client_id})) ? $self->{option_results}->{client_id} : undef; + $self->{client_secret} = (defined($self->{option_results}->{client_secret})) ? $self->{option_results}->{client_secret} : undef; + $self->{login_endpoint} = (defined($self->{option_results}->{login_endpoint})) ? $self->{option_results}->{login_endpoint} : 'https://login.microsoftonline.com'; + $self->{management_endpoint} = (defined($self->{option_results}->{management_endpoint})) ? $self->{option_results}->{management_endpoint} : 'https://management.azure.com'; + $self->{api_version} = (defined($self->{option_results}->{api_version})) ? $self->{option_results}->{api_version} : undef; + + if (!defined($self->{subscription}) || $self->{subscription} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --subscription option."); + $self->{output}->option_exit(); + } + if (!defined($self->{tenant}) || $self->{tenant} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --tenant option."); + $self->{output}->option_exit(); + } + if (!defined($self->{client_id}) || $self->{client_id} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --client-id option."); + $self->{output}->option_exit(); + } + if (!defined($self->{client_secret}) || $self->{client_secret} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --client-secret option."); + $self->{output}->option_exit(); + } + if (!defined($self->{api_version}) || $self->{api_version} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --api-version option."); + $self->{output}->option_exit(); + } + + $self->{cache}->check_options(option_results => $self->{option_results}); + + return 0; +} + +sub build_options_for_httplib { + my ($self, %options) = @_; + + $self->{option_results}->{timeout} = $self->{timeout}; + $self->{option_results}->{proxyurl} = $self->{proxyurl}; + $self->{option_results}->{ssl_opt} = $self->{ssl_opt}; + $self->{option_results}->{warning_status} = ''; + $self->{option_results}->{critical_status} = ''; + $self->{option_results}->{unknown_status} = ''; +} + +sub settings { + my ($self, %options) = @_; + + $self->build_options_for_httplib(); + $self->{http}->add_header(key => 'Accept', value => 'application/json'); + $self->{http}->add_header(key => 'Content-Type', value => 'application/x-www-form-urlencoded'); + if (defined($self->{access_token})) { + $self->{http}->add_header(key => 'Authorization', value => 'Bearer ' . $self->{access_token}); + } + $self->{http}->set_options(%{$self->{option_results}}); +} + +sub get_access_token { + my ($self, %options) = @_; + + my $has_cache_file = $options{statefile}->read(statefile => 'azure_api_' . md5_hex($self->{subscription}) . '_' . md5_hex($self->{tenant}) . '_' . md5_hex($self->{client_id})); + my $expires_on = $options{statefile}->get(name => 'expires_on'); + my $access_token = $options{statefile}->get(name => 'access_token'); + + if ($has_cache_file == 0 || !defined($access_token) || (($expires_on - time()) < 10)) { + my $uri = URI::Encode->new({encode_reserved => 1}); + my $encoded_management_endpoint = $uri->encode($self->{management_endpoint}); + my $post_data = 'grant_type=client_credentials' . + '&client_id=' . $self->{client_id} . + '&client_secret=' . $self->{client_secret} . + '&resource=' . $encoded_management_endpoint; + + $self->settings(); + + my $content = $self->{http}->request(method => 'POST', query_form_post => $post_data, + full_url => $self->{login_endpoint} . '/' . $self->{tenant} . '/oauth2/token', + hostname => ''); + + my $decoded; + eval { + $decoded = JSON::XS->new->utf8->decode($content); + }; + if ($@) { + $self->{output}->output_add(long_msg => $content, debug => 1); + $self->{output}->add_option_msg(short_msg => "Cannot decode json response"); + $self->{output}->option_exit(); + } + if (defined($decoded->{error})) { + $self->{output}->output_add(long_msg => "Error message : " . $decoded->{error_description}, debug => 1); + $self->{output}->add_option_msg(short_msg => "Login endpoint API return error code '" . $decoded->{error} . "' (add --debug option for detailed message)"); + $self->{output}->option_exit(); + } + + $access_token = $decoded->{access_token}; + my $datas = { last_timestamp => time(), access_token => $decoded->{access_token}, expires_on => $decoded->{expires_on} }; + $options{statefile}->write(data => $datas); + } + + return $access_token; +} + +sub request_api { + my ($self, %options) = @_; + + if (!defined($self->{access_token})) { + $self->{access_token} = $self->get_access_token(statefile => $self->{cache}); + } + + $self->settings(); + + my $content = $self->{http}->request(%options); + + my $decoded; + eval { + $decoded = JSON::XS->new->utf8->decode($content); + }; + if ($@) { + $self->{output}->output_add(long_msg => $content, debug => 1); + $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); + $self->{output}->option_exit(); + } + if (defined($decoded->{error})) { + $self->{output}->output_add(long_msg => "Error message : " . $decoded->{error}->{message}, debug => 1); + $self->{output}->add_option_msg(short_msg => "Management endpoint API return error code '" . $decoded->{error}->{code} . "' (add --debug option for detailed message)"); + $self->{output}->option_exit(); + } + + return $decoded; +} + +sub azure_get_metrics_set_url { + my ($self, %options) = @_; + + my $uri = URI::Encode->new({encode_reserved => 1}); + my $encoded_metrics = $uri->encode(join(',', @{$options{metrics}})); + my $encoded_aggregations = $uri->encode(join(',', @{$options{aggregations}})); + my $encoded_timespan = $uri->encode($options{start_time} . '/' . $options{end_time}); + + my $url = $self->{management_endpoint} . "/subscriptions/" . $self->{subscription} . "/resourceGroups/" . $options{resource_group} . + "/providers/" . $options{resource_namespace} . "/" . $options{resource_type} . "/" . $options{resource} . '/providers/microsoft.insights/metrics' . + "?api-version=" . $self->{api_version} . "&metricnames=" . $encoded_metrics . "&aggregation=" . $encoded_aggregations . "×pan=" . $encoded_timespan; + + return $url; +} + +sub azure_get_metrics { + my ($self, %options) = @_; + + my $results = {}; + my $start_time = DateTime->now->subtract(seconds => $options{timeframe})->iso8601.'Z'; + my $end_time = DateTime->now->iso8601.'Z'; + + my $full_url = $self->azure_get_metrics_set_url(%options, start_time => $start_time, end_time => $end_time); + my $response = $self->request_api(method => 'GET', full_url => $full_url, hostname => ''); + + foreach my $metric (@{$response->{value}}) { + my $metric_name = lc($metric->{name}->{value}); + $metric_name =~ s/ /_/g; + + $results->{$metric_name} = { points => 0 }; + foreach my $timeserie (@{$metric->{timeseries}}) { + foreach my $point (@{$timeserie->{data}}) { + if (defined($point->{average})) { + $results->{$metric_name}->{average} = 0 if (!defined($results->{$metric_name}->{average})); + $results->{$metric_name}->{average} += $point->{average}; + $results->{$metric_name}->{points}++; + } + if (defined($point->{minimum})) { + $results->{$metric_name}->{minimum} = $point->{minimum} + if (!defined($results->{$metric_name}->{minimum}) || $point->{minimum} < $results->{$metric_name}->{minimum}); + } + if (defined($point->{maximum})) { + $results->{$metric_name}->{maximum} = $point->{maximum} + if (!defined($results->{$metric_name}->{maximum}) || $point->{maximum} > $results->{$metric_name}->{maximum}); + } + if (defined($point->{total})) { + $results->{$metric_name}->{total} = 0 if (!defined($results->{$metric_name}->{total})); + $results->{$metric_name}->{total} += $point->{total}; + $results->{$metric_name}->{points}++; + } + } + } + + if (defined($results->{$metric_name}->{average})) { + $results->{$metric_name}->{average} /= $results->{$metric_name}->{points}; + } + } + + return $results, $response; +} + +sub azure_list_resources_set_url { + my ($self, %options) = @_; + + my $filter = ''; + my %filter; + $filter{resource_type} = "resourceType eq '" . $options{namespace} . '/' . $options{resource_type} . "'" if (defined($options{namespace}) && $options{namespace} ne '' && defined($options{resource_type}) && $options{resource_type} ne ''); + $filter{resource_group} = "resourceGroup eq '" . $options{resource_group} . "'" if (defined($options{resource_group}) && $options{resource_group} ne ''); + $filter{location} = "location eq '" . $options{location} . "'" if (defined($options{location}) && $options{location} ne ''); + + my $append = ''; + foreach (('resource_type', 'resource_group', 'location')) { + next if (!defined($filter{$_})); + $filter .= $append . $filter{$_}; + $append = ' and '; + } + + my $uri = URI::Encode->new({encode_reserved => 1}); + my $encoded_filter = $uri->encode($filter); + + my $url = $self->{management_endpoint} . "/subscriptions/" . $self->{subscription} . "/resources?api-version=" . $self->{api_version}; + $url .= "&\$filter=" . $encoded_filter if (defined($encoded_filter) && $encoded_filter ne ''); + + return $url; +} + +sub azure_list_resources { + my ($self, %options) = @_; + + my $results = {}; + my $full_url = $self->azure_list_resources_set_url(%options); + my $response = $self->request_api(method => 'GET', full_url => $full_url, hostname => ''); + + return $response->{value}; +} + +sub azure_list_vms_set_url { + my ($self, %options) = @_; + + my $url = $self->{management_endpoint} . "/subscriptions/" . $self->{subscription}; + $url .= "/resourceGroups/" . $options{resource_group} if (defined($options{resource_group}) && $options{resource_group} ne ''); + $url .= "/providers/Microsoft.Compute/virtualMachines?api-version=" . $self->{api_version}; + + return $url; +} + +sub azure_list_vms { + my ($self, %options) = @_; + + my $results = {}; + my $full_url = $self->azure_list_vms_set_url(%options); + my $response = $self->request_api(method => 'GET', full_url => $full_url, hostname => ''); + + return $response->{value}; +} + +sub azure_list_groups_set_url { + my ($self, %options) = @_; + + my $url = $self->{management_endpoint} . "/subscriptions/" . $self->{subscription} . "/resourcegroups?api-version=" . $self->{api_version}; + + return $url; +} + +sub azure_list_groups { + my ($self, %options) = @_; + + my $results = {}; + my $full_url = $self->azure_list_groups_set_url(%options); + my $response = $self->request_api(method => 'GET', full_url => $full_url, hostname => ''); + + return $response->{value}; +} + +sub azure_list_deployments_set_url { + my ($self, %options) = @_; + + my $url = $self->{management_endpoint} . "/subscriptions/" . $self->{subscription} . "/resourcegroups/" . + $options{resource_group} . "/providers/Microsoft.Resources/deployments?api-version=" . $self->{api_version}; + + return $url; +} + +sub azure_list_deployments { + my ($self, %options) = @_; + + my $results = {}; + my $full_url = $self->azure_list_deployments_set_url(%options); + my $response = $self->request_api(method => 'GET', full_url => $full_url, hostname => ''); + + return $response->{value}; +} + +1; + +__END__ + +=head1 NAME + +Microsoft Azure Rest API + +=head1 REST API OPTIONS + +Microsoft Azure Rest API + +To connect to the Azure Rest API, you must register an application. + +Follow the 'How-to guide' in https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal + +This custom mode is using the 'OAuth 2.0 Client Credentials Grant Flow' + +For futher informations, visit https://docs.microsoft.com/en-us/azure/active-directory/develop/v1-oauth2-client-creds-grant-flow + +=over 8 + +=item B<--subscription> + +Set Azure subscription ID. + +=item B<--tenant> + +Set Azure tenant ID. + +=item B<--client-id> + +Set Azure client ID. + +=item B<--client-secret> + +Set Azure client secret. + +=item B<--login-endpoint> + +Set Azure login endpoint URL (Default: 'https://login.microsoftonline.com') + +=item B<--management-endpoint> + +Set Azure management endpoint URL (Default: 'https://management.azure.com') + +=item B<--timeframe> + +Set timeframe in seconds (i.e. 3600 to check last hour). + +=item B<--interval> + +Set interval of the metric query (Can be : PT1M, PT5M, PT15M, PT30M, PT1H, PT6H, PT12H, PT24H). + +=item B<--aggregation> + +Set monitor aggregation (Can be multiple, Can be: 'minimum', 'maximum', 'average', 'total'). + +=item B<--zeroed> + +Set metrics value to 0 if none. Usefull when Monitor +does not return value when not defined. + +=item B<--timeout> + +Set timeout in seconds (Default: 10). + +=item B<--proxyurl> + +Proxy URL if any + +=back + +=head1 DESCRIPTION + +B. + +=cut diff --git a/cloud/azure/custom/azcli.pm b/cloud/azure/custom/azcli.pm index 97aad25bc..c0ebf0cfe 100644 --- a/cloud/azure/custom/azcli.pm +++ b/cloud/azure/custom/azcli.pm @@ -116,7 +116,7 @@ sub azure_get_metrics_set_cmd { "--interval $options{interval} --aggregation '" . join('\' \'', @{$options{aggregations}}) . "' --output json --resource '$options{resource}' " . "--resource-group '$options{resource_group}' --resource-type '$options{resource_type}' --resource-namespace '$options{resource_namespace}'"; $cmd_options .= " --subscription '$self->{subscription}'" if (defined($self->{subscription}) && $self->{subscription} ne ''); - + return $cmd_options; } @@ -193,7 +193,7 @@ sub azure_list_resources_set_cmd { $cmd_options .= " --location '$options{location}'" if (defined($options{location}) && $options{location} ne ''); $cmd_options .= " --resource-group '$options{resource_group}'" if (defined($options{resource_group}) && $options{resource_group} ne ''); $cmd_options .= " --subscription '$self->{subscription}'" if (defined($self->{subscription}) && $self->{subscription} ne ''); - + return $cmd_options; } @@ -222,7 +222,7 @@ sub azure_list_resources { $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); $self->{output}->option_exit(); } - + return $raw_results; } @@ -262,7 +262,7 @@ sub azure_list_vm_sizes { $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); $self->{output}->option_exit(); } - + return $raw_results; } @@ -275,7 +275,7 @@ sub azure_list_vms_set_cmd { $cmd_options .= " --resource-group '$options{resource_group}'" if (defined($options{resource_group}) && $options{resource_group} ne ''); $cmd_options .= " --show-details" if (defined($options{show_details})); $cmd_options .= " --subscription '$self->{subscription}'" if (defined($self->{subscription}) && $self->{subscription} ne ''); - + return $cmd_options; } @@ -304,7 +304,7 @@ sub azure_list_vms { $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); $self->{output}->option_exit(); } - + return $raw_results; } @@ -315,7 +315,7 @@ sub azure_list_groups_set_cmd { my $cmd_options = "group list --output json"; $cmd_options .= " --subscription '$self->{subscription}'" if (defined($self->{subscription}) && $self->{subscription} ne ''); - + return $cmd_options; } @@ -344,7 +344,7 @@ sub azure_list_groups { $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); $self->{output}->option_exit(); } - + return $raw_results; } @@ -355,7 +355,7 @@ sub azure_list_deployments_set_cmd { my $cmd_options = "group deployment list --resource-group '$options{resource_group}' --output json"; $cmd_options .= " --subscription '$self->{subscription}'" if (defined($self->{subscription}) && $self->{subscription} ne ''); - + return $cmd_options; } @@ -384,7 +384,7 @@ sub azure_list_deployments { $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); $self->{output}->option_exit(); } - + return $raw_results; } @@ -396,23 +396,28 @@ __END__ Microsoft Azure CLI -=head1 SYNOPSIS +=head1 AZCLI OPTIONS Microsoft Azure CLI 2.0 To install the Azure CLI 2.0 in a CentOS/RedHat environment : + (As root) + # rpm --import https://packages.microsoft.com/keys/microsoft.asc + # sh -c 'echo -e "[azure-cli]\nname=Azure CLI\nbaseurl=https://packages.microsoft.com/yumrepos/azure-cli\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/azure-cli.repo' + # yum install azure-cli + (As centreon-engine) + # az login + Go to https://aka.ms/devicelogin and enter the code given by the last command. For futher informations, visit https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest. -=head1 AZCLI OPTIONS - =over 8 =item B<--subscription> diff --git a/cloud/azure/management/resource/mode/items.pm b/cloud/azure/management/resource/mode/items.pm index 2ae8f5705..07b731389 100644 --- a/cloud/azure/management/resource/mode/items.pm +++ b/cloud/azure/management/resource/mode/items.pm @@ -132,7 +132,10 @@ sub manage_selection { }; foreach my $item (@{$resources}) { - next if ($item->{resourceGroup} !~ /$group->{name}/); + my $resource_group = ''; + $resource_group = $item->{resourceGroup} if (defined($item->{resourceGroup})); + $resource_group = $1 if (defined($item->{id}) && $item->{id} =~ /resourceGroups\/(.*)\/providers/); + next if (defined($resource_group) && $resource_group !~ /$group->{name}/); next if (!defined($self->{option_results}->{hidden}) && $item->{type} =~ /^Microsoft\..*\/.*\/.*/); $self->{groups}->{$group->{name}}->{total}++; $self->{groups}->{$group->{name}}->{compute}++ if ($item->{type} =~ /^Microsoft\.Compute\//); diff --git a/cloud/azure/management/resource/mode/listresources.pm b/cloud/azure/management/resource/mode/listresources.pm index 9183dee5e..66fd33cb4 100644 --- a/cloud/azure/management/resource/mode/listresources.pm +++ b/cloud/azure/management/resource/mode/listresources.pm @@ -67,8 +67,16 @@ sub run { && $resource->{name} !~ /$self->{option_results}->{filter_name}/); next if (defined($self->{option_results}->{filter_type}) && $self->{option_results}->{filter_type} ne '' && $resource->{type} !~ /$self->{option_results}->{filter_type}/); + my $resource_group = '-'; + $resource_group = $resource->{resourceGroup} if (defined($resource->{resourceGroup})); + $resource_group = $1 if (defined($resource->{id}) && $resource->{id} =~ /resourceGroups\/(.*)\/providers/); $self->{output}->output_add(long_msg => sprintf("[name = %s][resourcegroup = %s][location = %s][id = %s][type = %s]", - $resource->{name}, $resource->{resourceGroup}, $resource->{location}, $resource->{id}, $resource->{type})); + $resource->{name}, + $resource_group, + $resource->{location}, + $resource->{id}, + $resource->{type} + )); } $self->{output}->output_add(severity => 'OK', @@ -88,9 +96,12 @@ sub disco_show { $self->manage_selection(%options); foreach my $resource (@{$self->{resources}}) { + my $resource_group = '-'; + $resource_group = $resource->{resourceGroup} if (defined($resource->{resourceGroup})); + $resource_group = $1 if (defined($resource->{id}) && $resource->{id} =~ /resourceGroups\/(.*)\/providers/); $self->{output}->add_disco_entry( name => $resource->{name}, - resourcegroup => $resource->{resourceGroup}, + resourcegroup => $resource_group, location => $resource->{location}, id => $resource->{id}, type => $resource->{type}, diff --git a/cloud/azure/management/resource/plugin.pm b/cloud/azure/management/resource/plugin.pm index 3b23064ac..2bbefb5ba 100644 --- a/cloud/azure/management/resource/plugin.pm +++ b/cloud/azure/management/resource/plugin.pm @@ -38,9 +38,20 @@ sub new { ); $self->{custom_modes}{azcli} = 'cloud::azure::custom::azcli'; + $self->{custom_modes}{api} = 'cloud::azure::custom::api'; return $self; } +sub init { + my ($self, %options) = @_; + + $self->{options}->add_options(arguments => + { + 'api-version:s' => { name => 'api_version', default => '2018-01-01' }, + }); + + $self->SUPER::init(%options); +} 1; __END__ diff --git a/cloud/azure/network/networkinterface/mode/listresources.pm b/cloud/azure/network/networkinterface/mode/listresources.pm index 7709e2340..6dc00316f 100644 --- a/cloud/azure/network/networkinterface/mode/listresources.pm +++ b/cloud/azure/network/networkinterface/mode/listresources.pm @@ -64,8 +64,15 @@ sub run { foreach my $resource (@{$self->{resources}}) { next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && $resource->{name} !~ /$self->{option_results}->{filter_name}/); + my $resource_group = '-'; + $resource_group = $resource->{resourceGroup} if (defined($resource->{resourceGroup})); + $resource_group = $1 if (defined($resource->{id}) && $resource->{id} =~ /resourceGroups\/(.*)\/providers/); $self->{output}->output_add(long_msg => sprintf("[name = %s][resourcegroup = %s][location = %s][id = %s]", - $resource->{name}, $resource->{resourceGroup}, $resource->{location}, $resource->{id})); + $resource->{name}, + $resource_group, + $resource->{location}, + $resource->{id} + )); } $self->{output}->output_add(severity => 'OK', @@ -77,7 +84,7 @@ sub run { sub disco_format { my ($self, %options) = @_; - $self->{output}->add_disco_format(elements => ['name', 'resourcegroup', 'location', 'id'); + $self->{output}->add_disco_format(elements => ['name', 'resourcegroup', 'location', 'id']); } sub disco_show { @@ -85,9 +92,12 @@ sub disco_show { $self->manage_selection(%options); foreach my $resource (@{$self->{resources}}) { + my $resource_group = '-'; + $resource_group = $resource->{resourceGroup} if (defined($resource->{resourceGroup})); + $resource_group = $1 if (defined($resource->{id}) && $resource->{id} =~ /resourceGroups\/(.*)\/providers/); $self->{output}->add_disco_entry( name => $resource->{name}, - resourcegroup => $resource->{resourceGroup}, + resourcegroup => $resource_group, location => $resource->{location}, id => $resource->{id}, ); diff --git a/cloud/azure/network/networkinterface/mode/traffic.pm b/cloud/azure/network/networkinterface/mode/traffic.pm index eacb5905a..155ab5683 100644 --- a/cloud/azure/network/networkinterface/mode/traffic.pm +++ b/cloud/azure/network/networkinterface/mode/traffic.pm @@ -185,23 +185,12 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'networkInterfaces'; $self->{az_resource_namespace} = 'Microsoft.Network'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Network\/networkInterfaces\/.*$/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900; $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT5M"; - $self->{az_aggregations} = ['Total']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -227,12 +216,16 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; - $resource_name = $1 if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Network\/networkInterfaces\/(.*)$/); + if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Network\/networkInterfaces\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; + } ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource, - resource_group => $self->{az_resource_group}, + resource => $resource_name, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, resource_namespace => $self->{az_resource_namespace}, metrics => $self->{az_metrics}, diff --git a/cloud/azure/network/networkinterface/plugin.pm b/cloud/azure/network/networkinterface/plugin.pm index c0ef1ae52..0e25cfda3 100644 --- a/cloud/azure/network/networkinterface/plugin.pm +++ b/cloud/azure/network/networkinterface/plugin.pm @@ -36,9 +36,21 @@ sub new { ); $self->{custom_modes}{azcli} = 'cloud::azure::custom::azcli'; + $self->{custom_modes}{api} = 'cloud::azure::custom::api'; return $self; } +sub init { + my ($self, %options) = @_; + + $self->{options}->add_options(arguments => + { + 'api-version:s' => { name => 'api_version', default => '2018-01-01' }, + }); + + $self->SUPER::init(%options); +} + 1; __END__ diff --git a/cloud/azure/storage/storageaccount/mode/accountusedcapacity.pm b/cloud/azure/storage/storageaccount/mode/accountusedcapacity.pm index 07ec0f092..9af3b481d 100644 --- a/cloud/azure/storage/storageaccount/mode/accountusedcapacity.pm +++ b/cloud/azure/storage/storageaccount/mode/accountusedcapacity.pm @@ -81,23 +81,12 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'storageAccounts'; $self->{az_resource_namespace} = 'Microsoft.Storage'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/.*/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 3600; $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT1H"; - $self->{az_aggregations} = ['Total']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -118,14 +107,16 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; - if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { - $resource_name = $1; + if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; } ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource, - resource_group => $self->{az_resource_group}, + resource => $resource_name, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, resource_namespace => $self->{az_resource_namespace}, metrics => $self->{az_metrics}, diff --git a/cloud/azure/storage/storageaccount/mode/blobcapacity.pm b/cloud/azure/storage/storageaccount/mode/blobcapacity.pm index 4117fae24..f40de401c 100644 --- a/cloud/azure/storage/storageaccount/mode/blobcapacity.pm +++ b/cloud/azure/storage/storageaccount/mode/blobcapacity.pm @@ -81,23 +81,12 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'storageAccounts'; $self->{az_resource_namespace} = 'Microsoft.Storage'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/.*/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 3600; $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT1H"; - $self->{az_aggregations} = ['Total']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -118,16 +107,17 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; my $namespace_full = '/blobServices/default'; - if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/.*\/default$/) { - $resource_name = $1; - $namespace_full = ''; + if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; } ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource . $namespace_full, - resource_group => $self->{az_resource_group}, + resource => $resource_name . $namespace_full, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, resource_namespace => $self->{az_resource_namespace}, metrics => $self->{az_metrics}, diff --git a/cloud/azure/storage/storageaccount/mode/blobcontainercount.pm b/cloud/azure/storage/storageaccount/mode/blobcontainercount.pm index 86f0cc415..6962ae996 100644 --- a/cloud/azure/storage/storageaccount/mode/blobcontainercount.pm +++ b/cloud/azure/storage/storageaccount/mode/blobcontainercount.pm @@ -80,23 +80,12 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'storageAccounts'; $self->{az_resource_namespace} = 'Microsoft.Storage'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/.*/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 3600; $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT1H"; - $self->{az_aggregations} = ['Average']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -117,16 +106,17 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; my $namespace_full = '/blobServices/default'; - if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/.*\/default$/) { - $resource_name = $1; - $namespace_full = ''; + if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; } ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource . $namespace_full, - resource_group => $self->{az_resource_group}, + resource => $resource_name . $namespace_full, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, resource_namespace => $self->{az_resource_namespace}, metrics => $self->{az_metrics}, diff --git a/cloud/azure/storage/storageaccount/mode/blobcount.pm b/cloud/azure/storage/storageaccount/mode/blobcount.pm index 508c81a49..b8afea6ae 100644 --- a/cloud/azure/storage/storageaccount/mode/blobcount.pm +++ b/cloud/azure/storage/storageaccount/mode/blobcount.pm @@ -80,23 +80,12 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'storageAccounts'; $self->{az_resource_namespace} = 'Microsoft.Storage'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/.*/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 3600; $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT1H"; - $self->{az_aggregations} = ['Total']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -117,16 +106,17 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; my $namespace_full = '/blobServices/default'; - if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/.*\/default$/) { - $resource_name = $1; - $namespace_full = ''; + if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; } ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource . $namespace_full, - resource_group => $self->{az_resource_group}, + resource => $resource_name . $namespace_full, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, resource_namespace => $self->{az_resource_namespace}, metrics => $self->{az_metrics}, diff --git a/cloud/azure/storage/storageaccount/mode/filecapacity.pm b/cloud/azure/storage/storageaccount/mode/filecapacity.pm index d884b4391..c6e9f6d5b 100644 --- a/cloud/azure/storage/storageaccount/mode/filecapacity.pm +++ b/cloud/azure/storage/storageaccount/mode/filecapacity.pm @@ -81,23 +81,12 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'storageAccounts'; $self->{az_resource_namespace} = 'Microsoft.Storage'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/.*/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 3600; - $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT1H"; - + $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT1H"; $self->{az_aggregations} = ['Average']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -118,16 +107,17 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; my $namespace_full = '/fileServices/default'; - if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/.*\/default$/) { - $resource_name = $1; - $namespace_full = ''; + if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; } ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource . $namespace_full, - resource_group => $self->{az_resource_group}, + resource => $resource_name . $namespace_full, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, resource_namespace => $self->{az_resource_namespace}, metrics => $self->{az_metrics}, diff --git a/cloud/azure/storage/storageaccount/mode/filecount.pm b/cloud/azure/storage/storageaccount/mode/filecount.pm index 23f4de70e..ff93691c0 100644 --- a/cloud/azure/storage/storageaccount/mode/filecount.pm +++ b/cloud/azure/storage/storageaccount/mode/filecount.pm @@ -80,23 +80,12 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'storageAccounts'; $self->{az_resource_namespace} = 'Microsoft.Storage'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/.*/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 3600; $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT1H"; - $self->{az_aggregations} = ['Average']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -117,16 +106,17 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; my $namespace_full = '/fileServices/default'; - if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/.*\/default$/) { - $resource_name = $1; - $namespace_full = ''; + if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; } ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource . $namespace_full, - resource_group => $self->{az_resource_group}, + resource => $resource_name . $namespace_full, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, resource_namespace => $self->{az_resource_namespace}, metrics => $self->{az_metrics}, diff --git a/cloud/azure/storage/storageaccount/mode/filesharecount.pm b/cloud/azure/storage/storageaccount/mode/filesharecount.pm index 28a40e367..497517858 100644 --- a/cloud/azure/storage/storageaccount/mode/filesharecount.pm +++ b/cloud/azure/storage/storageaccount/mode/filesharecount.pm @@ -80,23 +80,12 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'storageAccounts'; $self->{az_resource_namespace} = 'Microsoft.Storage'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/.*/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 3600; - $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT1H"; - + $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT1H"; $self->{az_aggregations} = ['Average']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -117,16 +106,17 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; my $namespace_full = '/fileServices/default'; - if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/.*\/default$/) { - $resource_name = $1; - $namespace_full = ''; + if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; } ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource . $namespace_full, - resource_group => $self->{az_resource_group}, + resource => $resource_name . $namespace_full, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, resource_namespace => $self->{az_resource_namespace}, metrics => $self->{az_metrics}, diff --git a/cloud/azure/storage/storageaccount/mode/listresources.pm b/cloud/azure/storage/storageaccount/mode/listresources.pm index 803c5c112..da2864a62 100644 --- a/cloud/azure/storage/storageaccount/mode/listresources.pm +++ b/cloud/azure/storage/storageaccount/mode/listresources.pm @@ -64,8 +64,15 @@ sub run { foreach my $resource (@{$self->{resources}}) { next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && $resource->{name} !~ /$self->{option_results}->{filter_name}/); + my $resource_group = '-'; + $resource_group = $resource->{resourceGroup} if (defined($resource->{resourceGroup})); + $resource_group = $1 if (defined($resource->{id}) && $resource->{id} =~ /resourceGroups\/(.*)\/providers/); $self->{output}->output_add(long_msg => sprintf("[name = %s][resourcegroup = %s][location = %s][id = %s]", - $resource->{name}, $resource->{resourceGroup}, $resource->{location}, $resource->{id})); + $resource->{name}, + $resource_group, + $resource->{location}, + $resource->{id} + )); } $self->{output}->output_add(severity => 'OK', @@ -85,9 +92,12 @@ sub disco_show { $self->manage_selection(%options); foreach my $resource (@{$self->{resources}}) { + my $resource_group = '-'; + $resource_group = $resource->{resourceGroup} if (defined($resource->{resourceGroup})); + $resource_group = $1 if (defined($resource->{id}) && $resource->{id} =~ /resourceGroups\/(.*)\/providers/); $self->{output}->add_disco_entry( name => $resource->{name}, - resourcegroup => $resource->{resourceGroup}, + resourcegroup => $resource_group, location => $resource->{location}, id => $resource->{id}, ); diff --git a/cloud/azure/storage/storageaccount/mode/queuecapacity.pm b/cloud/azure/storage/storageaccount/mode/queuecapacity.pm index 03b2daa93..6cce547c9 100644 --- a/cloud/azure/storage/storageaccount/mode/queuecapacity.pm +++ b/cloud/azure/storage/storageaccount/mode/queuecapacity.pm @@ -81,23 +81,12 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'storageAccounts'; $self->{az_resource_namespace} = 'Microsoft.Storage'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/.*/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 3600; $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT1H"; - $self->{az_aggregations} = ['Average']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -118,16 +107,17 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; my $namespace_full = '/queueServices/default'; - if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/.*\/default$/) { - $resource_name = $1; - $namespace_full = ''; + if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; } ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource . $namespace_full, - resource_group => $self->{az_resource_group}, + resource => $resource_name . $namespace_full, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, resource_namespace => $self->{az_resource_namespace}, metrics => $self->{az_metrics}, diff --git a/cloud/azure/storage/storageaccount/mode/queuecount.pm b/cloud/azure/storage/storageaccount/mode/queuecount.pm index 33234e387..c1df4cfdc 100644 --- a/cloud/azure/storage/storageaccount/mode/queuecount.pm +++ b/cloud/azure/storage/storageaccount/mode/queuecount.pm @@ -80,23 +80,12 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'storageAccounts'; $self->{az_resource_namespace} = 'Microsoft.Storage'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/.*/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 3600; $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT1H"; - $self->{az_aggregations} = ['Average']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -117,16 +106,17 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; my $namespace_full = '/queueServices/default'; - if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/.*\/default$/) { - $resource_name = $1; - $namespace_full = ''; + if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; } ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource . $namespace_full, - resource_group => $self->{az_resource_group}, + resource => $resource_name . $namespace_full, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, resource_namespace => $self->{az_resource_namespace}, metrics => $self->{az_metrics}, diff --git a/cloud/azure/storage/storageaccount/mode/queuemessagecount.pm b/cloud/azure/storage/storageaccount/mode/queuemessagecount.pm index 0f202da2c..b51489bd0 100644 --- a/cloud/azure/storage/storageaccount/mode/queuemessagecount.pm +++ b/cloud/azure/storage/storageaccount/mode/queuemessagecount.pm @@ -80,23 +80,12 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'storageAccounts'; $self->{az_resource_namespace} = 'Microsoft.Storage'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/.*/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 3600; $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT1H"; - $self->{az_aggregations} = ['Average']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -117,16 +106,17 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; my $namespace_full = '/queueServices/default'; - if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/.*\/default$/) { - $resource_name = $1; - $namespace_full = ''; + if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; } ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource . $namespace_full, - resource_group => $self->{az_resource_group}, + resource => $resource_name . $namespace_full, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, resource_namespace => $self->{az_resource_namespace}, metrics => $self->{az_metrics}, diff --git a/cloud/azure/storage/storageaccount/mode/tablecapacity.pm b/cloud/azure/storage/storageaccount/mode/tablecapacity.pm index 2e6d6f059..88c1878fd 100644 --- a/cloud/azure/storage/storageaccount/mode/tablecapacity.pm +++ b/cloud/azure/storage/storageaccount/mode/tablecapacity.pm @@ -81,23 +81,12 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'storageAccounts'; $self->{az_resource_namespace} = 'Microsoft.Storage'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/.*/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 3600; $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT1H"; - $self->{az_aggregations} = ['Average']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -118,16 +107,17 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; my $namespace_full = '/tableServices/default'; - if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/.*\/default$/) { - $resource_name = $1; - $namespace_full = ''; + if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; } ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource . $namespace_full, - resource_group => $self->{az_resource_group}, + resource => $resource_name . $namespace_full, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, resource_namespace => $self->{az_resource_namespace}, metrics => $self->{az_metrics}, diff --git a/cloud/azure/storage/storageaccount/mode/tablecount.pm b/cloud/azure/storage/storageaccount/mode/tablecount.pm index dccef8d07..575caf5f9 100644 --- a/cloud/azure/storage/storageaccount/mode/tablecount.pm +++ b/cloud/azure/storage/storageaccount/mode/tablecount.pm @@ -80,23 +80,12 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'storageAccounts'; $self->{az_resource_namespace} = 'Microsoft.Storage'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/.*/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 3600; $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT1H"; - $self->{az_aggregations} = ['Average']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -117,16 +106,17 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; my $namespace_full = '/tableServices/default'; - if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/.*\/default$/) { - $resource_name = $1; - $namespace_full = ''; + if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; } ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource . $namespace_full, - resource_group => $self->{az_resource_group}, + resource => $resource_name . $namespace_full, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, resource_namespace => $self->{az_resource_namespace}, metrics => $self->{az_metrics}, diff --git a/cloud/azure/storage/storageaccount/mode/tableentitycount.pm b/cloud/azure/storage/storageaccount/mode/tableentitycount.pm index 51dc97e5c..54245424a 100644 --- a/cloud/azure/storage/storageaccount/mode/tableentitycount.pm +++ b/cloud/azure/storage/storageaccount/mode/tableentitycount.pm @@ -80,23 +80,12 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'storageAccounts'; $self->{az_resource_namespace} = 'Microsoft.Storage'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/.*/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 3600; $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT1H"; - $self->{az_aggregations} = ['Average']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -117,16 +106,17 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; my $namespace_full = '/tableServices/default'; - if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/.*\/default$/) { - $resource_name = $1; - $namespace_full = ''; + if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; } ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource . $namespace_full, - resource_group => $self->{az_resource_group}, + resource => $resource_name . $namespace_full, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, resource_namespace => $self->{az_resource_namespace}, metrics => $self->{az_metrics}, diff --git a/cloud/azure/storage/storageaccount/mode/transactionsavailability.pm b/cloud/azure/storage/storageaccount/mode/transactionsavailability.pm index 8187ffc5b..84faff0b5 100644 --- a/cloud/azure/storage/storageaccount/mode/transactionsavailability.pm +++ b/cloud/azure/storage/storageaccount/mode/transactionsavailability.pm @@ -85,24 +85,13 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'storageAccounts'; $self->{az_resource_namespace} = 'Microsoft.Storage'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/.*/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900; $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT5M"; $self->{az_resource_extra_namespace} = defined($self->{option_results}->{resource_namespace}) ? $self->{option_results}->{resource_namespace} : ''; - $self->{az_aggregations} = ['Average']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -123,21 +112,25 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; + my $namespace = $self->{az_resource_namespace}; my $namespace_full = ($self->{az_resource_extra_namespace} ne '') ? '/' . lc($self->{az_resource_extra_namespace}) . 'Services/default' : ''; my $namespace_name = ($self->{az_resource_extra_namespace} ne '') ? $self->{az_resource_extra_namespace} : "Account"; - if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/(.*)\/default$/ || - $resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { - $resource_name = $1; - $namespace_name = $2 if(defined($2)); + if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/(.*)\/default$/ || + $resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; + $namespace_full = '/' . $3 . '/default' if(defined($3)); + $namespace_name = $3 if(defined($3)); } $namespace_name =~ s/Services//g; ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource . $namespace_full, - resource_group => $self->{az_resource_group}, + resource => $resource_name . $namespace_full, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, - resource_namespace => $self->{az_resource_namespace}, + resource_namespace => $namespace, metrics => $self->{az_metrics}, aggregations => $self->{az_aggregations}, timeframe => $self->{az_timeframe}, diff --git a/cloud/azure/storage/storageaccount/mode/transactionscount.pm b/cloud/azure/storage/storageaccount/mode/transactionscount.pm index 5340fc161..583541ae8 100644 --- a/cloud/azure/storage/storageaccount/mode/transactionscount.pm +++ b/cloud/azure/storage/storageaccount/mode/transactionscount.pm @@ -140,24 +140,13 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'storageAccounts'; $self->{az_resource_namespace} = 'Microsoft.Storage'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/.*/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900; $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT5M"; $self->{az_resource_extra_namespace} = defined($self->{option_results}->{resource_namespace}) ? $self->{option_results}->{resource_namespace} : ''; - $self->{az_aggregations} = ['Total']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -180,21 +169,25 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; + my $namespace = $self->{az_resource_namespace}; my $namespace_full = ($self->{az_resource_extra_namespace} ne '') ? '/' . lc($self->{az_resource_extra_namespace}) . 'Services/default' : ''; my $namespace_name = ($self->{az_resource_extra_namespace} ne '') ? $self->{az_resource_extra_namespace} : "Account"; - if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/(.*)\/default$/ || - $resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { - $resource_name = $1; - $namespace_name = $2 if(defined($2)); + if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/(.*)\/default$/ || + $resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; + $namespace_full = '/' . $3 . '/default' if(defined($3)); + $namespace_name = $3 if(defined($3)); } $namespace_name =~ s/Services//g; ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource . $namespace_full, - resource_group => $self->{az_resource_group}, + resource => $resource_name . $namespace_full, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, - resource_namespace => $self->{az_resource_namespace}, + resource_namespace => $namespace, metrics => $self->{az_metrics}, aggregations => $self->{az_aggregations}, timeframe => $self->{az_timeframe}, diff --git a/cloud/azure/storage/storageaccount/mode/transactionslatency.pm b/cloud/azure/storage/storageaccount/mode/transactionslatency.pm index ae7b407a6..53cda2667 100644 --- a/cloud/azure/storage/storageaccount/mode/transactionslatency.pm +++ b/cloud/azure/storage/storageaccount/mode/transactionslatency.pm @@ -88,24 +88,13 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'storageAccounts'; $self->{az_resource_namespace} = 'Microsoft.Storage'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/.*/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900; $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT5M"; $self->{az_resource_extra_namespace} = defined($self->{option_results}->{resource_namespace}) ? $self->{option_results}->{resource_namespace} : ''; - $self->{az_aggregations} = ['Average']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -131,21 +120,25 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; + my $namespace = $self->{az_resource_namespace}; my $namespace_full = ($self->{az_resource_extra_namespace} ne '') ? '/' . lc($self->{az_resource_extra_namespace}) . 'Services/default' : ''; my $namespace_name = ($self->{az_resource_extra_namespace} ne '') ? $self->{az_resource_extra_namespace} : "Account"; - if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/(.*)\/default$/ || - $resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { - $resource_name = $1; - $namespace_name = $2 if(defined($2)); + if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/(.*)\/default$/ || + $resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; + $namespace_full = '/' . $3 . '/default' if(defined($3)); + $namespace_name = $3 if(defined($3)); } $namespace_name =~ s/Services//g; ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource . $namespace_full, - resource_group => $self->{az_resource_group}, + resource => $resource_name . $namespace_full, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, - resource_namespace => $self->{az_resource_namespace}, + resource_namespace => $namespace, metrics => $self->{az_metrics}, aggregations => $self->{az_aggregations}, timeframe => $self->{az_timeframe}, diff --git a/cloud/azure/storage/storageaccount/mode/transactionsthroughput.pm b/cloud/azure/storage/storageaccount/mode/transactionsthroughput.pm index aaebaab37..117f75ad9 100644 --- a/cloud/azure/storage/storageaccount/mode/transactionsthroughput.pm +++ b/cloud/azure/storage/storageaccount/mode/transactionsthroughput.pm @@ -143,24 +143,13 @@ sub check_options { $self->{output}->option_exit(); } - $self->{az_resource_group} = ''; + $self->{az_resource} = $self->{option_results}->{resource}; + $self->{az_resource_group} = $self->{option_results}->{resource_group} if (defined($self->{option_results}->{resource_group})); $self->{az_resource_type} = 'storageAccounts'; $self->{az_resource_namespace} = 'Microsoft.Storage'; - - foreach my $resource (@{$self->{option_results}->{resource}}) { - push @{$self->{az_resource}}, $resource; - if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/.*/) { - $self->{az_resource_type} = ''; - $self->{az_resource_namespace} = ''; - } else { - $self->{az_resource_group} = $self->{option_results}->{resource_group}; - } - } - $self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900; $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT5M"; $self->{az_resource_extra_namespace} = defined($self->{option_results}->{resource_namespace}) ? $self->{option_results}->{resource_namespace} : ''; - $self->{az_aggregations} = ['Total']; if (defined($self->{option_results}->{aggregation})) { $self->{az_aggregations} = []; @@ -186,21 +175,25 @@ sub manage_selection { my %metric_results; foreach my $resource (@{$self->{az_resource}}) { + my $resource_group = $self->{az_resource_group}; my $resource_name = $resource; + my $namespace = $self->{az_resource_namespace}; my $namespace_full = ($self->{az_resource_extra_namespace} ne '') ? '/' . lc($self->{az_resource_extra_namespace}) . 'Services/default' : ''; my $namespace_name = ($self->{az_resource_extra_namespace} ne '') ? $self->{az_resource_extra_namespace} : "Account"; - if ($resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/(.*)\/default$/ || - $resource_name =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { - $resource_name = $1; - $namespace_name = $2 if(defined($2)); + if ($resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)\/(.*)\/default$/ || + $resource =~ /^\/subscriptions\/.*\/resourceGroups\/(.*)\/providers\/Microsoft\.Storage\/storageAccounts\/(.*)$/) { + $resource_group = $1; + $resource_name = $2; + $namespace_full = '/' . $3 . '/default' if(defined($3)); + $namespace_name = $3 if(defined($3)); } $namespace_name =~ s/Services//g; ($metric_results{$resource_name}, undef, undef) = $options{custom}->azure_get_metrics( - resource => $resource . $namespace_full, - resource_group => $self->{az_resource_group}, + resource => $resource_name . $namespace_full, + resource_group => $resource_group, resource_type => $self->{az_resource_type}, - resource_namespace => $self->{az_resource_namespace}, + resource_namespace => $namespace, metrics => $self->{az_metrics}, aggregations => $self->{az_aggregations}, timeframe => $self->{az_timeframe}, diff --git a/cloud/azure/storage/storageaccount/plugin.pm b/cloud/azure/storage/storageaccount/plugin.pm index c8df31999..3e8a5164b 100644 --- a/cloud/azure/storage/storageaccount/plugin.pm +++ b/cloud/azure/storage/storageaccount/plugin.pm @@ -52,9 +52,21 @@ sub new { ); $self->{custom_modes}{azcli} = 'cloud::azure::custom::azcli'; + $self->{custom_modes}{api} = 'cloud::azure::custom::api'; return $self; } +sub init { + my ($self, %options) = @_; + + $self->{options}->add_options(arguments => + { + 'api-version:s' => { name => 'api_version', default => '2018-01-01' }, + }); + + $self->SUPER::init(%options); +} + 1; __END__