template_linkgraph.in 8.64 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2006, 2007 University of Utah and the Flux Group.
5 6 7 8 9
# All rights reserved.
#
use English;
use strict;
use Getopt::Std;
10
use Date::Parse;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
use Errno qw(EDQUOT);
use Data::Dumper;

#
# Simple graphing functions for link data.
#
# Exit codes are important; they tell the web page what has happened so
# it can say something useful to the user. Fatal errors are mostly done
# with die(), but expected errors use this routine. At some point we will
# use the DB to communicate the actual error.
#
# $status < 0 - Fatal error. Something went wrong we did not expect.
# $status = 0 - Everything okay.
# $status > 0 - Expected error. User not allowed for some reason. 
# 
sub usage()
{
    print(STDERR
	  "Usage: template_linkgraph [-q] [-s vnode] [-t vnode] ".
	  "-i <exptidx> -r <runid> <guid/vers> <graphtype>\n".
	  "switches and arguments:\n".
	  "-q           - be less chatty\n".
	  "-r <rund>    - Experiment run to work on\n".
	  "-i <exptidx> - Experiment index to work on\n".
	  "<guid/vers>  - GUID and version\n");
    exit(-1);
}
my $optlist	 = "qi:dr:s:t:";
my %options      = ();
my $quiet        = 0;
my $debug        = 0;
my $eid;
my $exptidx;
my $runidx;
my $guid;
my $version;
my $graphtype;
48 49
my $srclan;
my $dstlan;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
my $srcvnode;
my $dstvnode;

#
# Configure variables
#
my $TB		= "@prefix@";
my $EVENTSYS	= @EVENTSYS@;
my $TBOPS	= "@TBOPSEMAIL@";
my $TBLOGS	= "@TBLOGSEMAIL@";
my $TBDOCBASE	= "@TBDOCBASE@";
my $TBBASE	= "@TBBASE@";
my $CONTROL     = "@USERNODE@";
my $checkquota  = "$TB/sbin/checkquota";
my $dbcontrol   = "$TB/sbin/opsdb_control";
65
my $tempanalyze = "$TB/bin/template_analyze";
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

# Locals
my $template;
my $instance;
my $archive_tag;

# Protos
sub ParseArgs();
sub fatal($$);
sub cleanup();

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use libtblog;
use Template;
use Experiment;
86
use User;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

# In libdb
my $projroot = PROJROOT();

#
# Turn off line buffering on output
#
$| = 1;

#
# Set umask for start/swap. We want other members in the project to be
# able to swap/end experiments, so the log and intermediate files need
# to be 664 since some are opened for append.
#
umask(0002);

#
# Untaint the path
#
# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
111
# Verify user and get his DB uid and other info for later.
112
#
113 114 115
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    tbdie("You ($UID) do not exist!");
116
}
117
my $user_uid   = $this_user->uid();
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

# Now parse arguments.
ParseArgs();

# Temporary
libArchive::setdebug($debug);

#
# Grab template info and do access check.
#
$template = Template->Lookup($guid, $version);

if (!defined($template)) {
    tbdie("Experiment template $guid/$version does not exist!");
}
133 134
if (! $template->AccessCheck($this_user, TB_EXPT_READINFO)) {
    tberror("You do not have permission to access template $guid/$version");
135 136 137 138 139 140 141 142 143 144 145 146 147 148
    exit(1);
}
my $pid = $template->pid();
my $gid = $template->gid();

$instance = Template::Instance->LookupByExptidx($exptidx);
if (!defined($instance)) {
    fatal(-1, "Could not get instance record for experiment $exptidx!");
}

if ($instance->ArchiveTag(\$archive_tag) < 0) {
    fatal(-1, "Could not get current archive tag for instance $exptidx!");
}

149
if (system("$checkquota $user_uid") != 0) {
150 151
    tberror("You are over your disk quota on $CONTROL; ".
	    "please login there and cleanup!");
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    exit(1);
}

# Get the the runs.
my %runlist;
$instance->RunList(\%runlist) == 0
    or fatal(-1, "Could not get runlist for $instance");

#
# It is an error to analyze an instance with no runs; it sorta implies
# the the initial run is still active.
#
if (! keys(%runlist)) {
    tberror("There are no experiment runs in instance $instance!");
    exit(1);
}

169 170 171
my $dbname;
my $optargs = "";

172 173 174 175 176 177 178 179 180
if ($instance->Instantiated()) {
    my $experiment = Experiment->Lookup($instance->pid(), $instance->eid());
    
    fatal(-1, "Could not get experiment object from $instance!")
	if (!defined($experiment));

    fatal(-1, "No experiment database in use!")
	if (! $experiment->dpdb() || !$experiment->dpdbname());

181 182 183 184 185 186 187 188 189 190 191
    $dbname = $experiment->dpdbname();
    $eid    = $instance->eid();
}
else {
    #
    # We need to use a database from the archive. We do not want to check
    # this out multiple times, so look to see if a temp DB already exists.
    #
    # Note that we just want the last run since that will include the
    # accumulated data.
    #
192
    my $lastrun = $instance->LastRun();
193
    fatal(-1, "Could not get last run for instance $instance!")
194
	if (! defined($lastrun));
195

196
    my $idx = $lastrun->idx();
197 198 199 200 201 202 203 204 205
    $dbname = "${guid},${version}," . $instance->idx() . ",$idx";
    
    my $query_result =
	DBQueryFatal("select * from datapository_databases ".
		     "where dbname='$dbname'");
    if (! $query_result->numrows) {
	system("$tempanalyze -i $exptidx -r $idx $guid/$version") == 0 or
	    fatal(-1, "Could not check out database for run $idx!");
    }
206 207

    #
208 209 210 211
    # Since there is no current instance experiment, use the eid from the
    # template for the lookups below. This is not quite correct though, since
    # an instance might actually have a different topology or number of nodes, 
    # or be different in some other manner. Needs more thought.
212
    #
213 214
    $eid    = $template->eid();
}
215

216 217 218 219 220 221 222 223 224 225 226 227 228 229
#
# For a specific run, figure out the start and end times of the run
# and pass those along as options to the graphing code.
#
if (defined($runidx)) {
    my $rowref = $runlist{$runidx};
    my $start  = $rowref->{"start_time"};
    my $stop   = $rowref->{"stop_time"};

    if (defined($stop)) {
	$optargs = "-r " . str2time($start) . ":" . str2time($stop);
    }
    else {
	$optargs = "-r " . str2time($start) . ":0";
230
    }
231
}
232

233 234 235 236 237 238 239 240 241 242
# Specific source link/node
if (defined($srcvnode)) {
    my $query_result =
	DBQueryFatal("select ip from virt_lans ".
		     "where vname='$srclan' and vnode='$srcvnode' and ".
		     "      pid='$pid' and eid='$eid'");
    if ($query_result->numrows) {
	my ($ip) = $query_result->fetchrow_array();
	
	$optargs .= " -s $ip";
243
    }
244
}
245
    
246 247 248 249 250 251 252 253 254 255
# Specific destination link/node
if (defined($dstvnode)) {
    my $query_result =
	DBQueryFatal("select ip from virt_lans ".
		     "where vname='$dstlan' and vnode='$dstvnode' and ".
		     "      pid='$pid' and eid='$eid'");
    if ($query_result->numrows) {
	my ($ip) = $query_result->fetchrow_array();
	
	$optargs .= " -t $ip";
256
    }
257
}
Leigh Stoller's avatar
Leigh Stoller committed
258
#SENDMAIL($TBOPS, "foo", "$dbname $graphtype $optargs");
259 260 261 262
    
system("$dbcontrol graphdb $dbname $graphtype $optargs") == 0 or
    fatal(-1, "Failed to generate requested graph!");

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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
exit(0);

#
# Parse command arguments. Once we return from getopts, all that are
# left are the required arguments.
#
sub ParseArgs()
{
    if (! getopts($optlist, \%options)) {
	usage();
    }

    if (@ARGV != 2) {
	usage();
    }
    
    #
    # Pick up guid/version first and untaint.
    #
    my $tmp = shift(@ARGV);

    if ($tmp =~ /^([\w]*)\/([\d]*)$/) {
	$guid = $1;
	$version = $2;
    }
    else {
	tbdie("Bad data in argument: $tmp");
    }

    #
    # Pick up graph type.
    #
    $tmp = shift(@ARGV);

    if ($tmp =~ /^([\w]*)$/) {
	$graphtype = $1;
    }
    else {
	tbdie("Bad data in argument: $tmp");
    }

    if (defined($options{"i"})) {
	$exptidx = $options{"i"};
    
	if (! TBcheck_dbslot($exptidx, "default", "int",
			     TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	    tbdie("Improper experiment index!");
	}
	# real check.
	if ($exptidx =~ /^([\w]*)$/) {
	    $exptidx = $1;
	}
	else {
	    tbdie("Bad data in argument: $exptidx");
	}
    }
    else {
	tbdie("You must supply the -i option!");
    }

    if (defined($options{"r"})) {
	$runidx = $options{"r"};
    
	if (! TBcheck_dbslot($runidx, "default", "int",
			     TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	    tbdie("Improper run index!");
	}
330 331 332 333 334 335 336
	# real check.
	if ($runidx =~ /^([\w]*)$/) {
	    $runidx = $1;
	}
	else {
	    tbdie("Bad data in argument: $runidx");
	}
337 338
    }

339
    # These are really linkname/vnode ... so we can find exact IP addr.
340
    if (defined($options{"s"})) {
341
	my $tmp = $options{"s"};
342

343 344 345
	if ($tmp =~ /^([-\w]+)\/([-\w]+)$/) {
	    $srclan   = $1;
	    $srcvnode = $2;
346 347 348 349 350 351
	}
	else {
	    tbdie("Bad srcvnode name: $srcvnode!");
	}
    }
    if (defined($options{"t"})) {
352
	my $tmp = $options{"t"};
353

354 355 356
	if ($tmp =~ /^([-\w]+)\/([-\w]+)$/) {
	    $dstlan   = $1;
	    $dstvnode = $2;
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
	}
	else {
	    tbdie("Bad dstvnode name: $dstvnode!");
	}
    }

    if (defined($options{"q"})) {
	$quiet = 1;
    }
    if (defined($options{"d"})) {
	$debug = 2;
    }
}

#
# Cleanup the mess.
#
sub cleanup()
{
}

sub fatal($$)
{
    my ($errorstat, $msg) = @_;
    
    tberror $msg;
    tbinfo "Cleaning up and exiting with status $errorstat ...";

    exit($errorstat);
}