grabron.in 6.34 KB
Newer Older
Robert Ricci's avatar
Robert Ricci committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/perl -w
#
# 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
18
19
20
21
22
23
24
25
#
# 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
40
41
42
43
44
45
46
47
48
#
# 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
49
}
Robert Ricci's avatar
Robert Ricci committed
50
51
52
53
54
my ($localIface) = ($result->fetchrow());

#if (TBBackGround($logfile)) {
#   exit(0);
#
Robert Ricci's avatar
Robert Ricci committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

#
# 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() {
74
    my $result = DBQueryFatal("select node_id,iface, IP from interfaces");
Robert Ricci's avatar
Robert Ricci committed
75
76
77
78
79
80
81
82
    my %map = ();
    while (my ($node_id,$iface,$IP) = $result->fetchrow()) {
	$map{$IP} = [$node_id, $iface];
    }
    return %map;
}

sub get_bandwidths() {
83
84
    my $result = DBQueryFatal("select node_id1,iface1,node_id2,iface2, ".
	    "bandwidth from widearea_delays");
Robert Ricci's avatar
Robert Ricci committed
85
    my %map = ();
86
87
    while (my ($node1,$if1,$node2,$if2,$bandwidth) = $result->fetchrow()) {
	$map{"$node1:$if1 $node2:$if2"} = $bandwidth;
Robert Ricci's avatar
Robert Ricci committed
88
89
90
91
92
93
94
95
96
97
98
99
    }
    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) {
100
	my ($src,$dst,$latency,$stddev,$lossrate) = @$time;
Robert Ricci's avatar
Robert Ricci committed
101
102
103
104
105
106
107
108
109
110
111
112
	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}};
113
114
115
	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
116
117
118
	    $failed++;
	    next;
	}
119
	my $bandwidth = $bandwidths{"$node_id1:$iface1 $node_id2:$iface2"};
Robert Ricci's avatar
Robert Ricci committed
120
121
122
123
124
125
126
127
128
	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
129
130
131
	DBQueryFatal("replace into widearea_recent set time=$latency, " .
		"node_id1='$node_id1', iface1='$iface1', ".
		"node_id2='$node_id2', iface2='$iface2', " .
132
		" bandwidth=$bandwidth, time_stddev='$stddev', " .
133
134
		" lossrate='$lossrate', start_time=$::last_time, " .
		" end_time=$::end_time");
Robert Ricci's avatar
Robert Ricci committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
	#
	# 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
155
156
157
158
159
160
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

    }

    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";
215
216
217
218
219
220
221

    #
    # 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
    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;
243
	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
244
245
246
247
	    warn "Bad data line: $line\n";
	    next;
	}

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

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

    undef $remote;

    return @return;

}