\n";
printComputerInfo($computers[$compid]["IPaddress"],
$computers[$compid]["stateid"],
$computers[$compid]["owner"],
$computers[$compid]["platformid"],
$computers[$compid]["scheduleid"],
$computers[$compid]["currentimgid"],
$computers[$compid]["ram"],
$computers[$compid]["procnumber"],
$computers[$compid]["procspeed"],
$computers[$compid]["network"],
$computers[$compid]["hostname"],
$compid,
$computers[$compid]["type"],
$computers[$compid]["provisioningid"]);
print "
\n";
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn submitDeleteComputer()
///
/// \brief deletes a computer from the database and notifies the user
///
////////////////////////////////////////////////////////////////////////////////
function submitDeleteComputer() {
$compid = getContinuationVar("compid");
$deleted = getContinuationVar("deleted");
if($deleted) {
$query = "UPDATE computer "
. "SET deleted = 0 "
. "WHERE id = $compid";
$qh = doQuery($query, 190);
}
else {
$query = "UPDATE computer "
. "SET deleted = 1 "
. "WHERE id = $compid";
$qh = doQuery($query, 191);
}
$_SESSION['userresources'] = array();
viewComputers();
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn bulkAddComputer()
///
/// \brief prints a form for adding a block of computers
///
////////////////////////////////////////////////////////////////////////////////
function bulkAddComputer() {
global $submitErr;
$data = processBulkComputerInput(0);
$data2 = processComputerInput2(); //yes, this is somewhat redundant, but it
// makes things easier later
$states = array("2" => "available",
"10" => "maintenance",
"20" => "vmhostinuse");
$platforms = getPlatforms();
$tmp = getUserResources(array("scheduleAdmin"), array("manageGroup"));
$schedules = $tmp["schedule"];
$images = getImages();
$provisioning = getProvisioning();
print "
\n";
print "
\n";
print "NOTE : 'Start IP' and 'End IP' can only differ in the number ";
print "after the last '.'. The hostnames will be generated from the ";
print "'Hostname' field. The hostnames for each computer can only differ ";
print "by the value of a number in the first part of the hostname. Place ";
print "a '%' character in the 'Hostname' field where that number will be. ";
print "Then fill in 'Start value' and 'End value' with the first and last ";
print "values to be used in the hostname. ";
print "Required text fields are noted with * \n";
print "
\n";
print "
\n";
print "\n";
if($count > 1) {
print " \n";
print " \n";
$cdata = array('platforms' => $platforminput,
'schedules' => $scheduleinput,
'groups' => $groups,
'compidlist' => $compidlist);
# set a short timeout because this is a "last one in wins" page
$cont = addContinuationsEntry('submitComputerGroups', $cdata, 300, 0, 0, 1);
print " \n";
}
print " \n";
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn submitComputerGroups()
///
/// \brief updates computer groupings and notifies user
///
////////////////////////////////////////////////////////////////////////////////
function submitComputerGroups() {
$platforminput = getContinuationVar("platforms");
$scheduleinput = getContinuationVar("schedules");
$groups = getContinuationVar("groups");
$compidlist = getContinuationVar('compidlist');
$groupinput = processInputVar("computergroup", ARG_MULTINUMERIC);
if(empty($groups))
$bygroups = 0;
else
$bygroups = 1;
$computers = getComputers();
# build an array of memberships currently in the db
$tmp = getUserResources(array("groupAdmin"), array("manageGroup"), 1);
$computergroupsIDs = array_keys($tmp["computer"]); // ids of groups that user can manage
$resources = getUserResources(array("computerAdmin"),
array("manageGroup"));
$userCompIDs = array_keys($resources["computer"]); // ids of computers that user can manage
$computermembership = getResourceGroupMemberships("computer");
$basecomputergroups = $computermembership["computer"]; // all computer group memberships
$computergroups = array();
foreach(array_keys($basecomputergroups) as $compid) {
if(in_array($compid, $userCompIDs)) {
foreach($basecomputergroups[$compid] as $grpid) {
if($bygroups && ! in_array($grpid, $groups))
continue;
if(in_array($grpid, $computergroupsIDs)) {
if(array_key_exists($compid, $computergroups))
array_push($computergroups[$compid], $grpid);
else
$computergroups[$compid] = array($grpid);
}
}
}
}
$newmembers = array();
foreach(array_keys($groupinput) as $key) {
list($compid, $grpid) = explode(':', $key);
if(array_key_exists($compid, $newmembers)) {
array_push($newmembers[$compid], $grpid);
}
else {
$newmembers[$compid] = array($grpid);
}
}
$adds = array();
$removes = array();
foreach(array_keys($computers) as $compid) {
if($bygroups) {
if(! array_key_exists($compid, $compidlist))
continue;
}
else {
if(! in_array($computers[$compid]["platformid"], $platforminput) ||
! in_array($computers[$compid]["scheduleid"], $scheduleinput))
continue;
}
if(! array_key_exists($compid, $newmembers) &&
! array_key_exists($compid, $computergroups)) {
continue;
}
$id = $computers[$compid]["resourceid"];
// check that $compid is in $newmembers, if not, remove it from all groups
if(! array_key_exists($compid, $newmembers)) {
$removes[$id] = $computergroups[$compid];
continue;
}
// check that $compid is in $computergroups, if not, add all groups
// in $newmembers
if(! array_key_exists($compid, $computergroups)) {
$adds[$id] = $newmembers[$compid];
continue;
}
// adds are groupids that are in $newmembers, but not in $computergroups
$adds[$id] = array_diff($newmembers[$compid], $computergroups[$compid]);
if(count($adds[$id]) == 0) {
unset($adds[$id]);
}
// removes are groupids that are in $computergroups, but not in
// $newmembers
$removes[$id] = array_diff($computergroups[$compid], $newmembers[$compid]);
if(count($removes[$id]) == 0) {
unset($removes[$id]);
}
}
foreach(array_keys($adds) as $compid) {
foreach($adds[$compid] as $grpid) {
$query = "INSERT INTO resourcegroupmembers "
. "(resourceid, resourcegroupid) "
. "VALUES ($compid, $grpid)";
doQuery($query, 285);
}
}
foreach(array_keys($removes) as $compid) {
foreach($removes[$compid] as $grpid) {
$query = "DELETE FROM resourcegroupmembers "
. "WHERE resourceid = $compid AND "
. "resourcegroupid = $grpid";
doQuery($query, 286);
}
}
viewComputerGroups();
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn computerUtilities()
///
/// \brief prints a page for selecting multiple computers and then choosing from
/// a few utility type functions to operate on the selected computers
///
////////////////////////////////////////////////////////////////////////////////
function computerUtilities() {
global $user, $mode;
$data = processComputerInput(0);
if(empty($data['groups']))
$bygroups = 0;
else {
$bygroups = 1;
$compidlist = getCompIdList($data['groups']);
}
$computers = getComputers(1);
$resources = getUserResources(array("computerAdmin"), array("administer"));
$userCompIDs = array_keys($resources["computer"]);
$resources = getUserResources(array("imageAdmin", "imageCheckOut"));
$platforms = getPlatforms();
$tmp = getUserResources(array("scheduleAdmin"), array("manageGroup"));
$schedules = $tmp["schedule"];
$allschedules = getSchedules();
$images = getImages(1);
print "
Computer Utilities \n";
print "
\n";
print "\n";
if($count == 0) {
print " 0 computers found \n";
return;
}
print "Check All / \n";
print "Uncheck All \n";
print "\n";
if(count($resources['image'])) {
print " \n";
print " Reload selected computers with this image: ";
print " \n";
printSelectInput("imageid", $resources["image"], -1, 1);
print " \n";
print " ";
$cont = addContinuationsEntry('reloadComputers', array(), SECINDAY, 0);
print " \n";
print " \n";
}
print " \n";
print " Change state of or delete selected computers: ";
$states = array("2" => "available",
"23" => "hpc",
"10" => "maintenance",
"20" => "convert to vmhostinuse",
"999" => "DELETE");
print " \n";
printSelectInput("stateid", $states);
print " ";
print " \n";
$cont = addContinuationsEntry('compStateChange', array(), SECINDAY, 0);
print " \n";
print " \n";
if(count($schedules)) {
print " \n";
print " Change schedule of selected computers to: ";
uasort($schedules, "sortKeepIndex");
print " \n";
printSelectInput("scheduleid", $schedules);
print " ";
print " \n";
$cont = addContinuationsEntry('compScheduleChange', array(), SECINDAY, 0);
print " \n";
print " \n";
}
print "
\n";
print " \n";
print " \n";
print "
$count computers found
\n";
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn reloadComputers()
///
/// \brief confirms reloading submitted computers with the selected image
///
////////////////////////////////////////////////////////////////////////////////
function reloadComputers() {
global $user;
$data = processComputerInput3();
$computers = getComputers(1);
$imagedata = getImages(0, $data['imageid']);
$reloadnow = array();
$reloadasap = array();
$noreload = array();
foreach($data['computerids'] as $compid) {
switch($computers[$compid]['state']) {
case "available":
case "failed":
case "reloading":
array_push($reloadnow, $compid);
break;
case "inuse":
case "timeout":
case "reserved":
array_push($reloadasap, $compid);
break;
case "maintenance":
array_push($noreload, $compid);
break;
default:
array_push($noreload, $compid);
break;
}
}
print "
Reload Computers \n";
if(count($reloadnow)) {
print "The following computers will be immediately reloaded with ";
print "{$imagedata[$data['imageid']]['prettyname']}:
\n";
print "
\n";
foreach($reloadnow as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
print "
\n";
}
if(count($reloadasap)) {
print "The following computers are currently in use and will be ";
print "reloaded with {$imagedata[$data['imageid']]['prettyname']} at the end ";
print "of the user's reservation:
\n";
print "
\n";
foreach($reloadasap as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
print "
\n";
}
print "
\n";
print " \n";
$data['imagename'] = $imagedata[$data['imageid']]['prettyname'];
$cont = addContinuationsEntry('submitReloadComputers', $data, SECINDAY, 0, 0);
print " \n";
print " \n";
if(count($noreload)) {
print "The following computers are currently in the maintenance ";
print "state and therefore will have nothing done to them:
\n";
print "
\n";
foreach($noreload as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
print "
\n";
}
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn submitReloadComputers()
///
/// \brief configures system to reloaded submitted computers with submitted
/// image
///
////////////////////////////////////////////////////////////////////////////////
function submitReloadComputers() {
$data = getContinuationVar();
$start = getReloadStartTime();
$end = $start + 1200; // + 20 minutes
$startstamp = unixToDatetime($start);
$endstamp = unixToDatetime($end);
$imagerevisionid = getProductionRevisionid($data['imageid']);
// get semaphore lock
if(! semLock())
abort(3);
$computers = getComputers(1);
$reloadnow = array();
$reloadasap = array();
foreach($data['computerids'] as $compid) {
switch($computers[$compid]['state']) {
case "available":
case "failed":
array_push($reloadnow, $compid);
break;
case "reload":
case "reloading":
case "inuse":
case "timeout":
case "reserved":
array_push($reloadasap, $compid);
break;
}
}
$vclreloadid = getUserlistID('vclreload@Local');
$fails = array();
$passes = array();
foreach($reloadnow as $compid) {
if(simpleAddRequest($compid, $data['imageid'], $imagerevisionid, $startstamp, $endstamp, 19, $vclreloadid))
$passes[] = $compid;
else
$fails[] = $compid;
}
// release semaphore lock
semUnlock();
if(count($reloadasap)) {
$compids = implode(',', $reloadasap);
$query = "UPDATE computer "
. "SET nextimageid = {$data['imageid']} "
. "WHERE id IN ($compids)";
doQuery($query, 101);
}
print "
Reload Computers \n";
if(count($passes)) {
print "The following computers are being immediately reloaded with ";
print "{$data['imagename']}:
\n";
foreach($passes as $compid)
print "
{$computers[$compid]['hostname']} \n";
}
if(count($reloadasap)) {
if(count($passes))
print "
";
print "The following computers will be reloaded with ";
print "{$data['imagename']} after their current reservations are over:
\n";
foreach($reloadasap as $compid)
print "
{$computers[$compid]['hostname']} \n";
}
if(count($fails)) {
if(count($passes) || count($reloadasap))
print "
";
print "No functional management node was found for the following ";
print "computers. They could not be reloaded at this time:
\n";
foreach($fails as $compid)
print "
{$computers[$compid]['hostname']} \n";
}
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn compStateChange()
///
/// \brief confirms changing computers to selected state, and if new state is
/// maintenance, asks for reason
///
////////////////////////////////////////////////////////////////////////////////
function compStateChange() {
global $submitErr;
print "
Change State of Computers \n";
$data = processComputerInput3();
$computers = getComputers(1);
if($data['stateid'] == 10) {
$notes = explode('@', $data['notes']);
if(count($notes) != 2)
$notes[1] = "";
}
if($data['stateid'] == 2) {
print "You are about to place the following computers into the ";
print "available state:\n";
print "
\n";
}
elseif($data['stateid'] == 10) {
print "Please enter a reason you are changing the following computers to ";
print "the maintenance state: \n";
print " \n";
print "{$notes[1]} \n";
print " Selected computers:\n";
}
elseif($data['stateid'] == 20) {
$profiles = getVMProfiles();
$data['profiles'] = $profiles;
print "Select a VM Host Profile and then click Submit \n";
print "to place the computers into the vmhostinuse state: \n";
print " \n";
print "\n";
foreach($profiles as $id => $profile)
print "{$profile['profilename']} \n";
print " \n";
print " \n";
}
elseif($data['stateid'] == 23) {
print "You are about to place the following computers into the ";
print "hpc state:\n";
print " \n";
}
elseif($data['stateid'] == 999) {
print "You are about to DELETE the following computers";
print " \n";
}
$cont = addContinuationsEntry('submitCompStateChange', $data, SECINDAY, 0, 0);
print " \n";
print "\n";
foreach($data['computerids'] as $compid) {
print " \n";
#print " {$computers[$compid]['hostname']} \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
print " \n";
print " \n";
print " \n";
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn submitCompStateChange()
///
/// \brief configures system to put submitted computers in submitted state
///
////////////////////////////////////////////////////////////////////////////////
function submitCompStateChange() {
global $user;
print "
Change State of Computers \n";
$data = getContinuationVar();
$computers = getComputers(1);
# switching to available
if($data['stateid'] == 2) {
$compids = implode(',', $data['computerids']);
$query = "UPDATE computer "
. "SET stateid = 2, "
. "notes = '' "
. "WHERE id IN ($compids)";
doQuery($query, 101);
print "The following computers were changed to the available state:\n";
print "
\n";
foreach($data['computerids'] as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
}
# switching to maintenance
elseif($data['stateid'] == 10) {
$data['notes'] = processInputVar('notes', ARG_STRING);
if(get_magic_quotes_gpc())
$data['notes'] = stripslashes($data['notes']);
$data['notes'] = mysql_real_escape_string($data['notes']);
$data["notes"] = $user["unityid"] . " " . unixToDatetime(time()) . "@"
. $data["notes"];
$vclreloadid = getUserlistID('vclreload@Local');
$imageid = getImageId('noimage');
$imagerevisionid = getProductionRevisionid($imageid);
$noaction = array();
$changenow = array();
$changeasap = array();
$changetimes = array();
$changetimes2 = array();
foreach($data['computerids'] as $compid) {
if($computers[$compid]['state'] == 'maintenance')
array_push($noaction, $compid);
else
array_push($changeasap, $compid);
}
$passes = array();
$fails = array();
$blockallocs = array();
$blockfails = array();
// get semaphore lock
if(! semLock())
abort(3);
foreach($changeasap as $compid) {
# try to move future reservations off of computer
moveReservationsOffComputer($compid);
# get end time of last reservation
$query = "SELECT rq.end "
. "FROM request rq, "
. "reservation rs "
. "WHERE rs.requestid = rq.id AND "
. "rs.computerid = $compid AND "
. "rq.stateid NOT IN (1,5,12) "
. "ORDER BY end DESC "
. "LIMIT 1";
$qh = doQuery($query, 101);
$query2 = "SELECT t.end "
. "FROM blockComputers c, "
. "blockTimes t "
. "WHERE c.computerid = $compid AND "
. "c.blockTimeid = t.id AND "
. "t.end > NOW() "
. "ORDER BY t.end DESC "
. "LIMIT 1";
$qh2 = doQuery($query2, 101);
# create a really long reservation starting at that time in state tomaintenance
if($row = mysql_fetch_assoc($qh)) {
$start = $row['end'];
$changetimes[$compid] = $start;
$end = datetimeToUnix($start) + SECINWEEK; // hopefully keep future reservations off of it
$end = unixToDatetime($end);
if(simpleAddRequest($compid, $imageid, $imagerevisionid, $start, $end, 18, $vclreloadid))
$passes[] = $compid;
else
$fails[] = $compid;
}
// if part of a block allocation, create a really long reservation
# starting at the block allocation end time in state tomaintenance
elseif($row = mysql_fetch_assoc($qh2)) {
$start = $row['end'];
$changetimes2[$compid] = $start;
$end = datetimeToUnix($start) + SECINWEEK; // hopefully keep future reservations off of it
$end = unixToDatetime($end);
if(simpleAddRequest($compid, $imageid, $imagerevisionid, $start, $end, 18, $vclreloadid))
$blockallocs[] = $compid;
else
$blockfails[] = $compid;
}
# change to maintenance state and save in $changenow
// if we wait and put them all in maintenance at the same time,
# we may end up moving reservations to the computer later in the
# loop
else {
$query = "UPDATE computer "
. "SET stateid = 10, "
. "notes = '{$data['notes']}' "
. "WHERE id = $compid";
doQuery($query, 101);
unset_by_val($compid, $changeasap);
array_push($changenow, $compid);
}
}
// release semaphore lock
semUnlock();
if(count($noaction) || count($changeasap)) {
$comparr = array_merge($noaction, $changeasap);
$compids = implode(',', $comparr);
$query = "UPDATE computer "
. "SET notes = '{$data['notes']}' "
. "WHERE id IN ($compids)";
doQuery($query, 101);
}
if(count($changenow)) {
print "The following computers were immediately placed into the ";
print "maintenance state:\n";
print "
\n";
foreach($changenow as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
print "
\n";
}
if(count($passes)) {
print "The following computers currently have reservations on them ";
print "and will be placed in the maintenance state at the time listed ";
print "for each computer:\n";
print "
\n";
print " \n";
print " Computer \n";
print " Maintenance time \n";
print " \n";
foreach($passes as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
$time = date('n/j/y g:i a', datetimeToUnix($changetimes[$compid]));
print " $time \n";
print " \n";
}
print "
\n";
print "
\n";
}
if(count($fails)) {
print "The following computers currently have reservations on them ";
print "but no functional management node was found for them. Nothing will ";
print "be done with them at this time:\n";
print "
\n";
foreach($fails as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
print "
\n";
}
if(count($blockallocs)) {
print "The following computers are part of an upcoming block allocation ";
print "and will be placed in the maintenance state at the time listed ";
print "for each computer:\n";
print "
\n";
print " \n";
print " Computer \n";
print " Maintenance time \n";
print " \n";
foreach($blockallocs as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
$time = date('n/j/y g:i a', datetimeToUnix($changetimes2[$compid]));
print " $time \n";
print " \n";
}
print "
\n";
print "
\n";
}
if(count($blockfails)) {
print "The following computers currently have reservations on them ";
print "but no functional management node was found for them. Nothing will ";
print "be done with them at this time:\n";
print "
\n";
foreach($blockfails as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
print "
\n";
}
if(count($noaction)) {
print "The following computers were already in the maintenance ";
print "state and had their notes on being in the maintenance state ";
print "updated:\n";
print "
\n";
foreach($noaction as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
print "
\n";
}
}
# switching to vmhostinuse
elseif($data['stateid'] == 20) {
$profileid = processInputVar('profileid', ARG_NUMERIC);
if(! array_key_exists($profileid, $data['profiles'])) {
$keys = array_keys($data['profiles']);
$profileid = $keys[0];
}
$vclreloadid = getUserlistID('vclreload@Local');
$imagerevisionid = getProductionRevisionid($data['profiles'][$profileid]['imageid']);
$noaction = array();
$changenow = array();
$changeasap = array();
$blockalloc = array();
$changetimes = array();
$fails = array();
foreach($data['computerids'] as $compid) {
if($computers[$compid]['state'] == 'vmhostinuse')
array_push($noaction, $compid);
else
array_push($changeasap, $compid);
}
// get semaphore lock
if(! semLock())
abort(3);
foreach($changeasap as $compid) {
moveReservationsOffComputer($compid);
# get end time of last reservation
$query = "SELECT rq.end "
. "FROM request rq, "
. "reservation rs "
. "WHERE rs.requestid = rq.id AND "
. "rs.computerid = $compid AND "
. "rq.stateid NOT IN (1,5,12) "
. "ORDER BY end DESC "
. "LIMIT 1";
$qh = doQuery($query, 101);
$query2 = "SELECT c.computerid "
. "FROM blockComputers c, "
. "blockTimes t "
. "WHERE c.computerid = $compid AND "
. "c.blockTimeid = t.id AND "
. "t.end > NOW()";
$qh2 = doQuery($query2, 101);
if($row = mysql_fetch_assoc($qh)) {
// if there is a reservation, leave in $changeasap so we can
# notify that we can't change this one
}
// if computer allocated to block allocation remove from $changeasap
# and add to $blockalloc
elseif($row = mysql_fetch_assoc($qh2)) {
unset_by_val($compid, $changeasap);
$blockalloc[] = $compid;
}
# change to vmhostinuse state and save in $changenow
// if we wait and put them all in vmhostinuse at the same time,
# we may end up moving reservations to the computer later in the
# loop
else {
# create a reload reservation to load machine with image
# corresponding to selected vm profile
$start = getReloadStartTime();
$end = $start + SECINYEAR; # don't want anyone making a future reservation for this machine
$start = unixToDatetime($start);
$end = unixToDatetime($end);
if(simpleAddRequest($compid, $data['profiles'][$profileid]['imageid'],
$imagerevisionid, $start, $end, 21, $vclreloadid)) {
$changenow[] = $compid;
}
else
$fails[] = $compid;
unset_by_val($compid, $changeasap);
# check for existing vmhost entry
$query = "SELECT id, "
. "vmprofileid "
. "FROM vmhost "
. "WHERE computerid = $compid";
$qh = doQuery($query, 101);
if($row = mysql_fetch_assoc($qh)) {
if($row['vmprofileid'] != $profileid) {
# update vmprofile
$query = "UPDATE vmhost "
. "SET vmprofileid = $profileid "
. "WHERE id = {$row['id']}";
doQuery($query, 101);
}
}
else {
# create vmhost entry
$query = "INSERT INTO vmhost "
. "(computerid, "
. "vmlimit, "
. "vmprofileid) "
. "VALUES ($compid, "
. "2, "
. "$profileid)";
doQuery($query, 101);
}
}
}
// release semaphore lock
semUnlock();
if(count($changenow)) {
print "The following computers were placed into the ";
print "vmhostinuse state:\n";
print "
\n";
foreach($changenow as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
print "
\n";
}
if(count($fails)) {
print "The following computers had no reservations on them but no ";
print "functional management node was found to reload them with the ";
print "VM host image. Nothing will be done with them at this time:\n";
print "
\n";
foreach($fails as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
print "
\n";
}
if(count($changeasap)) {
print "The following computers currently have reservations on them ";
print "and cannot be placed in the vmhostinuse state at this time:\n";
print "
\n";
foreach($changeasap as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
print "
\n";
}
if(count($blockalloc)) {
print "The following computers are part of an upcoming block allocation ";
print "and cannot be placed in the vmhostinuse state at this time:\n";
print "
\n";
foreach($blockalloc as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
print "
\n";
}
if(count($noaction)) {
print "The following computers were already in the vmhostinuse ";
print "state:\n";
print "
\n";
foreach($noaction as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
print "
\n";
}
}
# switching to hpc
elseif($data['stateid'] == 23) {
$noaction = array();
$changenow = array();
$changeasap = array();
$blockalloc = array();
$changetimes = array();
foreach($data['computerids'] as $compid) {
if($computers[$compid]['state'] == 'hpc')
array_push($noaction, $compid);
else
array_push($changeasap, $compid);
}
if(! semLock())
abort(3);
foreach($changeasap as $compid) {
moveReservationsOffComputer($compid);
# get end time of last reservation
$query = "SELECT rq.end "
. "FROM request rq, "
. "reservation rs "
. "WHERE rs.requestid = rq.id AND "
. "rs.computerid = $compid AND "
. "rq.stateid NOT IN (1,5,12) "
. "ORDER BY end DESC "
. "LIMIT 1";
$qh = doQuery($query, 101);
$query2 = "SELECT c.computerid "
. "FROM blockComputers c, "
. "blockTimes t "
. "WHERE c.computerid = $compid AND "
. "c.blockTimeid = t.id AND "
. "t.end > NOW()";
$qh2 = doQuery($query2, 101);
if($row = mysql_fetch_assoc($qh)) {
// if there is a reservation, leave in $changeasap so we can
# notify that we can't change this one
}
// if computer allocated to block allocation remove from $changeasap
# and add to $blockalloc
elseif($row = mysql_fetch_assoc($qh2)) {
unset_by_val($compid, $changeasap);
$blockalloc[] = $compid;
}
# change to hpc state and save in $changenow
// if we wait and put them all in hpc at the same time,
# we may end up moving reservations to the computer later in the
# loop
else {
$query = "UPDATE computer "
. "SET stateid = 23 "
. "WHERE id = $compid";
doQuery($query, 101);
unset_by_val($compid, $changeasap);
array_push($changenow, $compid);
}
}
// release semaphore lock
semUnlock();
if(count($changenow)) {
print "The following computers were placed into the ";
print "hpc state:\n";
print "
\n";
foreach($changenow as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
print "
\n";
}
if(count($changeasap)) {
print "The following computers currently have reservations on them ";
print "and cannot be placed in the hpc state at this time:\n";
print "
\n";
foreach($changeasap as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
print "
\n";
}
if(count($blockalloc)) {
print "The following computers are part of an upcoming block allocation ";
print "and cannot be placed in the hpc state at this time:\n";
print "
\n";
foreach($blockalloc as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
print "
\n";
}
if(count($noaction)) {
print "The following computers were already in the hpc ";
print "state:\n";
print "
\n";
foreach($noaction as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
print "
\n";
}
}
elseif($data['stateid'] == 999) {
$compids = implode(',', $data['computerids']);
$query = "UPDATE computer "
. "SET deleted = 1, "
. "notes = '' "
. "WHERE id IN ($compids)";
doQuery($query, 999);
print "The following computers were deleted:\n";
print "
\n";
foreach($data['computerids'] as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
}
else
abort(50);
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn compScheduleChange()
///
/// \brief confirms changing computers to selected schedule
///
////////////////////////////////////////////////////////////////////////////////
function compScheduleChange() {
global $submitErr;
print "
Change Schedule of Computers \n";
$data = processComputerInput3();
$computers = getComputers(1);
$schedules = getSchedules();
print "You are about to place the following computer(s) into schedule ";
print "
{$schedules[$data['scheduleid']]['name']} ";
print ".
Note: ";
print "This will not affect reservations currently on the computer(s). It ";
print "will only affect new reservations made on the computer(s).
\n";
print "
\n";
$cdata = array('scheduleid' => $data['scheduleid'],
'schedule' => $schedules[$data['scheduleid']]['name'],
'computerids' => $data['computerids']);
$cont = addContinuationsEntry('submitCompScheduleChange', $cdata, SECINDAY, 1, 0);
print " \n";
print "\n";
foreach($data['computerids'] as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
print " \n";
print " \n";
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn submitCompScheduleChange()
///
/// \brief configures system to put submitted computers in submitted state
///
////////////////////////////////////////////////////////////////////////////////
function submitCompScheduleChange() {
global $user;
print "
Change Schedule of Computers \n";
$data = getContinuationVar();
$computers = getComputers(1);
$compids = implode(',', $data['computerids']);
$query = "UPDATE computer "
. "SET scheduleid = {$data['scheduleid']} "
. "WHERE id IN ($compids)";
doQuery($query, 101);
print "The schedule for the following computer(s) was set to ";
print "{$data['schedule']}:
\n";
print "
\n";
foreach($data['computerids'] as $compid) {
print " \n";
print " {$computers[$compid]['hostname']} \n";
print " \n";
}
print "
\n";
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn processComputerInput($checks)
///
/// \param $checks - (optional) 1 to perform validation, 0 not to
///
/// \return an array with the following keys:\n
/// bulk, ipaddress, stateid, platformid, scheduleid, currentimgid,
/// ram, numprocs, procspeed, network, hostname, compid, type
///
/// \brief validates input from the previous form; if anything was improperly
/// submitted, sets submitErr and submitErrMsg
///
////////////////////////////////////////////////////////////////////////////////
function processComputerInput($checks=1) {
global $submitErr, $submitErrMsg, $mode;
$return = processComputerInput2();
$return["bulk"] = getContinuationVar("bulk", processInputVar("bulk", ARG_NUMERIC));
$return["ipaddress"] = getContinuationVar("ipaddress", processInputVar("ipaddress", ARG_STRING));
$return["stateid"] = getContinuationVar("stateid", processInputVar("stateid", ARG_NUMERIC));
$return["owner"] = getContinuationVar("owner", processInputVar("owner", ARG_STRING));
$return["platformid"] = getContinuationVar("platformid", processInputVar("platformid", ARG_NUMERIC));
$return["scheduleid"] = getContinuationVar("scheduleid", processInputVar("scheduleid", ARG_NUMERIC));
$return["currentimgid"] = getContinuationVar("currentimgid", processInputVar("currentimgid", ARG_NUMERIC));
$return["ram"] = getContinuationVar("ram", processInputVar("ram", ARG_NUMERIC));
$return["numprocs"] = getContinuationVar("numprocs", processInputVar("numprocs", ARG_NUMERIC));
$return["procspeed"] = getContinuationVar("procspeed", processInputVar("procspeed", ARG_NUMERIC));
$return["network"] = getContinuationVar("network", processInputVar("network", ARG_NUMERIC));
$return["hostname"] = getContinuationVar("hostname", processInputVar("hostname", ARG_STRING));
$return["compid"] = getContinuationVar("compid", processInputVar("compid", ARG_NUMERIC));
$return["type"] = getContinuationVar("type", processInputVar("type", ARG_STRING, "lab"));
$return["provisioningid"] = getContinuationVar("provisioningid", processInputVar("provisioningid", ARG_NUMERIC));
$return["notes"] = getContinuationVar("notes", processInputVar("notes", ARG_STRING));
$return["computergroup"] = getContinuationVar("computergroup", processInputVar("computergroup", ARG_MULTINUMERIC));
$return["showcounts"] = getContinuationVar("showcounts", processInputVar("showcounts", ARG_NUMERIC));
$return["showdeleted"] = getContinuationVar('showdeleted', 0);
if(! $checks) {
return $return;
}
$ipaddressArr = explode('.', $return["ipaddress"]);
if(! preg_match('/^(([0-9]){1,3}\.){3}([0-9]){1,3}$/', $return["ipaddress"]) ||
$ipaddressArr[0] < 1 || $ipaddressArr[0] > 255 ||
$ipaddressArr[1] < 0 || $ipaddressArr[1] > 255 ||
$ipaddressArr[2] < 0 || $ipaddressArr[2] > 255 ||
$ipaddressArr[3] < 1 || $ipaddressArr[3] > 255) {
$submitErr |= IPADDRERR;
$submitErrMsg[IPADDRERR] = "Invalid IP address. Must be w.x.y.z with each of "
. "w, x, y, and z being between 1 and 255 (inclusive)";
}
/*if(! ($submitErr & IPADDRERR) &&
checkForIPaddress($return["ipaddress"], $return["compid"])) {
$submitErr |= IPADDRERR;
$submitErrMsg[IPADDRERR] = "There is already a computer with this IP address.";
}*/
if($return["ram"] < 32 || $return["ram"] > 512000) {
$submitErr |= RAMERR;
$submitErrMsg[RAMERR] = "RAM must be between 32 and 512000";
}
if($return["procspeed"] < 500 || $return["procspeed"] > 20000) {
$submitErr |= PROCSPEEDERR;
$submitErrMsg[PROCSPEEDERR] = "Processor Speed must be between 500 and 20000";
}
if(! preg_match('/^[a-zA-Z0-9_][-a-zA-Z0-9_.]{1,35}$/', $return["hostname"])) {
$submitErr |= HOSTNAMEERR;
$submitErrMsg[HOSTNAMEERR] = "Hostname must be <= 36 characters";
}
if(! ($submitErr & HOSTNAMEERR) &&
checkForHostname($return["hostname"], $return["compid"])) {
$submitErr |= HOSTNAMEERR;
$submitErrMsg[HOSTNAMEERR] = "There is already a computer with this hostname.";
}
if($mode != 'viewComputers' && ! validateUserid($return["owner"])) {
$submitErr |= OWNERERR;
$submitErrMsg[OWNERERR] = "Submitted ID is not valid";
}
if($mode == 'confirmAddComputer' &&
$return['type'] == 'virtualmachine' && $return['stateid'] != 10) {
$submitErr |= VMAVAILERR;
$submitErrMsg[VMAVAILERR] = "Virtual machines can only be added in the maintenance state.";
}
return $return;
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn processComputerInput2()
///
/// \return an array with the following keys:\n
/// platforms, schedules, showhostname, shownextimage, showipaddress, showram,
/// showstate, showprocnumber, showdepartment, showprocspeed, showplatform,
/// shownetwork, showschedule, showcomputerid, showcurrentimage, showtype,
/// showprovisioning
///
/// \brief processes platforms, schedules, and all of the flags for
/// what data to show
///
////////////////////////////////////////////////////////////////////////////////
function processComputerInput2() {
$return = array();
$return["platforms"] = getContinuationVar('platforms', processInputVar("platforms", ARG_MULTINUMERIC));
$return["schedules"] = getContinuationVar('schedules', processInputVar("schedules", ARG_MULTINUMERIC));
$return["groups"] = getContinuationVar('groups', processInputVar("groups", ARG_MULTINUMERIC));
$return["showhostname"] = getContinuationVar('showhostname', processInputVar("showhostname", ARG_NUMERIC, 0));
$return["shownextimage"] = getContinuationVar('shownextimage', processInputVar("shownextimage", ARG_NUMERIC, 0));
$return["showipaddress"] = getContinuationVar('showipaddress', processInputVar("showipaddress", ARG_NUMERIC, 0));
$return["showram"] = getContinuationVar('showram', processInputVar("showram", ARG_NUMERIC, 0));
$return["showstate"] = getContinuationVar('showstate', processInputVar("showstate", ARG_NUMERIC, 0));
$return["showprocnumber"] = getContinuationVar('showprocnumber', processInputVar("showprocnumber", ARG_NUMERIC, 0));
$return["showdepartment"] = getContinuationVar('showdepartment', processInputVar("showdepartment", ARG_NUMERIC, 0));
$return["showowner"] = getContinuationVar('showowner', processInputVar("showowner", ARG_NUMERIC, 0));
$return["showprocspeed"] = getContinuationVar('showprocspeed', processInputVar("showprocspeed", ARG_NUMERIC, 0));
$return["showplatform"] = getContinuationVar('showplatform', processInputVar("showplatform", ARG_NUMERIC, 0));
$return["shownetwork"] = getContinuationVar('shownetwork', processInputVar("shownetwork", ARG_NUMERIC, 0));
$return["showschedule"] = getContinuationVar('showschedule', processInputVar("showschedule", ARG_NUMERIC, 0));
$return["showcomputerid"] = getContinuationVar('showcomputerid', processInputVar("showcomputerid", ARG_NUMERIC, 0));
$return["showcurrentimage"] = getContinuationVar('showcurrentimage', processInputVar("showcurrentimage", ARG_NUMERIC, 0));
$return["showtype"] = getContinuationVar('showtype', processInputVar("showtype", ARG_NUMERIC, 0));
$return["showprovisioning"] = getContinuationVar('showprovisioning', processInputVar("showprovisioning", ARG_NUMERIC, 0));
$return["shownotes"] = getContinuationVar('shownotes', processInputVar("shownotes", ARG_NUMERIC, 0));
$return["showcounts"] = getContinuationVar('showcounts', processInputVar("showcounts", ARG_NUMERIC, 0));
return $return;
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn processComputerInput3()
///
/// \return an array with the following indexes:\n
/// notes, imageid, stateid, computerids
///
/// \brief processes input for computer utilities pages
///
////////////////////////////////////////////////////////////////////////////////
function processComputerInput3() {
global $submitErr, $submitErrMsg;
$compids = getContinuationVar("computerids", processInputVar('computerids', ARG_MULTINUMERIC));
$return['notes'] = getContinuationVar("notes", processInputVar('notes', ARG_STRING));
$return['imageid'] = getContinuationVar("imageid", processInputVar('imageid', ARG_NUMERIC));
$return['stateid'] = getContinuationVar("stateid", processInputVar('stateid', ARG_NUMERIC));
$return['scheduleid'] = getContinuationVar("scheduleid", processInputVar('scheduleid', ARG_NUMERIC));
$resources = getUserResources(array("computerAdmin"), array("administer"));
$userCompIDs = array_keys($resources["computer"]);
$remove = array_diff($compids, $userCompIDs);
$return['computerids'] = array_diff($compids, $remove);
return $return;
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn processBulkComputerInput($checks)
///
/// \param $checks - (optional) 1 to perform validation, 0 not to
///
/// \return an array with the following indexes:\n
/// startipaddress, endipaddress, starthostval, endhostval, stateid,
/// platformid, scheduleid, ram, numprocs, procspeed, network,
/// hostname, type, count (0 if any errors found)
///
/// \brief validates input from the previous form; if anything was improperly
/// submitted, sets submitErr and submitErrMsg
///
////////////////////////////////////////////////////////////////////////////////
function processBulkComputerInput($checks=1) {
global $submitErr, $submitErrMsg;
$return = processComputerInput2();
$ipaddress = getContinuationVar("ipaddress", processInputVar("ipaddress", ARG_STRING));
if(! empty($ipaddress)) {
$return["startipaddress"] = $ipaddress;
$tmp = $ipaddress;
$tmpArr = explode('.', $tmp);
array_pop($tmpArr);
$return["endipaddress"] = implode('.', $tmpArr);
$return["starthostval"] = "";
$return["endhostval"] = "";
}
else {
$return["startipaddress"] = getContinuationVar("startipaddress", processInputVar("startipaddress", ARG_STRING));
$return["endipaddress"] = getContinuationVar("endipaddress", processInputVar("endipaddress", ARG_STRING));
$return["starthostval"] = getContinuationVar("starthostval", processInputVar("starthostval", ARG_NUMERIC));
$return["endhostval"] = getContinuationVar("endhostval", processInputVar("endhostval", ARG_NUMERIC));
}
$return["startpripaddress"] = getContinuationVar("startpripaddress", processInputVar("startpripaddress", ARG_STRING));
$return["endpripaddress"] = getContinuationVar("endpripaddress", processInputVar("endpripaddress", ARG_STRING));
$return["startmac"] = getContinuationVar("startmac", processInputVar("startmac", ARG_STRING));
$return["stateid"] = getContinuationVar("stateid", processInputVar("stateid", ARG_NUMERIC));
$return["owner"] = getContinuationVar("owner", processInputVar("owner", ARG_STRING));
$return["platformid"] = getContinuationVar("platformid", processInputVar("platformid", ARG_NUMERIC));
$return["scheduleid"] = getContinuationVar("scheduleid", processInputVar("scheduleid", ARG_NUMERIC));
$return["ram"] = getContinuationVar("ram", processInputVar("ram", ARG_NUMERIC));
$return["numprocs"] = getContinuationVar("numprocs", processInputVar("numprocs", ARG_NUMERIC));
$return["procspeed"] = getContinuationVar("procspeed", processInputVar("procspeed", ARG_NUMERIC));
$return["network"] = getContinuationVar("network", processInputVar("network", ARG_NUMERIC));
$return["hostname"] = getContinuationVar("hostname", processInputVar("hostname", ARG_STRING));
$return["type"] = getContinuationVar("type", processInputVar("type", ARG_STRING));
$return["provisioningid"] = getContinuationVar("provisioningid", processInputVar("provisioningid", ARG_NUMERIC));
$return["computergroup"] = getContinuationVar("computergroup", processInputVar("computergroup", ARG_MULTINUMERIC));
$return['macs'] = getContinuationVar('macs', array());
if(! $checks) {
return $return;
}
$startaddrArr = explode('.', $return["startipaddress"]);
if(! preg_match('/^(([0-9]){1,3}\.){3}([0-9]){1,3}$/', $return["startipaddress"]) ||
$startaddrArr[0] < 1 || $startaddrArr[0] > 255 ||
$startaddrArr[1] < 0 || $startaddrArr[1] > 255 ||
$startaddrArr[2] < 0 || $startaddrArr[2] > 255 ||
$startaddrArr[3] < 1 || $startaddrArr[3] > 255) {
$submitErr |= IPADDRERR;
$submitErrMsg[IPADDRERR] = "Invalid IP address. Must be w.x.y.z with each of "
. "w, x, y, and z being between 1 and 255 (inclusive)";
}
$endaddrArr = explode('.', $return["endipaddress"]);
if(! preg_match('/^(([0-9]){1,3}\.){3}([0-9]){1,3}$/', $return["endipaddress"]) ||
$endaddrArr[0] < 1 || $endaddrArr[0] > 255 ||
$endaddrArr[1] < 0 || $endaddrArr[1] > 255 ||
$endaddrArr[2] < 0 || $endaddrArr[2] > 255 ||
$endaddrArr[3] < 1 || $endaddrArr[3] > 255) {
$submitErr |= IPADDRERR2;
$submitErrMsg[IPADDRERR2] = "Invalid IP address. Must be w.x.y.z with each of "
. "w, x, y, and z being between 1 and 255 (inclusive)";
}
$endpraddrArr = array();
if(! empty($return['startpripaddress']) ||
! empty($return['endpripaddress'])) {
$startpraddrArr = explode('.', $return["startpripaddress"]);
if(! preg_match('/^(([0-9]){1,3}\.){3}([0-9]){1,3}$/', $return["startpripaddress"]) ||
$startpraddrArr[0] < 1 || $startpraddrArr[0] > 255 ||
$startpraddrArr[1] < 0 || $startpraddrArr[1] > 255 ||
$startpraddrArr[2] < 0 || $startpraddrArr[2] > 255 ||
$startpraddrArr[3] < 1 || $startpraddrArr[3] > 255) {
$submitErr |= IPADDRERR3;
$submitErrMsg[IPADDRERR3] = "Invalid IP address. Must be w.x.y.z with each of "
. "w, x, y, and z being between 1 and 255 (inclusive)";
}
$endpraddrArr = explode('.', $return["endpripaddress"]);
if(! preg_match('/^(([0-9]){1,3}\.){3}([0-9]){1,3}$/', $return["endpripaddress"]) ||
$endpraddrArr[0] < 1 || $endpraddrArr[0] > 255 ||
$endpraddrArr[1] < 0 || $endpraddrArr[1] > 255 ||
$endpraddrArr[2] < 0 || $endpraddrArr[2] > 255 ||
$endpraddrArr[3] < 1 || $endpraddrArr[3] > 255) {
$submitErr |= IPADDRERR4;
$submitErrMsg[IPADDRERR4] = "Invalid IP address. Must be w.x.y.z with each of "
. "w, x, y, and z being between 1 and 255 (inclusive)";
}
}
if(! empty($return['startmac'])) {
if(! preg_match('/^(([A-Fa-f0-9]){2}:){5}([A-Fa-f0-9]){2}$/', $return["startmac"])) {
$submitErr |= MACADDRERR;
$submitErrMsg[MACADDRERR] = "Invalid MAC address. Must be XX:XX:XX:XX:XX:XX "
. "with each pair of XX being from 00 to FF (inclusive)";
}
elseif(! $submitErr) {
$tmp = explode(':', $return['startmac']);
$topdec = hexdec($tmp[0] . $tmp[1] . $tmp[2]);
$botdec = hexdec($tmp[3] . $tmp[4] . $tmp[5]);
$topmac = "{$tmp[0]}:{$tmp[1]}:{$tmp[2]}";
$topplus = implode(':', str_split(dechex($topdec + 1), 2));
$start = $botdec;
$return['macs'] = array();
$eth0macs = array();
$eth1macs = array();
$toggle = 0;
$end = $start + (($endaddrArr[3] - $startaddrArr[3] + 1) * 2);
for($i = $start; $i < $end; $i++) {
if($i > 16777215) {
$val = $i - 16777216;
$tmp = sprintf('%06x', $val);
$tmp2 = str_split($tmp, 2);
$return['macs'][] = $topplus . ':' . implode(':', $tmp2);
}
else {
$tmp = sprintf('%06x', $i);
$tmp2 = str_split($tmp, 2);
$return['macs'][] = $topmac . ':' . implode(':', $tmp2);
}
if($toggle % 2)
$eth1macs[] = $topmac . ':' . implode(':', $tmp2);
else
$eth0macs[] = $topmac . ':' . implode(':', $tmp2);
$toggle++;
}
$ineth0s = implode("','", $eth0macs);
$ineth1s = implode("','", $eth1macs);
$query = "SELECT id "
. "FROM computer "
. "WHERE eth0macaddress IN ('$ineth0s') OR "
. "eth1macaddress IN ('$ineth1s')";
$qh = doQuery($query, 101);
if(mysql_num_rows($qh)) {
$submitErr |= MACADDRERR;
$submitErrMsg[MACADDRERR] = "The specified starting MAC address "
. "combined with the number of computers "
. "entered will result in a MAC address "
. "already assigned to another computer.";
}
elseif($i > 16777215 && $topdec == 16777215) {
$submitErr |= MACADDRERR;
$submitErrMsg[MACADDRERR] = "Starting MAC address too large for given "
. "given number of machines";
}
}
}
if($return["ram"] < 32 || $return["ram"] > 512000) {
$submitErr |= RAMERR;
$submitErrMsg[RAMERR] = "RAM must be between 32 and 512000";
}
if($return["procspeed"] < 500 || $return["procspeed"] > 20000) {
$submitErr |= PROCSPEEDERR;
$submitErrMsg[PROCSPEEDERR] = "Processor Speed must be between 500 and 20000";
}
if(! preg_match('/^[a-zA-Z0-9_%][-a-zA-Z0-9_.%]{1,35}$/', $return["hostname"])) {
$submitErr |= HOSTNAMEERR;
$submitErrMsg[HOSTNAMEERR] = "Hostname must be <= 36 characters";
}
if(empty($return["starthostval"]) && $return["starthostval"] != 0) {
$submitErr |= STARTHOSTVALERR;
$submitErrMsg[STARTHOSTVALERR] = "Start value can only be numeric.";
}
if(empty($return["endhostval"]) && $return["endhostval"] != 0) {
$submitErr |= ENDHOSTVALERR;
$submitErrMsg[ENDHOSTVALERR] = "End value can only be numeric.";
}
if(! ($submitErr & IPADDRERR2 || $submitErr & ENDHOSTVALERR) &&
($endaddrArr[3] - $startaddrArr[3] != $return["endhostval"] - $return["starthostval"])) {
$numipaddrs = $endaddrArr[3] - $startaddrArr[3] + 1;
$numhostnames = $return["endhostval"] - $return["starthostval"] + 1;
$submitErr |= IPADDRERR2;
$submitErrMsg[IPADDRERR2] = "The number of IP addresses ($numipaddrs) "
. "does not match the number of hostnames ($numhostnames).";
$submitErr |= ENDHOSTVALERR;
$submitErrMsg[ENDHOSTVALERR] = "The number of IP addresses ($numipaddrs) "
. "does not match the number of hostnames ($numhostnames).";
}
if(! empty($return['startpripaddress']) && ! empty($return['endpripaddress']) &&
(! ($submitErr & IPADDRERR2 || $submitErr & IPADDRERR4) &&
! empty($endpraddrArr) &&
($endaddrArr[3] - $startaddrArr[3] != $endpraddrArr[3] - $startpraddrArr[3]))) {
$numpubaddrs = $endaddrArr[3] - $startaddrArr[3] + 1;
$numpraddrs = $endpraddrArr[3] - $startpraddrArr[3] + 1;
$submitErr |= IPADDRERR2;
$submitErrMsg[IPADDRERR2] = "The number of public IP addresses ($numpubaddrs) "
. "does not match the number of private IP addresses ($numpraddrs).";
$submitErr |= IPADDRERR4;
$submitErrMsg[IPADDRERR4] = $submitErrMsg[IPADDRERR2];
}
if(! validateUserid($return["owner"])) {
$submitErr |= OWNERERR;
$submitErrMsg[OWNERERR] = "Submitted ID is not valid";
}
$return['count'] = 0;
if(! $submitErr)
$return['count'] = $endaddrArr[3] - $startaddrArr[3] + 1;
return $return;
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn checkForHostname($hostname, $compid)
///
/// \param $hostname - a computer hostname
/// \param $compid - a computer id to ignore
///
/// \return 1 if $hostname is already in the computer table, 0 if not
///
/// \brief checks for $hostname being somewhere in the computer table except
/// for $compid
///
////////////////////////////////////////////////////////////////////////////////
function checkForHostname($hostname, $compid) {
$query = "SELECT id FROM computer "
. "WHERE hostname = '$hostname'";
if(! empty($compid))
$query .= " AND id != $compid";
$qh = doQuery($query, 101);
if(mysql_num_rows($qh))
return 1;
return 0;
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn checkForIPaddress($ipaddress, $compid)
///
/// \param $ipaddress - a computer ip address
/// \param $compid - a computer id to ignore
///
/// \return 1 if $ipaddress is already in the computer table, 0 if not
///
/// \brief checks for $ipaddress being somewhere in the computer table except
/// for $compid
///
////////////////////////////////////////////////////////////////////////////////
function checkForIPaddress($ipaddress, $compid) {
$query = "SELECT id FROM computer "
. "WHERE IPaddress = '$ipaddress'";
if(! empty($compid))
$query .= " AND id != $compid";
$qh = doQuery($query, 101);
if(mysql_num_rows($qh))
return 1;
return 0;
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn getCompIdList($groups)
///
/// \param $groups - an array of computer group ids
///
/// \return an array where each key is a computer id and each value is 1
///
/// \brief builds a list of computer ids that are either in $groups or are not
/// in any groups but owned by the logged in user
///
////////////////////////////////////////////////////////////////////////////////
function getCompIdList($groups) {
global $user;
$inlist = implode(',', $groups);
$query = "SELECT DISTINCT r.subid as compid "
. "FROM resource r, "
. "resourcegroupmembers rgm "
. "WHERE r.id = rgm.resourceid AND "
. "rgm.resourcegroupid IN ($inlist)";
$qh = doQuery($query, 101);
$compidlist = array();
while($row = mysql_fetch_assoc($qh))
$compidlist[$row['compid']] = 1;
$query = "SELECT id "
. "FROM computer "
. "WHERE ownerid = {$user['id']} AND "
. "id NOT IN (SELECT r.subid "
. "FROM resource r, "
. "resourcegroupmembers rgm "
. "WHERE r.id = rgm.resourceid AND "
. "r.resourcetypeid = 12)";
$qh = doQuery($query, 101);
while($row = mysql_fetch_assoc($qh))
$compidlist[$row['id']] = 1;
return $compidlist;
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn updateComputer($data)
///
/// \param $data - an array with the following indexes:\n
/// ipaddress, stateid, platformid, scheduleid,
/// ram, numprocs, procspeed, network, hostname, compid, type
///
/// \return number of rows affected by the update\n
/// \b NOTE: mysql reports that no rows were affected if none of the fields
/// were actually changed even if the update matched a row
///
/// \brief performs a query to update the computer with data from $data
///
////////////////////////////////////////////////////////////////////////////////
function updateComputer($data) {
$ownerid = getUserlistID($data["owner"]);
$query = "UPDATE computer "
. "SET stateid = {$data["stateid"]}, "
. "ownerid = $ownerid, "
. "platformid = {$data["platformid"]}, "
. "scheduleid = {$data["scheduleid"]}, "
. "RAM = {$data["ram"]}, "
. "procnumber = {$data["numprocs"]}, "
. "procspeed = {$data["procspeed"]}, "
. "network = {$data["network"]}, "
. "hostname = '{$data["hostname"]}', "
. "IPaddress = '{$data["ipaddress"]}', "
. "type = '{$data["type"]}', "
. "provisioningid = {$data["provisioningid"]}, "
. "notes = '{$data["notes"]}' "
. "WHERE id = {$data["compid"]}";
$qh = doQuery($query, 185);
return mysql_affected_rows($GLOBALS["mysql_link_vcl"]);
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn addComputer($data)
///
/// \param $data - an array with the following indexes:\n
/// ipaddress, stateid, platformid, scheduleid,
/// ram, numprocs, procspeed, network, hostname, type
///
/// \brief adds a computer to the computer table
///
////////////////////////////////////////////////////////////////////////////////
function addComputer($data) {
global $user;
$ownerid = getUserlistID($data["owner"]);
if($data['stateid'] == 10) {
$now = unixToDatetime(time());
$notes = "'{$user['unityid']} $now@in maintenance state when added to database'";
}
else
$notes = 'NULL';
$query = "INSERT INTO computer "
. "(stateid, "
. "ownerid, "
. "platformid, "
. "scheduleid, "
. "currentimageid, "
. "RAM, "
. "procnumber, "
. "procspeed, "
. "network, "
. "hostname, "
. "IPaddress, "
. "type, "
. "notes, "
. "provisioningid) "
. "VALUES ({$data["stateid"]}, "
. "$ownerid, "
. "{$data["platformid"]}, "
. "{$data["scheduleid"]}, "
. "4, "
. "{$data["ram"]}, "
. "{$data["numprocs"]}, "
. "{$data["procspeed"]}, "
. "{$data["network"]}, "
. "'{$data["hostname"]}', "
. "'{$data["ipaddress"]}', "
. "'{$data["type"]}', "
. "$notes, "
. "'{$data["provisioningid"]}')";
doQuery($query, 195);
$compid = dbLastInsertID();
$query = "INSERT INTO resource "
. "(resourcetypeid, "
. "subid) "
. "VALUES (12, "
. "$compid)";
doQuery($query, 198);
$resid = dbLastInsertID();
// add computer into selected groups
if(! empty($data['computergroup'])) {
$vals = array();
foreach(array_keys($data["computergroup"]) as $groupid)
$vals[] = "($resid, $groupid)";
$allvals = implode(',', $vals);
$query = "INSERT INTO resourcegroupmembers "
. "(resourceid, "
. "resourcegroupid) "
. "VALUES $allvals";
doQuery($query, 101);
}
if($data['stateid'] == 20) {
$profileid = processInputVar('profileid', ARG_NUMERIC);
$profiles = getContinuationVar('profiles');
if(! array_key_exists($profileid, $profiles)) {
$tmp = array_keys($profiles);
$profileid = $tmp[0];
}
# create vmhost entry
$query = "INSERT INTO vmhost "
. "(computerid, "
. "vmlimit, "
. "vmprofileid) "
. "VALUES ($compid, "
. "2, "
. "$profileid)";
doQuery($query, 101);
}
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn printComputerInfo($ipaddress, $stateid, $owner, $platformid,
/// $scheduleid, $currentimgid, $ram, $numprocs,
/// $procspeed, $network, $hostname, $compid, $type,
/// $provisioningid)
///
/// \param $ipaddress - IP address of computer
/// \param $stateid - stateid of computer
/// \param $owner - owner of computer
/// \param $platformid - platformid of computer
/// \param $scheduleid - scheduleid of computer
/// \param $currentimgid - current imageid of computer
/// \param $ram - ram in MB of computer
/// \param $numprocs - number of processors in computer
/// \param $procspeed - processor speed of computer
/// \param $network - network speed of computer's NIC in MBbps
/// \param $hostname - hostname of computer
/// \param $compid - id of computer from computer table
/// \param $type - type of computer (blade or lab)
/// \param $provisioningid - id of provisioning engine
///
/// \brief prints a table of information about the computer
///
////////////////////////////////////////////////////////////////////////////////
function printComputerInfo($ipaddress, $stateid, $owner, $platformid,
$scheduleid, $currentimgid, $ram, $numprocs,
$procspeed, $network, $hostname, $compid, $type,
$provisioningid) {
$states = getStates();
$platforms = getPlatforms();
$schedules = getSchedules();
$images = getImages();
$provisioning = getProvisioning();
print "
\n";
print " \n";
print " Hostname: \n";
print " $hostname \n";
print " \n";
print " \n";
print " IP Address: \n";
print " $ipaddress \n";
print " \n";
print " \n";
print " State: \n";
print " " . $states[$stateid] . " \n";
print " \n";
print " \n";
print " Owner: \n";
print " $owner \n";
print " \n";
print " \n";
print " Platform: \n";
print " " . $platforms[$platformid] . " \n";
print " \n";
print " \n";
print " Schedule: \n";
print " " . $schedules[$scheduleid]["name"] . " \n";
print " \n";
if(! empty($currentimgid)) {
print " \n";
print " Current Image: \n";
print " " . $images[$currentimgid]["prettyname"] . " \n";
print " \n";
}
print " \n";
print " RAM (MB): \n";
print " $ram \n";
print " \n";
print " \n";
print " No. Processors: \n";
print " $numprocs \n";
print " \n";
print " \n";
print " Processor Speed (MHz): \n";
print " $procspeed \n";
print " \n";
print " \n";
print " Network Speed (Mbps): \n";
print " $network \n";
print " \n";
if(! empty($compid)) {
print " \n";
print " Computer ID: \n";
print " $compid \n";
print " \n";
}
print " \n";
print " Type: \n";
print " $type \n";
print " \n";
print " \n";
print " Provisioning Engine: \n";
print " " . $provisioning[$provisioningid]['prettyname'] . " \n";
print " \n";
print "
\n";
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn getComputerSelection($data)
///
/// \param $data - array of data as returned from processComputerInput
///
/// \return an array of data to be passed to addContinuationsEntry
///
/// \brief returns only the selection data from $data
///
////////////////////////////////////////////////////////////////////////////////
function getComputerSelection($data) {
$ret = array();
$ret['platforms'] = $data['platforms'];
$ret['schedules'] = $data['schedules'];
$ret['groups'] = $data['groups'];
$keys = array("showhostname", "shownextimage", "showipaddress",
"showram", "showstate", "showprocnumber", "showdepartment",
"showowner", "showprocspeed", "showplatform", "shownetwork",
"showschedule", "showcomputerid", "showcurrentimage",
"showtype", "showdeleted", "shownotes", "showcounts",
"showprovisioning");
foreach($keys as $key)
$ret[$key] = $data[$key];
return $ret;
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn jsonCompGroupingComps()
///
/// \brief accepts a groupid via form input and prints a json array with 3
/// arrays: an array of computers that are in the group, an array of computers
/// not in it, and an array of all computers user has access to
///
////////////////////////////////////////////////////////////////////////////////
function jsonCompGroupingComps() {
$groupid = processInputVar('groupid', ARG_NUMERIC);
$groups = getUserResources(array("computerAdmin"), array("manageGroup"), 1);
if(! array_key_exists($groupid, $groups['computer'])) {
$arr = array('incomps' => array(), 'outcomps' => array(), 'all' => array());
header('Content-Type: text/json-comment-filtered; charset=utf-8');
print '/*{"items":' . json_encode($arr) . '}*/';
return;
}
$resources = getUserResources(array('computerAdmin'), array('manageGroup'));
uasort($resources['computer'], 'sortKeepIndex');
$memberships = getResourceGroupMemberships('computer');
$all = array();
$in = array();
$out = array();
foreach($resources['computer'] as $id => $comp) {
if(array_key_exists($id, $memberships['computer']) &&
in_array($groupid, $memberships['computer'][$id])) {
$all[] = array('inout' => 1, 'id' => $id, 'name' => $comp);
$in[] = array('name' => $comp, 'id' => $id);
}
else {
$all[] = array('inout' => 0, 'id' => $id, 'name' => $comp);
$out[] = array('name' => $comp, 'id' => $id);
}
}
$arr = array('incomps' => $in, 'outcomps' => $out, 'all' => $all);
print '/*{"items":' . json_encode($arr) . '}*/';
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn jsonCompGroupingGroups()
///
/// \brief accepts a computer id via form input and prints a json array with 3
/// arrays: an array of groups that the computer is in, an array of groups it
/// is not in and an array of all groups user has access to
///
////////////////////////////////////////////////////////////////////////////////
function jsonCompGroupingGroups() {
$compid = processInputVar('compid', ARG_NUMERIC);
$resources = getUserResources(array("computerAdmin"), array("manageGroup"));
if(! array_key_exists($compid, $resources['computer'])) {
$arr = array('ingroups' => array(), 'outgroups' => array(), 'all' => array());
header('Content-Type: text/json-comment-filtered; charset=utf-8');
print '/*{"items":' . json_encode($arr) . '}*/';
return;
}
$groups = getUserResources(array('computerAdmin'), array('manageGroup'), 1);
$memberships = getResourceGroupMemberships('computer');
$in = array();
$out = array();
$all = array();
foreach($groups['computer'] as $id => $group) {
if(array_key_exists($compid, $memberships['computer']) &&
in_array($id, $memberships['computer'][$compid])) {
$all[] = array('inout' => 1, 'id' => $id, 'name' => $group);
$in[] = array('name' => $group, 'id' => $id);
}
else {
$all[] = array('inout' => 0, 'id' => $id, 'name' => $group);
$out[] = array('name' => $group, 'id' => $id);
}
}
$arr = array('ingroups' => $in, 'outgroups' => $out, 'all' => $all);
print '/*{"items":' . json_encode($arr) . '}*/';
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn AJaddCompToGroup()
///
/// \brief accepts a groupid and a comma delimited list of computer ids to be
/// added to the group; adds them and returns an array of computer ids that were
/// added
///
////////////////////////////////////////////////////////////////////////////////
function AJaddCompToGroup() {
$groupid = processInputVar('id', ARG_NUMERIC);
$groups = getUserResources(array("computerAdmin"), array("manageGroup"), 1);
if(! array_key_exists($groupid, $groups['computer'])) {
$arr = array('comps' => array(), 'addrem' => 1);
header('Content-Type: text/json-comment-filtered; charset=utf-8');
print '/*{"items":' . json_encode($arr) . '}*/';
return;
}
$resources = getUserResources(array("computerAdmin"), array("manageGroup"));
$tmp = processInputVar('listids', ARG_STRING);
$tmp = explode(',', $tmp);
$compids = array();
foreach($tmp as $id) {
if(! is_numeric($id))
continue;
if(! array_key_exists($id, $resources['computer'])) {
$arr = array('comps' => array(), 'addrem' => 1);
header('Content-Type: text/json-comment-filtered; charset=utf-8');
print '/*{"items":' . json_encode($arr) . '}*/';
return;
}
$compids[] = $id;
}
$allcomps = getComputers();
$adds = array();
foreach($compids as $id) {
$adds[] = "({$allcomps[$id]['resourceid']}, $groupid)";
}
$query = "INSERT IGNORE INTO resourcegroupmembers "
. "(resourceid, resourcegroupid) VALUES ";
$query .= implode(',', $adds);
doQuery($query, 287);
$_SESSION['userresources'] = array();
$arr = array('comps' => $compids, 'addrem' => 1);
header('Content-Type: text/json-comment-filtered; charset=utf-8');
print '/*{"items":' . json_encode($arr) . '}*/';
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn AJremCompFromGroup()
///
/// \brief accepts a groupid and a comma delimited list of computer ids to be
/// removed from the group; removes them and returns an array of computer ids
/// that were removed
///
////////////////////////////////////////////////////////////////////////////////
function AJremCompFromGroup() {
$groupid = processInputVar('id', ARG_NUMERIC);
$groups = getUserResources(array("computerAdmin"), array("manageGroup"), 1);
if(! array_key_exists($groupid, $groups['computer'])) {
$arr = array('comps' => array(), 'addrem' => 0);
header('Content-Type: text/json-comment-filtered; charset=utf-8');
print '/*{"items":' . json_encode($arr) . '}*/';
return;
}
$resources = getUserResources(array("computerAdmin"), array("manageGroup"));
$tmp = processInputVar('listids', ARG_STRING);
$tmp = explode(',', $tmp);
$compids = array();
foreach($tmp as $id) {
if(! is_numeric($id))
continue;
if(! array_key_exists($id, $resources['computer'])) {
$arr = array('comps' => array(), 'addrem' => 0, 'id' => $id, 'extra' => $resources['computer']);
header('Content-Type: text/json-comment-filtered; charset=utf-8');
print '/*{"items":' . json_encode($arr) . '}*/';
return;
}
$compids[] = $id;
}
$allcomps = getComputers();
foreach($compids as $id) {
$query = "DELETE FROM resourcegroupmembers "
. "WHERE resourceid = {$allcomps[$id]['resourceid']} AND "
. "resourcegroupid = $groupid";
doQuery($query, 288);
}
$_SESSION['userresources'] = array();
$arr = array('comps' => $compids, 'addrem' => 0);
header('Content-Type: text/json-comment-filtered; charset=utf-8');
print '/*{"items":' . json_encode($arr) . '}*/';
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn AJaddGroupToComp()
///
/// \brief accepts a computer id and a comma delimited list of group ids that
/// the computer should be added to; adds it to them and returns an array of
/// groups it was added to
///
////////////////////////////////////////////////////////////////////////////////
function AJaddGroupToComp() {
$compid = processInputVar('id', ARG_NUMERIC);
$resources = getUserResources(array("computerAdmin"), array("manageGroup"));
if(! array_key_exists($compid, $resources['computer'])) {
$arr = array('groups' => array(), 'addrem' => 1);
header('Content-Type: text/json-comment-filtered; charset=utf-8');
print '/*{"items":' . json_encode($arr) . '}*/';
return;
}
$groups = getUserResources(array("computerAdmin"), array("manageGroup"), 1);
$tmp = processInputVar('listids', ARG_STRING);
$tmp = explode(',', $tmp);
$groupids = array();
foreach($tmp as $id) {
if(! is_numeric($id))
continue;
if(! array_key_exists($id, $groups['computer'])) {
$arr = array('groups' => array(), 'addrem' => 1);
header('Content-Type: text/json-comment-filtered; charset=utf-8');
print '/*{"items":' . json_encode($arr) . '}*/';
return;
}
$groupids[] = $id;
}
$comp = getComputers(0, $compid);
$adds = array();
foreach($groupids as $id) {
$adds[] = "({$comp[$compid]['resourceid']}, $id)";
}
$query = "INSERT IGNORE INTO resourcegroupmembers "
. "(resourceid, resourcegroupid) VALUES ";
$query .= implode(',', $adds);
doQuery($query, 101);
$_SESSION['userresources'] = array();
$arr = array('groups' => $groupids, 'addrem' => 1);
header('Content-Type: text/json-comment-filtered; charset=utf-8');
print '/*{"items":' . json_encode($arr) . '}*/';
}
////////////////////////////////////////////////////////////////////////////////
///
/// \fn AJremGroupFromComp()
///
/// \brief accepts a computer id and a comma delimited list of group ids that
/// the computer should be removed from; removes it from them and returns an
/// array of groups it was removed from
///
////////////////////////////////////////////////////////////////////////////////
function AJremGroupFromComp() {
$compid = processInputVar('id', ARG_NUMERIC);
$resources = getUserResources(array("computerAdmin"), array("manageGroup"));
if(! array_key_exists($compid, $resources['computer'])) {
$arr = array('groups' => array(), 'addrem' => 0);
header('Content-Type: text/json-comment-filtered; charset=utf-8');
print '/*{"items":' . json_encode($arr) . '}*/';
return;
}
$groups = getUserResources(array("computerAdmin"), array("manageGroup"), 1);
$tmp = processInputVar('listids', ARG_STRING);
$tmp = explode(',', $tmp);
$groupids = array();
foreach($tmp as $id) {
if(! is_numeric($id))
continue;
if(! array_key_exists($id, $groups['computer'])) {
$arr = array('groups' => array(), 'addrem' => 0);
header('Content-Type: text/json-comment-filtered; charset=utf-8');
print '/*{"items":' . json_encode($arr) . '}*/';
return;
}
$groupids[] = $id;
}
$comp = getComputers(0, $compid);
foreach($groupids as $id) {
$query = "DELETE FROM resourcegroupmembers "
. "WHERE resourceid = {$comp[$compid]['resourceid']} AND "
. "resourcegroupid = $id";
doQuery($query, 288);
}
$_SESSION['userresources'] = array();
$arr = array('groups' => $groupids, 'addrem' => 0);
header('Content-Type: text/json-comment-filtered; charset=utf-8');
print '/*{"items":' . json_encode($arr) . '}*/';
}
?>