From 6fb5e91654d5b5ce7004c808a78ebe857bec13ca Mon Sep 17 00:00:00 2001 From: Thibault S <48209914+thibaults-centreon@users.noreply.github.com> Date: Thu, 14 Oct 2021 17:21:10 +0200 Subject: [PATCH] resourceid management (#3195) --- cloud/azure/custom/api.pm | 1 + .../management/insightsmetrics/mode/cpu.pm | 12 ++++-- .../insightsmetrics/mode/listlogicaldisks.pm | 41 +++++++++++++++++-- .../insightsmetrics/mode/logicaldisks.pm | 12 ++++-- .../management/insightsmetrics/mode/memory.pm | 10 ++++- 5 files changed, 64 insertions(+), 12 deletions(-) diff --git a/cloud/azure/custom/api.pm b/cloud/azure/custom/api.pm index f075862d6..95d45abc1 100644 --- a/cloud/azure/custom/api.pm +++ b/cloud/azure/custom/api.pm @@ -691,6 +691,7 @@ sub azure_get_insights_analytics { foreach (@{$_->{rows}}) { $raw_results->{data}->{$j}->{tags} = @$_[$raw_results->{index}->{Tags}]; $raw_results->{data}->{$j}->{computer} = @$_[$raw_results->{index}->{Computer}]; + $raw_results->{data}->{$j}->{resourceid} = @$_[$raw_results->{index}->{_ResourceId}]; if (!defined($options{disco})) { $raw_results->{data}->{$j}->{timegenerated} = @$_[$raw_results->{index}->{TimeGenerated}]; $raw_results->{data}->{$j}->{name} = @$_[$raw_results->{index}->{Name}]; diff --git a/cloud/azure/management/insightsmetrics/mode/cpu.pm b/cloud/azure/management/insightsmetrics/mode/cpu.pm index 16ba5a59a..c1da337b2 100644 --- a/cloud/azure/management/insightsmetrics/mode/cpu.pm +++ b/cloud/azure/management/insightsmetrics/mode/cpu.pm @@ -84,9 +84,10 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - 'filter-computer:s' => { name => 'filter_computer' }, - 'filter-cpu:s' => { name => 'filter_cpu' }, - 'workspace-id:s' => { name => 'workspace_id' } + 'filter-computer:s' => { name => 'filter_computer' }, + 'filter-cpu:s' => { name => 'filter_cpu' }, + 'filter-resourceid:s' => { name => 'filter_resourceid' }, + 'workspace-id:s' => { name => 'workspace_id' } }); return $self; @@ -103,6 +104,7 @@ sub manage_selection { my $query = 'InsightsMetrics | where Namespace == "Processor" | summarize arg_max(TimeGenerated, *) by Tags, Name, Computer'; $query .= '| where Computer == "' . $self->{option_results}->{filter_computer} . '"' if defined $self->{option_results}->{filter_computer} && $self->{option_results}->{filter_computer} ne ''; + $query .= '| where _ResourceId == "' . $self->{option_results}->{filter_resourceid} . '"' if defined $self->{option_results}->{filter_resourceid} && $self->{option_results}->{filter_resourceid} ne ''; my $results = $options{custom}->azure_get_insights_analytics( workspace_id => $self->{option_results}->{workspace_id}, @@ -164,6 +166,10 @@ perl centreon_plugins.pl --plugin=cloud::azure::management::insightsmetrics::plu =over 8 +=item B<--workspace-id> +(Mandatory) +Specify the Azure Log Analytics Workspace ID. + =item B<--filter-computer> Filter on a specific Azure "computer". diff --git a/cloud/azure/management/insightsmetrics/mode/listlogicaldisks.pm b/cloud/azure/management/insightsmetrics/mode/listlogicaldisks.pm index 5d8c9fdb6..1a15608b3 100644 --- a/cloud/azure/management/insightsmetrics/mode/listlogicaldisks.pm +++ b/cloud/azure/management/insightsmetrics/mode/listlogicaldisks.pm @@ -32,7 +32,8 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - 'computer:s' => { name => 'computer' }, + 'name' => { name => 'name' }, + 'resource:s' => { name => 'resource' }, 'workspace-id:s' => { name => 'workspace_id' } }); return $self; @@ -42,8 +43,8 @@ sub check_options { my ($self, %options) = @_; $self->SUPER::init(%options); - if (!defined($self->{option_results}->{computer}) || $self->{option_results}->{computer} eq '') { - $self->{output}->add_option_msg(short_msg => "Need to specify --computer option."); + if (!defined($self->{option_results}->{resource}) || $self->{option_results}->{resource} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --resource option."); $self->{output}->option_exit(); } } @@ -51,7 +52,12 @@ sub check_options { sub manage_selection { my ($self, %options) = @_; - my $query = 'InsightsMetrics | where Namespace == "LogicalDisk" | distinct Tags, Computer | where Computer == "' . $self->{option_results}->{computer} . '"'; + my $query = 'InsightsMetrics | where Namespace == "LogicalDisk" | distinct Tags, _ResourceId, Computer'; + if (defined($self->{option_results}->{name})) { + $query .= '| where Computer == "' . $self->{option_results}->{resource} . '"'; + } else { + $query .= '| where _ResourceId == "' . $self->{option_results}->{resource} . '"'; + } my $results = $options{custom}->azure_get_insights_analytics( workspace_id => $self->{option_results}->{workspace_id}, @@ -66,6 +72,11 @@ sub manage_selection { $self->{logicaldisk}->{$decoded_tag->{"vm.azm.ms/mountId"}}->{name} = $decoded_tag->{"vm.azm.ms/mountId"}; } + + if (scalar(keys %{$self->{logicaldisk}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No logical disks found. Can be: filters, cache file."); + $self->{output}->option_exit(); + } } sub run { @@ -107,8 +118,30 @@ __END__ List Azure Computer logical disks. +Example: + +perl centreon_plugins.pl --plugin=cloud::azure::management::insightsmetrics::plugin --custommode=api --mode=list-logical-disks +--subscription=1111 --tenant=2222 --client-id=3333 --client-secret=4444 --workspace-id=5555 --verbose --resource='azure-vm1' +--name + =over 8 +=item B<--workspace-id> +(Mandatory) +Specify the Azure Log Analytics Workspace ID. + +=item B<--resource> + +(Mandatory) +Specify the Azure VM Resource ID or name. Short name can be used if the option --name is defined. +Example: --resource='/subscriptions/1234abcd-5678-defg-9012-3456789abcde/resourcegroups/my_resourcegroup/providers/microsoft.compute/virtualmachines/azure-vm1' + +=item B<--name> + +(Optional) +Use only the name of the VM resource rather than the full ID. +Example: --resource='azure-vm1' --name + =back =cut diff --git a/cloud/azure/management/insightsmetrics/mode/logicaldisks.pm b/cloud/azure/management/insightsmetrics/mode/logicaldisks.pm index 4cccd84d6..95b87a228 100644 --- a/cloud/azure/management/insightsmetrics/mode/logicaldisks.pm +++ b/cloud/azure/management/insightsmetrics/mode/logicaldisks.pm @@ -148,9 +148,10 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - 'filter-computer:s' => { name => 'filter_computer' }, - 'filter-disk:s' => { name => 'filter_disk' }, - 'workspace-id:s' => { name => 'workspace_id' } + 'filter-computer:s' => { name => 'filter_computer' }, + 'filter-disk:s' => { name => 'filter_disk' }, + 'filter-resourceid:s' => { name => 'filter_resourceid' }, + 'workspace-id:s' => { name => 'workspace_id' } }); return $self; @@ -167,6 +168,7 @@ sub manage_selection { my $query = 'InsightsMetrics | where Namespace == "LogicalDisk" | summarize arg_max(TimeGenerated, *) by Tags, Name, Computer'; $query .= '| where Computer == "' . $self->{option_results}->{filter_computer} . '"' if defined $self->{option_results}->{filter_computer} && $self->{option_results}->{filter_computer} ne ''; + $query .= '| where _ResourceId == "' . $self->{option_results}->{filter_resourceid} . '"' if defined $self->{option_results}->{filter_resourceid} && $self->{option_results}->{filter_resourceid} ne ''; my $results = $options{custom}->azure_get_insights_analytics( workspace_id => $self->{option_results}->{workspace_id}, @@ -241,6 +243,10 @@ perl centreon_plugins.pl --plugin=cloud::azure::management::insightsmetrics::plu =over 8 +=item B<--workspace-id> +(Mandatory) +Specify the Azure Log Analytics Workspace ID. + =item B<--filter-computer> Filter on a specific Azure "computer". diff --git a/cloud/azure/management/insightsmetrics/mode/memory.pm b/cloud/azure/management/insightsmetrics/mode/memory.pm index 3a5e2fcce..b898035f3 100644 --- a/cloud/azure/management/insightsmetrics/mode/memory.pm +++ b/cloud/azure/management/insightsmetrics/mode/memory.pm @@ -97,8 +97,9 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - 'filter-computer:s' => { name => 'filter_computer' }, - 'workspace-id:s' => { name => 'workspace_id' } + 'filter-computer:s' => { name => 'filter_computer' }, + 'filter-resourceid:s' => { name => 'filter_resourceid' }, + 'workspace-id:s' => { name => 'workspace_id' } }); return $self; @@ -115,6 +116,7 @@ sub manage_selection { my $query = 'InsightsMetrics | where Namespace == "Memory" | summarize arg_max(TimeGenerated, *) by Tags, Name, Computer'; $query .= '| where Computer == "' . $self->{option_results}->{filter_computer} . '"' if defined $self->{option_results}->{filter_computer} && $self->{option_results}->{filter_computer} ne ''; + $query .= '| where _ResourceId == "' . $self->{option_results}->{filter_resourceid} . '"' if defined $self->{option_results}->{filter_resourceid} && $self->{option_results}->{filter_resourceid} ne ''; my $results = $options{custom}->azure_get_insights_analytics( workspace_id => $self->{option_results}->{workspace_id}, @@ -173,6 +175,10 @@ perl centreon_plugins.pl --plugin=cloud::azure::management::insightsmetrics::plu =over 8 +=item B<--workspace-id> +(Mandatory) +Specify the Azure Log Analytics Workspace ID. + =item B<--filter-computer> Filter on a specific Azure "computer".