All new accounts created on Gitlab now require administrator approval. If you invite any collaborators, please let Flux staff know so they can approve the accounts.

powertime.php3 2.78 KB
Newer Older
Timothy Stack's avatar
 
Timothy Stack committed
1 2 3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2005, 2006, 2007 University of Utah and the Flux Group.
Timothy Stack's avatar
 
Timothy Stack committed
5 6 7
# All rights reserved.
#
include("defs.php3");
8
include_once("node_defs.php");
Timothy Stack's avatar
 
Timothy Stack committed
9 10 11 12

#
# Only known and logged in admins can update last_power times.
#
13 14 15
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
Timothy Stack's avatar
 
Timothy Stack committed
16

17
if (!$isadmin && !STUDLY()) {
Timothy Stack's avatar
 
Timothy Stack committed
18 19 20 21
    USERERROR("Not enough permission.", 1);
}

#
22 23
# Verify page arguments. Note that "node_id" appears to be a comma separated
# list of nodes, while "nodes" is an array passed by the form.
Timothy Stack's avatar
 
Timothy Stack committed
24
#
25
$optargs = OptionalPageArguments("node_id",   PAGEARG_STRING,
26
				 "nodes",     PAGEARG_ARRAY,
27 28 29
				 "poweron",   PAGEARG_STRING,
				 "confirmed", PAGEARG_STRING);

Timothy Stack's avatar
 
Timothy Stack committed
30 31 32 33 34 35
if ((!isset($node_id) || strcmp($node_id, "") == 0) && !isset($nodes)) {
    USERERROR("You must provide a node ID.", 1);
}

$body_str = "<center>";

36
if (isset($confirmed)) {
Timothy Stack's avatar
 
Timothy Stack committed
37 38 39 40 41
    $body_str .= "Updated power time for:<br><br>";
    foreach ($nodes as $ni) {
	if (!TBvalid_node_id($ni)) {
	    USERERROR("Invalid node ID: $ni", 1);
	}
42
	if (! ($node = Node::Lookup($ni))) {
Timothy Stack's avatar
 
Timothy Stack committed
43 44
	    USERERROR("Invalid node ID: $ni", 1);
	}
Timothy Stack's avatar
 
Timothy Stack committed
45 46 47 48 49 50 51 52 53 54 55 56

	if (isset($poweron) && $poweron == "Yep") {
		DBQueryFatal("update outlets " .
			     "set last_power=CURRENT_TIMESTAMP " .
			     "where node_id='$ni'");
	}
	if (!isset($poweron) || $poweron != "Yep") {
		DBQueryFatal("update nodes " .
			     "set eventstate='POWEROFF'," .
			     "state_timestamp=unix_timestamp(NOW()) " .
			     "where node_id='$ni'");
	}
Timothy Stack's avatar
Timothy Stack committed
57
	$body_str .= "<b>$ni</b><br>";
Timothy Stack's avatar
 
Timothy Stack committed
58 59 60 61 62
    }
}
else {
    $body_str .= "Update last power time for:<br>";
    $body_str .= "<form action='powertime.php3' method=get><br><table>";
63
    $body_str .= "<tr><th>Update?</th><th>Node ID</th><th>Last Power</th></tr>";
Timothy Stack's avatar
 
Timothy Stack committed
64 65 66 67
    foreach (split(",", $node_id) as $ni) {
	if (!TBvalid_node_id($ni)) {
	    USERERROR("Invalid node ID: $ni", 1);
	}
68
	if (! ($node = Node::Lookup($ni))) {
Timothy Stack's avatar
 
Timothy Stack committed
69 70 71
	    USERERROR("Invalid node ID: $ni", 1);
	}
	
72 73 74 75 76 77 78
	$query_result =
		DBQueryFatal("SELECT (UNIX_TIMESTAMP(NOW()) - ".
			     "UNIX_TIMESTAMP(last_power)) ".
			     "FROM outlets WHERE node_id='$ni'");
	$row = mysql_fetch_array($query_result);
	$last_power_min = floor($row[0] / 60);
	
Timothy Stack's avatar
 
Timothy Stack committed
79 80 81 82 83 84 85 86
	$body_str .= "<tr>
		      <td class=pad4 align=center>
		        <input type=checkbox
			       name=\"nodes[]\"
			       value=\"$ni\"
			       checked>
		      </td>
		      <td class=pad4>$ni</td>
87
		      <td class=pad4><b>$last_power_min</b> minutes ago</td>
Timothy Stack's avatar
 
Timothy Stack committed
88 89 90
		     </tr>";
    }
    $body_str .= "</table><br>";
Timothy Stack's avatar
 
Timothy Stack committed
91 92
    $body_str .= "<input type=checkbox name=poweron value=Yep checked>Power On";
    $body_str .= "<br><input type=submit name=confirmed value=Confirm></form>";
Timothy Stack's avatar
 
Timothy Stack committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
}

$body_str .= "</center>";

#
# Standard Testbed Header
#
PAGEHEADER("Update Power Time");

echo "$body_str";

#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>