idletimes.in 5.03 KB
Newer Older
1 2 3
#!/usr/bin/perl -w

#
4
# Copyright (c) 2000-2011 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/>.
# 
# }}}
24 25 26 27 28 29 30 31
#

# idletimes - show how long nodes/expts have been idle

# Configure variables
use lib '@prefix@/lib';
use libdb;
use English;
32
use Experiment;
33 34 35 36 37 38
use Getopt::Std;

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

# Defaults
39
my $defidlehours = TBGetSiteVar("idle/threshold");
40 41 42 43 44 45

# Don't put 'my' on these, or they won't be settable with ${$var}
$h = 0; # help mode
$d = 0; # debug mode
$a = 0; # show all
$n = 0; # show nodes
46
$s = 0; # summary nodes
47
$t = $defidlehours; # Threshold idle time
48 49
my $experiment;
my $whereclause = " pid is not null and eid is not null ";
50 51 52 53 54

print "Got ARGV = ",join(" ",@ARGV),"\n" if $d;

sub help {
    die("Usage:
55
idletimes [-h] [-d] [-a] [-n] [s] [-t hrs] [eid]
56 57 58 59
 -h	Show this help message
 -d	Enable debugging/verbose output
 -a	Show all expts (or nodes with -n)
 -n	Show nodes instead of experiments
60
 -s     Summary mode for experiment; exit with status only.
61 62 63 64 65
 -t hrs	Threshold idle time (in hours, default $defidlehours)
Note: idletimes shows experiments that are set to idle_ignore.
");
}

66
my $optlist = "hdant:s";
67 68 69 70 71 72 73
my %opt = ();
if (! getopts($optlist,\%opt)) { help(); }
# Copy the options into global vars
foreach $var (keys %opt) {
    ${$var} = $opt{$var};
    print "\$$var = $opt{$var} (".${$var}.")\n" if $d;
}
74 75 76 77 78 79 80 81 82 83 84 85 86
if (@ARGV) {
    my $pideid = $ARGV[0];
    $experiment = Experiment->Lookup($pideid);
    if (!defined($experiment)) {
	die("Experiment $pideid does not exist\n");
    }
    if ($experiment->state() ne EXPTSTATE_ACTIVE) {
	die("Experiment $pideid is not active\n");
    }
    my $pid = $experiment->pid();
    my $eid = $experiment->eid();
    $whereclause = "pid='$pid' and eid='$eid'";
}
87

88
print "Settings: h=$h  d=$d  a=$a  n=$n  s=$s t=$t\n" if $d;
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

if ($h) { help(); }

# Construct the query
my $lastact_query = "greatest(last_tty_act, last_net_act, ".
  "last_cpu_act, last_ext_act)";
my $sql = "";
if (!$n) {
    $sql .= "select pid, eid, last_report,
max(last_tty_act) as last_tty_act, max(last_net_act) as last_net_act,
max(last_cpu_act) as last_cpu_act, max(last_ext_act) as last_ext_act,
max($lastact_query) as last_act, round((unix_timestamp(now()) -
unix_timestamp(max($lastact_query)))/3600,2) as idle_time,
count(r.node_id) as nodes
from node_activity as n left join reserved as r on n.node_id=r.node_id
104
where ($whereclause)
105 106 107 108 109 110 111
group by pid,eid order by pid,eid";
} else {
    $sql .= "select pid, eid, n.node_id, last_report,
last_tty_act, last_net_act, last_cpu_act, last_ext_act,
 $lastact_query as last_act, round((unix_timestamp(now()) -
unix_timestamp($lastact_query))/3600,2) as idle_time
from node_activity as n left join reserved as r on n.node_id=r.node_id
112
where ($whereclause)
113 114 115 116 117
order by pid,eid,n.node_id";
}

my $q = DBQueryFatal($sql);

118 119 120 121 122 123
if ($s && $experiment) {
    # No output; For a single experiment, there should be a single row.
    my %row = $q->fetchhash();
    exit($row{'idle_time'} >= $defidlehours);
}
elsif (!$n) {
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    $FORMAT_NAME = "ByExpt";
    print <<EOT;
PID          EID          Nodes  HrsIdle     Last Activity     Act. type
------------------------------------------------------------------------
EOT
} else {
    $FORMAT_NAME = "ByNode";
    print <<EOT;
PID          EID          NodeId  HrsIdle     Last Activity     Act. type
-------------------------------------------------------------------------
EOT
}

format ByExpt =
@<<<<<<<<<<< @<<<<<<<<<<<<< @##  @##.##   @<<<<<<<<<<<<<<<<<<  @<<<<<<<<<<<<<<
$pid,        $eid,       $nodes, $time,   $act,                $typestr
.

format ByNode =
@<<<<<<<<<<< @<<<<<<<<<<<<< @<<<< @##.##   @<<<<<<<<<<<<<<<<<<  @<<<<<<<<<<<<<<
$pid,        $eid,          $id,  $time,   $act,                $typestr
.



while (%r = $q->fetchhash()) {
    $pid = $r{'pid'};
    $eid = $r{'eid'};
    #rep = $r{'last_report'};
    $tty = $r{'last_tty_act'};
    $net = $r{'last_net_act'};
    $cpu = $r{'last_cpu_act'};
    $ext = $r{'last_ext_act'};
    $act = $r{'last_act'};
    $time= $r{'idle_time'};
    $nodes=0;
    $id="";
    if (!$n) { $nodes = $r{'nodes'}; }
    else { $id = $r{'node_id'}; }
    %type = ();
    if ($tty eq $act) { $type{"tty"} = 1; }
    if ($net eq $act) { $type{"net"} = 1; }
    if ($cpu eq $act) { $type{"cpu"} = 1; }
    if ($ext eq $act) { $type{"ext"} = 1; }
    $typestr = join(",",keys %type);

    if (!$a && ($time < $t)) { next; }
    write();
}

exit(0);