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

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2008 University of Utah and the Flux Group.
6 7 8 9 10 11 12 13 14
# All rights reserved.
#

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

# Configure variables
use lib '@prefix@/lib';
use libdb;
use English;
15
use Experiment;
16 17 18 19 20 21
use Getopt::Std;

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

# Defaults
22
my $defidlehours = TBGetSiteVar("idle/threshold");
23 24 25 26 27 28

# 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
29
$s = 0; # summary nodes
30
$t = $defidlehours; # Threshold idle time
31 32
my $experiment;
my $whereclause = " pid is not null and eid is not null ";
33 34 35 36 37

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

sub help {
    die("Usage:
38
idletimes [-h] [-d] [-a] [-n] [s] [-t hrs] [eid]
39 40 41 42
 -h	Show this help message
 -d	Enable debugging/verbose output
 -a	Show all expts (or nodes with -n)
 -n	Show nodes instead of experiments
43
 -s     Summary mode for experiment; exit with status only.
44 45 46 47 48
 -t hrs	Threshold idle time (in hours, default $defidlehours)
Note: idletimes shows experiments that are set to idle_ignore.
");
}

49
my $optlist = "hdant:s";
50 51 52 53 54 55 56
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;
}
57 58 59 60 61 62 63 64 65 66 67 68 69
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'";
}
70

71
print "Settings: h=$h  d=$d  a=$a  n=$n  s=$s t=$t\n" if $d;
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

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
87
where ($whereclause)
88 89 90 91 92 93 94
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
95
where ($whereclause)
96 97 98 99 100 101 102
order by pid,eid,n.node_id";
}

my $q = DBQueryFatal($sql);

if ($d) { print $q->as_string; $q->dataseek(0); }

103 104 105 106 107 108
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) {
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 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
    $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);