#!/usr/local/bin/perl

use strict;
use Getopt::Std;
use LWP::UserAgent;
use Time::HiRes qw( gettimeofday tv_interval );

my %opt;
getopts('m:r:h:p:', \%opt);
# m monitor URL
# r regex to match
# h proxy host
# p proxy port

$opt{m} || die "no monitor url\n";

my $ua = LWP::UserAgent->new;
$ua->timeout(10);
if($opt{h} && $opt{p}){
    $ua->proxy('http', "http://$opt{h}:$opt{p}/");
}

my $start = [gettimeofday];

my $response = $ua->get($opt{m});

my $delta = sprintf( '%.2f', tv_interval($start) );

if( $response->is_success ){
    if( $opt{r} ){
        unless( $response->decoded_content =~ /$opt{r}/ ){
            die "no match\n";
        }
    }
}else{
    die $response->status_line;
}


print "$delta\n";
