line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HealthCheck::Diagnostic::WebRequest; |
2
|
1
|
|
|
1
|
|
112727
|
use parent 'HealthCheck::Diagnostic'; |
|
1
|
|
|
|
|
368
|
|
|
1
|
|
|
|
|
7
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# ABSTRACT: Make HTTP/HTTPS requests to web servers to check connectivity |
5
|
1
|
|
|
1
|
|
7372
|
use version; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
4
|
|
6
|
|
|
|
|
|
|
our $VERSION = 'v1.4.1'; # VERSION |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
75
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
21
|
|
9
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
49
|
|
12
|
1
|
|
|
1
|
|
831
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
47568
|
|
|
1
|
|
|
|
|
35
|
|
13
|
1
|
|
|
1
|
|
10
|
use HTTP::Request; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
14
|
1
|
|
|
1
|
|
5
|
use Scalar::Util 'blessed'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1076
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub new { |
17
|
15
|
|
|
15
|
1
|
56512
|
my ($class, @params) = @_; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my %params = @params == 1 && ( ref $params[0] || '' ) eq 'HASH' |
20
|
15
|
50
|
33
|
|
|
81
|
? %{ $params[0] } : @params; |
|
0
|
|
|
|
|
0
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my @bad_params = grep { |
23
|
15
|
|
|
|
|
49
|
!/^( content_regex |
|
27
|
|
|
|
|
148
|
|
24
|
|
|
|
|
|
|
| id |
25
|
|
|
|
|
|
|
| label |
26
|
|
|
|
|
|
|
| no_follow_redirects |
27
|
|
|
|
|
|
|
| options |
28
|
|
|
|
|
|
|
| request |
29
|
|
|
|
|
|
|
| status_code |
30
|
|
|
|
|
|
|
| status_code_eval |
31
|
|
|
|
|
|
|
| tags |
32
|
|
|
|
|
|
|
| timeout |
33
|
|
|
|
|
|
|
| url |
34
|
|
|
|
|
|
|
)$/x |
35
|
|
|
|
|
|
|
} keys %params; |
36
|
|
|
|
|
|
|
|
37
|
15
|
50
|
|
|
|
42
|
carp("Invalid parameter: " . join(", ", @bad_params)) if @bad_params; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
die "No url or HTTP::Request specified!" unless ($params{url} || |
40
|
|
|
|
|
|
|
($params{request} && blessed $params{request} && |
41
|
15
|
0
|
0
|
|
|
40
|
$params{request}->isa('HTTP::Request'))); |
|
|
|
0
|
|
|
|
|
|
|
|
33
|
|
|
|
|
42
|
|
|
|
|
|
|
die "The 'request' and 'url' options are mutually exclusive!" |
43
|
15
|
50
|
33
|
|
|
71
|
if $params{url} && $params{request}; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# Process and serialize the status code checker |
46
|
15
|
|
100
|
|
|
60
|
$params{status_code} ||= '200'; |
47
|
15
|
|
|
|
|
28
|
my (@and, @or); |
48
|
15
|
|
|
|
|
112
|
foreach my $part (split qr{\s*,\s*}, $params{status_code}) { |
49
|
|
|
|
|
|
|
# Strict validation of each part, since we're throwing these into an eval |
50
|
17
|
|
|
|
|
102
|
my ($op, $code) = $part =~ m{\A\s*(>=|>|<=|<|!=|!)?\s*(\d{3})\z}; |
51
|
|
|
|
|
|
|
|
52
|
17
|
50
|
|
|
|
54
|
die "The 'status_code' condition '$part' is not in the correct format!" |
53
|
|
|
|
|
|
|
unless defined $code; |
54
|
17
|
100
|
100
|
|
|
61
|
$op = '!=' if defined $op && $op eq '!'; |
55
|
|
|
|
|
|
|
|
56
|
17
|
100
|
|
|
|
34
|
unless ($op) { push @or, '$_ == '.$code; } |
|
10
|
|
|
|
|
35
|
|
57
|
7
|
|
|
|
|
24
|
else { push @and, '$_ '."$op $code"; } |
58
|
|
|
|
|
|
|
} |
59
|
15
|
100
|
|
|
|
60
|
push @or, '('.join(' && ', @and).')' if @and; # merge @and as one big condition into @or |
60
|
15
|
|
|
|
|
48
|
$params{status_code_eval} = join ' || ', @or; |
61
|
|
|
|
|
|
|
|
62
|
15
|
|
33
|
|
|
87
|
$params{request} //= HTTP::Request->new('GET', $params{url}); |
63
|
15
|
|
100
|
|
|
9578
|
$params{options} //= {}; |
64
|
15
|
|
50
|
|
|
84
|
$params{options}{agent} //= LWP::UserAgent->_agent . |
|
|
|
66
|
|
|
|
|
65
|
|
|
|
|
|
|
" HealthCheck-Diagnostic-WebRequest/" . ( $class->VERSION || '0' ); |
66
|
15
|
|
100
|
|
|
269
|
$params{options}{timeout} //= 7; # Decided by committee |
67
|
|
|
|
|
|
|
|
68
|
15
|
|
|
|
|
76
|
return $class->SUPER::new( |
69
|
|
|
|
|
|
|
label => 'web_request', |
70
|
|
|
|
|
|
|
%params, |
71
|
|
|
|
|
|
|
); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub check { |
75
|
21
|
|
|
21
|
1
|
7534
|
my ($self, @args) = @_; |
76
|
|
|
|
|
|
|
|
77
|
21
|
100
|
|
|
|
305
|
croak("check cannot be called as a class method") |
78
|
|
|
|
|
|
|
unless ref $self; |
79
|
20
|
|
|
|
|
62
|
return $self->SUPER::check(@args); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub run { |
83
|
20
|
|
|
20
|
1
|
444
|
my ( $self, %params ) = @_; |
84
|
20
|
|
|
|
|
33
|
my $ua = LWP::UserAgent->new( %{$self->{options}} ); |
|
20
|
|
|
|
|
87
|
|
85
|
|
|
|
|
|
|
|
86
|
20
|
50
|
|
|
|
7269
|
$ua->requests_redirectable([]) if $self->{'no_follow_redirects'}; |
87
|
|
|
|
|
|
|
|
88
|
20
|
|
|
|
|
63
|
my $response = $ua->request( $self->{request} ); |
89
|
|
|
|
|
|
|
|
90
|
20
|
|
|
|
|
27589
|
my @results = $self->check_status( $response ); |
91
|
|
|
|
|
|
|
push @results, $self->check_content( $response ) |
92
|
20
|
100
|
|
|
|
117
|
if $results[0]->{status} eq 'OK'; |
93
|
|
|
|
|
|
|
|
94
|
20
|
|
|
|
|
45
|
my $info = join '; ', map { $_->{info} } @results; |
|
24
|
|
|
|
|
83
|
|
95
|
|
|
|
|
|
|
|
96
|
20
|
|
|
|
|
380
|
return { info => $info, results => \@results }; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub check_status { |
100
|
20
|
|
|
20
|
0
|
48
|
my ( $self, $response ) = @_; |
101
|
20
|
|
|
|
|
29
|
my $status; |
102
|
|
|
|
|
|
|
|
103
|
20
|
|
100
|
|
|
48
|
my $client_warning = $response->header('Client-Warning') // ''; |
104
|
20
|
|
100
|
|
|
1023
|
my $proxy_error = $response->header('X-Squid-Error') // ''; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# Eval the status checker |
107
|
20
|
|
|
|
|
977
|
my $success; |
108
|
|
|
|
|
|
|
{ |
109
|
20
|
|
|
|
|
32
|
local $_ = $response->code; |
|
20
|
|
|
|
|
51
|
|
110
|
20
|
|
|
|
|
1594
|
$success = eval $self->{status_code_eval}; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# An unfortunate post-constructor die, but this would be a validation bug (ie: our fault) |
114
|
20
|
50
|
|
|
|
85
|
die "Status code checker eval '".$self->{status_code_eval}."' failed: $@" if $@; |
115
|
|
|
|
|
|
|
|
116
|
20
|
100
|
|
|
|
57
|
$status = $success ? 'OK' : 'CRITICAL'; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# Proxy error is an automatic failure |
119
|
20
|
100
|
|
|
|
39
|
$status = 'CRITICAL' if $proxy_error; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
my $info = sprintf( "Requested %s and got%s status code %s", |
122
|
|
|
|
|
|
|
$self->{request}->uri, |
123
|
20
|
100
|
|
|
|
79
|
$status eq 'OK' ? ' expected' : '', |
124
|
|
|
|
|
|
|
$response->code, |
125
|
|
|
|
|
|
|
); |
126
|
20
|
100
|
|
|
|
561
|
$info .= " from proxy with error '$proxy_error'" if $proxy_error; |
127
|
20
|
100
|
100
|
|
|
96
|
$info .= ", expected ".$self->{status_code} unless $status eq 'OK' || $proxy_error; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
# If LWP returned 'Internal response', the status code doesn't actually mean anything |
130
|
20
|
100
|
100
|
|
|
61
|
if ($client_warning && $client_warning eq 'Internal response') { |
131
|
2
|
|
|
|
|
4
|
$status = 'CRITICAL'; |
132
|
2
|
|
|
|
|
5
|
$info = "User Agent returned: ".$response->message; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
20
|
|
|
|
|
110
|
return { status => $status, info => $info }; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub check_content { |
139
|
10
|
|
|
10
|
0
|
24
|
my ( $self, $response ) = @_; |
140
|
|
|
|
|
|
|
|
141
|
10
|
100
|
|
|
|
24
|
return unless $self->{content_regex}; |
142
|
|
|
|
|
|
|
|
143
|
4
|
|
|
|
|
8
|
my $regex = $self->{content_regex}; |
144
|
4
|
|
|
|
|
18
|
my $content = $response->content; |
145
|
4
|
100
|
|
|
|
78
|
my $status = $content =~ /$regex/ ? 'OK' : 'CRITICAL'; |
146
|
4
|
100
|
|
|
|
12
|
my $successful = $status eq 'OK' ? 'matches' : 'does not match'; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
return { |
149
|
4
|
|
|
|
|
19
|
status => $status, |
150
|
|
|
|
|
|
|
info => "Response content $successful /$regex/", |
151
|
|
|
|
|
|
|
}; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
1; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
__END__ |