diff --git a/centreon-plugins/apps/protocols/http/mode/expectedcontent.pm b/centreon-plugins/apps/protocols/http/mode/expectedcontent.pm index f228155b4..109516b4c 100644 --- a/centreon-plugins/apps/protocols/http/mode/expectedcontent.pm +++ b/centreon-plugins/apps/protocols/http/mode/expectedcontent.pm @@ -26,6 +26,36 @@ use strict; use warnings; use centreon::plugins::http; use Time::HiRes qw(gettimeofday tv_interval); +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); + +sub custom_content_threshold { + my ($self, %options) = @_; + + $self->{instance_mode}->{content_status} = catalog_status_threshold($self, %options); + return $self->{instance_mode}->{content_status}; +} + + +sub custom_content_output { + my ($self, %options) = @_; + + my $msg = 'content test'; + if (!$self->{output}->is_status(value => $self->{instance_mode}->{content_status}, compare => 'ok', litteral => 1)) { + my $filter = $self->{instance_mode}->{option_results}->{lc($self->{instance_mode}->{content_status}) . '_content'}; + $filter =~ s/\$self->\{result_values\}->/%/g; + $msg = sprintf("content test [filter: '%s']", $filter); + } + + return $msg; +} + +sub custom_content_calc { + my ($self, %options) = @_; + + $self->{result_values}->{content} = $options{new_datas}->{$self->{instance} . '_content'}; + $self->{result_values}->{code} = $options{new_datas}->{$self->{instance} . '_code'}; + return 0; +} sub set_counters { my ($self, %options) = @_; @@ -35,6 +65,14 @@ sub set_counters { ]; $self->{maps_counters}->{global} = [ + { label => 'content', threshold => 0, set => { + key_values => [ { name => 'content' }, { name => 'code' } ], + closure_custom_output => $self->can('custom_content_output'), + closure_custom_calc => $self->can('custom_content_calc'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => $self->can('custom_content_threshold'), + } + }, { label => 'size', display_ok => 0, set => { key_values => [ { name => 'size' } ], output_template => 'Content size : %s', @@ -80,6 +118,7 @@ sub new { "username:s" => { name => 'username' }, "password:s" => { name => 'password' }, "expected-string:s" => { name => 'expected_string' }, + "extracted-pattern:s" => { name => 'extracted_pattern' }, "timeout:s" => { name => 'timeout' }, "no-follow" => { name => 'no_follow', }, "cert-file:s" => { name => 'cert_file' }, @@ -94,6 +133,9 @@ sub new { "unknown-status:s" => { name => 'unknown_status' }, "warning-status:s" => { name => 'warning_status' }, "critical-status:s" => { name => 'critical_status' }, + "unknown-content:s" => { name => 'unknown_content', default => '' }, + "warning-content:s" => { name => 'warning_content', default => '' }, + "critical-content:s" => { name => 'critical_content', default => '' }, }); $self->{http} = centreon::plugins::http->new(%options); @@ -104,11 +146,13 @@ sub check_options { my ($self, %options) = @_; $self->SUPER::check_options(%options); - if (!defined($self->{option_results}->{expected_string})) { - $self->{output}->add_option_msg(short_msg => "You need to specify --expected-string option."); - $self->{output}->option_exit(); + # Legacy compat + if (defined($self->{option_results}->{expected_string}) && $self->{option_results}->{expected_string} ne '') { + $self->{option_results}->{critical_content} = "%{content} =~ /$self->{option_results}->{expected_string}/mi"; } + $self->{http}->set_options(%{$self->{option_results}}); + $self->change_macros(macros => ['warning_content', 'critical_content', 'unknown_content']); } sub manage_selection { @@ -118,25 +162,19 @@ sub manage_selection { my $webcontent = $self->{http}->request(); my $timeelapsed = tv_interval($timing0, [gettimeofday]); - $self->{global} = { time => $timeelapsed }; - - $self->{output}->output_add(long_msg => $webcontent); + $self->{global} = { time => $timeelapsed, content => $webcontent, code => $self->{http}->get_code() }; - if ($webcontent =~ /$self->{option_results}->{expected_string}/mi) { - $self->{output}->output_add(severity => 'OK', - short_msg => sprintf("'%s' is present in content.", $self->{option_results}->{expected_string})); - } else { - $self->{output}->output_add(severity => 'CRITICAL', - short_msg => sprintf("'%s' is not present in content.", $self->{option_results}->{expected_string})); - } - - my $extracted = $1; - if (defined($extracted) && $extracted =~ /(\d+([\.,]\d+)?)/) { - $extracted = $1; - $extracted =~ s/,/\./; - $self->{global}->{extracted} = $extracted; + if (defined($self->{option_results}->{extracted_pattern}) && $self->{option_results}->{extracted_pattern} ne '' && + $webcontent =~ /$self->{option_results}->{extracted_pattern}/mi) { + my $extracted = $1; + if (defined($extracted) && $extracted =~ /(\d+([\.,]\d+)?)/) { + $extracted =~ s/,/\./; + $self->{global}->{extracted} = $extracted, + } } + $self->{output}->output_add(long_msg => $webcontent); + # Size check { require bytes; @@ -243,6 +281,10 @@ Set POST params (Multiple option. Example: --post-param='key=value') Save cookies in a file (Example: '/tmp/lwp_cookies.dat') +=item B<--extracted-pattern> + +Set pattern to extracted a number (used --warning-extracted and --critical-extracted option). + =item B<--unknown-status> Threshold warning for http response code (Default: '%{http_code} < 200 or %{http_code} >= 300') @@ -279,9 +321,20 @@ Threshold warning for extracted value Threshold critical for extracted value -=item B<--expected-string> +=item B<--unknown-content> -Specify String to check on the Webpage +Set warning threshold for content page (Default: ''). +Can used special variables like: %{content}, %{code} + +=item B<--warning-content> + +Set warning threshold for status (Default: ''). +Can used special variables like: %{content}, %{code} + +=item B<--critical-content> + +Set critical threshold for content page (Default: ''). +Can used special variables like: %{content}, %{code} =back