#! /usr/bin/perl

###########################################################################
# This script starts a web100 monitoring program and executes a series
# of iperf transfers with varying times for a static socket buffer size
###########################################################################
#
# Author: Yee-Ting Li, UCL HEP
# 
# Change History:
#  12-04-02: initial release
#  21-06-2002: added logvars support
#  27-06-2002: halved socket buffers due to bug in iperf
#
###########################################################################

require 'cook.pl';

$DEBUG = 1;

if ( $#ARGV < 0 ) {
    &printHelp;
}


$deltaTime = 1000; # every second

# parse through the args
for ( $i=0; $i<$#ARGV+1; $i++ ) {

    if( $ARGV[$i] =~ /^-(.*)/ ) {

	$switch = $1;

	# connection host
	if ( $switch eq 'd' ) {
	    $destip = $ARGV[$i+1];
	}
	elsif ( $switch eq 'p' ) {
	    $dest_port = $ARGV[$i+1];
	}
	elsif ( $switch eq 'B' ) {
	    $bound_ip = $ARGV[$i+1];
	}
	elsif ( $switch eq 'w' ) {
	    @sbs = split( /,/, $ARGV[$i+1] );
	}
	elsif ( $switch eq 't' ) {
	    $time = $ARGV[$i+1];
	}
	elsif ( $switch eq 'l' ) {
	    $LOGVAR = `which logvars`; chomp($LOGVAR);
	    $logvar = 1;
	    $deltaTime = $ARGV[$i+1];
	}
	elsif ( $switch eq 'f' ) {
	    $outfile = $ARGV[$i+1];
	}
	elsif ( $switch eq 'h' ) {
	    &printHelp;
	}


    }


}


# go through an put default values

if ( $dest_port eq '' ) { $dest_port = 5001; }
if ( $#sbs eq -1  ) { 
    @sbs = ( '128k', '256k', '512k', '1024k', '2048k', '4096k' ); 
}

# divide socket buffer sizes by half for executation
foreach my $sb (@sbs) {
    $unit;
    if ( $sb =~ /(\D+)$/ ) {
	$unit = $1;
	$sb =~ s/\D+//g;
    }
    $sb /= 2;

    $sb = $sb . $unit;
}




if ( $DEBUG ) { print "\ndest: $destip, port: $dest_port, bound: $bound_ip, time: $time\nsocket buffers: @sbs\n" ; }


$sourceip = `hostname`; chomp($sourceip);
#$IPERF = `which iperf1.6.1`; chomp( $IPERF);
$IPERF = `which iperf1.6.1`; chomp( $IPERF);


### Setup to go

# Get the time and date
my ($sec, $minutes, $hour, $mday, $month, $year, $wday, $yday, $isdst) = gmtime();

# create a directory based on dd-mm-yy in working directory
$dir = sprintf "%2.2d-%2.2d-%4d", $mday, $month+1, $year+1900;
`mkdir $dir \&> /dev/null`;

$count = 1;
$total = $#sbs + 1;

@files;

print STDERR '=' x 72 . "\n";

foreach $sb (@sbs) {

    my $tempsb = $sb *2;
    $tempsb .= $unit;
    print STDERR "Conducting test socket buffer $tempsb ($count/$total)\n";

    # get time again
    ($sec, $minutes, $hour, $mday, $month, $year, $wday, $yday, $isdst) = gmtime();

    # create a filename to store output
    $filename = sprintf "$dir\/iperf_%4d%2.2d%2.2d-%2.2d%2.2d%2.2d_" . "$sourceip" . "_" . "$destip" . "_t" . "$time" . "_w" . $sb*2 ."$unit.log", $year+1900, $month+1, $mday, $hour, $minutes, $sec;


    # web100 support
    if ( $logvar eq 1 ) {
	my ( $web100file ) = split /\.log/, $filename;
	$web100file .= '.web100';
	$logvar_cmd = "$LOGVAR -c new -t $deltaTime -s \&> $web100file \&";
	system( $logvar_cmd );
    }

    # add filename to array
    push ( @files, $filename );

    if ( $bound_ip eq '' ) {
	$iperf_cmd = "$IPERF -c $destip -p $dest_port -w $sb -t $time -fb \&> $filename";}
    else { 
	$iperf_cmd = "$IPERF -c $destip -p $dest_port -w $sb -t $time -fb -B $bound_ip \&> $filename";}

    # execute iperf
    system( $iperf_cmd );

    $count++ ;
}

print STDERR '=' x 72 . "\n";
print STDERR "Tests complete!\n";
print STDERR "\nCooking...\n";
print STDERR '=' x 72 . "\n";



if ( $outfile eq '' ) {
    &iperfCook( @files );
}
else {
    
    open(SAVEOUT, ">&STDOUT");
    open (STDOUT,">$outfile") || warn "SSH_Open: Could not redirect STDOUT to $filename: $!\n";
    select(STDOUT); $| = 1;	
    &iperfCook( @files );   
    # redirect stdout to stdout
    open(STDOUT, ">&SAVEOUT");
    open ( OUT, "<$outfile" );
    while ( <OUT> ) {
	print $_;
    }

}
print STDERR '=' x 72 . "\n";

print STDERR "\nComplete!\n";


sub printHelp{
    print "Usage: do_time.pl -s <source> -d <sink> [-w <window>] [-p <port>] [-t <t1,t2,t3...> [-l <logvar_resolution>]\n";
exit;
}



