Commit e9aaf165 authored by Robert Ricci's avatar Robert Ricci

More lib-ification. These scripts now all use libdb for their database access.

Some minor cleanup/commenting as well.

Also, a few more access checks. Only admins are able to run 'inuse' (to prevent
users from seeing each other's experiments.) You are also only allowed to run
tbreport for experiments in projects that you are trusted by.
parent f870a7e9
#!/usr/local/bin/perl -w #!/usr/local/bin/perl -w
use Mysql;
#
# avail - prints a list of all available nodes
#
#
# Configure variables
#
use lib '@prefix@/lib';
use libdb;
my $d = 0; #debug mode my $d = 0; #debug mode
...@@ -18,8 +27,6 @@ if ($#ARGV >= 0 && $ARGV[0] =~ /^-?h/i ) { ...@@ -18,8 +27,6 @@ if ($#ARGV >= 0 && $ARGV[0] =~ /^-?h/i ) {
); );
} }
my $dbh = Mysql->connect("localhost","@TBDBNAME@","script","none");
print "Got ARGS = ",join(" ",@ARGV),"\n" if $d; print "Got ARGS = ",join(" ",@ARGV),"\n" if $d;
my %args = (); my %args = ();
...@@ -83,10 +90,9 @@ my $cmd = "select $cols from $join where $cond order by priority"; ...@@ -83,10 +90,9 @@ my $cmd = "select $cols from $join where $cond order by priority";
print "Sending cmd:\n$cmd\n" if $d; print "Sending cmd:\n$cmd\n" if $d;
my $sth = $dbh-> my $result = DBQueryFatal($cmd);
query($cmd);
print $sth->as_string(); print $result->as_string();
# Hacky. Batch daemon depends on this exit code. # Hacky. Batch daemon depends on this exit code.
exit($sth->numrows); exit($result->numrows);
#!/usr/local/bin/perl -w #!/usr/local/bin/perl -w
use Mysql;
my $dbh = Mysql->connect("localhost","@TBDBNAME@","script","none"); #
# inuse - print a list of all testbed nodes that are allocated to
# experiments
#
my $sth = $dbh-> use English;
query("select r.* from reserved as r left join nodes as n on r.node_id=n.node_id order by priority");
print $sth->as_string(); #
# Configure variables
#
use lib '@prefix@/lib';
use libdb;
if (($UID != 0) && (!TBAdmin())) {
die "Sorry, only testbed administrators can use this script\n";
}
my $result = DBQueryFatal("select r.* from reserved as r left join nodes " .
" as n on r.node_id=n.node_id order by priority");
print $result->as_string();
...@@ -8,3 +8,4 @@ TBLOGSEMAIL=ricci@cs.utah.edu ...@@ -8,3 +8,4 @@ TBLOGSEMAIL=ricci@cs.utah.edu
BOSSNODE=boss.emulab.net BOSSNODE=boss.emulab.net
USERNODE=users.emulab.net USERNODE=users.emulab.net
OURDOMAIN=emulab.net OURDOMAIN=emulab.net
DELAYCAPACITY=2
...@@ -7,44 +7,39 @@ $switch_speed = 100; ...@@ -7,44 +7,39 @@ $switch_speed = 100;
###################################################################### ######################################################################
use DBI;
my $TBDB = "@TBDBNAME@";
my $dbh = DBI->connect("DBI:mysql:database=$TBDB;host=localhost");
my $TBROOT = "@prefix@"; my $TBROOT = "@prefix@";
push(@INC,"$TBROOT/lib"); use lib '@prefix@/lib';
require exitonwarn; require exitonwarn;
use libdb;
%switches=(); %switches=();
%used_switches=(); %used_switches=();
# Read class/type maps # Read class/type maps
$sth = $dbh->prepare("select class,type,delay_capacity from node_types"); my $result = DBQueryFatal("select class,type,delay_capacity from node_types");
$sth->execute; while (($class,$type,$delaycapacity) = $result->fetchrow_array) {
while (($class,$type,$delaycapacity) = $sth->fetchrow_array) {
$classes{$type} = $class; $classes{$type} = $class;
$nodetypes{$type} = $delaycapacity; $nodetypes{$type} = $delaycapacity;
} }
$sth->finish; $result->finish;
# switches can't delay # switches can't delay
$nodetypes{"switch"} = 0; $nodetypes{"switch"} = 0;
# Print switches # Print switches
$sth = $dbh->prepare("select node_id from nodes where role = \"testswitch\""); $result = DBQueryFatal("select node_id from nodes where role = \"testswitch\"");
$sth->execute; while (($switch) = $result->fetchrow_array) {
while (($switch) = $sth->fetchrow_array) {
print "node $switch switch:1\n"; print "node $switch switch:1\n";
$switches{$switch} = 1; $switches{$switch} = 1;
} }
$sth->finish; $result->finish;
# Find available nodes # Find available nodes
$sth = $dbh->prepare("select a.node_id,a.type from" . $result = DBQueryFatal("select a.node_id,a.type from" .
" nodes as a left join reserved as b" . " nodes as a left join reserved as b" .
" on a.node_id=b.node_id" . " on a.node_id=b.node_id" .
" where b.node_id is null and a.role = \"testnode\""); " where b.node_id is null and a.role = \"testnode\"");
$sth->execute; while (($node,$type) = $result->fetchrow_array) {
while (($node,$type) = $sth->fetchrow_array) {
# Shark hack # Shark hack
if (($shelf,$number) = ($node =~ /^sh(\d+)-(\d+)/)) { if (($shelf,$number) = ($node =~ /^sh(\d+)-(\d+)/)) {
if ($number == 1) { if ($number == 1) {
...@@ -54,7 +49,7 @@ while (($node,$type) = $sth->fetchrow_array) { ...@@ -54,7 +49,7 @@ while (($node,$type) = $sth->fetchrow_array) {
$nodes{$node} = $type; $nodes{$node} = $type;
} }
} }
$sth->finish; $result->finish;
$sharklinks = ""; $sharklinks = "";
...@@ -72,54 +67,27 @@ foreach $node (keys(%nodes)) { ...@@ -72,54 +67,27 @@ foreach $node (keys(%nodes)) {
} }
# Read interfaces # Read interfaces
$sth = $dbh->prepare("SELECT node_id,card,port,iface,interface_type" . $result = DBQueryFatal("SELECT node_id,card,port,iface,interface_type" .
" from interfaces"); " from interfaces");
$sth->execute; while (($node,$card,$port,$iface,$type) = $result->fetchrow_array) {
while (($node,$card,$port,$iface,$type) = $sth->fetchrow_array) {
$interfacemap{"$node:$card:$port"} = $iface; $interfacemap{"$node:$card:$port"} = $iface;
if ($type ne "") { if ($type ne "") {
$interfacetypes{"$node:$card:$port"} = $type; $interfacetypes{"$node:$card:$port"} = $type;
} }
} }
$sth->finish; $result->finish;
# Read interface types # Read interface types
$sth = $dbh->prepare("SELECT type,max_speed from interface_types"); $result = DBQueryFatal("SELECT type,max_speed from interface_types");
$sth->execute; while (($type,$speed) = $result->fetchrow_array) {
while (($type,$speed) = $sth->fetchrow_array) {
$interfacespeeds{$type} = $speed; $interfacespeeds{$type} = $speed;
} }
$sth->finish; $result->finish;
# Print out links $result = DBQueryFatal("SELECT node_id1,card1,port1,node_id2,card2,port2" .
sub get_iface {
($node,$card,$port) = @_;
if (defined($interfacemap{"$node:$card:$port"})) {
return $interfacemap{"$node:$card:$port"};
} else {
# shark hack
if ($node =~ /^sh/) {
return "eth0";
} else {
return "(null)";
}
# end shark hack
}
};
sub get_ifacebw {
($node,$card,$port) = @_;
if (defined($interfacetypes{"$node:$card:$port"})) {
return $interfacespeeds{$interfacetypes{"$node:$card:$port"}};
} else {
return $switch_speed;
}
};
$sth = $dbh->prepare("SELECT node_id1,card1,port1,node_id2,card2,port2" .
" from wires where type=\"Node\" or type=\"Trunk\""); " from wires where type=\"Node\" or type=\"Trunk\"");
$sth->execute;
while (($node1,$card1,$port1,$node2,$card2,$port2) = while (($node1,$card1,$port1,$node2,$card2,$port2) =
$sth->fetchrow_array) { $result->fetchrow_array) {
if ((defined($nodes{$node1}) || defined($switches{$node1})) && if ((defined($nodes{$node1}) || defined($switches{$node1})) &&
(defined($nodes{$node2}) || defined($switches{$node2}))) { (defined($nodes{$node2}) || defined($switches{$node2}))) {
$iface1 = get_iface($node1,$card1,$port1); $iface1 = get_iface($node1,$card1,$port1);
...@@ -152,7 +120,7 @@ while (($node1,$card1,$port1,$node2,$card2,$port2) = ...@@ -152,7 +120,7 @@ while (($node1,$card1,$port1,$node2,$card2,$port2) =
} }
} }
} }
$sth->finish; $result->finish;
foreach $interconnect (keys(%interconnects)) { foreach $interconnect (keys(%interconnects)) {
($src,$dst) = split(":",$interconnect); ($src,$dst) = split(":",$interconnect);
print "link link-$interconnect $src $dst $interconnects{$interconnect} 1\n"; print "link link-$interconnect $src $dst $interconnects{$interconnect} 1\n";
...@@ -168,3 +136,30 @@ foreach $switch (keys(%used_switches)) { ...@@ -168,3 +136,30 @@ foreach $switch (keys(%used_switches)) {
" $switch 100000 1000\n"; " $switch 100000 1000\n";
} }
} }
# Print out links
sub get_iface {
($node,$card,$port) = @_;
if (defined($interfacemap{"$node:$card:$port"})) {
return $interfacemap{"$node:$card:$port"};
} else {
# shark hack
if ($node =~ /^sh/) {
return "eth0";
} else {
return "(null)";
}
# end shark hack
}
};
# Find the bandwidth for an interface
sub get_ifacebw {
($node,$card,$port) = @_;
if (defined($interfacetypes{"$node:$card:$port"})) {
return $interfacespeeds{$interfacetypes{"$node:$card:$port"}};
} else {
return $switch_speed;
}
}
#!/usr/bin/perl -w #!/usr/bin/perl -w
# tbreport #
# This just provides the necessary information from the IR file. # tbreport - given a pid and eid, print out useful information, including
# a list of nodes and links, about the experiment. Only useful when the
# experiment is in the active, swapped, or testing states
#
sub usage {
print "Usage: $0 [-h] [-v] pid eid\n";
print "-h Shows this message\n";
print "-v Give verbose output\n";
return 1;
}
my $TBROOT = "@prefix@"; my $TBROOT = "@prefix@";
my $DBNAME = "@TBDBNAME@"; use lib '@prefix@/lib';
push(@INC,"$TBROOT/lib"); use libdb;
require exitonwarn; require exitonwarn;
use Getopt::Long;
# #
# Turn off line buffering on output # Turn off line buffering on output
# #
$| = 1; $| = 1;
use DBI; #
$dbh = DBI->connect("DBI:mysql:database=$DBNAME;host=localhost") # Get options
|| die "Could not connect to DB.\n"; #
my %opt = ();
my $verbose = 0;
GetOptions(\%opt,"h","v");
$verbose = 0; if ($opt{h}) {
if ($#ARGV == 2) { exit &usage;
($v,$pid,$eid) = @ARGV; }
if ($v ne "-v") { if ($opt{v}) {
print STDERR "Syntax: $0 [-v] pid eid\n";
exit(1);
}
$verbose = 1; $verbose = 1;
} elsif ($#ARGV == 1) {
($pid,$eid) = @ARGV;
} else {
print STDERR "Syntax: $0 [-v] pid eid\n";
exit(1);
} }
if (@ARGV != 2) {
exit &usage;
}
my ($pid,$eid) = @ARGV;
# Experiment #
$sth=$dbh->prepare("SELECT state from experiments where" . # Make sure the experimenter is a member of the right project, and that
" pid=\"$pid\" and eid=\"$eid\""); # the experiment really exists.
$sth->execute(); #
if (! ($state = $sth->fetchrow_array())) { if ((!ProjMember($pid)) && (!TBAdmin())) {
die "You are not a member of project $pid\n";
}
my $state = ExpState($pid,$eid);
if (!$state) {
print STDERR "Can not find experiment $eid.\n"; print STDERR "Can not find experiment $eid.\n";
exit(1); exit(1);
} }
$sth->finish();
print "Experiment: $eid\n"; print "Experiment: $eid\n";
print "State: $state\n"; print "State: $state\n";
print "\n"; print "\n";
if (($state ne "swapped") && ($state ne "active") && ($state ne "testing")) { if (($state ne EXPTSTATE_SWAPPED) && ($state ne EXPTSTATE_ACTIVE) &&
($state ne EXPTSTATE_TESTING)) {
# nothing to do # nothing to do
print "No more information available.\n"; print "No more information available.\n";
exit(0); exit(0);
...@@ -54,15 +71,14 @@ if (($state ne "swapped") && ($state ne "active") && ($state ne "testing")) { ...@@ -54,15 +71,14 @@ if (($state ne "swapped") && ($state ne "active") && ($state ne "testing")) {
# Virtual # Virtual
# Display node info # Display node info
$sth=$dbh->prepare("SELECT vname,ips,osid,cmd_line,rpms,deltas,startupcmd," . my $result = DBQueryFatal("SELECT vname,ips,osid,cmd_line,rpms,deltas," .
"tarfiles,type from virt_nodes where pid=\"$pid\"" . "startupcmd,tarfiles,type from virt_nodes where pid=\"$pid\"" .
" and eid=\"$eid\""); " and eid=\"$eid\"");
$sth->execute();
print "Node Info:\n"; print "Node Info:\n";
printf "%-15s %-10s %-15s\n", "ID", "Type", "OSID"; printf "%-15s %-10s %-15s\n", "ID", "Type", "OSID";
print "--------------- ---------- ---------------\n"; print "--------------- ---------- ---------------\n";
while (($vname,$ips,$osid,$cmd_line,$rpms,$deltas,$startupcmd,$tarfiles,$type) while (($vname,$ips,$osid,$cmd_line,$rpms,$deltas,$startupcmd,$tarfiles,$type)
= $sth->fetchrow_array()) { = $result->fetchrow_array()) {
printf "%-15s %-10s %-15s\n", $vname, $type, $osid; printf "%-15s %-10s %-15s\n", $vname, $type, $osid;
if ($verbose) { if ($verbose) {
if ($cmd_line ne "") { if ($cmd_line ne "") {
...@@ -86,60 +102,60 @@ while (($vname,$ips,$osid,$cmd_line,$rpms,$deltas,$startupcmd,$tarfiles,$type) ...@@ -86,60 +102,60 @@ while (($vname,$ips,$osid,$cmd_line,$rpms,$deltas,$startupcmd,$tarfiles,$type)
$ipmap{"$vname:$port"} = $ip; $ipmap{"$vname:$port"} = $ip;
} }
} }
$sth->finish(); $result->finish();
print "\n"; print "\n";
# Display link info # Display link info
if ($state eq "swapped") { if ($state eq EXPTSTATE_SWAPPED) {
$sth=$dbh->prepare("SELECT vname,member,delay,bandwidth,lossrate" . my $result = DBQueryFatal("SELECT vname,member,delay,bandwidth,lossrate" .
" from virt_lans where pid=\"$pid\" and eid=\"$eid\""); " from virt_lans where pid=\"$pid\" and eid=\"$eid\"");
$sth->execute();
print "Lan/Link Info:\n"; print "Lan/Link Info:\n";
printf "%-15s %-15s %-15s %-10s %-10s %-10s\n", "ID", "Member", printf "%-15s %-15s %-15s %-10s %-10s %-10s\n", "ID", "Member",
"IP", "Delay", "Bandwidth", "Loss Rate"; "IP", "Delay", "Bandwidth", "Loss Rate";
print "--------------- --------------- --------------- ----------" print "--------------- --------------- --------------- ----------"
. " ---------- ----------\n"; . " ---------- ----------\n";
while (($vname,$member,$delay,$bandwidth,$lossrate) while (($vname,$member,$delay,$bandwidth,$lossrate)
= $sth->fetchrow_array()) { = $result->fetchrow_array()) {
printf "%-15s %-15s %-15s %-10s %-10s %-10s\n", $vname, printf "%-15s %-15s %-15s %-10s %-10s %-10s\n", $vname,
$member, $ipmap{$member}, $delay, $bandwidth, $lossrate; $member, $ipmap{$member}, $delay, $bandwidth, $lossrate;
} }
$sth->finish(); $result->finish();
print "\n"; print "\n";
} }
# Mapping # Mapping
if (($state eq "active") || ($state eq "testing")) { if (($state eq EXPTSTATE_ACTIVE) || ($state eq EXPTSTATE_TESTING)) {
print "Node Mapping:\n"; print "Node Mapping:\n";
printf "%-15s %-15s %s\n", "Virtual", "Physical", "Qualified Name"; printf "%-15s %-15s %s\n", "Virtual", "Physical", "Qualified Name";
print "--------------- --------------- --------------------\n"; print "--------------- --------------- --------------------\n";
$sth = $dbh->prepare("SELECT vname,node_id from reserved" . my $result = DBQueryFatal("SELECT vname,node_id from reserved" .
" where pid=\"$pid\" and eid=\"$eid\""); " where pid=\"$pid\" and eid=\"$eid\"");
$sth->execute(); while (($v,$p) = $result->fetchrow_array()) {
while (($v,$p) = $sth->fetchrow_array()) { # If they have no virtual name, we use the physical one
if (!$v) {
$v = $p;
}
printf "%-15s %-15s %s\n", $v, $p, "$v.$eid.$pid.emulab.net"; printf "%-15s %-15s %s\n", $v, $p, "$v.$eid.$pid.emulab.net";
} }
$sth->finish(); $result->finish();
print "\n"; print "\n";
$sth=$dbh->prepare("SELECT vnode,vport,pport from portmap" . $result = DBQueryFatal("SELECT vnode,vport,pport from portmap" .
" where pid=\"$pid\" and eid=\"$eid\""); " where pid=\"$pid\" and eid=\"$eid\"");
$sth->execute; while (($vnode,$vport,$pport) = $result->fetchrow_array) {
while (($vnode,$vport,$pport) = $sth->fetchrow_array) {
$portmap{"$vnode:$vport"} = $pport; $portmap{"$vnode:$vport"} = $pport;
} }
$sth->finish; $result->finish;
$sth=$dbh->prepare("SELECT vname,member,delay,bandwidth,lossrate" . $result = DBQueryFatal("SELECT vname,member,delay,bandwidth,lossrate" .
" from virt_lans where pid=\"$pid\" and eid=\"$eid\""); " from virt_lans where pid=\"$pid\" and eid=\"$eid\"");
$sth->execute();
print "Lan/Link Info:\n"; print "Lan/Link Info:\n";
printf "%-15s %-15s %-15s %-10s %-10s %-10s\n", "ID", "Member", printf "%-15s %-15s %-15s %-10s %-10s %-10s\n", "ID", "Member",
"IP", "Delay", "Bandwidth", "Loss Rate"; "IP", "Delay", "Bandwidth", "Loss Rate";
print "--------------- --------------- --------------- ----------" print "--------------- --------------- --------------- ----------"
. " ---------- ----------\n"; . " ---------- ----------\n";
while (($vname,$member,$delay,$bandwidth,$lossrate) while (($vname,$member,$delay,$bandwidth,$lossrate)
= $sth->fetchrow_array()) { = $result->fetchrow_array()) {
($vnode,$vport) = split(":",$member); ($vnode,$vport) = split(":",$member);
if (defined($portmap{$member})) { if (defined($portmap{$member})) {
$pport = $portmap{$member}; $pport = $portmap{$member};
...@@ -148,9 +164,9 @@ if (($state eq "active") || ($state eq "testing")) { ...@@ -148,9 +164,9 @@ if (($state eq "active") || ($state eq "testing")) {
$pport = "eth0"; $pport = "eth0";
} }
printf "%-15s %-15s %-15s %-10s %-10s %-10s\n", $vname, printf "%-15s %-15s %-15s %-10s %-10s %-10s\n", $vname,
"$vnode:$pport", $ipmap{$member}, $delay, $bandwidth, $lossrate; "$vnode:$pport", $ipmap{$member}, $delay, $bandwidth, $lossrate;
} }
$sth->finish(); $result->finish();
} }
0; 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment