expected-content: add capability to check headers

This commit is contained in:
garnier-quentin 2019-03-25 13:45:37 +01:00
parent 6d24e1fb36
commit db50bade74
3 changed files with 53 additions and 6 deletions

View File

@ -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

View File

@ -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];

View File

@ -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});
}