| 1 | package Monitoring::Sneck::Checks::http; | |||||||||||||||||||||||||||||||||
| 2 | ||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.014; | |||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | |||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | |||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use parent 'Monitoring::Sneck::Checks::base'; | |||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Time::HiRes (); | |||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||
| 8 | ||||||||||||||||||||||||||||||||||
| 9 | =head1 NAME | |||||||||||||||||||||||||||||||||
| 10 | ||||||||||||||||||||||||||||||||||
| 11 | Monitoring::Sneck::Checks::http - checks a HTTP or HTTPS server | |||||||||||||||||||||||||||||||||
| 12 | ||||||||||||||||||||||||||||||||||
| 13 | =head1 VERSION | |||||||||||||||||||||||||||||||||
| 14 | ||||||||||||||||||||||||||||||||||
| 15 | Version 0.0.1 | |||||||||||||||||||||||||||||||||
| 16 | ||||||||||||||||||||||||||||||||||
| 17 | =cut | |||||||||||||||||||||||||||||||||
| 18 | ||||||||||||||||||||||||||||||||||
| 19 | 4 | our $VERSION = '0.0.1'; | ||||||||||||||||||||||||||||||||
| 20 | ||||||||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSIS | |||||||||||||||||||||||||||||||||
| 22 | ||||||||||||||||||||||||||||||||||
| 23 | www|perl:http -H www.example.org -S -w 1 -c 2 -s 'Welcome' | |||||||||||||||||||||||||||||||||
| 24 | api|perl:http -u https://api.example.org/health -e 200 -r '"status":\s*"ok"' | |||||||||||||||||||||||||||||||||
| 25 | feed|perl:http -H www.example.org -u /feed.xml -x '//item' | |||||||||||||||||||||||||||||||||
| 26 | to_https|perl:http -H www.example.org -L https://www.example.org/ | |||||||||||||||||||||||||||||||||
| 27 | hsts|perl:http -H www.example.org -S -E 'Strict-Transport-Security: max-age=[0-9]{8}' -E Content-Security-Policy | |||||||||||||||||||||||||||||||||
| 28 | ||||||||||||||||||||||||||||||||||
| 29 | =head1 DESCRIPTION | |||||||||||||||||||||||||||||||||
| 30 | ||||||||||||||||||||||||||||||||||
| 31 | Fetches a URL through L<LWP::UserAgent>. With no --expect, 2xx and 3xx | |||||||||||||||||||||||||||||||||
| 32 | are OK, 4xx is WARNING, and anything else is CRITICAL, including not | |||||||||||||||||||||||||||||||||
| 33 | being able to connect. Content that does not match is CRITICAL. | |||||||||||||||||||||||||||||||||
| 34 | ||||||||||||||||||||||||||||||||||
| 35 | Certificates are checked for HTTPS unless --insecure is given. See | |||||||||||||||||||||||||||||||||
| 36 | L<Monitoring::Sneck::Checks::https_cert> for when they expire. | |||||||||||||||||||||||||||||||||
| 37 | ||||||||||||||||||||||||||||||||||
| 38 | =head1 OPTIONS | |||||||||||||||||||||||||||||||||
| 39 | ||||||||||||||||||||||||||||||||||
| 40 | - -H, --hostname :: The host. Required unless --url is a whole URL. | |||||||||||||||||||||||||||||||||
| 41 | ||||||||||||||||||||||||||||||||||
| 42 | - -u, --url :: The path, or a whole URL, in which case --hostname, | |||||||||||||||||||||||||||||||||
| 43 | --port, and --ssl are ignored. | |||||||||||||||||||||||||||||||||
| 44 | Default :: / | |||||||||||||||||||||||||||||||||
| 45 | ||||||||||||||||||||||||||||||||||
| 46 | - -p, --port :: The port. | |||||||||||||||||||||||||||||||||
| 47 | Default :: 80, or 443 with --ssl | |||||||||||||||||||||||||||||||||
| 48 | ||||||||||||||||||||||||||||||||||
| 49 | - -S, --ssl :: Use HTTPS. | |||||||||||||||||||||||||||||||||
| 50 | ||||||||||||||||||||||||||||||||||
| 51 | - -j, --method :: The method. | |||||||||||||||||||||||||||||||||
| 52 | Default :: GET | |||||||||||||||||||||||||||||||||
| 53 | ||||||||||||||||||||||||||||||||||
| 54 | - -P, --post :: Content to send, which makes the method POST unless | |||||||||||||||||||||||||||||||||
| 55 | --method is given. | |||||||||||||||||||||||||||||||||
| 56 | ||||||||||||||||||||||||||||||||||
| 57 | - -T, --content-type :: The type of what --post sends. | |||||||||||||||||||||||||||||||||
| 58 | Default :: application/x-www-form-urlencoded | |||||||||||||||||||||||||||||||||
| 59 | ||||||||||||||||||||||||||||||||||
| 60 | - -k, --header :: A header to send, as "Name: value". May be given more | |||||||||||||||||||||||||||||||||
| 61 | than once. | |||||||||||||||||||||||||||||||||
| 62 | ||||||||||||||||||||||||||||||||||
| 63 | - -e, --expect :: Status codes that are OK, comma separated. Anything | |||||||||||||||||||||||||||||||||
| 64 | else is CRITICAL. | |||||||||||||||||||||||||||||||||
| 65 | ||||||||||||||||||||||||||||||||||
| 66 | - -s, --string :: Text the content must have. | |||||||||||||||||||||||||||||||||
| 67 | ||||||||||||||||||||||||||||||||||
| 68 | - -r, --regex :: A regex the content must match. | |||||||||||||||||||||||||||||||||
| 69 | ||||||||||||||||||||||||||||||||||
| 70 | - --invert-regex :: The content must not match --regex. | |||||||||||||||||||||||||||||||||
| 71 | ||||||||||||||||||||||||||||||||||
| 72 | - -x, --xpath :: A XPath expression that must find something in the | |||||||||||||||||||||||||||||||||
| 73 | content, parsed as XML if it says it is, else as HTML. | |||||||||||||||||||||||||||||||||
| 74 | ||||||||||||||||||||||||||||||||||
| 75 | - -f, --follow :: Follow up to 5 redirects. | |||||||||||||||||||||||||||||||||
| 76 | ||||||||||||||||||||||||||||||||||
| 77 | - -E, --expect-header :: "Name", a header that must be there, or | |||||||||||||||||||||||||||||||||
| 78 | "Name: regex", one that must match the regex. Names are not case | |||||||||||||||||||||||||||||||||
| 79 | sensitive. May be given more than once. | |||||||||||||||||||||||||||||||||
| 80 | ||||||||||||||||||||||||||||||||||
| 81 | - -L, --location :: Where it must redirect to. A relative one is taken | |||||||||||||||||||||||||||||||||
| 82 | from the URL fetched, as is the Location header, so /login and | |||||||||||||||||||||||||||||||||
| 83 | https://www.example.org/login are the same for https://www.example.org/. | |||||||||||||||||||||||||||||||||
| 84 | No Location, or a different one, is CRITICAL. Can not be used with | |||||||||||||||||||||||||||||||||
| 85 | --follow. | |||||||||||||||||||||||||||||||||
| 86 | ||||||||||||||||||||||||||||||||||
| 87 | - -w, --warning :: Range of seconds for the response time. | |||||||||||||||||||||||||||||||||
| 88 | ||||||||||||||||||||||||||||||||||
| 89 | - -c, --critical :: Range of seconds for the response time. | |||||||||||||||||||||||||||||||||
| 90 | ||||||||||||||||||||||||||||||||||
| 91 | - --insecure :: Do not check the certificate. | |||||||||||||||||||||||||||||||||
| 92 | ||||||||||||||||||||||||||||||||||
| 93 | - --ca-file :: The CA file to check the certificate against. | |||||||||||||||||||||||||||||||||
| 94 | ||||||||||||||||||||||||||||||||||
| 95 | =cut | |||||||||||||||||||||||||||||||||
| 96 | ||||||||||||||||||||||||||||||||||
| 97 | 43 | ▶ | sub option_spec { | |||||||||||||||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||
| 98 | return ( | |||||||||||||||||||||||||||||||||
| 99 | 43 | 'hostname|H=s', 'url|u=s', 'port|p=i', 'ssl|S', 'method|j=s', | ||||||||||||||||||||||||||||||||
| 100 | 'post|P=s', 'content-type|T=s', 'header|k=s@', 'expect|e=s', 'string|s=s', | |||||||||||||||||||||||||||||||||
| 101 | 'regex|r=s', 'invert-regex', 'xpath|x=s', 'location|L=s', 'expect-header|E=s@', | |||||||||||||||||||||||||||||||||
| 102 | 'follow|f', 'warning|w=s', 'critical|c=s', 'insecure', 'ca-file=s', | |||||||||||||||||||||||||||||||||
| 103 | ); | |||||||||||||||||||||||||||||||||
| 104 | } | |||||||||||||||||||||||||||||||||
| 105 | ||||||||||||||||||||||||||||||||||
| 106 | 43 | ▶ | sub defaults { return { url => '/', 'content-type' => 'application/x-www-form-urlencoded' } } | |||||||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||
| 107 | ||||||||||||||||||||||||||||||||||
| 108 | 43 | ▶ | sub ranges { return ( 'warning', 'critical' ) } | |||||||||||||||||||||||||||||||
|
Subroutine
ranges: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||
| 109 | ||||||||||||||||||||||||||||||||||
| 110 | 43 | ▶ | sub validate { | |||||||||||||||||||||||||||||||
|
Subroutine
validate: called CC=19 SCAR=29.5
Pod
documented
| ||||||||||||||||||||||||||||||||||
| 111 | 43 | my ( $self, $options ) = @_; | ||||||||||||||||||||||||||||||||
| 112 | ||||||||||||||||||||||||||||||||||
| 113 | 43 | my @problems; | ||||||||||||||||||||||||||||||||
| 114 | 43 | ▶ | if ( !defined( $options->{hostname} ) && $options->{url} !~ m{\Ahttps?://}i ) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $options->{"hostname"} or $options->{"url"} =~ m[\Ahttps?://]iu
MC/DC100%2/2
defined $options->{"hostname"} or $options->{"url"} =~ m[\Ahttps?://]iu
defined $options->{"hostname"}
$options->{"url"} =~ m[\Ahttps?://]iu
Truth table100%3/3
A or B
A= defined $options->{"hostname"}B= $options->{"url"} =~ m[\Ahttps?://]iu
| ||||||||||||||||||||||||||||||||||
| 115 | 1 | push( @problems, '--hostname is required unless --url is a whole URL' ); | ||||||||||||||||||||||||||||||||
| 116 | } | |||||||||||||||||||||||||||||||||
| 117 | 43 | ▶ | if ( defined( $options->{hostname} ) && !$self->is_host( $options->{hostname} ) ) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $options->{"hostname"} and not $self->is_host($options->{"hostname"})
MC/DC100%2/2
defined $options->{"hostname"} and not $self->is_host($options->{"hostname"})
defined $options->{"hostname"}
not $self->is_host($options->{"hostname"})
Truth table100%3/3
A and not(B)
A= defined $options->{"hostname"}B= not $self->is_host($options->{"hostname"})
| ||||||||||||||||||||||||||||||||||
| 118 | 1 | push( @problems, '--hostname "' . $options->{hostname} . '" is not a IP or hostname' ); | ||||||||||||||||||||||||||||||||
| 119 | } | |||||||||||||||||||||||||||||||||
| 120 | 43 | ▶ | foreach my $header ( @{ $options->{'expect-header'} // [] } ) { | |||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"expect-header"} // []
MC/DC100%1/1
$options->{"expect-header"} // []
$options->{"expect-header"}
Truth table100%2/2
A
A= $options->{"expect-header"}
| ||||||||||||||||||||||||||||||||||
| 121 | 4 | my ( $name, $regex ) = $header =~ /\A([^:\s]+)(?::\s*(.*))?\z/; | ||||||||||||||||||||||||||||||||
| 122 | 4 | ▶ | if ( !defined($name) ) { | |||||||||||||||||||||||||||||||
|
Branch: 2 branches
| ||||||||||||||||||||||||||||||||||
| 123 | 0 | push( @problems, '--expect-header "' . $header . '" is not Name or "Name: regex"' ); | ||||||||||||||||||||||||||||||||
| 124 | 3 | ▶ | } elsif ( defined($regex) && !eval { qr/$regex/; 1 } ) { | |||||||||||||||||||||||||||||||
|
Condition100%3/3
defined $regex and not eval {
do {
qr/$regex/u;
1
}
}
MC/DC100%2/2
defined $regex and not eval {
do {
qr/$regex/u;
1
}
}
defined $regex
not eval {
do {
qr/$regex/u;
1
}
}
Truth table100%3/3
A and not(B)
A= defined $regexB= not eval {
do {
qr/$regex/u;
1
}
}
| ||||||||||||||||||||||||||||||||||
| 125 | 1 | push( @problems, '--expect-header "' . $header . '" does not have a valid regex' ); | ||||||||||||||||||||||||||||||||
| 126 | } | |||||||||||||||||||||||||||||||||
| 127 | } | |||||||||||||||||||||||||||||||||
| 128 | push( @problems, '--location and --follow can not both be given' ) | |||||||||||||||||||||||||||||||||
| 129 | 43 | ▶ | if defined( $options->{location} ) && $options->{follow}; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $options->{"location"} and $options->{"follow"}
MC/DC100%2/2
defined $options->{"location"} and $options->{"follow"}
defined $options->{"location"}
$options->{"follow"}
Truth table100%3/3
A and B
A= defined $options->{"location"}B= $options->{"follow"}
| ||||||||||||||||||||||||||||||||||
| 130 | 43 | ▶ | if ( defined( $options->{expect} ) && $options->{expect} !~ /\A[0-9]{3}(?:,[0-9]{3})*\z/ ) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $options->{"expect"} and not $options->{"expect"} =~ /\A[0-9]{3}(?:,[0-9]{3})*\z/u
MC/DC100%2/2
defined $options->{"expect"} and not $options->{"expect"} =~ /\A[0-9]{3}(?:,[0-9]{3})*\z/u
defined $options->{"expect"}
not $options->{"expect"} =~ /\A[0-9]{3}(?:,[0-9]{3})*\z/u
Truth table100%3/3
A and not(B)
A= defined $options->{"expect"}B= not $options->{"expect"} =~ /\A[0-9]{3}(?:,[0-9]{3})*\z/u
| ||||||||||||||||||||||||||||||||||
| 131 | 1 | push( @problems, '--expect must be status codes, comma separated' ); | ||||||||||||||||||||||||||||||||
| 132 | } | |||||||||||||||||||||||||||||||||
| 133 | 43 | ▶ | if ( defined( $options->{regex} ) && !eval { qr/$options->{regex}/; 1 } ) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $options->{"regex"} and not eval {
do {
qr/$options->{"regex"}/u;
1
}
}
MC/DC100%2/2
defined $options->{"regex"} and not eval {
do {
qr/$options->{"regex"}/u;
1
}
}
defined $options->{"regex"}
not eval {
do {
qr/$options->{"regex"}/u;
1
}
}
Truth table100%3/3
A and not(B)
A= defined $options->{"regex"}B= not eval {
do {
qr/$options->{"regex"}/u;
1
}
}
| ||||||||||||||||||||||||||||||||||
| 134 | 1 | push( @problems, '--regex is not a valid regex' ); | ||||||||||||||||||||||||||||||||
| 135 | } | |||||||||||||||||||||||||||||||||
| 136 | 43 | ▶ | foreach my $header ( @{ $options->{header} // [] } ) { | |||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"header"} // []
MC/DC100%1/1
$options->{"header"} // []
$options->{"header"}
Truth table100%2/2
A
A= $options->{"header"}
| ||||||||||||||||||||||||||||||||||
| 137 | 2 | ▶ | push( @problems, '--header "' . $header . '" is not "Name: value"' ) if $header !~ /\A[^:\s]+:/; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 138 | } | |||||||||||||||||||||||||||||||||
| 139 | ||||||||||||||||||||||||||||||||||
| 140 | 43 | return @problems; | ||||||||||||||||||||||||||||||||
| 141 | } ## end sub validate | |||||||||||||||||||||||||||||||||
| 142 | ||||||||||||||||||||||||||||||||||
| 143 | =head1 METHODS | |||||||||||||||||||||||||||||||||
| 144 | ||||||||||||||||||||||||||||||||||
| 145 | =head2 url | |||||||||||||||||||||||||||||||||
| 146 | ||||||||||||||||||||||||||||||||||
| 147 | Returns the URL to fetch. | |||||||||||||||||||||||||||||||||
| 148 | ||||||||||||||||||||||||||||||||||
| 149 | =cut | |||||||||||||||||||||||||||||||||
| 150 | ||||||||||||||||||||||||||||||||||
| 151 | 43 | ▶ | sub url { | |||||||||||||||||||||||||||||||
|
Subroutine
url: called CC=6 SCAR=17.9
Pod
documented
| ||||||||||||||||||||||||||||||||||
| 152 | 43 | my ($self) = @_; | ||||||||||||||||||||||||||||||||
| 153 | ||||||||||||||||||||||||||||||||||
| 154 | 43 | my $url = $self->opt('url'); | ||||||||||||||||||||||||||||||||
| 155 | 43 | ▶ | return $url if $url =~ m{\Ahttps?://}i; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 156 | ||||||||||||||||||||||||||||||||||
| 157 | 34 | my $host = $self->opt('hostname'); | ||||||||||||||||||||||||||||||||
| 158 | 34 | ▶ | $host = '[' . $host . ']' if $host =~ /:/; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 159 | 34 | ▶ | my $scheme = $self->opt('ssl') ? 'https' : 'http'; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 160 | 34 | ▶ | my $port = defined( $self->opt('port') ) ? ':' . $self->opt('port') : ''; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 161 | 34 | ▶ | $url = '/' . $url if $url !~ m{\A/}; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 162 | ||||||||||||||||||||||||||||||||||
| 163 | 34 | return $scheme . '://' . $host . $port . $url; | ||||||||||||||||||||||||||||||||
| 164 | } ## end sub url | |||||||||||||||||||||||||||||||||
| 165 | ||||||||||||||||||||||||||||||||||
| 166 | =head2 check | |||||||||||||||||||||||||||||||||
| 167 | ||||||||||||||||||||||||||||||||||
| 168 | Does the check. | |||||||||||||||||||||||||||||||||
| 169 | ||||||||||||||||||||||||||||||||||
| 170 | =cut | |||||||||||||||||||||||||||||||||
| 171 | ||||||||||||||||||||||||||||||||||
| 172 | 33 | ▶ | sub check { | |||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=19 SCAR=29.6
Pod
documented
| ||||||||||||||||||||||||||||||||||
| 173 | 33 | my ($self) = @_; | ||||||||||||||||||||||||||||||||
| 174 | ||||||||||||||||||||||||||||||||||
| 175 | 33 | require HTTP::Request; | ||||||||||||||||||||||||||||||||
| 176 | 33 | ▶ | my $user_agent = $self->user_agent( | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 177 | insecure => $self->opt('insecure'), | |||||||||||||||||||||||||||||||||
| 178 | ca_file => $self->opt('ca-file'), | |||||||||||||||||||||||||||||||||
| 179 | max_redirect => $self->opt('follow') ? 5 : 0, | |||||||||||||||||||||||||||||||||
| 180 | ); | |||||||||||||||||||||||||||||||||
| 181 | 33 | ▶ | my $method = $self->opt('method') // ( defined( $self->opt('post') ) ? 'POST' : 'GET' ); | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$self->opt("method") // (defined $self->opt("post") ? "POST" : "GET")
MC/DC100%1/1
$self->opt("method") // (defined $self->opt("post") ? "POST" : "GET")
$self->opt("method")
Truth table100%2/2
A
A= $self->opt("method")
| ||||||||||||||||||||||||||||||||||
| 182 | 33 | my $request = HTTP::Request->new( uc($method), $self->url ); | ||||||||||||||||||||||||||||||||
| 183 | 33 | ▶ | foreach my $header ( @{ $self->opt('header') // [] } ) { | |||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->opt("header") // []
MC/DC100%1/1
$self->opt("header") // []
$self->opt("header")
Truth table100%2/2
A
A= $self->opt("header")
| ||||||||||||||||||||||||||||||||||
| 184 | 1 | my ( $name, $value ) = split( /:\s*/, $header, 2 ); | ||||||||||||||||||||||||||||||||
| 185 | 1 | $request->header( $name => $value ); | ||||||||||||||||||||||||||||||||
| 186 | } | |||||||||||||||||||||||||||||||||
| 187 | 33 | ▶ | if ( defined( $self->opt('post') ) ) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 188 | 1 | $request->content_type( $self->opt('content-type') ); | ||||||||||||||||||||||||||||||||
| 189 | 1 | $request->content( $self->opt('post') ); | ||||||||||||||||||||||||||||||||
| 190 | } | |||||||||||||||||||||||||||||||||
| 191 | ||||||||||||||||||||||||||||||||||
| 192 | 33 | my $start = Time::HiRes::time; | ||||||||||||||||||||||||||||||||
| 193 | 33 | my $response = $user_agent->request($request); | ||||||||||||||||||||||||||||||||
| 194 | 33 | my $seconds = sprintf( '%.3f', Time::HiRes::time - $start ); | ||||||||||||||||||||||||||||||||
| 195 | ||||||||||||||||||||||||||||||||||
| 196 | # LWP makes up a 500 when it never got a answer | |||||||||||||||||||||||||||||||||
| 197 | 33 | ▶ | my $internal = ( $response->header('Client-Warning') // '' ) eq 'Internal response'; | |||||||||||||||||||||||||||||||
|
Condition100%2/2
$response->header("Client-Warning") // ""
MC/DC100%1/1
$response->header("Client-Warning") // ""
$response->header("Client-Warning")
Truth table100%2/2
A
A= $response->header("Client-Warning")
| ||||||||||||||||||||||||||||||||||
| 198 | 33 | ▶ | if ($internal) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 199 | 1 | ▶ | my $why = $response->message // 'failed'; | |||||||||||||||||||||||||||||||
|
Condition50%1/2
$response->message // "failed"
MC/DC0%0/1
$response->message // "failed"
$response->message
Truth table50%1/2
A
A= $response->message
| ||||||||||||||||||||||||||||||||||
| 200 | 1 | $why =~ s/\s+\z//; | ||||||||||||||||||||||||||||||||
| 201 | 1 | return $self->finish( $self->CRITICAL, $self->url . ', ' . $why ); | ||||||||||||||||||||||||||||||||
| 202 | } | |||||||||||||||||||||||||||||||||
| 203 | ||||||||||||||||||||||||||||||||||
| 204 | 32 | ▶ | my $content = $response->decoded_content // $response->content // ''; | |||||||||||||||||||||||||||||||
|
Condition40%2/5
($response->decoded_content // $response->content) // ""
$response->decoded_content // $response->content
MC/DC0%0/2
($response->decoded_content // $response->content) // ""
$response->decoded_content
$response->content
Truth table33%1/3
A // B
A= $response->decoded_contentB= $response->content
| ||||||||||||||||||||||||||||||||||
| 205 | 32 | my @codes = ( $self->status_code( $response->code ) ); | ||||||||||||||||||||||||||||||||
| 206 | 32 | my @problems; | ||||||||||||||||||||||||||||||||
| 207 | 32 | ▶ | if ( $codes[0] != $self->OK ) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 208 | 3 | push( @problems, 'unexpected status' ); | ||||||||||||||||||||||||||||||||
| 209 | } | |||||||||||||||||||||||||||||||||
| 210 | ||||||||||||||||||||||||||||||||||
| 211 | 32 | foreach my $found ( $self->content_problems( $response, $content ) ) { | ||||||||||||||||||||||||||||||||
| 212 | 15 | push( @codes, $found->[0] ); | ||||||||||||||||||||||||||||||||
| 213 | 15 | push( @problems, $found->[1] ); | ||||||||||||||||||||||||||||||||
| 214 | } | |||||||||||||||||||||||||||||||||
| 215 | ||||||||||||||||||||||||||||||||||
| 216 | 32 | my $time_code = $self->threshold_status( $seconds, $self->opt('warning'), $self->opt('critical') ); | ||||||||||||||||||||||||||||||||
| 217 | 32 | push( @codes, $time_code ); | ||||||||||||||||||||||||||||||||
| 218 | 32 | ▶ | push( @problems, 'slow' ) if $time_code != $self->OK; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 219 | ||||||||||||||||||||||||||||||||||
| 220 | 32 | $self->perfdata( | ||||||||||||||||||||||||||||||||
| 221 | label => 'time', | |||||||||||||||||||||||||||||||||
| 222 | value => $seconds, | |||||||||||||||||||||||||||||||||
| 223 | uom => 's', | |||||||||||||||||||||||||||||||||
| 224 | warning => $self->opt('warning'), | |||||||||||||||||||||||||||||||||
| 225 | critical => $self->opt('critical'), | |||||||||||||||||||||||||||||||||
| 226 | min => 0 | |||||||||||||||||||||||||||||||||
| 227 | ); | |||||||||||||||||||||||||||||||||
| 228 | 32 | ▶ | $self->perfdata( label => 'size', value => length( $response->content // '' ), uom => 'B', min => 0 ); | |||||||||||||||||||||||||||||||
|
Condition50%1/2
$response->content // ""
MC/DC0%0/1
$response->content // ""
$response->content
Truth table50%1/2
A
A= $response->content
| ||||||||||||||||||||||||||||||||||
| 229 | ||||||||||||||||||||||||||||||||||
| 230 | 32 | ▶ | my $message | |||||||||||||||||||||||||||||||
|
Condition50%2/4
$response->protocol // "HTTP"
$response->content // ""
MC/DC0%0/1
$response->protocol // "HTTP"
$response->protocol
MC/DC0%0/1
$response->content // ""
$response->content
Truth table50%1/2
A
A= $response->protocol
Truth table50%1/2
A
A= $response->content
| ||||||||||||||||||||||||||||||||||
| 231 | = ( $response->protocol // 'HTTP' ) . ' ' | |||||||||||||||||||||||||||||||||
| 232 | . $response->status_line . ' - ' | |||||||||||||||||||||||||||||||||
| 233 | . length( $response->content // '' ) | |||||||||||||||||||||||||||||||||
| 234 | . ' bytes in ' | |||||||||||||||||||||||||||||||||
| 235 | . $seconds | |||||||||||||||||||||||||||||||||
| 236 | . ' seconds'; | |||||||||||||||||||||||||||||||||
| 237 | 32 | ▶ | $message = $message . ', ' . join( ', ', @problems ) if defined( $problems[0] ); | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 238 | ||||||||||||||||||||||||||||||||||
| 239 | 32 | return $self->finish( $self->worst(@codes), $message ); | ||||||||||||||||||||||||||||||||
| 240 | } ## end sub check | |||||||||||||||||||||||||||||||||
| 241 | ||||||||||||||||||||||||||||||||||
| 242 | =head2 content_problems | |||||||||||||||||||||||||||||||||
| 243 | ||||||||||||||||||||||||||||||||||
| 244 | Checks the response as --location, --expect-header, --string, --regex, | |||||||||||||||||||||||||||||||||
| 245 | and --xpath say. | |||||||||||||||||||||||||||||||||
| 246 | A subclass may add its own checks to these. | |||||||||||||||||||||||||||||||||
| 247 | ||||||||||||||||||||||||||||||||||
| 248 | - response :: The L<HTTP::Response>. | |||||||||||||||||||||||||||||||||
| 249 | ||||||||||||||||||||||||||||||||||
| 250 | - content :: Its decoded content. | |||||||||||||||||||||||||||||||||
| 251 | ||||||||||||||||||||||||||||||||||
| 252 | Returns a list of array refs of the code and what is wrong, one for | |||||||||||||||||||||||||||||||||
| 253 | each problem. | |||||||||||||||||||||||||||||||||
| 254 | ||||||||||||||||||||||||||||||||||
| 255 | foreach my $found ( $check->content_problems( $response, $content ) ) { | |||||||||||||||||||||||||||||||||
| 256 | my ( $code, $problem ) = @{$found}; | |||||||||||||||||||||||||||||||||
| 257 | } | |||||||||||||||||||||||||||||||||
| 258 | ||||||||||||||||||||||||||||||||||
| 259 | =cut | |||||||||||||||||||||||||||||||||
| 260 | ||||||||||||||||||||||||||||||||||
| 261 | 32 | ▶ | sub content_problems { | |||||||||||||||||||||||||||||||
|
Subroutine
content_problems: called CC=18 SCAR=29.0
Pod
documented
| ||||||||||||||||||||||||||||||||||
| 262 | 32 | my ( $self, $response, $content ) = @_; | ||||||||||||||||||||||||||||||||
| 263 | ||||||||||||||||||||||||||||||||||
| 264 | 32 | my @found; | ||||||||||||||||||||||||||||||||
| 265 | 32 | ▶ | if ( defined( $self->opt('location') ) ) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 266 | 5 | require URI; | ||||||||||||||||||||||||||||||||
| 267 | 5 | my $wanted = URI->new_abs( $self->opt('location'), $self->url )->canonical->as_string; | ||||||||||||||||||||||||||||||||
| 268 | 5 | my $located = $response->header('Location'); | ||||||||||||||||||||||||||||||||
| 269 | 5 | ▶ | if ( !defined($located) ) { | |||||||||||||||||||||||||||||||
|
Branch: 2 branches
| ||||||||||||||||||||||||||||||||||
| 270 | 1 | push( @found, [ $self->CRITICAL, 'no Location, wanted ' . $wanted ] ); | ||||||||||||||||||||||||||||||||
| 271 | } elsif ( ( my $absolute = URI->new_abs( $located, $self->url )->canonical->as_string ) ne $wanted ) { | |||||||||||||||||||||||||||||||||
| 272 | 1 | push( @found, [ $self->CRITICAL, 'redirects to ' . $absolute . ', not ' . $wanted ] ); | ||||||||||||||||||||||||||||||||
| 273 | } | |||||||||||||||||||||||||||||||||
| 274 | } ## end if ( defined( $self->opt('location') ) ) | |||||||||||||||||||||||||||||||||
| 275 | 32 | ▶ | foreach my $header ( @{ $self->opt('expect-header') // [] } ) { | |||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->opt("expect-header") // []
MC/DC100%1/1
$self->opt("expect-header") // []
$self->opt("expect-header")
Truth table100%2/2
A
A= $self->opt("expect-header")
| ||||||||||||||||||||||||||||||||||
| 276 | 3 | my ( $name, $regex ) = $header =~ /\A([^:\s]+)(?::\s*(.*))?\z/; | ||||||||||||||||||||||||||||||||
| 277 | 3 | my $value = $response->header($name); | ||||||||||||||||||||||||||||||||
| 278 | 3 | ▶ | if ( !defined($value) ) { | |||||||||||||||||||||||||||||||
|
Branch: 2 branches
| ||||||||||||||||||||||||||||||||||
| 279 | 1 | push( @found, [ $self->CRITICAL, 'no ' . $name . ' header' ] ); | ||||||||||||||||||||||||||||||||
| 280 | ▶ | } elsif ( defined($regex) && $value !~ /$regex/ ) { | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $regex and not $value =~ /$regex/u
MC/DC50%1/2
defined $regex and not $value =~ /$regex/u
defined $regex
not $value =~ /$regex/u
Truth table66%2/3
A and not(B)
A= defined $regexB= not $value =~ /$regex/u
| ||||||||||||||||||||||||||||||||||
| 281 | 1 | push( @found, [ $self->CRITICAL, $name . ' is "' . $value . '", not matching /' . $regex . '/' ] ); | ||||||||||||||||||||||||||||||||
| 282 | } | |||||||||||||||||||||||||||||||||
| 283 | } | |||||||||||||||||||||||||||||||||
| 284 | 32 | ▶ | if ( defined( $self->opt('string') ) && index( $content, $self->opt('string') ) < 0 ) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->opt("string") and index($content, $self->opt("string")) == -1
MC/DC100%2/2
defined $self->opt("string") and index($content, $self->opt("string")) == -1
defined $self->opt("string")
index($content, $self->opt("string")) == -1
Truth table100%3/3
A and B
A= defined $self->opt("string")B= index($content, $self->opt("string")) == -1
| ||||||||||||||||||||||||||||||||||
| 285 | 1 | push( @found, [ $self->CRITICAL, 'did not find "' . $self->opt('string') . '"' ] ); | ||||||||||||||||||||||||||||||||
| 286 | } | |||||||||||||||||||||||||||||||||
| 287 | 32 | ▶ | if ( defined( $self->opt('regex') ) ) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 288 | 2 | my $regex = $self->opt('regex'); | ||||||||||||||||||||||||||||||||
| 289 | 2 | ▶ | my $matched = ( $content =~ /$regex/ ) ? 1 : 0; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 290 | 2 | ▶ | if ( $matched xor !$self->opt('invert-regex') ) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$matched xor not $self->opt("invert-regex")
MC/DC50%1/2
$matched xor not $self->opt("invert-regex")
$matched
not $self->opt("invert-regex")
Truth table50%2/4
A xor not(B)
A= $matchedB= not $self->opt("invert-regex")
| ||||||||||||||||||||||||||||||||||
| 291 | 1 | ▶ | push( @found, [ $self->CRITICAL, ( $matched ? 'matched' : 'did not match' ) . ' /' . $regex . '/' ] ); | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 292 | } | |||||||||||||||||||||||||||||||||
| 293 | } | |||||||||||||||||||||||||||||||||
| 294 | 32 | ▶ | if ( defined( $self->opt('xpath') ) && !$self->xpath_count( $response, $content ) ) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->opt("xpath") and not $self->xpath_count($response, $content)
MC/DC100%2/2
defined $self->opt("xpath") and not $self->xpath_count($response, $content)
defined $self->opt("xpath")
not $self->xpath_count($response, $content)
Truth table100%3/3
A and not(B)
A= defined $self->opt("xpath")B= not $self->xpath_count($response, $content)
| ||||||||||||||||||||||||||||||||||
| 295 | 1 | push( @found, [ $self->CRITICAL, 'XPath ' . $self->opt('xpath') . ' found nothing' ] ); | ||||||||||||||||||||||||||||||||
| 296 | } | |||||||||||||||||||||||||||||||||
| 297 | ||||||||||||||||||||||||||||||||||
| 298 | 32 | return @found; | ||||||||||||||||||||||||||||||||
| 299 | } ## end sub content_problems | |||||||||||||||||||||||||||||||||
| 300 | ||||||||||||||||||||||||||||||||||
| 301 | =head2 status_code | |||||||||||||||||||||||||||||||||
| 302 | ||||||||||||||||||||||||||||||||||
| 303 | Returns the code for a HTTP status, as said under L</DESCRIPTION>. | |||||||||||||||||||||||||||||||||
| 304 | ||||||||||||||||||||||||||||||||||
| 305 | $check->status_code(404); # WARNING, unless --expect is given | |||||||||||||||||||||||||||||||||
| 306 | ||||||||||||||||||||||||||||||||||
| 307 | =cut | |||||||||||||||||||||||||||||||||
| 308 | ||||||||||||||||||||||||||||||||||
| 309 | 32 | ▶ | sub status_code { | |||||||||||||||||||||||||||||||
|
Subroutine
status_code: called CC=7 SCAR=19.5
Pod
documented
| ||||||||||||||||||||||||||||||||||
| 310 | 32 | my ( $self, $status ) = @_; | ||||||||||||||||||||||||||||||||
| 311 | ||||||||||||||||||||||||||||||||||
| 312 | 32 | ▶ | if ( defined( $self->opt('expect') ) ) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 313 | 2 | my %expected = map { $_ => 1 } split( /,/, $self->opt('expect') ); | ||||||||||||||||||||||||||||||||
| 314 | 2 | ▶ | return $expected{$status} ? $self->OK : $self->CRITICAL; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 315 | } | |||||||||||||||||||||||||||||||||
| 316 | 30 | ▶ | return $self->OK if $status >= 200 && $status < 400; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$status >= 200 and $status < 400
MC/DC50%1/2
$status >= 200 and $status < 400
$status >= 200
$status < 400
Truth table66%2/3
A and B
A= $status >= 200B= $status < 400
| ||||||||||||||||||||||||||||||||||
| 317 | 2 | ▶ | return $self->WARNING if $status >= 400 && $status < 500; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$status >= 400 and $status < 500
MC/DC50%1/2
$status >= 400 and $status < 500
$status >= 400
$status < 500
Truth table66%2/3
A and B
A= $status >= 400B= $status < 500
| ||||||||||||||||||||||||||||||||||
| 318 | ||||||||||||||||||||||||||||||||||
| 319 | 1 | return $self->CRITICAL; | ||||||||||||||||||||||||||||||||
| 320 | } ## end sub status_code | |||||||||||||||||||||||||||||||||
| 321 | ||||||||||||||||||||||||||||||||||
| 322 | =head2 xpath_count | |||||||||||||||||||||||||||||||||
| 323 | ||||||||||||||||||||||||||||||||||
| 324 | Returns how many nodes --xpath finds in the content of a response, 0 | |||||||||||||||||||||||||||||||||
| 325 | if it can not be parsed. | |||||||||||||||||||||||||||||||||
| 326 | ||||||||||||||||||||||||||||||||||
| 327 | =cut | |||||||||||||||||||||||||||||||||
| 328 | ||||||||||||||||||||||||||||||||||
| 329 | 3 | ▶ | sub xpath_count { | |||||||||||||||||||||||||||||||
|
Subroutine
xpath_count: called CC=7 SCAR=19.8
Pod
documented
| ||||||||||||||||||||||||||||||||||
| 330 | 3 | my ( $self, $response, $content ) = @_; | ||||||||||||||||||||||||||||||||
| 331 | ||||||||||||||||||||||||||||||||||
| 332 | 3 | require XML::LibXML; | ||||||||||||||||||||||||||||||||
| 333 | 3 | ▶ | my $type = $response->content_type // ''; | |||||||||||||||||||||||||||||||
|
Condition50%1/2
$response->content_type // ""
MC/DC0%0/1
$response->content_type // ""
$response->content_type
Truth table50%1/2
A
A= $response->content_type
| ||||||||||||||||||||||||||||||||||
| 334 | 3 | ▶ | my $is_xml = ( $type =~ /xml/ && $type !~ /html/ ) ? 1 : 0; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$type =~ /xml/u and not $type =~ /html/u
MC/DC50%1/2
$type =~ /xml/u and not $type =~ /html/u
$type =~ /xml/u
not $type =~ /html/u
Truth table66%2/3
A and not(B)
A= $type =~ /xml/uB= not $type =~ /html/u
| ||||||||||||||||||||||||||||||||||
| 335 | 3 | my $document = eval { | ||||||||||||||||||||||||||||||||
| 336 | 3 | ▶ | $is_xml | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 337 | ? XML::LibXML->load_xml( string => $content, no_network => 1 ) | |||||||||||||||||||||||||||||||||
| 338 | : XML::LibXML->load_html( string => $content, recover => 2, suppress_errors => 1, no_network => 1 ); | |||||||||||||||||||||||||||||||||
| 339 | }; | |||||||||||||||||||||||||||||||||
| 340 | 3 | ▶ | return 0 if !defined($document); | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 341 | ||||||||||||||||||||||||||||||||||
| 342 | 3 | my $nodes = eval { $document->findnodes( $self->opt('xpath') ) }; | ||||||||||||||||||||||||||||||||
| 343 | 3 | ▶ | return 0 if !defined($nodes); | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 344 | ||||||||||||||||||||||||||||||||||
| 345 | 3 | return $nodes->size; | ||||||||||||||||||||||||||||||||
| 346 | } ## end sub xpath_count | |||||||||||||||||||||||||||||||||
| 347 | ||||||||||||||||||||||||||||||||||
| 348 | 4 | 1; | ||||||||||||||||||||||||||||||||