#!/usr/bin/perl5
# 
# No buffering on stdout.
require "ctime.pl";

$packsize = 10000;
$tests = 3;

$| = 1;

while (1) {

    ($b_tran, $b_rec, $b_min, $b_avg, $b_max) 
        = &onepingset("eecs.berkeley.edu", $tests, $packsize);

    ($s_mtrans, $s_rec, $s_min, $s_avg, $s_max) 
        = &onepingset("207.45.223.33", $tests, $packsize);

    ($m_mtrans, $m_rec, $m_min, $m_avg, $m_max) 
        = &onepingset("mail.candw.ag", $tests, $packsize);

    ($z_trans, $z_rec, $z_min, $z_avg, $z_max) 
       = &onepingset("mail.anguillanet.com", $tests, $packsize);

    ($l_trans, $l_rec, $l_min, $l_avg, $l_max) 
       = &onepingset("weather.ai", $tests, $packsize);

    printf("%3s=%4.0fkb/s  %3s=%4.0fkb/s  %3s=%6.0fkb/s  %3s=%6.0fkb/s %3s=%4.0fkb/s\n",
             "UCB", 2*$packsize*8/$b_min, 
             "SPR", 2*$packsize*8/$s_min, 
             "AG",  2*$packsize*8/$m_min,
             "AI",  2*$packsize*8/$z_min,
             "OFF", 2*$packsize*8/$l_min);

    sleep 60;
}

exit 0;


# Old format
# 10 packets transmitted, 10 packets received, 0% packet loss
# round-trip min/avg/max = 416.3/496.6/630.3 ms

# New

sub onepingset {
    my $host = shift(@_);
    my $count = shift(@_);
    my $size = shift(@_);

#    printf(" host %s,   count %f,  size %f \n". $host, $count, $size);

    my $transmitted=0;
    my $received=0;
    my $min=0;
    my $avg=0;
    my $max=0;

    open(PING, "/bin/ping -c $count -s $size $host |") || die "cand run ping\n";
    $head = <PING>;
    while(<PING>) {
#        printf("%s\n", $_);
#       10 packets transmitted, 10 packets received, 0% packet loss
        if ($_ =~ /packets/) {
            ($transmitted, $p, $t, $received, $p2, $r, $perc, $p3, $l) 
               = split (' ');
#            printf("10 %s packets %s transmitted %s 9 %s packets %s %s\n", 
#                   $transmitted,     %p,         %t, $received, $p2, $r);
        }
#          round-trip min/avg/max = 159.3/249.5/367.3 ms
        if ($_ =~ /trip/) {
            ($r, $man, $e, $stats, $ms) 
               = split (' ');
#            printf("man %s   eq %s  stats %s  ms %s \n", $man, $e, $stats, $ms);

            ($min, $avg, $max) = split (/\//, $stats);
#            printf("min %s  avg  %s  max %s\n", $transmitted, $received);
        }

    }
    close(PING);
    if ($min == 0) {
       $min = 100000;
    }
    return($transmitted, $received, $min, $avg, $max);
};


#Sun Dec 10  7:47:32 1995
# extra space makes extra string
 
sub getdate {
    $datestring = &ctime(time);

    ($weedday, $month, $day, $mytime, $year) 
               = split (' ', $datestring);

    return("$month $day $mytime ");
}



