allow multiple metrics in azure getmetrics, fix interval option

This commit is contained in:
Colin Gagnaire 2019-08-05 11:06:53 +02:00
parent 65c10eb5b9
commit 9944f38314
2 changed files with 33 additions and 26 deletions

View File

@ -95,7 +95,7 @@ sub check_options {
if (defined($self->{option_results}->{aggregation})) { if (defined($self->{option_results}->{aggregation})) {
foreach my $aggregation (@{$self->{option_results}->{aggregation}}) { foreach my $aggregation (@{$self->{option_results}->{aggregation}}) {
if ($aggregation !~ /average|maximum|minimum|total/i) { if ($aggregation !~ /average|maximum|minimum|total|count/i) {
$self->{output}->add_option_msg(short_msg => "Aggregation '" . $aggregation . "' is not handled"); $self->{output}->add_option_msg(short_msg => "Aggregation '" . $aggregation . "' is not handled");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
@ -280,9 +280,11 @@ sub azure_get_metrics_set_url {
my $encoded_aggregations = $uri->encode(join(',', @{$options{aggregations}})); my $encoded_aggregations = $uri->encode(join(',', @{$options{aggregations}}));
my $encoded_timespan = $uri->encode($options{start_time} . '/' . $options{end_time}); my $encoded_timespan = $uri->encode($options{start_time} . '/' . $options{end_time});
my $url = $self->{management_endpoint} . "/subscriptions/" . $self->{subscription} . "/resourceGroups/" . $options{resource_group} . my $url = $self->{management_endpoint} . "/subscriptions/" . $self->{subscription} . "/resourceGroups/" .
"/providers/" . $options{resource_namespace} . "/" . $options{resource_type} . "/" . $options{resource} . '/providers/microsoft.insights/metrics' . $options{resource_group} . "/providers/" . $options{resource_namespace} . "/" . $options{resource_type} .
"?api-version=" . $self->{api_version} . "&metricnames=" . $encoded_metrics . "&aggregation=" . $encoded_aggregations . "&timespan=" . $encoded_timespan; "/" . $options{resource} . "/providers/microsoft.insights/metrics?api-version=" . $self->{api_version} .
"&metricnames=" . $encoded_metrics . "&aggregation=" . $encoded_aggregations .
"&timespan=" . $encoded_timespan . "&interval=" . $options{interval};
return $url; return $url;
} }

View File

@ -29,27 +29,30 @@ use Data::Dumper;
sub custom_metric_perfdata { sub custom_metric_perfdata {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{output}->perfdata_add(label => $self->{result_values}->{perf_label}, $self->{output}->perfdata_add(
value => $self->{result_values}->{value}, label => $self->{result_values}->{perf_label},
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-metric'), value => $self->{result_values}->{value},
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-metric'), warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-metric'),
); critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-metric'),
);
} }
sub custom_metric_threshold { sub custom_metric_threshold {
my ($self, %options) = @_; my ($self, %options) = @_;
my $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{value}, my $exit = $self->{perfdata}->threshold_check(
threshold => [ { label => 'critical-metric', exit_litteral => 'critical' }, value => $self->{result_values}->{value},
{ label => 'warning-metric', exit_litteral => 'warning' } ]); threshold => [ { label => 'critical-metric', exit_litteral => 'critical' },
{ label => 'warning-metric', exit_litteral => 'warning' } ]);
return $exit; return $exit;
} }
sub custom_metric_output { sub custom_metric_output {
my ($self, %options) = @_; my ($self, %options) = @_;
my $msg = "Metric '" . $self->{result_values}->{label} . "' of resource '" . $self->{result_values}->{display} . "' value is " . $self->{result_values}->{value}; my $msg = "Metric '" . $self->{result_values}->{name} . "' of resource '" . $self->{result_values}->{display} .
"' and aggregation '" . $self->{result_values}->{aggregation} . "' value is " . $self->{result_values}->{value};
return $msg; return $msg;
} }
@ -57,6 +60,7 @@ sub custom_metric_calc {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{result_values}->{value} = $options{new_datas}->{$self->{instance} . '_value'}; $self->{result_values}->{value} = $options{new_datas}->{$self->{instance} . '_value'};
$self->{result_values}->{name} = $options{new_datas}->{$self->{instance} . '_name'};
$self->{result_values}->{label} = $options{new_datas}->{$self->{instance} . '_label'}; $self->{result_values}->{label} = $options{new_datas}->{$self->{instance} . '_label'};
$self->{result_values}->{aggregation} = $options{new_datas}->{$self->{instance} . '_aggregation'}; $self->{result_values}->{aggregation} = $options{new_datas}->{$self->{instance} . '_aggregation'};
$self->{result_values}->{perf_label} = $options{new_datas}->{$self->{instance} . '_perf_label'}; $self->{result_values}->{perf_label} = $options{new_datas}->{$self->{instance} . '_perf_label'};
@ -68,12 +72,12 @@ sub set_counters {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{maps_counters_type} = [ $self->{maps_counters_type} = [
{ name => 'metrics', type => 0 }, { name => 'metrics', type => 1, message_multiple => 'All metrics are ok' },
]; ];
$self->{maps_counters}->{metrics} = [ $self->{maps_counters}->{metrics} = [
{ label => 'metric', set => { { label => 'metric', set => {
key_values => [ { name => 'value' }, { name => 'label' }, { name => 'aggregation' }, key_values => [ { name => 'value' }, { name => 'name' }, { name => 'label' }, { name => 'aggregation' },
{ name => 'perf_label' }, { name => 'display' } ], { name => 'perf_label' }, { name => 'display' } ],
closure_custom_calc => $self->can('custom_metric_calc'), closure_custom_calc => $self->can('custom_metric_calc'),
closure_custom_output => $self->can('custom_metric_output'), closure_custom_output => $self->can('custom_metric_output'),
@ -89,14 +93,13 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options); my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => $options{options}->add_options(arguments => {
{ "resource:s" => { name => 'resource' },
"resource:s" => { name => 'resource' }, "resource-group:s" => { name => 'resource_group' },
"resource-group:s" => { name => 'resource_group' }, "resource-type:s" => { name => 'resource_type' },
"resource-type:s" => { name => 'resource_type' }, "resource-namespace:s" => { name => 'resource_namespace' },
"resource-namespace:s" => { name => 'resource_namespace' }, "metric:s@" => { name => 'metric' },
"metric:s@" => { name => 'metric' }, });
});
return $self; return $self;
} }
@ -106,7 +109,7 @@ sub check_options {
$self->SUPER::check_options(%options); $self->SUPER::check_options(%options);
if (!defined($self->{option_results}->{resource})) { if (!defined($self->{option_results}->{resource})) {
$self->{output}->add_option_msg(short_msg => "Need to specify either --resource <name> with --resource-group and --resource-type options or --resource <id>."); $self->{output}->add_option_msg(short_msg => "Need to specify either --resource <name> with --resource-group, --resource-type and --resource-namespace options or --resource <id>.");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
@ -164,8 +167,9 @@ sub manage_selection {
foreach my $aggregation (('minimum', 'maximum', 'average', 'total')) { foreach my $aggregation (('minimum', 'maximum', 'average', 'total')) {
next if (!defined($results->{$label}->{$aggregation})); next if (!defined($results->{$label}->{$aggregation}));
$self->{metrics} = { $self->{metrics}->{$label . '_' . $aggregation} = {
display => $self->{az_resource}, display => $self->{az_resource},
name => $results->{$label}->{name},
label => $label, label => $label,
aggregation => $aggregation, aggregation => $aggregation,
value => $results->{$label}->{$aggregation}, value => $results->{$label}->{$aggregation},
@ -173,6 +177,7 @@ sub manage_selection {
}; };
} }
} }
print Dumper $self->{metrics};
$self->{output}->output_add(long_msg => sprintf("Raw data:\n%s", Dumper($raw_results)), debug => 1); $self->{output}->output_add(long_msg => sprintf("Raw data:\n%s", Dumper($raw_results)), debug => 1);
} }
@ -214,7 +219,7 @@ Set resource type (Required if resource's name is used).
=item B<--metric> =item B<--metric>
Set monitor metrics (Required). Set monitor metrics (Required) (Can be multiple).
=item B<--warning-metric> =item B<--warning-metric>