enh(mode): Azure getmetrics dimension filter (#2031)

This commit is contained in:
Thibault S 2020-06-03 22:39:16 +02:00 committed by GitHub
parent db9abbae05
commit 2deab981ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 31 deletions

View File

@ -279,7 +279,7 @@ sub convert_duration {
$duration = $d->minutes * 60 + $d->seconds;
}
return $duration;
return $duration;
}
sub azure_get_metrics_set_url {
@ -295,8 +295,9 @@ sub azure_get_metrics_set_url {
"/" . $options{resource} . "/providers/microsoft.insights/metrics?api-version=" . $self->{api_version} .
"&metricnames=" . $encoded_metrics . "&aggregation=" . $encoded_aggregations .
"&timespan=" . $encoded_timespan . "&interval=" . $options{interval};
$url .= "&\$filter=" . $options{dimension} if defined($options{dimension});
return $url;
return $url;
}
sub azure_get_metrics {
@ -351,8 +352,8 @@ sub azure_get_resource_health_set_url {
my $url = $self->{management_endpoint} . "/subscriptions/" . $self->{subscription} . "/resourceGroups/" .
$options{resource_group} . "/providers/" . $options{resource_namespace} . "/" . $options{resource_type} .
"/" . $options{resource} . "/providers/Microsoft.ResourceHealth/availabilityStatuses/current?api-version=" . $self->{api_version};
return $url;
return $url;
}
sub azure_get_resource_health {

View File

@ -113,7 +113,7 @@ sub execute {
my ($self, %options) = @_;
$self->{output}->output_add(long_msg => "Command line: '" . $self->{option_results}->{command} . " " . $options{cmd_options} . "'", debug => 1);
my ($response) = centreon::plugins::misc::execute(
output => $self->{output},
options => $self->{option_results},
@ -133,7 +133,7 @@ sub execute {
$self->{output}->option_exit();
}
return $raw_results;
return $raw_results;
}
sub convert_duration {
@ -161,7 +161,7 @@ sub convert_duration {
$duration = $d->minutes * 60 + $d->seconds;
}
return $duration;
return $duration;
}
sub azure_get_metrics_set_cmd {
@ -173,20 +173,21 @@ sub azure_get_metrics_set_cmd {
"--interval $options{interval} --aggregation '" . join('\' \'', @{$options{aggregations}}) . "' --only-show-errors --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 '');
$cmd_options .= " --filter '$options{dimension}'" if defined($options{dimension});
return $cmd_options;
return $cmd_options;
}
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 $cmd_options = $self->azure_get_metrics_set_cmd(%options, start_time => $start_time, end_time => $end_time);
my $raw_results = $self->execute(cmd_options => $cmd_options);
foreach my $metric (@{$raw_results->{value}}) {
my $metric_name = lc($metric->{name}->{value});
$metric_name =~ s/ /_/g;
@ -214,7 +215,7 @@ sub azure_get_metrics {
}
}
}
if (defined($results->{$metric_name}->{average})) {
$results->{$metric_name}->{average} /= $results->{$metric_name}->{points};
}

View File

@ -70,11 +70,11 @@ sub custom_metric_calc {
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'metrics', type => 1, message_multiple => 'All metrics are ok' },
];
$self->{maps_counters}->{metrics} = [
{ label => 'metric', set => {
key_values => [ { name => 'value' }, { name => 'name' }, { name => 'label' }, { name => 'aggregation' },
@ -85,22 +85,23 @@ sub set_counters {
closure_custom_threshold_check => $self->can('custom_metric_threshold'),
}
}
];
];
}
sub new {
my ($class, %options) = @_;
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' },
"resource-type:s" => { name => 'resource_type' },
"resource-namespace:s" => { name => 'resource_namespace' },
"metric:s@" => { name => 'metric' },
"filter-dimension:s" => { name => 'filter_dimension'}
});
return $self;
}
@ -118,13 +119,13 @@ sub check_options {
if ($self->{az_resource} =~ /^\/subscriptions\/.*\/resourceGroups\/.*\/providers\/Microsoft\..*\/.*\/.*$/) {
$self->{az_resource_group} = '';
$self->{az_resource_type} = '';
$self->{az_resource_namespace} = '';
$self->{az_resource_namespace} = '';
} else {
$self->{az_resource_group} = $self->{option_results}->{resource_group};
$self->{az_resource_type} = $self->{option_results}->{resource_type};
$self->{az_resource_namespace} = $self->{option_results}->{resource_namespace};
}
$self->{az_metrics} = [];
if (defined($self->{option_results}->{metric})) {
$self->{az_metrics} = $self->{option_results}->{metric};
@ -133,10 +134,10 @@ sub check_options {
$self->{output}->add_option_msg(short_msg => "Need to specify --metric option.");
$self->{output}->option_exit();
}
$self->{az_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 600;
$self->{az_interval} = defined($self->{option_results}->{interval}) ? $self->{option_results}->{interval} : "PT1M";
$self->{az_aggregation} = ['Average'];
if (defined($self->{option_results}->{aggregation})) {
$self->{az_aggregation} = [];
@ -146,27 +147,32 @@ sub check_options {
}
}
}
if (defined($self->{option_results}->{filter_dimension}) && $self->{option_results}->{filter_dimension} ne '') {
$self->{az_metrics_dimension} = $self->{option_results}->{filter_dimension};
}
}
sub manage_selection {
my ($self, %options) = @_;
my ($results, $raw_results) = $options{custom}->azure_get_metrics(
resource => $self->{az_resource},
resource_group => $self->{az_resource_group},
resource_type => $self->{az_resource_type},
resource_namespace => $self->{az_resource_namespace},
metrics => $self->{az_metrics},
aggregations => $self->{az_aggregation},
timeframe => $self->{az_timeframe},
interval => $self->{az_interval},
resource => $self->{az_resource},
resource_group => $self->{az_resource_group},
resource_type => $self->{az_resource_type},
resource_namespace => $self->{az_resource_namespace},
metrics => $self->{az_metrics},
aggregations => $self->{az_aggregation},
timeframe => $self->{az_timeframe},
interval => $self->{az_interval},
dimension => $self->{az_metrics_dimension}
);
$self->{metrics} = {};
foreach my $label (keys %{$results}) {
foreach my $aggregation (('minimum', 'maximum', 'average', 'total')) {
next if (!defined($results->{$label}->{$aggregation}));
$self->{metrics}->{$label . '_' . $aggregation} = {
display => $self->{az_resource},
name => $results->{$label}->{name},
@ -177,6 +183,10 @@ sub manage_selection {
};
}
}
if (scalar(keys %{$self->{metrics}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No metric found (Are the filters properly set?)");
$self->{output}->option_exit();
}
$self->{output}->output_add(long_msg => sprintf("Raw data:\n%s", Dumper($raw_results)), debug => 1);
}
@ -220,6 +230,12 @@ Set resource type (Required if resource's name is used).
Set monitor metrics (Required) (Can be multiple).
=item B<--filter-dimension>
Specify the metric dimension (required for some specific metrics)
Syntax example:
--filter-dimension="$metricname eq '$metricvalue'"
=item B<--warning-metric>
Threshold warning.