From c5236d0343c4420fedbd0fa4028a9e6c1ccba26b Mon Sep 17 00:00:00 2001 From: CPbN <40244829+CPbN@users.noreply.github.com> Date: Thu, 24 Oct 2019 17:19:16 +0200 Subject: [PATCH] InfluxDB, allow to select custom field --- .../database/influxdb/custom/api.pm | 10 +++---- .../database/influxdb/mode/query.pm | 26 +++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/centreon-plugins/database/influxdb/custom/api.pm b/centreon-plugins/database/influxdb/custom/api.pm index 21fe4f5c4..6c3c2fa4c 100644 --- a/centreon-plugins/database/influxdb/custom/api.pm +++ b/centreon-plugins/database/influxdb/custom/api.pm @@ -175,7 +175,7 @@ sub query { my $data; foreach my $query (@{$options{queries}}) { - my $results = $self->request(method => 'POST', url_path => '/query', post_param => ['q=' . $query]); + my $results = $self->request(method => 'POST', url_path => '/query?epoch=s', post_param => ['q=' . $query]); if (defined($results->{results}[0]->{error})) { $self->{output}->add_option_msg(short_msg => "API returns error '" . $results->{results}[0]->{error} . "'"); @@ -196,22 +196,22 @@ sub compute { my $points = 0; foreach my $value (@{$options{values}}) { $result = 0 if (!defined($result)); - $result += $$value[1]; + $result += $$value[$options{column}]; $points++; } $result /= $points; } elsif ($options{aggregation} eq 'minimum') { foreach my $value (@{$options{values}}) { - $result = $$value[1] if (!defined($result) || $$value[1] < $result); + $result = $$value[$options{column}] if (!defined($result) || $$value[$options{column}] < $result); } } elsif ($options{aggregation} eq 'maximum') { foreach my $value (@{$options{values}}) { - $result = $$value[1] if (!defined($result) || $$value[1] > $result); + $result = $$value[$options{column}] if (!defined($result) || $$value[$options{column}] > $result); } } elsif ($options{aggregation} eq 'sum') { foreach my $value (@{$options{values}}) { $result = 0 if (!defined($result)); - $result += $$value[1]; + $result += $$value[$options{column}]; } } diff --git a/centreon-plugins/database/influxdb/mode/query.pm b/centreon-plugins/database/influxdb/mode/query.pm index e0b774df6..742edd148 100644 --- a/centreon-plugins/database/influxdb/mode/query.pm +++ b/centreon-plugins/database/influxdb/mode/query.pm @@ -25,6 +25,7 @@ use base qw(centreon::plugins::templates::counter); use strict; use warnings; use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); +use List::MoreUtils qw(first_index); sub custom_status_perfdata { my ($self, %options) = @_; @@ -141,21 +142,24 @@ sub manage_selection { $self->{queries_results} = {}; my (@results, @queries); + my $query_index = -1; foreach my $label (keys %{$self->{queries}}) { + $query_index++; + @queries = (); push @queries, $self->{queries}->{$label}; - } - - my $queries_results = $options{custom}->query(queries => \@queries) if (scalar(@queries) > 0); - foreach my $result (@{$queries_results}) { - next if (!defined($result->{tags}->{$self->{option_results}->{instance}})); - my $value; - $value = $options{custom}->compute(aggregation => $self->{option_results}->{aggregation}, values => $result->{values}) if (defined($result->{values})); - - $self->{queries_results}->{$result->{tags}->{$self->{option_results}->{instance}}}->{instance} = $result->{tags}->{$self->{option_results}->{instance}}; - $self->{queries_results}->{$result->{tags}->{$self->{option_results}->{instance}}}->{$result->{columns}[1]} = $value; + my $queries_results = $options{custom}->query(queries => \@queries) if (scalar(@queries) > 0); + + foreach my $result (@{$queries_results}) { + next if (!defined($result->{tags}->{$self->{option_results}->{instance}})); + my $column_index = first_index { $_ eq $self->{custom_keys}[$query_index] } @{$result->{columns}}; + my $value; + $value = $options{custom}->compute(aggregation => $self->{option_results}->{aggregation}, values => $result->{values}, column => $column_index ) if (defined($result->{values})); + $self->{queries_results}->{$result->{tags}->{$self->{option_results}->{instance}}}->{instance} = $result->{tags}->{$self->{option_results}->{instance}}; + $self->{queries_results}->{$result->{tags}->{$self->{option_results}->{instance}}}->{$result->{columns}[$column_index]} = $value; + } } - + if (scalar(keys %{$self->{queries_results}}) <= 0) { $self->{output}->add_option_msg(short_msg => "No queries found."); $self->{output}->option_exit();