currentusage.php3 10.4 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2016 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{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/>.
# 
# }}}
23 24
#
require("defs.php3");
25 26 27
# Analytics on this page messes up the stats
$noAnalytics = 1;

28 29 30 31
#
# This script uses Sajax ... BEWARE!
#
require("Sajax.php");
32

Leigh B. Stoller's avatar
Leigh B. Stoller committed
33 34
#
# Get current user.
35 36
#
$this_user = CheckLogin($check_status);
37

Leigh B. Stoller's avatar
Leigh B. Stoller committed
38 39 40
#
# For anonymous users, show experiment stats.
#
41
function SHOWSTATS($mini = 0)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
{
    $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 ".
73
		     "state='active' and swap_requests > 0 and idle_ignore=0 ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
74 75 76 77 78 79 80 81 82
		     "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();
83

84 85
    $output = "<table valign=top align=center width=100% height=100%
                    cellpadding=0 cellspacing=1 border=0>
86 87 88 89 90 91 92 93 94 95 96
                <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>
97 98 99
                    </td></tr>";
    if (!$mini) {
	$output .= "<tr><td align=right class=menuoptusage>
100 101 102
                        <font size=+1>$freepcs</font></td>
                    <td align=left  class=menuoptusage>
                        <font size=+1>Free PCs</font></td>
103 104 105
                </tr>";
    }
    $output .= "</table>\n";
106
    return $output;
107 108
}

109 110 111 112 113
#
# Logged in users, show free node counts.
#
function ShowStatus()
{
114 115 116
    global $this_user;
    
    $freepcs = TBFreePCs($this_user);
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    $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
138 139 140 141 142
#
# Logged in users, show free node counts.
#
function SHOWFREENODES()
{
143
    global $this_user;
144
    $freecounts = array();
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    $findinset  = "";
    $pids       = array();
    $clause     = "0";
    
    if ($this_user) {
        $uid_idx = $this_user->uid_idx();
        $query_result =
            DBQueryFatal("select distinct pid from group_membership ".
                         "where uid_idx='$uid_idx'");
        if (mysql_num_rows($query_result)) {
            $clauses = array();
            while ($row = mysql_fetch_row($query_result)) {
                $pid = $row[0];
                $pids[] = $pid;
                $clauses[] = "p.pid='$pid'";
            }
Leigh B Stoller's avatar
Leigh B Stoller committed
161 162
            $clause = "p.pid is null or " . join(" or ", $clauses);
            $findinset = "or FIND_IN_SET(n.reserved_pid, '" . join(",", $pids)."')";
163 164
        }
    }
165
    
166 167 168 169
    # 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 ".
170 171 172 173 174
                     "left join node_type_attributes as attr on ".
                     "     attr.type=n.type and ".
                     "     attr.attrkey='noshowfreenodes' ".
		     "where (role='testnode') and class='pc' and ".
                     "      attr.attrvalue is null");
175 176 177 178
    while ($row = mysql_fetch_array($query_result)) {
	$type              = $row[0];
	$freecounts[$type] = 0;
    }
179 180 181 182 183

    if (!count($freecounts)) {
	return "";
    }
	
Leigh B. Stoller's avatar
Leigh B. Stoller committed
184 185 186 187 188
    # 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 ".
189 190 191 192 193 194 195
                     "left join node_type_attributes as attr on ".
                     "     attr.type=n.type and ".
                     "     attr.attrkey='noshowfreenodes' ".
		     "where (role='testnode') and class='pc' and ".
		     "      r.pid is null and ".
                     "      attr.attrvalue is null and ".
                     "      (n.reserved_pid is null $findinset) ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
196 197 198 199 200 201 202 203 204 205
		     "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)) {
206 207 208 209

            $policy_result =
                DBQueryFatal("select max(count) from group_policies as p ".
                             "where p.policy='type' and p.auxdata='$type' and ".
210 211
                             "      (p.pid='-' or $clause) ".
                             "having max(count)>=0");
212 213 214 215 216 217 218
        
            if (mysql_num_rows($policy_result)) {
                $row = mysql_fetch_row($policy_result);
                if ($count > $row[0]) {
                    $count = $row[0];
                }
            }
219
	    $freecounts[$type] += $count;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
220 221
	}
    }
222
    $output = "";
223

224
    $freepcs   = TBFreePCs($this_user);
225 226
    $reloading = TBReloadingPCs();

227 228 229 230
    $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
231

232 233
    $pccount = count($freecounts);
    $newrow  = 1;
234 235
    $maxcols = (int) ($pccount / 3);
    if ($pccount % 3)
236 237
	$maxcols++;
    $cols    = 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
238 239 240
    foreach($freecounts as $key => $value) {
	$freecount = $freecounts[$key];

241
	if ($newrow) {
242
	    $output .= "<tr>\n";
243
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
244
	
245
	$output .= "<td class=usagefreenodes align=right>
246 247
                     <a target=_parent href=shownodetype.php3?node_type=$key>
                        $key</a></td>
248
                    <td class=usagefreenodes align=left>${freecount}</td>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
249

250 251
	$cols++;
	$newrow = 0;
252
	if ($cols == $maxcols || $pccount <= 3) {
253 254 255 256 257
	    $cols   = 0;
	    $newrow = 1;
	}

	if ($newrow) {
258
	    $output .= "</tr>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
259 260
	}
    }
261 262 263
    if (! $newrow) {
        # Fill out to $maxcols
	for ($i = $cols + 1; $i <= $maxcols; $i++) {
264 265
	    $output .= "<td class=usagefreenodes>&nbsp</td>";
	    $output .= "<td class=usagefreenodes>&nbsp</td>";
266 267
	}
	$output .= "</tr>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
268
    }
269 270 271
    # Fill in up to 3 rows.
    if ($pccount < 3) {
	for ($i = $pccount + 1; $i <= 3; $i++) {
272 273
	    $output .= "<tr><td class=usagefreenodes>&nbsp</td>
                            <td class=usagefreenodes>&nbsp</td></tr>\n";
274 275
	}
    }
276

277 278 279 280 281 282 283
    $output .= "</table>";
    return $output;
}

#
# This is for the Sajax request.
#
284
function FreeNodeHtml($usagemode = null) {
285
    global $this_user;
286

287
    if ($this_user) {
288 289 290
	if ($usagemode == null || $usagemode == "status") {
	    return ShowStatus();
	}
291 292 293
	elseif ($usagemode == "stats") {
	    return SHOWSTATS(1);
	}
294 295 296
	else {
	    return SHOWFREENODES();
	}
297 298 299 300
    }
    else {
	return SHOWSTATS();
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
301
}
302 303 304 305 306 307 308 309 310 311 312

#
# 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
313 314
     
#
315
# If user is anonymous, show experiment stats, otherwise useful info.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
316
# 
317
if ($this_user) {
318 319
    sajax_init();
    sajax_export("FreeNodeHtml");
320 321 322 323 324 325 326 327

    # 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';
    }
328 329 330 331 332 333 334 335
    sajax_handle_client_request();

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

    ?>
    function FreeNodeHtml_CB(stuff) {
336
	getObjbyName('usagefreenodes').innerHTML = stuff;
337
	setTimeout('GetFreeNodeHtml()', 60000);
338 339 340
    }
    function GetFreeNodeHtml() {
	x_FreeNodeHtml(FreeNodeHtml_CB);
341
    }
342 343
    setTimeout('GetFreeNodeHtml()', 60000);
    
344 345 346
    <?php
    echo "</script>\n";
	  
347 348
    echo "<div id=usagefreenodes>\n";
    echo   ShowStatus();
349 350
    echo "</div>\n";
    echo "</body></html>";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
351 352
}
else {
353 354 355 356 357
    PAGEBEGINNING("Current Usage", 1, 1);
    echo "<div id=usage>\n";
    echo   SHOWSTATS();
    echo "</div>\n";
    echo "</body></html>";
358
}
Mike Hibler's avatar
Mike Hibler committed
359
?>