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
use Mysql;
#
# avail - prints a list of all available nodes
#
#
# Configure variables
#
use lib '@prefix@/lib';
use libdb;
my $d = 0; #debug mode
......@@ -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;
my %args = ();
......@@ -83,10 +90,9 @@ my $cmd = "select $cols from $join where $cond order by priority";
print "Sending cmd:\n$cmd\n" if $d;
my $sth = $dbh->
query($cmd);
my $result = DBQueryFatal($cmd);
print $sth->as_string();
print $result->as_string();
# Hacky. Batch daemon depends on this exit code.
exit($sth->numrows);
exit($result->numrows);
#!/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->
query("select r.* from reserved as r left join nodes as n on r.node_id=n.node_id order by priority");
use English;
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
BOSSNODE=boss.emulab.net
USERNODE=users.emulab.net
OURDOMAIN=emulab.net
DELAYCAPACITY=2
......@@ -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@";
push(@INC,"$TBROOT/lib");
use lib '@prefix@/lib';
require exitonwarn;
use libdb;
%switches=();
%used_switches=();
# Read class/type maps
$sth = $dbh->prepare("select class,type,delay_capacity from node_types");
$sth->execute;
while (($class,$type,$delaycapacity) = $sth->fetchrow_array) {
my $result = DBQueryFatal("select class,type,delay_capacity from node_types");
while (($class,$type,$delaycapacity) = $result->fetchrow_array) {
$classes{$type} = $class;
$nodetypes{$type} = $delaycapacity;
}
$sth->finish;
$result->finish;
# switches can't delay
$nodetypes{"switch"} = 0;
# Print switches
$sth = $dbh->prepare("select node_id from nodes where role = \"testswitch\"");
$sth->execute;
while (($switch) = $sth->fetchrow_array) {
$result = DBQueryFatal("select node_id from nodes where role = \"testswitch\"");
while (($switch) = $result->fetchrow_array) {
print "node $switch switch:1\n";
$switches{$switch} = 1;
}
$sth->finish;
$result->finish;
# 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" .
" on a.node_id=b.node_id" .
" where b.node_id is null and a.role = \"testnode\"");
$sth->execute;
while (($node,$type) = $sth->fetchrow_array) {
while (($node,$type) = $result->fetchrow_array) {
# Shark hack
if (($shelf,$number) = ($node =~ /^sh(\d+)-(\d+)/)) {
if ($number == 1) {
......@@ -54,7 +49,7 @@ while (($node,$type) = $sth->fetchrow_array) {
$nodes{$node} = $type;
}
}
$sth->finish;
$result->finish;
$sharklinks = "";
......@@ -72,54 +67,27 @@ foreach $node (keys(%nodes)) {
}
# 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");
$sth->execute;
while (($node,$card,$port,$iface,$type) = $sth->fetchrow_array) {
while (($node,$card,$port,$iface,$type) = $result->fetchrow_array) {
$interfacemap{"$node:$card:$port"} = $iface;
if ($type ne "") {
$interfacetypes{"$node:$card:$port"} = $type;
}
}
$sth->finish;
$result->finish;
# Read interface types
$sth = $dbh->prepare("SELECT type,max_speed from interface_types");
$sth->execute;
while (($type,$speed) = $sth->fetchrow_array) {
$result = DBQueryFatal("SELECT type,max_speed from interface_types");
while (($type,$speed) = $result->fetchrow_array) {
$interfacespeeds{$type} = $speed;
}
$sth->finish;
$result->finish;
# 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
}
};
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" .
$result = DBQueryFatal("SELECT node_id1,card1,port1,node_id2,card2,port2" .
" from wires where type=\"Node\" or type=\"Trunk\"");
$sth->execute;
while (($node1,$card1,$port1,$node2,$card2,$port2) =
$sth->fetchrow_array) {
$result->fetchrow_array) {
if ((defined($nodes{$node1}) || defined($switches{$node1})) &&
(defined($nodes{$node2}) || defined($switches{$node2}))) {
$iface1 = get_iface($node1,$card1,$port1);
......@@ -152,7 +120,7 @@ while (($node1,$card1,$port1,$node2,$card2,$port2) =
}
}
}
$sth->finish;
$result->finish;
foreach $interconnect (keys(%interconnects)) {
($src,$dst) = split(":",$interconnect);
print "link link-$interconnect $src $dst $interconnects{$interconnect} 1\n";
......@@ -168,3 +136,30 @@ foreach $switch (keys(%used_switches)) {
" $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
# 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 $DBNAME = "@TBDBNAME@";
push(@INC,"$TBROOT/lib");
use lib '@prefix@/lib';
use libdb;
require exitonwarn;
use Getopt::Long;
#
# Turn off line buffering on output
#
$| = 1;
use DBI;
$dbh = DBI->connect("DBI:mysql:database=$DBNAME;host=localhost")
|| die "Could not connect to DB.\n";
#
# Get options
#
my %opt = ();
my $verbose = 0;
GetOptions(\%opt,"h","v");
$verbose = 0;
if ($#ARGV == 2) {
($v,$pid,$eid) = @ARGV;
if ($v ne "-v") {
print STDERR "Syntax: $0 [-v] pid eid\n";
exit(1);
}
if ($opt{h}) {
exit &usage;
}
if ($opt{v}) {
$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;
#
# Make sure the experimenter is a member of the right project, and that
# the experiment really exists.
#
if ((!ProjMember($pid)) && (!TBAdmin())) {
die "You are not a member of project $pid\n";
}
my $state = ExpState($pid,$eid);
# Experiment
$sth=$dbh->prepare("SELECT state from experiments where" .
" pid=\"$pid\" and eid=\"$eid\"");
$sth->execute();
if (! ($state = $sth->fetchrow_array())) {
if (!$state) {
print STDERR "Can not find experiment $eid.\n";
exit(1);
}
$sth->finish();
print "Experiment: $eid\n";
print "State: $state\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
print "No more information available.\n";
exit(0);
......@@ -54,15 +71,14 @@ if (($state ne "swapped") && ($state ne "active") && ($state ne "testing")) {
# Virtual
# Display node info
$sth=$dbh->prepare("SELECT vname,ips,osid,cmd_line,rpms,deltas,startupcmd," .
"tarfiles,type from virt_nodes where pid=\"$pid\"" .
my $result = DBQueryFatal("SELECT vname,ips,osid,cmd_line,rpms,deltas," .
"startupcmd,tarfiles,type from virt_nodes where pid=\"$pid\"" .
" and eid=\"$eid\"");
$sth->execute();
print "Node Info:\n";
printf "%-15s %-10s %-15s\n", "ID", "Type", "OSID";
print "--------------- ---------- ---------------\n";
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;
if ($verbose) {
if ($cmd_line ne "") {
......@@ -86,60 +102,60 @@ while (($vname,$ips,$osid,$cmd_line,$rpms,$deltas,$startupcmd,$tarfiles,$type)
$ipmap{"$vname:$port"} = $ip;
}
}
$sth->finish();
$result->finish();
print "\n";
# Display link info
if ($state eq "swapped") {
$sth=$dbh->prepare("SELECT vname,member,delay,bandwidth,lossrate" .
if ($state eq EXPTSTATE_SWAPPED) {
my $result = DBQueryFatal("SELECT vname,member,delay,bandwidth,lossrate" .
" from virt_lans where pid=\"$pid\" and eid=\"$eid\"");
$sth->execute();
print "Lan/Link Info:\n";
printf "%-15s %-15s %-15s %-10s %-10s %-10s\n", "ID", "Member",
"IP", "Delay", "Bandwidth", "Loss Rate";
print "--------------- --------------- --------------- ----------"
. " ---------- ----------\n";
while (($vname,$member,$delay,$bandwidth,$lossrate)
= $sth->fetchrow_array()) {
= $result->fetchrow_array()) {
printf "%-15s %-15s %-15s %-10s %-10s %-10s\n", $vname,
$member, $ipmap{$member}, $delay, $bandwidth, $lossrate;
}
$sth->finish();
$result->finish();
print "\n";
}
# Mapping
if (($state eq "active") || ($state eq "testing")) {
if (($state eq EXPTSTATE_ACTIVE) || ($state eq EXPTSTATE_TESTING)) {
print "Node Mapping:\n";
printf "%-15s %-15s %s\n", "Virtual", "Physical", "Qualified Name";
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\"");
$sth->execute();
while (($v,$p) = $sth->fetchrow_array()) {
while (($v,$p) = $result->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";
}
$sth->finish();
$result->finish();
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\"");
$sth->execute;
while (($vnode,$vport,$pport) = $sth->fetchrow_array) {
while (($vnode,$vport,$pport) = $result->fetchrow_array) {
$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\"");
$sth->execute();
print "Lan/Link Info:\n";
printf "%-15s %-15s %-15s %-10s %-10s %-10s\n", "ID", "Member",
"IP", "Delay", "Bandwidth", "Loss Rate";
print "--------------- --------------- --------------- ----------"
. " ---------- ----------\n";
while (($vname,$member,$delay,$bandwidth,$lossrate)
= $sth->fetchrow_array()) {
= $result->fetchrow_array()) {
($vnode,$vport) = split(":",$member);
if (defined($portmap{$member})) {
$pport = $portmap{$member};
......@@ -150,7 +166,7 @@ if (($state eq "active") || ($state eq "testing")) {
printf "%-15s %-15s %-15s %-10s %-10s %-10s\n", $vname,
"$vnode:$pport", $ipmap{$member}, $delay, $bandwidth, $lossrate;
}
$sth->finish();
$result->finish();
}
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