grabron.in 6.45 KB
Newer Older
Robert Ricci's avatar
Robert Ricci committed
1
#!/usr/bin/perl -w
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3 4 5 6 7

#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
# All rights reserved.
#
Robert Ricci's avatar
Robert Ricci committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#
# grabron - Grab data from Dave Andersen's DBSYNC program, and upload it into
# the testbed database
#

use strict;

use IO::Socket;
use Net::hostent;

use lib '@prefix@/lib';
use libdb;
use libtestbed;

my $TB = '@prefix@';

Robert Ricci's avatar
Robert Ricci committed
24 25 26 27 28 29 30 31
#
# This is the magic node which is equivalent to the TBDB_WIDEAREA_LOCALNODE .
# This could be moved to a configure variable, but I'm not going to do so
# until there's a need to go to that trouble. It's hacky, no matter where
# you put it.
#
my $MAGICNODE = "ron2";

Robert Ricci's avatar
Robert Ricci committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45
my $logfile = "$TB/log/grabron";

if (@ARGV != 2) {
    die "Usage: $0 <host> <port>\n";
}
my ($host, $port) = @ARGV;

$| = 1;

#
# Time in beteen polls
#
my $interval = 8 * 60 *60; # 8 hours

Robert Ricci's avatar
Robert Ricci committed
46 47 48 49 50 51 52 53 54
#
# Find out the interface to use for the TBDB_WIDEAREA_LOCALNODE, since I don't
# want to hardcode it anywhere.
# XXX: What if it has more than one interface?
#
my $result = DBQueryFatal("select iface from interfaces where node_id='" .
	TBDB_WIDEAREA_LOCALNODE . "'");
if ($result->num_rows() != 1){
    die "Unable to determine iface for " . TBDB_WIDEAREA_LOCALNODE . "\n";
Robert Ricci's avatar
Robert Ricci committed
55
}
Robert Ricci's avatar
Robert Ricci committed
56 57
my ($localIface) = ($result->fetchrow());

58 59 60
if (TBBackGround($logfile)) {
   exit(0);
}
Robert Ricci's avatar
Robert Ricci committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

#
# Start out with the the last $interval seconds
#
my $last_time = time - $interval;

while (1) {
    my @times = get_times($last_time);
    $last_time = time;
    if (@times) {
    	my %ipmap = get_ipmap();
    	my %bandwidths = get_bandwidths();
    	upload_times(\@times,\%ipmap,\%bandwidths);
    }

    sleep($interval);
}

sub get_ipmap() {
80
    my $result = DBQueryFatal("select node_id,iface, IP from interfaces");
Robert Ricci's avatar
Robert Ricci committed
81 82 83 84 85 86 87 88
    my %map = ();
    while (my ($node_id,$iface,$IP) = $result->fetchrow()) {
	$map{$IP} = [$node_id, $iface];
    }
    return %map;
}

sub get_bandwidths() {
89 90
    my $result = DBQueryFatal("select node_id1,iface1,node_id2,iface2, ".
	    "bandwidth from widearea_delays");
Robert Ricci's avatar
Robert Ricci committed
91
    my %map = ();
92 93
    while (my ($node1,$if1,$node2,$if2,$bandwidth) = $result->fetchrow()) {
	$map{"$node1:$if1 $node2:$if2"} = $bandwidth;
Robert Ricci's avatar
Robert Ricci committed
94 95 96 97 98 99 100 101 102 103 104 105
    }
    return %map;
}

sub upload_times($$$) {
    my ($rTimes,$rIPmap,$rBandwidths) = @_;
    my @times = @$rTimes;
    my %ipmap = %$rIPmap;
    my %bandwidths = %$rBandwidths;
    my $suceeded = 0;
    my $failed = 0;
    foreach my $time (@times) {
106
	my ($src,$dst,$latency,$stddev,$lossrate) = @$time;
Robert Ricci's avatar
Robert Ricci committed
107 108 109 110 111 112 113 114 115 116 117 118
	if (!$ipmap{$src}) {
	    warn "Got an IP ($src) that's not in the interfaces table\n";
	    $failed++;
	    next;
	}
	my ($node_id1, $iface1) = @{$ipmap{$src}};
	if (!$ipmap{$dst}) {
	    warn "Got an IP ($dst) that's not in the interfaces table\n";
	    $failed++;
	    next;
	}
	my ($node_id2, $iface2) = @{$ipmap{$dst}};
119 120 121
	if (!$bandwidths{"$node_id1:$iface1 $node_id2:$iface2"}) {
	    warn "Got a pair ($node_id1:$iface1 $node_id2:$iface2) that has ".
		    "no bandwidth\n";
Robert Ricci's avatar
Robert Ricci committed
122 123 124
	    $failed++;
	    next;
	}
125
	my $bandwidth = $bandwidths{"$node_id1:$iface1 $node_id2:$iface2"};
Robert Ricci's avatar
Robert Ricci committed
126 127 128 129 130 131 132 133 134
	if (!defined($latency)) {
	    #
	    # If no latency, that means we can't get there from here, so
	    # we provide -1s in bandwidth and latency, to specify that
	    #
	    $latency = -1;
	    $bandwidth = -1;
	}
	$suceeded++;
Robert Ricci's avatar
Robert Ricci committed
135 136 137
	DBQueryFatal("replace into widearea_recent set time=$latency, " .
		"node_id1='$node_id1', iface1='$iface1', ".
		"node_id2='$node_id2', iface2='$iface2', " .
138
		" bandwidth=$bandwidth, time_stddev='$stddev', " .
139 140
		" lossrate='$lossrate', start_time=$::last_time, " .
		" end_time=$::end_time");
Robert Ricci's avatar
Robert Ricci committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	#
	# If either (or both) endpoints are the magic node, we update
	# the database again, with the WIDEAREA_LOCALNODE instead.
	#
	if (($node_id1 eq $MAGICNODE) || ($node_id2 eq $MAGICNODE)) {
	    if ($node_id1 eq $MAGICNODE) {
		$node_id1 = TBDB_WIDEAREA_LOCALNODE;
		$iface1 = $localIface;
	    }
	    if ($node_id2 eq $MAGICNODE) {
		$node_id2 = TBDB_WIDEAREA_LOCALNODE;
		$iface2 = $localIface;
	    }
	    DBQueryFatal("replace into widearea_recent set time=$latency, ".
		" node_id1='$node_id1', iface1='$iface1', ".
		" node_id2='$node_id2', iface2='$iface2', " .
		" bandwidth=$bandwidth, time_stddev='$stddev', " .
		" lossrate='$lossrate', start_time=$::last_time, " .
		" end_time=$::end_time");
	}
Robert Ricci's avatar
Robert Ricci committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

    }

    print "Database upload done: $suceeded suceeded, $failed failed\n";
}

sub get_times($) {
    my ($last_time) = (@_);
    print "Connecting to $host:$port\n";

    my $remote = IO::Socket::INET->new ( Proto => "tcp",
    					 PeerAddr => $host,
					 PeerPort => $port );
    if (!$remote) {
	warn "Unable to connect to to $host:$port\n";
	return ();
    }

    $remote->autoflush(1); # Equivalent of $| = 1

    my ($version, $start, $end, $ent);
    $version = <$remote>;
    $start = <$remote>;
    $end = <$remote>;
    $ent = <$remote>;

    if (!$version || !$start || !$end || !$ent) {
    	warn "Unable to read data from network\n";
    	undef $remote;
    	return ();
    }

    $version =~ /DBSYNC (\d+\.\d+)/;
    if (!$1 || ($1 != "1.0")) {
	warn "Wrong version reported\n";
	undef $remote;
	return ();
    }

    #
    # We don't currently care about these
    #
    $start =~ /DBSTART (\d+)/;
    $ent =~ /DBENT (\d+)/;

    $end =~ /DBEND (\d+\.?\d+)/;
    if (!$1) {
	warn "No end time reported\n";
	undef $remote;
	return ();
    }

    my $end_time = sprintf("%i",$1);
    if ($last_time > $end_time) {
	warn "No new data available\n";
	undef $remote;
	return ();
    }

    print "Giving: $last_time $end_time\n";
221 222 223 224 225 226 227

    #
    # Set some gloabls, so that other functions see these times
    #
    $::last_time = $last_time;
    $::end_time = $end_time;

Robert Ricci's avatar
Robert Ricci committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    print $remote "$last_time $end_time\n";

    #
    # XXX: Timeout waiting for data
    #
    my @data = <$remote>;
    if (!@data) {
	warn "No data returned\n";
	undef $remote;
	return ();
    }

    if ($data[0] !~ /^\d/) {
    	warn "An error was reported: \n", @data, "\n";
    	undef $remote;
    	return ();
    }

    my @return;
    foreach my $line (@data) {
	chomp $line;
249
	if (! ($line =~ /(\d+\.\d+.\d+\.\d+)\s+(\d+\.\d+.\d+\.\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)\s+(\d+)\s+(\d+)/) ) {
Robert Ricci's avatar
Robert Ricci committed
250 251 252 253
	    warn "Bad data line: $line\n";
	    next;
	}

254 255
	my ($src,$dst,$avg,$stddev,$successful,$unsuccessful)
	    = ($1,$2,$3,$4,$5,$6);
Robert Ricci's avatar
Robert Ricci committed
256
	if ($successful > 0) {
257 258
	    my $lossrate = ($unsuccessful*1.0)/($successful + $unsuccessful);
	    push @return, [$src,$dst,$avg,$stddev,$lossrate];
Robert Ricci's avatar
Robert Ricci committed
259
	} elsif ($unsuccessful > 0) {
260
	    push @return, [$src,$dst,undef,$stddev,1];
Robert Ricci's avatar
Robert Ricci committed
261 262 263 264 265 266 267 268 269 270
	}
    }

    print "Got " . scalar(@return) . " lines of new data\n";

    undef $remote;

    return @return;

}