inuse.in 4.52 KB
Newer Older
1
#!/usr/bin/perl -w
Mac Newbold's avatar
Mac Newbold committed
2

Leigh Stoller's avatar
Leigh Stoller committed
3
#
Robert Ricci's avatar
Robert Ricci committed
4
# Copyright (c) 2000-2002,2010 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{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/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
24 25
#

26 27 28 29
#
# inuse - print a list of all testbed nodes that are allocated to
# experiments
#
Mac Newbold's avatar
Mac Newbold committed
30

31
use English;
Robert Ricci's avatar
Robert Ricci committed
32 33
use Getopt::Std;
use strict;
Mac Newbold's avatar
Mac Newbold committed
34

35 36 37 38 39 40
#
# Configure variables
#
use lib '@prefix@/lib';
use libdb;

Robert Ricci's avatar
Robert Ricci committed
41 42
sub usage() {
    print STDOUT "Usage: inuse [-a | -t type] [-p pid [-e eid]] [-s|-r] " .
43
        "[-f|-F] [-h] [-R] [-u feature | -U feature]\n";
Robert Ricci's avatar
Robert Ricci committed
44 45 46 47 48 49 50 51 52
    print STDOUT "  -h          This message\n";
    print STDOUT "  -a          Print all nodes (default: only PCs\n";
    print STDOUT "  -t type     Limit to nodes of type (or class)\n";
    print STDOUT "  -p pid      Limit to project\n";
    print STDOUT "  -e eid      Limit to experiment\n";
    print STDOUT "  -s          Sort by pid,eid, rather than node id\n";
    print STDOUT "  -r          Sort by reserve time rather than node id\n";
    print STDOUT "  -f          Include free nodes\n";
    print STDOUT "  -F          Include ONLY free nodes\n";
Robert Ricci's avatar
Robert Ricci committed
53
    print STDOUT "  -R          Only include pre-reserved nodes\n";
54 55
    print STDOUT "  -u feature  Only include nodes with the given feature\n";
    print STDOUT "  -U feature  Exclude nodes with the given feature\n";
Robert Ricci's avatar
Robert Ricci committed
56 57
    exit (-1);
}
58
my $optlist = "t:p:e:srafFhRu:U:";
Robert Ricci's avatar
Robert Ricci committed
59

Robert Ricci's avatar
Robert Ricci committed
60
my @columns = ("n.node_id","n.type","pid","eid","reserved_pid");
Robert Ricci's avatar
Robert Ricci committed
61 62 63 64 65 66 67 68 69 70 71

my %opt = ();
if (! getopts($optlist, \%opt)) {
    usage();
}
if (@ARGV != 0 || $opt{h}) {
    usage();
}

# Check for mutually exclusive options or ones that must go together
if (($opt{s} && $opt{r}) || ($opt{e} && !$opt{p}) ||
72 73
    ($opt{a} && $opt{t}) || ($opt{f} && $opt{F}) ||
    ($opt{u} && $opt{U})) {
Robert Ricci's avatar
Robert Ricci committed
74 75
    usage();
}
76 77 78 79 80

if (($UID != 0) && (!TBAdmin())) {
	die "Sorry, only testbed administrators can use this script\n";
}

Robert Ricci's avatar
Robert Ricci committed
81 82 83 84 85 86 87 88
my @whereclauses = ();

# Require that the node be reserved (unless otherwise specified)
if ($opt{F}) {
    # include only free nodes
    push @whereclauses, "r.pid is null";
} elsif ($opt{f}) {
    # Nothing to insert - include all nodes
Robert Ricci's avatar
Robert Ricci committed
89 90 91
} elsif ($opt{R}) {
    # include only pre-reserved nodes
    push @whereclauses, "reserved_pid is not null";
Robert Ricci's avatar
Robert Ricci committed
92 93
} else {
    # include only reserved nodes (default)
Robert Ricci's avatar
Robert Ricci committed
94
    push @whereclauses, "r.pid is not null or reserved_pid is not null";
Robert Ricci's avatar
Robert Ricci committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
}

# Figure out type limits
if ($opt{t}) {
    push @whereclauses, "t.type='$opt{t}' or t.class='$opt{t}'";
} elsif ($opt{a}) {
    # Push no type-related clauses
} else {
    # Default to only showing nodes with class 'pc'
    push @whereclauses, "t.class='pc'";
}

if ($opt{p}) {
    if ($opt{e}) {
        push @whereclauses, "pid='$opt{p}' and eid='$opt{e}'";
    } else {
        push @whereclauses, "pid='$opt{p}'";
    }
}

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
if ($opt{u} || $opt{U}) {
    my $feature = defined($opt{u}) ? $opt{u} : $opt{U};
    my $subquery = "(".
       "EXISTS (SELECT * FROM node_features AS nf " .
       "        WHERE nf.node_id = n.node_id " .
       "        AND feature = '$feature') " .
       " OR " .
       "EXISTS (SELECT * FROM node_type_features AS ntf " .
       "        WHERE ntf.type = n.type " .
       "        AND feature = '$feature') " .
       ")";
    if (defined($opt{u})) {
        push @whereclauses, $subquery;
    } else {
        push @whereclauses, "NOT $subquery";
    }
}

Robert Ricci's avatar
Robert Ricci committed
133 134 135 136 137 138 139 140 141
my $whereclause = "";
if (@whereclauses) {
    $whereclause = "where " . join(" and ",map {"($_)"} @whereclauses);
}

my $sortclause = "order by priority";
if ($opt{s}) {
    $sortclause = "order by pid,eid,priority";
} elsif ($opt{r}) {
Robert Ricci's avatar
Robert Ricci committed
142
    push @columns, "rsrv_time";
Robert Ricci's avatar
Robert Ricci committed
143 144 145
    $sortclause = "order by rsrv_time,priority";
}

146

Robert Ricci's avatar
Robert Ricci committed
147 148 149 150 151 152 153
my $columns = join(",",@columns);

my $result = DBQueryFatal("select $columns from nodes as n " .
                "left join reserved as r on r.node_id=n.node_id " .
                "left join node_types as t on n.type=t.type " .
                "$whereclause " .
                "$sortclause");
154 155

print $result->as_string();
Mac Newbold's avatar
Mac Newbold committed
156