From 1ee9b28e1fb0dc8d4c6e8fd2bfdf1c71f60925c0 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Fri, 3 Apr 2020 11:45:02 +0200 Subject: [PATCH] Fix #1886 --- .../azure/compute/virtualmachine/mode/cpu.pm | 15 +-- centreon-plugins/cloud/azure/custom/api.pm | 20 +++ centreon-plugins/cloud/azure/custom/azcli.pm | 16 +++ .../azure/management/monitor/mode/logs.pm | 121 ++++++++++++++++++ .../cloud/azure/management/monitor/plugin.pm | 3 +- 5 files changed, 166 insertions(+), 9 deletions(-) create mode 100644 centreon-plugins/cloud/azure/management/monitor/mode/logs.pm diff --git a/centreon-plugins/cloud/azure/compute/virtualmachine/mode/cpu.pm b/centreon-plugins/cloud/azure/compute/virtualmachine/mode/cpu.pm index 145c887fb..8149deda8 100644 --- a/centreon-plugins/cloud/azure/compute/virtualmachine/mode/cpu.pm +++ b/centreon-plugins/cloud/azure/compute/virtualmachine/mode/cpu.pm @@ -81,13 +81,12 @@ sub new { my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; - $options{options}->add_options(arguments => - { - "resource:s@" => { name => 'resource' }, - "resource-group:s" => { name => 'resource_group' }, - "filter-metric:s" => { name => 'filter_metric' }, - }); - + $options{options}->add_options(arguments =>v{ + 'resource:s@' => { name => 'resource' }, + 'resource-group:s' => { name => 'resource_group' }, + 'filter-metric:s' => { name => 'filter_metric' } + }); + return $self; } @@ -99,7 +98,7 @@ 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} = $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'; diff --git a/centreon-plugins/cloud/azure/custom/api.pm b/centreon-plugins/cloud/azure/custom/api.pm index 165520f3c..5bfa01459 100644 --- a/centreon-plugins/cloud/azure/custom/api.pm +++ b/centreon-plugins/cloud/azure/custom/api.pm @@ -635,6 +635,26 @@ sub azure_list_sqldatabases { return $response->{value}; } +sub azure_get_log_analytics_set_url { + my ($self, %options) = @_; + + my $uri = URI::Encode->new({encode_reserved => 1}); + my $encoded_query = $uri->encode($options{query}); + my $encoded_interval = $uri->encode($options{interval}); + my $url = $self->{management_endpoint} . '/v1/workspaces/' . $options{workspace_id} . '/query?query=' . $encoded_query . '×pan=' . $encoded_interval; + + return $url; +} + +sub azure_get_log_analytics { + my ($self, %options) = @_; + + my $full_url = $self->azure_get_log_analytics_set_url(%options); + my $response = $self->request_api(method => 'GET', full_url => $full_url, hostname => ''); + + return $response; +} + 1; __END__ diff --git a/centreon-plugins/cloud/azure/custom/azcli.pm b/centreon-plugins/cloud/azure/custom/azcli.pm index 3f2b7deed..5ae02540e 100644 --- a/centreon-plugins/cloud/azure/custom/azcli.pm +++ b/centreon-plugins/cloud/azure/custom/azcli.pm @@ -507,6 +507,22 @@ sub azure_list_sqldatabases { return $raw_results; } +sub azure_get_log_analytics_set_cmd { + my ($self, %options) = @_; + + return if (defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne ''); + + my $cmd_options = "monitor log-analytics query --workspace '$options{workspace_id}' --analytics-query \"$options{query}\" --timespan '$options{interval}'"; + return $cmd_options; +} + +sub azure_get_log_analytics { + my ($self, %options) = @_; + + my $cmd_options = $self->azure_get_log_analytics_set_cmd(%options); + return $self->execute(cmd_options => $cmd_options); +} + 1; __END__ diff --git a/centreon-plugins/cloud/azure/management/monitor/mode/logs.pm b/centreon-plugins/cloud/azure/management/monitor/mode/logs.pm new file mode 100644 index 000000000..27dc87574 --- /dev/null +++ b/centreon-plugins/cloud/azure/management/monitor/mode/logs.pm @@ -0,0 +1,121 @@ +# +# Copyright 2020 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::management::monitor::mode::logs; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0 } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'total', nlabel => 'logs.total.count', set => { + key_values => [ { name => 'total' } ], + output_template => 'total logs: %s', + perfdatas => [ + { value => 'total_absolute', template => '%s', min => 0 } + ] + } + } + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'workspace-id:s' => { name => 'workspace_id' }, + 'query:s' => { name => 'query'} + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + if (!defined($self->{option_results}->{workspace_id}) || $self->{option_results}->{workspace_id} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --workspace-id ."); + $self->{output}->option_exit(); + } + + if (!defined($self->{option_results}->{query}) || $self->{option_results}->{query} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify a query."); + $self->{output}->option_exit(); + } + + $self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : 'PT5M'; +} + +sub manage_selection { + my ($self, %options) = @_; + + my ($log_results) = $options{custom}->azure_get_log_analytics( + workspace_id => $self->{option_results}->{workspace_id}, + query => $self->{option_results}->{query}, + interval => $self->{az_interval} + ); + + $self->{global} = { total => 0 }; + foreach my $table (@{$log_results->{tables}}) { + foreach (@{$table->{rows}}) { + $self->{global}->{total} += $_->[2]; + } + } +} + +1; + +__END__ + +=head1 MODE + +Check logs queries. +You should set option: --management-endpoint='https://api.loganalytics.io' + +=over 8 + +=item B<--workspace-id> + +Set workspace id (Required). + +=item B<--query> + +Set query (Required). +Syntax: https://docs.microsoft.com/en-us/azure/kusto/query/ + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: count. + +=back + +=cut diff --git a/centreon-plugins/cloud/azure/management/monitor/plugin.pm b/centreon-plugins/cloud/azure/management/monitor/plugin.pm index a217fb34e..197528585 100644 --- a/centreon-plugins/cloud/azure/management/monitor/plugin.pm +++ b/centreon-plugins/cloud/azure/management/monitor/plugin.pm @@ -31,10 +31,11 @@ sub new { $self->{version} = '1.0'; %{$self->{modes}} = ( + 'alert' => 'cloud::azure::management::monitor::mode::alert', 'discovery' => 'cloud::azure::management::monitor::mode::discovery', 'get-metrics' => 'cloud::azure::management::monitor::mode::getmetrics', 'health' => 'cloud::azure::management::monitor::mode::health', - 'alert' => 'cloud::azure::management::monitor::mode::alert', + 'logs' => 'cloud::azure::management::monitor::mode::logs' ); $self->{custom_modes}{azcli} = 'cloud::azure::custom::azcli';