#! /usr/bin/perl use strict; use warnings; use English qw( -no_match_vars ); use WWW::Mechanize; use constant TRUE => 1; { usage() if @ARGV != 2; my $url = shift; my $data_file = shift; my ($fj, $start, $end, $spent, $gap, $date, $resp); my $fh; open( $fh, '>', $data_file ) or die "Unable to open '$data_file' : $OS_ERROR"; my $mech = WWW::Mechanize->new(); while ( TRUE ) { my($hour, $min) = (localtime())[2,1]; $date = sprintf("%02d:%02d", $hour, $min); $start = time(); $resp = undef; if( eval { $resp = $mech->get($url); 1 } ) { $end = time(); $spent = $end - $start; } else { $spent = undef; } print $fh "$date\t" . (defined($spent) ? $spent : 'undef') . "\n"; print "$date\t" . (defined($spent) ? $spent : 'undef') . "\n"; if( $spent ) { $gap = ( $spent >= 30 ? 0 : 30 - $spent ); } else { $gap = 30; } sleep $gap; } } sub usage { print <<"EOF"; Usage: $PROGRAM_NAME Fetch and write the time spent (in seconds) into . URL is fetched every 30 seconds. The time spent fetching the URL is subtracted from the sleeped time and is set to zero if it exceeds 30 seconds. Timing infor is printed into the outfile and STDOUT in the following format: hour:min\\t For example 13:53 0 13:54 1 EOF exit 1; }