diff --git a/centreon-plugins/apps/protocols/http/mode/collection.pm b/centreon-plugins/apps/protocols/http/mode/collection.pm index fd6ef1b36..5c2dbd193 100644 --- a/centreon-plugins/apps/protocols/http/mode/collection.pm +++ b/centreon-plugins/apps/protocols/http/mode/collection.pm @@ -26,6 +26,7 @@ use strict; use warnings; use centreon::plugins::http; use Safe; +use centreon::plugins::misc; use centreon::plugins::statefile; use Digest::MD5 qw(md5_hex); use Time::HiRes qw(gettimeofday tv_interval); @@ -361,13 +362,20 @@ sub parse_txt { my $modifier = defined($options{conf}->{modifier}) ? $options{conf}->{modifier} : ''; + my @entries = (); + foreach (@{$options{conf}->{entries}}) { + next if ($_->{offset} !~ /^[0-9]+$/); + + push @entries, $_; + } + my $i = 0; while ($options{content} =~ /(?$modifier)$options{conf}->{re}/g) { my $instance = $i; my $name = $options{name} . ucfirst($options{conf}->{name}); my $entry = {}; - foreach (@{$options{conf}->{entries}}) { + foreach (@entries) { my $offset = "\$" . $_->{offset}; my $value = eval "$offset"; if (!defined($value)) { @@ -1320,6 +1328,87 @@ sub exec_func_assign { $self->set_special_variable_value(value => $assign_var, %$result); } +sub exec_func_capture { + my ($self, %options) = @_; + + #{ + # "type": "capture", + # "src": "%(snmp.leefs.content)", + # "pattern": "(?msi)Vertical BER Analysis.*?Bit Error Rate: (\S+)", + # "groups": [ + # { "offset": 1, "save": "%(bitErrorRate)" } + # ] + #} + if (!defined($options{src}) || $options{src} eq '') { + $self->{output}->add_option_msg(short_msg => "$self->{current_section} please set src attribute"); + $self->{output}->option_exit(); + } + if (!defined($options{pattern}) || $options{pattern} eq '') { + $self->{output}->add_option_msg(short_msg => "$self->{current_section} please set pattern attribute"); + $self->{output}->option_exit(); + } + if (!defined($options{groups}) || ref($options{groups}) ne 'ARRAY') { + $self->{output}->add_option_msg(short_msg => "$self->{current_section} please set groups attribute"); + $self->{output}->option_exit(); + } + + my $result = $self->parse_special_variable(chars => [split //, $options{src}], start => 0); + if ($result->{type} !~ /^(?:0|4)$/) { + $self->{output}->add_option_msg(short_msg => $self->{current_section} . " special variable type not allowed in src attribute"); + $self->{output}->option_exit(); + } + my $data = $self->get_special_variable_value(%$result); + + my @matches = ($data =~ /$options{pattern}/); + + foreach (@{$options{groups}}) { + next if ($_->{offset} !~ /^[0-9]+/); + + my $value = ''; + if (defined($matches[ $_->{offset} ])) { + $value = $matches[ $_->{offset} ]; + } + + my $save = $self->parse_special_variable(chars => [split //, $_->{save}], start => 0); + if ($save->{type} !~ /^(?:0|4)$/) { + $self->{output}->add_option_msg(short_msg => $self->{current_section} . " special variable type not allowed in save attribute"); + $self->{output}->option_exit(); + } + $self->set_special_variable_value(value => $value, %$save); + } +} + +sub exec_func_scientific2number { + my ($self, %options) = @_; + + #{ + # "type": "scientific2number", + # "src": "%(bitErrorRate)", + # "save": "%(bitErrorRate)", + #} + if (!defined($options{src}) || $options{src} eq '') { + $self->{output}->add_option_msg(short_msg => "$self->{current_section} please set src attribute"); + $self->{output}->option_exit(); + } + my $result = $self->parse_special_variable(chars => [split //, $options{src}], start => 0); + if ($result->{type} !~ /^(?:0|4)$/) { + $self->{output}->add_option_msg(short_msg => $self->{current_section} . " special variable type not allowed in src attribute"); + $self->{output}->option_exit(); + } + my $data = $self->get_special_variable_value(%$result); + + $data = centreon::plugins::misc::expand_exponential(value => $data); + + if (defined($options{save}) && $options{save} ne '') { + my $save = $self->parse_special_variable(chars => [split //, $options{save}], start => 0); + if ($save->{type} !~ /^(?:0|4)$/) { + $self->{output}->add_option_msg(short_msg => $self->{current_section} . " special variable type not allowed in save attribute"); + $self->{output}->option_exit(); + } + $self->set_special_variable_value(value => $data, %$save); + } +} + sub set_functions { my ($self, %options) = @_; @@ -1330,7 +1419,7 @@ sub set_functions { $self->{current_section} = '[' . $options{section} . ' > ' . $i . ']'; next if (defined($_->{position}) && $options{position} ne $_->{position}); next if (!defined($_->{position}) && !(defined($options{default}) && $options{default} == 1)); - + next if (!defined($_->{type})); if ($_->{type} eq 'map') { @@ -1347,6 +1436,10 @@ sub set_functions { $self->exec_func_replace(%$_); } elsif (lc($_->{type}) eq 'assign') { $self->exec_func_assign(%$_); + } elsif (lc($_->{type}) eq 'capture') { + $self->exec_func_capture(%$_); + } elsif (lc($_->{type}) eq 'scientific2number') { + $self->exec_func_scientific2number(%$_); } } } @@ -1355,7 +1448,11 @@ sub substitute_constants { my ($self, %options) = @_; return undef if (!defined($options{value})); - $options{value} =~ s/%\((constants\.[a-zA-Z0-9\._:]+?)\)/$self->{constants}->{$1}/g; + while ($options{value} =~ /%\((constants\.[a-zA-Z0-9\._:]+?)\)/g) { + my $value = defined($self->{constants}->{$1}) ? $self->{constants}->{$1} : ''; + $options{value} =~ s/%\($1\)/$value/g; + } + return $options{value}; } diff --git a/centreon-plugins/apps/protocols/snmp/mode/collection.pm b/centreon-plugins/apps/protocols/snmp/mode/collection.pm index 1b9704220..2fb2ff55b 100644 --- a/centreon-plugins/apps/protocols/snmp/mode/collection.pm +++ b/centreon-plugins/apps/protocols/snmp/mode/collection.pm @@ -26,6 +26,7 @@ use strict; use warnings; use JSON::XS; use Safe; +use centreon::plugins::misc; use centreon::plugins::statefile; use Digest::MD5 qw(md5_hex); @@ -1148,7 +1149,7 @@ sub exec_func_replace { } my $result = $self->parse_special_variable(chars => [split //, $options{src}], start => 0); - if ($result->{type} !~ /^(?:0|4)$/) { + if ($result->{type} !~ /^(?:0|1|4)$/) { $self->{output}->add_option_msg(short_msg => $self->{current_section} . " special variable type not allowed in src attribute"); $self->{output}->option_exit(); } @@ -1183,7 +1184,7 @@ sub exec_func_assign { } my $result = $self->parse_special_variable(chars => [split //, $options{save}], start => 0); - if ($result->{type} !~ /^(?:0|4)$/) { + if ($result->{type} !~ /^(?:0|1|4)$/) { $self->{output}->add_option_msg(short_msg => $self->{current_section} . " special variable type not allowed in src attribute"); $self->{output}->option_exit(); } @@ -1197,6 +1198,87 @@ sub exec_func_assign { $self->set_special_variable_value(value => $assign_var, %$result); } +sub exec_func_capture { + my ($self, %options) = @_; + + #{ + # "type": "capture", + # "src": "%(snmp.leefs.content)", + # "pattern": "(?msi)Vertical BER Analysis.*?Bit Error Rate: (\S+)", + # "groups": [ + # { "offset": 1, "save": "%(bitErrorRate)" } + # ] + #} + if (!defined($options{src}) || $options{src} eq '') { + $self->{output}->add_option_msg(short_msg => "$self->{current_section} please set src attribute"); + $self->{output}->option_exit(); + } + if (!defined($options{pattern}) || $options{pattern} eq '') { + $self->{output}->add_option_msg(short_msg => "$self->{current_section} please set pattern attribute"); + $self->{output}->option_exit(); + } + if (!defined($options{groups}) || ref($options{groups}) ne 'ARRAY') { + $self->{output}->add_option_msg(short_msg => "$self->{current_section} please set groups attribute"); + $self->{output}->option_exit(); + } + + my $result = $self->parse_special_variable(chars => [split //, $options{src}], start => 0); + if ($result->{type} !~ /^(?:0|1|4)$/) { + $self->{output}->add_option_msg(short_msg => $self->{current_section} . " special variable type not allowed in src attribute"); + $self->{output}->option_exit(); + } + my $data = $self->get_special_variable_value(%$result); + + my @matches = ($data =~ /$options{pattern}/); + + foreach (@{$options{groups}}) { + next if ($_->{offset} !~ /^[0-9]+/); + + my $value = ''; + if (defined($matches[ $_->{offset} ])) { + $value = $matches[ $_->{offset} ]; + } + + my $save = $self->parse_special_variable(chars => [split //, $_->{save}], start => 0); + if ($save->{type} !~ /^(?:0|1|4)$/) { + $self->{output}->add_option_msg(short_msg => $self->{current_section} . " special variable type not allowed in save attribute"); + $self->{output}->option_exit(); + } + $self->set_special_variable_value(value => $value, %$save); + } +} + +sub exec_func_scientific2number { + my ($self, %options) = @_; + + #{ + # "type": "scientific2number", + # "src": "%(bitErrorRate)", + # "save": "%(bitErrorRate)", + #} + if (!defined($options{src}) || $options{src} eq '') { + $self->{output}->add_option_msg(short_msg => "$self->{current_section} please set src attribute"); + $self->{output}->option_exit(); + } + my $result = $self->parse_special_variable(chars => [split //, $options{src}], start => 0); + if ($result->{type} !~ /^(?:0|1|4)$/) { + $self->{output}->add_option_msg(short_msg => $self->{current_section} . " special variable type not allowed in src attribute"); + $self->{output}->option_exit(); + } + my $data = $self->get_special_variable_value(%$result); + + $data = centreon::plugins::misc::expand_exponential(value => $data); + + if (defined($options{save}) && $options{save} ne '') { + my $save = $self->parse_special_variable(chars => [split //, $options{save}], start => 0); + if ($save->{type} !~ /^(?:0|1|4)$/) { + $self->{output}->add_option_msg(short_msg => $self->{current_section} . " special variable type not allowed in save attribute"); + $self->{output}->option_exit(); + } + $self->set_special_variable_value(value => $data, %$save); + } +} + sub set_functions { my ($self, %options) = @_; @@ -1222,8 +1304,12 @@ sub set_functions { $self->exec_func_count(%$_); } elsif (lc($_->{type}) eq 'replace') { $self->exec_func_replace(%$_); - } elsif (lc($_->{type}) eq 'assign') { + } elsif (lc($_->{type}) eq 'assign') { $self->exec_func_assign(%$_); + } elsif (lc($_->{type}) eq 'capture') { + $self->exec_func_capture(%$_); + } elsif (lc($_->{type}) eq 'scientific2number') { + $self->exec_func_scientific2number(%$_); } } } diff --git a/centreon-plugins/centreon/common/protocols/sql/mode/collection.pm b/centreon-plugins/centreon/common/protocols/sql/mode/collection.pm index 61acd0477..69e88208b 100644 --- a/centreon-plugins/centreon/common/protocols/sql/mode/collection.pm +++ b/centreon-plugins/centreon/common/protocols/sql/mode/collection.pm @@ -26,6 +26,7 @@ use strict; use warnings; use JSON::XS; use Safe; +use centreon::plugins::misc; use centreon::plugins::statefile; use Digest::MD5 qw(md5_hex); use Time::HiRes qw(gettimeofday tv_interval); @@ -1101,6 +1102,87 @@ sub exec_func_assign { $self->set_special_variable_value(value => $assign_var, %$result); } +sub exec_func_capture { + my ($self, %options) = @_; + + #{ + # "type": "capture", + # "src": "%(snmp.leefs.content)", + # "pattern": "(?msi)Vertical BER Analysis.*?Bit Error Rate: (\S+)", + # "groups": [ + # { "offset": 1, "save": "%(bitErrorRate)" } + # ] + #} + if (!defined($options{src}) || $options{src} eq '') { + $self->{output}->add_option_msg(short_msg => "$self->{current_section} please set src attribute"); + $self->{output}->option_exit(); + } + if (!defined($options{pattern}) || $options{pattern} eq '') { + $self->{output}->add_option_msg(short_msg => "$self->{current_section} please set pattern attribute"); + $self->{output}->option_exit(); + } + if (!defined($options{groups}) || ref($options{groups}) ne 'ARRAY') { + $self->{output}->add_option_msg(short_msg => "$self->{current_section} please set groups attribute"); + $self->{output}->option_exit(); + } + + my $result = $self->parse_special_variable(chars => [split //, $options{src}], start => 0); + if ($result->{type} !~ /^(?:0|4)$/) { + $self->{output}->add_option_msg(short_msg => $self->{current_section} . " special variable type not allowed in src attribute"); + $self->{output}->option_exit(); + } + my $data = $self->get_special_variable_value(%$result); + + my @matches = ($data =~ /$options{pattern}/); + + foreach (@{$options{groups}}) { + next if ($_->{offset} !~ /^[0-9]+/); + + my $value = ''; + if (defined($matches[ $_->{offset} ])) { + $value = $matches[ $_->{offset} ]; + } + + my $save = $self->parse_special_variable(chars => [split //, $_->{save}], start => 0); + if ($save->{type} !~ /^(?:0|4)$/) { + $self->{output}->add_option_msg(short_msg => $self->{current_section} . " special variable type not allowed in save attribute"); + $self->{output}->option_exit(); + } + $self->set_special_variable_value(value => $value, %$save); + } +} + +sub exec_func_scientific2number { + my ($self, %options) = @_; + + #{ + # "type": "scientific2number", + # "src": "%(bitErrorRate)", + # "save": "%(bitErrorRate)", + #} + if (!defined($options{src}) || $options{src} eq '') { + $self->{output}->add_option_msg(short_msg => "$self->{current_section} please set src attribute"); + $self->{output}->option_exit(); + } + my $result = $self->parse_special_variable(chars => [split //, $options{src}], start => 0); + if ($result->{type} !~ /^(?:0|4)$/) { + $self->{output}->add_option_msg(short_msg => $self->{current_section} . " special variable type not allowed in src attribute"); + $self->{output}->option_exit(); + } + my $data = $self->get_special_variable_value(%$result); + + $data = centreon::plugins::misc::expand_exponential(value => $data); + + if (defined($options{save}) && $options{save} ne '') { + my $save = $self->parse_special_variable(chars => [split //, $options{save}], start => 0); + if ($save->{type} !~ /^(?:0|4)$/) { + $self->{output}->add_option_msg(short_msg => $self->{current_section} . " special variable type not allowed in save attribute"); + $self->{output}->option_exit(); + } + $self->set_special_variable_value(value => $data, %$save); + } +} + sub set_functions { my ($self, %options) = @_; @@ -1128,6 +1210,10 @@ sub set_functions { $self->exec_func_replace(%$_); } elsif (lc($_->{type}) eq 'assign') { $self->exec_func_assign(%$_); + } elsif (lc($_->{type}) eq 'capture') { + $self->exec_func_capture(%$_); + } elsif (lc($_->{type}) eq 'scientific2number') { + $self->exec_func_scientific2number(%$_); } } }