showsamples.pl 7 KB
Newer Older
1
#!/usr/bin/perl
Mike Hibler's avatar
Mike Hibler committed
2 3
#
# Copyright (c) 2006 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Mike Hibler's avatar
Mike Hibler committed
23 24
#

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#
# Show latency and/or BW samples between a pair of sites based on indices.
#
#
my $NLIST = "/usr/testbed/bin/node_list";
my $pprefix = "planet-";

# XXX Need to configure this stuff!
use lib '/usr/testbed/lib';
use libtbdb;
use Socket;
use Getopt::Std;
use Class::Struct;
use libxmlrpc;
use strict;

my $starttime = 0;
my $dolatency = 0;
my $dobw = 0;
my $doloss = 0;
my $bidir = 0;
my $number = 0;
47
my $usenames = 0;
48 49 50 51 52 53 54 55 56 57 58 59 60

my $PWDFILE = "/usr/testbed/etc/pelabdb.pwd";
my $DBNAME  = "pelab";
my $DBUSER  = "pelab";

my $pid;
my $eid;

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
61
if (! getopts("BNbdle:S:n:", \%options)) {
62 63 64 65 66 67 68 69
    usage();
}
if (defined($options{"e"})) {
    ($pid,$eid) = split('/', $options{"e"});
}
if (defined($options{"B"})) {
    $bidir = 1;
}
70 71 72
if (defined($options{"N"})) {
    $usenames = 1;
}
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
if (defined($options{"d"})) {
    $dolatency = 1;
}
if (defined($options{"b"})) {
    $dobw = 1;
}
if (defined($options{"l"})) {
    $doloss = 1;
}
if (defined($options{"n"})) {
    $number = $options{"n"};
}
if (defined($options{"S"})) {
    my $high = time();
    my $low = $high - (23 * 60 * 60); # XXX

    $starttime = $options{"S"};
    if ($starttime && ($starttime < $low || $starttime > $high)) {
	die("Bogus timestamp $starttime, should be in [$low - $high]\n");
    }
}
if (!defined($pid) && @ARGV != 2) {
95
    print STDERR "usage: showsamples.pl [-BNbdl] <srcix>|all <dstix>|all\n";
96 97
    print STDERR "   show database records for given site indices\n";
    print STDERR "       -B        show both srcix -> dstix and dstix -> srcix\n";
98
    print STDERR "       -N        two args are names rather than indicies\n";
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    print STDERR "       -b        show bandwidth\n";
    print STDERR "       -d        show delay (the default)\n";
    print STDERR "       -l        show loss\n";
    print STDERR "       -n <num>  show only the last <num> records\n";
    print STDERR "       -S <time> show records no later than unix timestamp <time>\n\n";
    print STDERR "usage: showsamples.pl -e pid/eid\n";
    print STDERR "   show mapping of name->ix for all plab nodes in <pid>/<eid>\n";
    exit(1);
}
my ($srcix,$dstix) = @ARGV;
if ($srcix eq "all") {
    undef $srcix;
}
if ($dstix eq "all") {
    undef $dstix;
}

# Get DB password and connect.
my $DBPWD   = `cat $PWDFILE`;
if ($DBPWD =~ /^([\w]*)\s([\w]*)$/) {
    $DBPWD = $1;
}
else{
    fatal("Bad chars in password!");
}
TBDBConnect($DBNAME, $DBUSER, $DBPWD) == 0
    or die("Could not connect to pelab database!\n");

#
# XXX figure out how many pairs there are, and for each, who the
# corresponding planetlab node is.  Can probably get all this easier
# via XMLRPC...
#
if (defined($pid)) {
    my @nodelist = split('\s+', `$NLIST -m -e $pid,$eid`);
    chomp(@nodelist);
    foreach my $mapping (@nodelist) {
	if ($mapping =~ /^(${pprefix}[\d]+)=([\w]*)$/) {
	    my $vnode = $1;
	    my $pnode = $2;

	    # Grab the site index.
	    my $query_result =
		DBQueryFatal("select site_idx from site_mapping ".
			     "where node_id='$pnode'");

	    if (! $query_result->numrows) {
		die("Could not map $pnode to its site index!\n");
	    }
	    my ($site_index) = $query_result->fetchrow_array();
	    
	    print "$vnode ($pnode) is site $site_index\n";
	}
    }
    exit(0);
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
} elsif ($usenames) {
    my $sname = $srcix;
    my $dname = $dstix;

    my $query_result =
        DBQueryFatal("select site_idx from site_mapping ".
		     "where node_id='$sname'");
    if ($query_result->numrows) {
	$srcix = $query_result->fetchrow_array();
	print "$sname is site $srcix\n";
    } else {
	die("could not find index for $sname\n");
    }

    $query_result =
        DBQueryFatal("select site_idx from site_mapping ".
		     "where node_id='$dname'");
    if ($query_result->numrows) {
	$dstix = $query_result->fetchrow_array();
	print "$dname is site $dstix\n";
    } else {
	die("could not find index for $dname\n");
    }
} 
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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

if (!$dolatency && !$dobw && !$doloss) {
    $dolatency = 1;
}
if ($starttime == 0) {
    $starttime = time();
}

get_plabinfo($srcix, $dstix);

exit(0);

#
# Grab data from DB.
#
sub get_plabinfo($$)
{
    my ($srcix,$dstix) = @_;

    my $fields;
    my $where2;
    if ($dolatency) {
	$fields = "latency";
	$where2 = "latency is not NULL";
    }
    if ($dobw) {
	if ($fields) {
	    $fields .= ",";
	    $where2 .= " or ";
	}
	$fields .= "bw";
	$where2 .= "bw is not NULL";
    }
    if ($doloss) {
	if (!$fields) {
	    $fields .= ",";
	    $where2 .= " or ";
	}
	$fields .= "loss";
	$where2 .= "loss is not NULL";
    }

    my $wherespec;
    if (defined($srcix)) {
	$wherespec = "(srcsite_idx='$srcix'";
	if (defined($dstix)) {
	    $wherespec .= " and dstsite_idx='$dstix'";
	    if ($bidir) {
		$wherespec .=
		    " or srcsite_idx='$dstix' and dstsite_idx='$srcix'";
	    }
	}
	$wherespec .= ")";
    } elsif (defined($dstix)) {
	$wherespec = "(dstsite_idx='$dstix')";
    }
    if (defined($wherespec)) {
	$wherespec .= " and ";
    }
    $wherespec .= "unixstamp <= $starttime ";

    print "$fields for ";
    if (defined($srcix)) {
	print "site $srcix ";
    } else {
	print "all sites ";
    }
    print "and ";
    if (defined($dstix)) {
	print "site $dstix";
    } else {
	print "all sites";
    }
    print ":\n";

    my $limit = "";
    if ($number > 0) {
	$limit = "limit $number";
    }

    my $query =
	"select srcsite_idx,dstsite_idx,$fields,unixstamp ".
	"from pair_data where $wherespec ".
	" and ($where2) ".
	"order by unixstamp desc,srcsite_idx,dstsite_idx $limit";
#    print "query is: $query\n";
    my $query_result = DBQueryFatal($query);
    if (!$query_result->numrows) {
	return;
    }

    printf "%-6s %-6s ", "src", "dst";
    if ($dolatency) {
	printf "%-6s ", "delay";
    }
    if ($dobw) {
	printf "%-6s ", "bw";
    }
    if ($doloss) {
	printf "%-4s ", "loss";
    }
    printf "%-25s\n", "date";

    my @vals;
    while (@vals = $query_result->fetchrow_array()) {
	printf "%-6s %-6s ", $vals[0], $vals[1];
	shift @vals;
	shift @vals;
	if ($dolatency) {
	    printf "%6.1f ", $vals[0];
	    shift @vals;
	}
	if ($dobw) {
	    printf "%6d ", $vals[0];
	    shift @vals;
	}
	if ($doloss) {
	    printf "%4.2f ", $vals[0];
	    shift @vals;
	}
	printf "%s\n", scalar(localtime($vals[0])); 
    }
}