| 1 | package Nagios::WebTransact::Timed; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use vars qw($VERSION @ISA) ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 5 | $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 5 | @ISA = qw(Nagios::WebTransact) ; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use HTTP::Request::Common qw(GET POST) ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use HTTP::Cookies ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use LWP::UserAgent ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use Time::HiRes qw(gettimeofday tv_interval) ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use Carp ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use Nagios::WebTransact ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use constant FALSE => 0 ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 5 | ▶ | use constant TRUE => ! FALSE ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 5 | ▶ | use constant FAIL => FALSE ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 5 | ▶ | use constant OK => TRUE ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | # ie Normal Perl semantics. 'Success' is TRUE (1). | ||||||||||||||||||||||||||||
| 24 | # Caller must map this to Unix/Nagios return codes. | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 5 | ▶ | use constant GET_TIME_THRESHOLD => 10 ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 5 | ▶ | use constant FAIL_RATIO_PERCENT => 50 ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 4 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=23 SCAR=32.4
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 4 | my ($self, $cgi_parm_vals_hr) = @_ ; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 4 | my %defaults = ( cookies => TRUE, | |||||||||||||||||||||||||||
| 33 | debug => TRUE, | ||||||||||||||||||||||||||||
| 34 | timeout => GET_TIME_THRESHOLD, | ||||||||||||||||||||||||||||
| 35 | agent => 'Mozilla/4.7', | ||||||||||||||||||||||||||||
| 36 | proxy => {}, | ||||||||||||||||||||||||||||
| 37 | fail_if_1 => FALSE, | ||||||||||||||||||||||||||||
| 38 | verbose => 1, | ||||||||||||||||||||||||||||
| 39 | download_images => FALSE, | ||||||||||||||||||||||||||||
| 40 | indent_level => 0, | ||||||||||||||||||||||||||||
| 41 | fail_ratio_percent => FAIL_RATIO_PERCENT | ||||||||||||||||||||||||||||
| 42 | ) ; | ||||||||||||||||||||||||||||
| 43 | # check semantics. | ||||||||||||||||||||||||||||
| 44 | # $fail_if_1 ? return FAIL if any URL fails | ||||||||||||||||||||||||||||
| 45 | # ! $fail_if_1 ? return FAIL if all URLs fail | ||||||||||||||||||||||||||||
| 46 | # (same as return OK if any URL ok) | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 4 | my %parms = (%defaults, @_) ; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 4 | my $debug = $parms{debug} ; | |||||||||||||||||||||||||||
| 52 | 4 | my $verbose = $parms{verbose} ; | |||||||||||||||||||||||||||
| 53 | 4 | my $indent = $parms{indent_level} ; | |||||||||||||||||||||||||||
| 54 | 4 | ▶ | my $fail_ratio_percent = $parms{fail_ratio_percent} || FAIL_RATIO_PERCENT ; | ||||||||||||||||||||||||||
|
Condition50%1/2
$parms{'fail_ratio_percent'} || 50
MC/DC0%0/1
$parms{'fail_ratio_percent'} || 50
$parms{'fail_ratio_percent'}
Truth table50%1/2
A
A= $parms{'fail_ratio_percent'}
| |||||||||||||||||||||||||||||
| 55 | 4 | ▶ | croak("Expecting fail_ratio_percent as a percentage (0-100%), got \$fail_ratio:_percent: $fail_ratio_percent\n") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$fail_ratio_percent < 0 or $fail_ratio_percent > 100
MC/DC0%0/2
$fail_ratio_percent < 0 or $fail_ratio_percent > 100
$fail_ratio_percent < 0
$fail_ratio_percent > 100
Truth table33%1/3
A or B
A= $fail_ratio_percent < 0B= $fail_ratio_percent > 100
| |||||||||||||||||||||||||||||
| 56 | if $fail_ratio_percent < 0 or $fail_ratio_percent > 100 ; | ||||||||||||||||||||||||||||
| 57 | 4 | my $fail_ratio = $fail_ratio_percent / 100 ; | |||||||||||||||||||||||||||
| 58 | 4 | my $timeout = $parms{timeout} ; | |||||||||||||||||||||||||||
| 59 | 4 | ▶ | croak("Expecting timeout as a natural number (0 ... not_too_big), got \$timeout: $timeout.\n") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | if $timeout < 0 ; | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 4 | my ($ua, %downloaded) ; | |||||||||||||||||||||||||||
| 63 | 4 | keys %downloaded = 128 ; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 4 | $ua = new LWP::UserAgent ; | |||||||||||||||||||||||||||
| 66 | 4 | $ua->agent($parms{agent}) ; | |||||||||||||||||||||||||||
| 67 | 4 | $ua->timeout($timeout) ; | |||||||||||||||||||||||||||
| 68 | $ua->cookie_jar(HTTP::Cookies->new) | ||||||||||||||||||||||||||||
| 69 | 4 | ▶ | if $parms{cookies} ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | $ua->proxy(['http', 'ftp'] => $parms{proxy}{server}) | ||||||||||||||||||||||||||||
| 71 | 4 | ▶ | if exists $parms{proxy}{server} ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 4 | my @urls = @{ $self->{urls} } ; | |||||||||||||||||||||||||||
| 74 | 4 | my $Fault_Threshold = int( scalar @urls * $fail_ratio + 0.5 ) * $timeout ; | |||||||||||||||||||||||||||
| 75 | 4 | my $check_time = 0 ; | |||||||||||||||||||||||||||
| 76 | 4 | my @get_times = () ; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 4 | foreach my $url_r ( @urls ) { | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 17 | my $req = $self->make_req( $url_r->{Method}, $url_r->{Url}, $url_r->{Qs_var}, $url_r->{Qs_fixed}, $cgi_parm_vals_hr ) ; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | $req->proxy_authorization_basic( $parms{proxy}{account}, $parms{proxy}{pass} ) | ||||||||||||||||||||||||||||
| 83 | 17 | ▶ | if exists $parms{proxy}{account} ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 17 | ▶ | print STDERR ' ' x $indent, '... ' ,$req->as_string, "\n" if $debug ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 17 | my $t0 = [gettimeofday] ; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 17 | my $res = $ua->request($req) ; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 17 | my $elapsed = tv_interval ($t0) ; | |||||||||||||||||||||||||||
| 92 | 17 | ▶ | my $rounded_elapsed = ( ($elapsed < GET_TIME_THRESHOLD and $res->is_success) ? sprintf('%3.2f', $elapsed ) : GET_TIME_THRESHOLD ) ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$elapsed < 10 and $res->is_success
MC/DC100%2/2
$elapsed < 10 and $res->is_success
$elapsed < 10
$res->is_success
Truth table100%3/3
A and B
A= $elapsed < 10B= $res->is_success
| |||||||||||||||||||||||||||||
| 93 | 17 | push @get_times, $rounded_elapsed ; | |||||||||||||||||||||||||||
| 94 | 17 | $check_time += $rounded_elapsed ; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 17 | ▶ | print STDERR ' ' x $indent, '... ' , $res->as_string ,"\n" if $debug ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 17 | ▶ | if ( $verbose ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 1 | ▶ | my $url_report = sprintf("%-95s%10s%-5.2f%-40s\n", substr(' ' x $indent . (! $indent ? '--getting ' : '') . $url_r->{Url}, 0, 95), | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 100 | , ' ' x 10, | ||||||||||||||||||||||||||||
| 101 | , $rounded_elapsed, | ||||||||||||||||||||||||||||
| 102 | , (! $indent ? 'Total check time: ' | ||||||||||||||||||||||||||||
| 103 | : ' image download time: ') . sprintf('%5.2f', $check_time)) ; | ||||||||||||||||||||||||||||
| 104 | 1 | print STDERR $url_report ; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 17 | ▶ | unless ( $check_time <= $Fault_Threshold ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 2 | my $i = 0 ; | |||||||||||||||||||||||||||
| 109 | 2 | foreach (@urls) { | |||||||||||||||||||||||||||
| 110 | 3 | ▶ | $get_times[$i] = GET_TIME_THRESHOLD if not defined $get_times[$i] ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 3 | $i++ ; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 2 | return (FAIL, 'Transaction failed. Timeout', \@get_times) ; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 15 | ▶ | if ( $parms{download_images} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 1 | my ($image_dl_ok, $image_dl_msg, $image_get_times_ar, $number_imgs_dl ) = &download_images($res, \%parms, \%downloaded) ; | |||||||||||||||||||||||||||
| 118 | 1 | ▶ | return (FAIL, $image_dl_msg) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | unless $image_dl_ok ; | ||||||||||||||||||||||||||||
| 120 | 1 | $self->{number_of_images_downloaded} += $number_imgs_dl ; | |||||||||||||||||||||||||||
| 121 | $get_times[-1] += $_ | ||||||||||||||||||||||||||||
| 122 | 1 | foreach @$image_get_times_ar ; | |||||||||||||||||||||||||||
| 123 | # &download_images() will call check() which returns here the list of image download times in @$image_get_times_ar. | ||||||||||||||||||||||||||||
| 124 | # Each elt in this list is added to the last html download time ($get_times[-1]) leaving @get_times containing | ||||||||||||||||||||||||||||
| 125 | # the total download time for the page (downloaded sequentially and without heed to 'if modified' headers). | ||||||||||||||||||||||||||||
| 126 | 1 | ▶ | printf "%137s%5.2f\n", 'Total page download time: ', $get_times[-1] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | if $verbose ; | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 2 | return (OK, 'Transaction completed Ok.', \@get_times) ; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 1 | ▶ | sub download_images { | ||||||||||||||||||||||||||
|
Subroutine
download_images: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 1 | my ($res, $parms_hr, $downloaded_hr) = @_ ; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 1 | require HTML::LinkExtor ; | |||||||||||||||||||||||||||
| 139 | 1 | require URI::URL ; | |||||||||||||||||||||||||||
| 140 | 1 | URI::URL->import(qw(url)) ; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 1 | my @imgs = () ; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 5 | ▶ | my $cb = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 145 | 5 | my($tag, %attr) = @_; | |||||||||||||||||||||||||||
| 146 | 5 | ▶ | return if $tag ne 'img'; # we only look closer at <img ...> | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | push(@imgs, $attr{src}); | |||||||||||||||||||||||||||
| 148 | 1 | } ; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 1 | my $p = HTML::LinkExtor->new($cb) ; | |||||||||||||||||||||||||||
| 151 | 1 | $p->parse($res->as_string) ; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 1 | my $base = $res->base; | |||||||||||||||||||||||||||
| 154 | 0 | my @imgs_abs = grep ! $downloaded_hr->{$_}++, map { my $x = url($_, $base)->abs; } @imgs; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 1 | my @img_urls = map { Method => 'GET', Url => $_->as_string, Qs_var => [], Qs_fixed => [], Exp => '.', Exp_Fault => 'NeverInAnImage' }, @imgs_abs ; | |||||||||||||||||||||||||||
| 157 | # url() returns an array ref containing the abs url and the base. | ||||||||||||||||||||||||||||
| 158 | 1 | ▶ | if ( my $number_of_images_not_already_downloaded =scalar @img_urls ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | # If there are no images that have not been downloaded, then don't try to call ->check([]) since it will return FAIL. | ||||||||||||||||||||||||||||
| 160 | 0 | my $img_trx = __PACKAGE__->new(\@img_urls) ; | |||||||||||||||||||||||||||
| 161 | 0 | my %image_dl_parms = (%$parms_hr, fail_if_1 => FALSE, download_images => FALSE, indent_level => 1) ; | |||||||||||||||||||||||||||
| 162 | 0 | return ( $img_trx->check({}, %image_dl_parms), $number_of_images_not_already_downloaded ) ; | |||||||||||||||||||||||||||
| 163 | } else { | ||||||||||||||||||||||||||||
| 164 | 1 | return (OK, 'Downloaded all __zero__ images found in ' . $res->base, [], 0) ; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 5 | 1 ; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | __END__ | ||||||||||||||||||||||||||||