From 0a51a150dc50dde283507831014c89c939133f7d Mon Sep 17 00:00:00 2001 From: qgarnier Date: Thu, 28 Jul 2022 16:29:02 +0200 Subject: [PATCH] (plugin) apps::protocols::http - mode collection add filter option for count method (#3792) --- .../apps/protocols/http/mode/collection.pm | 20 +++++++++++++++---- .../common/protocols/sql/mode/collection.pm | 20 +++++++++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/centreon-plugins/apps/protocols/http/mode/collection.pm b/centreon-plugins/apps/protocols/http/mode/collection.pm index 19105eb81..dc87ae9c0 100644 --- a/centreon-plugins/apps/protocols/http/mode/collection.pm +++ b/centreon-plugins/apps/protocols/http/mode/collection.pm @@ -1275,7 +1275,19 @@ sub exec_func_count { my $data = $self->get_special_variable_value(%$result); my $value = 0; if (defined($data)) { - $value = scalar(keys %$data); + if (defined($options{filter}) && $options{filter} ne '') { + my $count = 0; + foreach my $instance (keys %$data) { + my $values = $self->{expand}; + foreach my $label (keys %{$data->{$instance}}) { + $values->{'src.' . $label} = $data->{$instance}->{$label}; + } + $count++ unless ($self->check_filter(filter => $options{filter}, values => $values)); + } + $value = $count; + } else { + $value = scalar(keys %$data); + } } if (defined($options{save}) && $options{save} ne '') { @@ -1501,7 +1513,7 @@ sub check_filter { my ($self, %options) = @_; return 0 if (!defined($options{filter}) || $options{filter} eq ''); - our $expand = $self->{expand}; + our $expand = $options{values}; $options{filter} =~ s/%\(([a-zA-Z0-9\._:]+?)\)/\$expand->{'$1'}/g; my $result = $self->{safe}->reval("$options{filter}"); if ($@) { @@ -1585,7 +1597,7 @@ sub add_selection { $self->set_expand_table(section => "selection > $i > expand_table", expand => $_->{expand_table}); $self->set_expand(section => "selection > $i > expand", expand => $_->{expand}); $self->set_functions(section => "selection > $i > functions", functions => $_->{functions}, position => 'after_expand', default => 1); - next if ($self->check_filter(filter => $_->{filter})); + next if ($self->check_filter(filter => $_->{filter}, values => $self->{expand})); next if ($self->check_filter_option()); $config->{unknown} = $self->prepare_variables(section => "selection > $i > unknown", value => $_->{unknown}); $config->{warning} = $self->prepare_variables(section => "selection > $i > warning", value => $_->{warning}); @@ -1630,7 +1642,7 @@ sub add_selection_loop { $self->set_expand_table(section => "selection_loop > $i > expand_table", expand => $_->{expand_table}); $self->set_expand(section => "selection_loop > $i > expand", expand => $_->{expand}); $self->set_functions(section => "selection_loop > $i > functions", functions => $_->{functions}, position => 'after_expand', default => 1); - next if ($self->check_filter(filter => $_->{filter})); + next if ($self->check_filter(filter => $_->{filter}, values => $self->{expand})); next if ($self->check_filter_option()); $config->{unknown} = $self->prepare_variables(section => "selection_loop > $i > unknown", value => $_->{unknown}); $config->{warning} = $self->prepare_variables(section => "selection_loop > $i > warning", value => $_->{warning}); diff --git a/centreon-plugins/centreon/common/protocols/sql/mode/collection.pm b/centreon-plugins/centreon/common/protocols/sql/mode/collection.pm index 71ae13925..04af9bf9c 100644 --- a/centreon-plugins/centreon/common/protocols/sql/mode/collection.pm +++ b/centreon-plugins/centreon/common/protocols/sql/mode/collection.pm @@ -1042,7 +1042,19 @@ sub exec_func_count { my $data = $self->get_special_variable_value(%$result); my $value = 0; if (defined($data)) { - $value = scalar(keys %$data); + if (defined($options{filter}) && $options{filter} ne '') { + my $count = 0; + foreach my $instance (keys %$data) { + my $values = $self->{expand}; + foreach my $label (keys %{$data->{$instance}}) { + $values->{'src.' . $label} = $data->{$instance}->{$label}; + } + $count++ unless ($self->check_filter(filter => $options{filter}, values => $values)); + } + $value = $count; + } else { + $value = scalar(keys %$data); + } } if (defined($options{save}) && $options{save} ne '') { @@ -1256,7 +1268,7 @@ sub check_filter { my ($self, %options) = @_; return 0 if (!defined($options{filter}) || $options{filter} eq ''); - our $expand = $self->{expand}; + our $expand = $options{values}; $options{filter} =~ s/%\(([a-zA-Z0-9\._:]+?)\)/\$expand->{'$1'}/g; my $result = $self->{safe}->reval("$options{filter}"); if ($@) { @@ -1340,7 +1352,7 @@ sub add_selection { $self->set_expand_table(section => "selection > $i > expand_table", expand => $_->{expand_table}); $self->set_expand(section => "selection > $i > expand", expand => $_->{expand}); $self->set_functions(section => "selection > $i > functions", functions => $_->{functions}, position => 'after_expand', default => 1); - next if ($self->check_filter(filter => $_->{filter})); + next if ($self->check_filter(filter => $_->{filter}, values => $self->{expand})); next if ($self->check_filter_option()); $config->{unknown} = $self->prepare_variables(section => "selection > $i > unknown", value => $_->{unknown}); $config->{warning} = $self->prepare_variables(section => "selection > $i > warning", value => $_->{warning}); @@ -1385,7 +1397,7 @@ sub add_selection_loop { $self->set_expand_table(section => "selection_loop > $i > expand_table", expand => $_->{expand_table}); $self->set_expand(section => "selection_loop > $i > expand", expand => $_->{expand}); $self->set_functions(section => "selection_loop > $i > functions", functions => $_->{functions}, position => 'after_expand', default => 1); - next if ($self->check_filter(filter => $_->{filter})); + next if ($self->check_filter(filter => $_->{filter}, values => $self->{expand})); next if ($self->check_filter_option()); $config->{unknown} = $self->prepare_variables(section => "selection_loop > $i > unknown", value => $_->{unknown}); $config->{warning} = $self->prepare_variables(section => "selection_loop > $i > warning", value => $_->{warning});