powertime.php3 3.52 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2005-2012 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
#
include("defs.php3");
25
include_once("node_defs.php");
26 27 28 29

#
# Only known and logged in admins can update last_power times.
#
30 31 32
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
33

34
if (!$isadmin && !STUDLY()) {
35 36 37 38
    USERERROR("Not enough permission.", 1);
}

#
39 40
# 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.
41
#
42
$optargs = OptionalPageArguments("node_id",   PAGEARG_STRING,
43
				 "nodes",     PAGEARG_ARRAY,
44 45 46
				 "poweron",   PAGEARG_STRING,
				 "confirmed", PAGEARG_STRING);

47 48 49 50 51 52
if ((!isset($node_id) || strcmp($node_id, "") == 0) && !isset($nodes)) {
    USERERROR("You must provide a node ID.", 1);
}

$body_str = "<center>";

53
if (isset($confirmed)) {
54 55 56
    $body_str .= "Updated power time for:<br><br>";
    foreach ($nodes as $ni) {
	if (!TBvalid_node_id($ni)) {
57
	    $ni = CleanString($ni);
58 59
	    USERERROR("Invalid node ID: $ni", 1);
	}
60
	if (! ($node = Node::Lookup($ni))) {
61 62
	    USERERROR("Invalid node ID: $ni", 1);
	}
63 64 65 66 67 68 69 70 71 72 73 74

	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
75
	$body_str .= "<b>$ni</b><br>";
76 77 78 79 80
    }
}
else {
    $body_str .= "Update last power time for:<br>";
    $body_str .= "<form action='powertime.php3' method=get><br><table>";
81
    $body_str .= "<tr><th>Update?</th><th>Node ID</th><th>Last Power</th></tr>";
82
    foreach (preg_split("/,/", $node_id) as $ni) {
83
	if (!TBvalid_node_id($ni)) {
84
	    $ni = CleanString($ni);
85 86
	    USERERROR("Invalid node ID: $ni", 1);
	}
87
	if (! ($node = Node::Lookup($ni))) {
88 89 90
	    USERERROR("Invalid node ID: $ni", 1);
	}
	
91 92 93 94 95 96 97
	$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);
	
98 99 100 101 102 103 104 105
	$body_str .= "<tr>
		      <td class=pad4 align=center>
		        <input type=checkbox
			       name=\"nodes[]\"
			       value=\"$ni\"
			       checked>
		      </td>
		      <td class=pad4>$ni</td>
106
		      <td class=pad4><b>$last_power_min</b> minutes ago</td>
107 108 109
		     </tr>";
    }
    $body_str .= "</table><br>";
110 111
    $body_str .= "<input type=checkbox name=poweron value=Yep checked>Power On";
    $body_str .= "<br><input type=submit name=confirmed value=Confirm></form>";
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
}

$body_str .= "</center>";

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

echo "$body_str";

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