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 1/3] 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(); From c1faa1c69ef36fb70bc000e0f3c9f95c77456158 Mon Sep 17 00:00:00 2001 From: CPbN <40244829+CPbN@users.noreply.github.com> Date: Thu, 24 Oct 2019 17:25:39 +0200 Subject: [PATCH 2/3] InfluxDB, typo --- centreon-plugins/database/influxdb/mode/query.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/centreon-plugins/database/influxdb/mode/query.pm b/centreon-plugins/database/influxdb/mode/query.pm index 3ed831c4d..c1c7fd7d8 100644 --- a/centreon-plugins/database/influxdb/mode/query.pm +++ b/centreon-plugins/database/influxdb/mode/query.pm @@ -154,7 +154,7 @@ sub manage_selection { 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})); + $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; } From 57bc6647f7b6a06efd464c133a7439652cf77ab4 Mon Sep 17 00:00:00 2001 From: CPbN <40244829+CPbN@users.noreply.github.com> Date: Thu, 7 Nov 2019 12:01:13 +0100 Subject: [PATCH 3/3] Compute index without additional mod --- centreon-plugins/database/influxdb/mode/query.pm | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/centreon-plugins/database/influxdb/mode/query.pm b/centreon-plugins/database/influxdb/mode/query.pm index c1c7fd7d8..438fc74ef 100644 --- a/centreon-plugins/database/influxdb/mode/query.pm +++ b/centreon-plugins/database/influxdb/mode/query.pm @@ -25,7 +25,6 @@ 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) = @_; @@ -152,7 +151,7 @@ sub manage_selection { 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 ($column_index) = grep { $result->{columns}[$_] eq $self->{custom_keys}[$query_index] } (0 .. @{$result->{columns}}-1); 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}};