currentusage.php3 5.11 KB
Newer Older
1 2 3
<?php
#
# EMULAB-COPYRIGHT
Timothy Stack's avatar
 
Timothy Stack committed
4
# Copyright (c) 2000-2006 University of Utah and the Flux Group.
5 6 7 8
# All rights reserved.
#
require("defs.php3");

Leigh B. Stoller's avatar
Leigh B. Stoller committed
9 10 11 12
#
# Get current user.
# 
$uid = GETLOGIN();
13

Leigh B. Stoller's avatar
Leigh B. Stoller committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#
# For anonymous users, show experiment stats.
#
function SHOWSTATS()
{
    $query_result =
	DBQueryFatal("select count(*) from experiments as e " .
	     "left join experiment_stats as s on s.exptidx=e.idx " .
	     "left join experiment_resources as rs on rs.idx=s.rsrcidx ".
	     "where state='active' and rs.pnodes>0 and " .
		     "      e.pid!='emulab-ops' and ".
		     "      not (e.pid='ron' and e.eid='all')");
    
    if (mysql_num_rows($query_result) != 1) {
	$active_expts = "ERR";
    }
    else {
	$row = mysql_fetch_array($query_result);
	$active_expts = $row[0];
    }

    $query_result =
	DBQueryFatal("select count(*) from experiments where ".
		     "state='swapped' and pid!='emulab-ops' and ".
		     "pid!='testbed'");
    if (mysql_num_rows($query_result) != 1) {
	$swapped_expts = "ERR";
    }
    else {
	$row = mysql_fetch_array($query_result);
	$swapped_expts = $row[0];
    }

    $query_result =
	DBQueryFatal("select count(*) from experiments where ".
		     "swap_requests > 0 and idle_ignore=0 ".
		     "and pid!='emulab-ops' and pid!='testbed'");
    if (mysql_num_rows($query_result) != 1) {
	$idle_expts = "ERR";
    }
    else {
	$row = mysql_fetch_array($query_result);
	$idle_expts = $row[0];
    }
    $freepcs = TBFreePCs();
59

Leigh B. Stoller's avatar
Leigh B. Stoller committed
60 61 62
    PAGEBEGINNING("Current Usage", 1, 1);
    ?>
     <table valign=top align=center width=100% height=100% border=1>
Timothy Stack's avatar
 
Timothy Stack committed
63 64
     <tr><th nowrap colspan=2 class='usagetitle'>
	   Current Experiments</th></tr>
Leigh B. Stoller's avatar
Leigh B. Stoller committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
     <tr><td class="menuoptusage" align=right>
          <?php echo $active_expts ?></td> 
         <td align="left" class="menuoptusage">
             <a target=_parent href=explist.php3#active>Active</a>
          </td></tr>
     <tr><td align="right" class="menuoptusage"><?php echo $idle_expts ?></td>
         <td align="left" class="menuoptusage">Idle</td></tr>
     <tr><td align="right" class="menuoptusage">
	     <?php echo $swapped_expts ?></td>
         <td align="left" class="menuoptusage">
            <a target=_parent href=explist.php3#swapped>Swapped</a>
          </td></tr>
     <tr><td align="right" class="menuoptusage">
	     <b><?php echo $freepcs ?></b></td>
         <td align="left" class="menuoptusage"><b>Free PCs</b></td>
     </tr>
     </table>
   <?php
83 84
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
85 86 87 88 89
#
# Logged in users, show free node counts.
#
function SHOWFREENODES()
{
90 91 92 93 94 95 96 97 98 99
    # Get typelist and set freecounts to zero.
    $query_result =
	DBQueryFatal("select n.type from nodes as n ".
		     "left join node_types as nt on n.type=nt.type ".
		     "where (role='testnode') and class='pc' ");
    while ($row = mysql_fetch_array($query_result)) {
	$type              = $row[0];
	$freecounts[$type] = 0;
    }
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
100 101 102 103 104 105
    # Get free totals by type.
    $query_result =
	DBQueryFatal("select n.eventstate,n.type,count(*) from nodes as n ".
		     "left join node_types as nt on n.type=nt.type ".
		     "left join reserved as r on r.node_id=n.node_id ".
		     "where (role='testnode') and class='pc' ".
106
		     "      and r.pid is null and n.reserved_pid is null ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
		     "group BY n.eventstate,n.type");

    while ($row = mysql_fetch_array($query_result)) {
	$type  = $row[1];
	$count = $row[2];
        # XXX Yeah, I'm a doofus and can't figure out how to do this in SQL.
	if (($row[0] == TBDB_NODESTATE_ISUP) ||
	    ($row[0] == TBDB_NODESTATE_PXEWAIT) ||
	    ($row[0] == TBDB_NODESTATE_ALWAYSUP) ||
	    ($row[0] == TBDB_NODESTATE_POWEROFF)) {
	    $freecounts[$type] = $count;
	}
    }
    PAGEBEGINNING("Free Node Summary", 1, 1);

122 123 124
    $freepcs   = TBFreePCs();
    $reloading = TBReloadingPCs();

Leigh B. Stoller's avatar
Leigh B. Stoller committed
125
    echo "<table valign=top align=center width=100% height=100% border=1>
126
          <tr><td nowrap colspan=4 class=menuoptusage align=center>
127
 	       <font size=+1>$freepcs Free PCs</font></td></tr>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
128

129 130
    $pccount = count($freecounts);
    $newrow  = 1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
131 132 133
    foreach($freecounts as $key => $value) {
	$freecount = $freecounts[$key];

134
	if ($newrow || $pccount <= 3) 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
135 136 137 138 139 140 141 142
	    echo "<tr>\n";
	$newrow = ($newrow ? 0 : 1);
	
	echo "<td class=menuoptusage align=right>
                  <a target=_parent href=shownodetype.php3?node_type=$key>
                      $key</a></td>
              <td class=menuoptusage align=left>${freecount}</td>\n";

143
	if ($newrow || $pccount <= 3) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
144 145 146
	    echo "</tr>\n";
	}
    }
147
    if (! $newrow && $pccount > 3) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
148 149
	echo "<td></td><td></td></tr>\n";
    }
150 151 152 153 154 155 156
    # Fill in up to 3 rows.
    if ($pccount < 3) {
	for ($i = $pccount + 1; $i <= 3; $i++) {
	    echo "<tr><td class=menuoptusage>&nbsp</td>
                      <td class=menuoptusage>&nbsp</td></tr>\n";
	}
    }
157

Leigh B. Stoller's avatar
Leigh B. Stoller committed
158 159
    echo "<tr>
          <td class=menuoptusage colspan=4 align=center>
160
               <b>$reloading PCs reloading</b></td>
Leigh B. Stoller's avatar
Leigh B. Stoller committed
161 162 163 164 165
          </tr>\n";
    echo "</table>";
}
     
#
166
# If user is anonymous, show experiment stats, otherwise useful info.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
167
# 
168 169 170 171 172 173 174
if ($uid) {
    #
    # Auto refresh, but only for an hour of idle time.
    #
    if ($CHECKLOGIN_IDLETIME < (60 * 60)) {
	$autorefresh = 90;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
175 176 177 178
    SHOWFREENODES();
}
else {
    SHOWSTATS();
179 180
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
181
echo "</body></html>";
182