diff --git a/centreon-plugins/apps/protocols/http/mode/expectedcontent.pm b/centreon-plugins/apps/protocols/http/mode/expectedcontent.pm index 109516b4c..2f0b7e363 100644 --- a/centreon-plugins/apps/protocols/http/mode/expectedcontent.pm +++ b/centreon-plugins/apps/protocols/http/mode/expectedcontent.pm @@ -54,6 +54,8 @@ sub custom_content_calc { $self->{result_values}->{content} = $options{new_datas}->{$self->{instance} . '_content'}; $self->{result_values}->{code} = $options{new_datas}->{$self->{instance} . '_code'}; + $self->{result_values}->{header} = $options{new_datas}->{$self->{instance} . '_header'}; + $self->{result_values}->{first_header} = $options{new_datas}->{$self->{instance} . '_first_header'}; return 0; } @@ -66,7 +68,7 @@ sub set_counters { $self->{maps_counters}->{global} = [ { label => 'content', threshold => 0, set => { - key_values => [ { name => 'content' }, { name => 'code' } ], + key_values => [ { name => 'content' }, { name => 'code' }, { name => 'first_header' }, { name => 'header' } ], closure_custom_output => $self->can('custom_content_output'), closure_custom_calc => $self->can('custom_content_calc'), closure_custom_perfdata => sub { return 0; }, @@ -161,8 +163,14 @@ sub manage_selection { my $timing0 = [gettimeofday]; my $webcontent = $self->{http}->request(); my $timeelapsed = tv_interval($timing0, [gettimeofday]); - - $self->{global} = { time => $timeelapsed, content => $webcontent, code => $self->{http}->get_code() }; + + $self->{global} = { + time => $timeelapsed, + content => $webcontent, + code => $self->{http}->get_code(), + header => $self->{http}->get_header(), + first_header => $self->{http}->get_first_header(), + }; if (defined($self->{option_results}->{extracted_pattern}) && $self->{option_results}->{extracted_pattern} ne '' && $webcontent =~ /$self->{option_results}->{extracted_pattern}/mi) { @@ -324,17 +332,17 @@ Threshold critical for extracted value =item B<--unknown-content> Set warning threshold for content page (Default: ''). -Can used special variables like: %{content}, %{code} +Can used special variables like: %{content}, %{header}, %{first_header}, %{code} =item B<--warning-content> Set warning threshold for status (Default: ''). -Can used special variables like: %{content}, %{code} +Can used special variables like: %{content}, %{header}, %{first_header}, %{code} =item B<--critical-content> Set critical threshold for content page (Default: ''). -Can used special variables like: %{content}, %{code} +Can used special variables like: %{content}, %{header}, %{first_header}, %{code} =back diff --git a/centreon-plugins/centreon/plugins/backend/http/curl.pm b/centreon-plugins/centreon/plugins/backend/http/curl.pm index 542537dc5..215395cae 100644 --- a/centreon-plugins/centreon/plugins/backend/http/curl.pm +++ b/centreon-plugins/centreon/plugins/backend/http/curl.pm @@ -381,9 +381,27 @@ sub request { return $self->{response_body}; } +sub get_headers { + my ($self, %options) = @_; + + my $headers = ''; + foreach (keys %{$self->{response_headers}->[$options{nheader}]}) { + next if (/response_line/); + foreach my $value (@{$self->{response_headers}->[$options{nheader}]->{$_}}) { + $headers .= "$_: " . $value . "\n"; + } + } + + return $headers; +} + sub get_first_header { my ($self, %options) = @_; + if (!defined($options{name})) { + return $self->get_headers(nheader => 0); + } + return undef if (!defined($self->{response_headers}->[0]->{ lc($options{name}) })); return wantarray ? @{$self->{response_headers}->[0]->{ lc($options{name}) }} : $self->{response_headers}->[0]->{ lc($options{name}) }->[0]; @@ -392,6 +410,10 @@ sub get_first_header { sub get_header { my ($self, %options) = @_; + if (!defined($options{name})) { + return $self->get_headers(nheader => -1); + } + return undef if (!defined($self->{response_headers}->[-1]->{ lc($options{name}) })); return wantarray ? @{$self->{response_headers}->[-1]->{ lc($options{name}) }} : $self->{response_headers}->[-1]->{ lc($options{name}) }->[0]; diff --git a/centreon-plugins/centreon/plugins/backend/http/lwp.pm b/centreon-plugins/centreon/plugins/backend/http/lwp.pm index ef3c660d0..22b8dc7d2 100644 --- a/centreon-plugins/centreon/plugins/backend/http/lwp.pm +++ b/centreon-plugins/centreon/plugins/backend/http/lwp.pm @@ -238,10 +238,24 @@ sub request { return $self->{response}->content; } +sub get_headers { + my ($self, %options) = @_; + + my $headers = ''; + foreach ($options{response}->header_field_names()) { + $headers .= "$_: " . $options{response}->header($_) . "\n"; + } + + return $headers; +} + sub get_first_header { my ($self, %options) = @_; my @redirects = $self->{response}->redirects(); + if (!defined($options{name})) { + return $self->get_headers(response => defined($redirects[0]) ? $redirects[0] : $self->{response}); + } return defined($redirects[0]) ? @@ -253,6 +267,9 @@ sub get_first_header { sub get_header { my ($self, %options) = @_; + if (!defined($options{name})) { + return $self->get_headers(response => $self->{response}); + } return $self->{headers}->header($options{name}); }