currentusage.php3 7.86 KB
Newer Older
1 2 3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2011 University of Utah and the Flux Group.
5 6 7
# All rights reserved.
#
require("defs.php3");
8 9 10 11
#
# This script uses Sajax ... BEWARE!
#
require("Sajax.php");
12

Leigh B. Stoller's avatar
Leigh B. Stoller committed
13 14
#
# Get current user.
15 16
#
$this_user = CheckLogin($check_status);
17

Leigh B. Stoller's avatar
Leigh B. Stoller committed
18 19 20
#
# For anonymous users, show experiment stats.
#
21
function SHOWSTATS($mini = 0)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
{
    $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 ".
53
		     "state='active' and swap_requests > 0 and idle_ignore=0 ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
54 55 56 57 58 59 60 61 62
		     "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();
63

64 65
    $output = "<table valign=top align=center width=100% height=100%
                    cellpadding=0 cellspacing=1 border=0>
66 67 68 69 70 71 72 73 74 75 76
                <tr><th nowrap colspan=2 class='usagetitle'>
	            Current Experiments</th></tr>
                <tr><td class=menuoptusage align=right>$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>$idle_expts</td>
                    <td align=left  class=menuoptusage>Idle</td></tr>
                <tr><td align=right class=menuoptusage>$swapped_expts</td>
                    <td align=left  class=menuoptusage>
                        <a target=_parent href=explist.php3#swapped>Swapped</a>
77 78 79
                    </td></tr>";
    if (!$mini) {
	$output .= "<tr><td align=right class=menuoptusage>
80 81 82
                        <font size=+1>$freepcs</font></td>
                    <td align=left  class=menuoptusage>
                        <font size=+1>Free PCs</font></td>
83 84 85
                </tr>";
    }
    $output .= "</table>\n";
86
    return $output;
87 88
}

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
#
# Logged in users, show free node counts.
#
function ShowStatus()
{
    $freepcs = TBFreePCs();
    $reload  = TBReloadingPCs();
    $users   = TBLoggedIn();
    $active  = TBActiveExperiments();
    $output  = "";

    $output .= "<table valign=top align=center width=100% height=100% 
		 cellspacing=1 cellpadding=0>";

    $output .= "<tr><td nowrap class=usagefreenodes>$freepcs Free PCs</td>".
	"</tr>\n";
    $output .= "<tr><td nowrap class=usagefreenodes>$reload PCs reloading</td>".
	"</tr>\n";
    $output .= "<tr><td nowrap class=usagefreenodes>$users active users</td>".
	"</tr>\n";
    $output .= "<tr><td nowrap class=usagefreenodes>$active active expts.</td>".
	"</tr>\n";
    
    $output .= "</table>";
    return $output;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
116 117 118 119 120
#
# Logged in users, show free node counts.
#
function SHOWFREENODES()
{
121 122
    $freecounts = array();
    
123 124 125 126 127 128 129 130 131
    # 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;
    }
132 133 134 135 136

    if (!count($freecounts)) {
	return "";
    }
	
Leigh B. Stoller's avatar
Leigh B. Stoller committed
137 138 139 140 141 142
    # 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' ".
143
		     "      and r.pid is null and n.reserved_pid is null ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
144 145 146 147 148 149 150 151 152 153
		     "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)) {
154
	    $freecounts[$type] += $count;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
155 156
	}
    }
157
    $output = "";
158

159 160 161
    $freepcs   = TBFreePCs();
    $reloading = TBReloadingPCs();

162 163 164 165
    $output .= "<table valign=top align=center width=100% height=100% border=1
		 cellspacing=1 cellpadding=0>
                 <tr><td nowrap colspan=8 class=usagefreenodes align=center>
 	           <b>$freepcs Free PCs, $reloading reloading</b></td></tr>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
166

167 168
    $pccount = count($freecounts);
    $newrow  = 1;
169 170
    $maxcols = (int) ($pccount / 3);
    if ($pccount % 3)
171 172
	$maxcols++;
    $cols    = 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
173 174 175
    foreach($freecounts as $key => $value) {
	$freecount = $freecounts[$key];

176
	if ($newrow) {
177
	    $output .= "<tr>\n";
178
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
179
	
180
	$output .= "<td class=usagefreenodes align=right>
181 182
                     <a target=_parent href=shownodetype.php3?node_type=$key>
                        $key</a></td>
183
                    <td class=usagefreenodes align=left>${freecount}</td>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
184

185 186
	$cols++;
	$newrow = 0;
187
	if ($cols == $maxcols || $pccount <= 3) {
188 189 190 191 192
	    $cols   = 0;
	    $newrow = 1;
	}

	if ($newrow) {
193
	    $output .= "</tr>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
194 195
	}
    }
196 197 198
    if (! $newrow) {
        # Fill out to $maxcols
	for ($i = $cols + 1; $i <= $maxcols; $i++) {
199 200
	    $output .= "<td class=usagefreenodes>&nbsp</td>";
	    $output .= "<td class=usagefreenodes>&nbsp</td>";
201 202
	}
	$output .= "</tr>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
203
    }
204 205 206
    # Fill in up to 3 rows.
    if ($pccount < 3) {
	for ($i = $pccount + 1; $i <= 3; $i++) {
207 208
	    $output .= "<tr><td class=usagefreenodes>&nbsp</td>
                            <td class=usagefreenodes>&nbsp</td></tr>\n";
209 210
	}
    }
211

212 213 214 215 216 217 218
    $output .= "</table>";
    return $output;
}

#
# This is for the Sajax request.
#
219
function FreeNodeHtml($usagemode = null) {
220
    global $this_user;
221

222
    if ($this_user) {
223 224 225
	if ($usagemode == null || $usagemode == "status") {
	    return ShowStatus();
	}
226 227 228
	elseif ($usagemode == "stats") {
	    return SHOWSTATS(1);
	}
229 230 231
	else {
	    return SHOWFREENODES();
	}
232 233 234 235
    }
    else {
	return SHOWSTATS();
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
236
}
237 238 239 240 241 242 243 244 245 246 247

#
# We need all errors to come back this function so that the Sajax request
# fails and the timer is terminated. See below.
# 
function handle_error($message, $death)
{
    echo "failed:$message";
    # Always exit; ignore $death.
    exit(1);
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
248 249
     
#
250
# If user is anonymous, show experiment stats, otherwise useful info.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
251
# 
252
if ($this_user) {
253 254
    sajax_init();
    sajax_export("FreeNodeHtml");
255 256 257 258 259 260 261 262

    # If this call is to client request function, then turn off
    # interactive mode; errors will cause the Sajax request to fail
    # and the timer to stop.
    if (sajax_client_request()) {
	$session_interactive  = 0;
	$session_errorhandler = 'handle_error';
    }
263 264 265 266 267 268 269 270
    sajax_handle_client_request();

    PAGEBEGINNING("Free Node Summary", 1, 1);
    echo "<script>\n";
    sajax_show_javascript();

    ?>
    function FreeNodeHtml_CB(stuff) {
271
	getObjbyName('usagefreenodes').innerHTML = stuff;
272
	setTimeout('GetFreeNodeHtml()', 60000);
273 274 275
    }
    function GetFreeNodeHtml() {
	x_FreeNodeHtml(FreeNodeHtml_CB);
276
    }
277 278
    setTimeout('GetFreeNodeHtml()', 60000);
    
279 280 281
    <?php
    echo "</script>\n";
	  
282 283
    echo "<div id=usagefreenodes>\n";
    echo   ShowStatus();
284 285
    echo "</div>\n";
    echo "</body></html>";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
286 287
}
else {
288 289 290 291 292
    PAGEBEGINNING("Current Usage", 1, 1);
    echo "<div id=usage>\n";
    echo   SHOWSTATS();
    echo "</div>\n";
    echo "</body></html>";
293
}
Mike Hibler's avatar
Mike Hibler committed
294
?>