line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Apache::HealthCheck; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
21541
|
use 5.006001; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
49
|
|
4
|
1
|
|
|
1
|
|
449
|
use Apache::Constants qw(:common); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use LWP::UserAgent; |
6
|
|
|
|
|
|
|
use HTTP::Request; |
7
|
|
|
|
|
|
|
use strict; |
8
|
|
|
|
|
|
|
use warnings; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @ISA = qw(); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub handler { |
15
|
|
|
|
|
|
|
my ($r) = @_; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my @check_urls = $r->dir_config->get('CheckURL'); |
18
|
|
|
|
|
|
|
my $hn_success = $r->dir_config('HeaderNameSuccess'); |
19
|
|
|
|
|
|
|
my $hn_fail = $r->dir_config('HeaderNameFail'); |
20
|
|
|
|
|
|
|
my $hv_success = $r->dir_config('HeaderValueSuccess'); |
21
|
|
|
|
|
|
|
my $hv_fail = $r->dir_config('HeaderValueFail'); |
22
|
|
|
|
|
|
|
my $rc_success = $r->dir_config('ReturnCodeSuccess'); |
23
|
|
|
|
|
|
|
my $rc_fail = $r->dir_config('ReturnCodeFail'); |
24
|
|
|
|
|
|
|
my $html_success = $r->dir_config('HTMLSuccess'); |
25
|
|
|
|
|
|
|
my $html_fail = $r->dir_config('HTMLFail'); |
26
|
|
|
|
|
|
|
my $track_in_hdr = lc($r->dir_config('TrackResultsInHeader')) eq "on" ? 1 : 0; |
27
|
|
|
|
|
|
|
my $timeout = $r->dir_config('CheckTimeout') ? $r->dir_config('CheckTimeout') : 10; |
28
|
|
|
|
|
|
|
my $check_ua = $r->dir_config('CheckUserAgent') ? $r->dir_config('CheckUserAgent') : "Mozilla/5.0 (compatible; Apache::HealthCheck $VERSION;)"; |
29
|
|
|
|
|
|
|
my $check_method = $r->dir_config('CheckMethod') ? $r->dir_config('CheckMethod') : "HEAD"; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new( |
32
|
|
|
|
|
|
|
timeout => $timeout, |
33
|
|
|
|
|
|
|
agent => $check_ua, |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
my $tests = scalar(@check_urls); |
37
|
|
|
|
|
|
|
my $passed; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $i = 0; |
40
|
|
|
|
|
|
|
foreach my $url (@check_urls) { |
41
|
|
|
|
|
|
|
my ($result) = check_url($url, $ua, $check_method); |
42
|
|
|
|
|
|
|
++$i; |
43
|
|
|
|
|
|
|
$passed += $result; |
44
|
|
|
|
|
|
|
if ($track_in_hdr) { |
45
|
|
|
|
|
|
|
if ($result) { |
46
|
|
|
|
|
|
|
$r->header_out("X-HealthCheck-$i-Success" => $url); |
47
|
|
|
|
|
|
|
} else { |
48
|
|
|
|
|
|
|
$r->header_out("X-HealthCheck-$i-Fail" => $url); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
$r->content_type('text/html'); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
if ($track_in_hdr) { |
56
|
|
|
|
|
|
|
$r->header_out("X-HealthCheck-Results" => "$passed of $tests tests passed!"); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
if ($passed == $tests) { |
60
|
|
|
|
|
|
|
# all passed! |
61
|
|
|
|
|
|
|
$rc_success = $rc_success ? $rc_success : OK; |
62
|
|
|
|
|
|
|
$html_success = $html_success ? $html_success : "SUCCESS"; |
63
|
|
|
|
|
|
|
send_result($r, $hn_success, $hv_success, $rc_success, $html_success); |
64
|
|
|
|
|
|
|
} else { |
65
|
|
|
|
|
|
|
# not all passed! |
66
|
|
|
|
|
|
|
$rc_fail = $rc_fail ? $rc_fail : SERVER_ERROR; |
67
|
|
|
|
|
|
|
$html_fail = $html_fail ? $html_fail : "FAIL"; |
68
|
|
|
|
|
|
|
send_result($r, $hn_fail, $hv_fail, $rc_fail, $html_fail); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
return OK; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub send_result { |
74
|
|
|
|
|
|
|
my ($r, $hn, $hv, $rc, $html) = @_; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
if ($hn && $hv) { |
77
|
|
|
|
|
|
|
$r->header_out($hn => $hv); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
$r->status($rc); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# print the header |
82
|
|
|
|
|
|
|
$r->send_http_header; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
if (-e $html) { |
85
|
|
|
|
|
|
|
open(HTML, '<', $html); |
86
|
|
|
|
|
|
|
{ |
87
|
|
|
|
|
|
|
local $/; |
88
|
|
|
|
|
|
|
print ; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
close(HTML); |
91
|
|
|
|
|
|
|
} else { |
92
|
|
|
|
|
|
|
print "$html"; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub check_url { |
97
|
|
|
|
|
|
|
my ($in_url, $ua, $method) = @_; |
98
|
|
|
|
|
|
|
my ($url, $valid_rc_string) = split(/\s+/, $in_url); |
99
|
|
|
|
|
|
|
my (@valid_rc) = split(/\s*,\s*/, $valid_rc_string); |
100
|
|
|
|
|
|
|
my $req = HTTP::Request->new(uc($method), $url); |
101
|
|
|
|
|
|
|
my $resp = $ua->simple_request($req); |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
if ($resp) { |
104
|
|
|
|
|
|
|
# check if it's a valid return code.. |
105
|
|
|
|
|
|
|
return is_valid_rc($resp->code, @valid_rc); |
106
|
|
|
|
|
|
|
} else { |
107
|
|
|
|
|
|
|
# if nothing came back, return undef! |
108
|
|
|
|
|
|
|
return undef; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub is_valid_rc { |
113
|
|
|
|
|
|
|
my ($rc, @vrcs) = @_; |
114
|
|
|
|
|
|
|
foreach my $vrc (@vrcs) { |
115
|
|
|
|
|
|
|
if ($rc == $vrc) { |
116
|
|
|
|
|
|
|
return 1; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
return undef; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# Preloaded methods go here. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
1; |
125
|
|
|
|
|
|
|
__END__ |