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.

plabmetrics.php3 65.9 KB
Newer Older
1 2 3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2003, 2004, 2006, 2007 University of Utah and the Flux Group.
5 6 7
# All rights reserved.
#
include("defs.php3");
8
include("showstuff.php3");
9

10 11 12 13
# a few constants
$DEF_LIMIT = 20;
$DEF_NUMPAGENO = 10;

14 15 16
$FLEXLAB_XMLRPC_SRV = 'ops.emulab.net';
$FLEXLAB_XMLRPC_SRV_PORT = 3993;

17 18 19 20 21
#
# Collect errors.
#
$opterrs = array();

22 23 24
#
# Only known and logged in users can get plab data.
#
25 26 27
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
28

29 30 31 32 33 34 35 36
if (!$PLABSUPPORT) {
    PAGEHEADER("PlanetLab Metrics");
    echo "<p>This site does not have PlanetLab support configured.</p>";
    echo "<p>Email $TBMAILADDR_OPS if this confuses you!</p>";
    PAGEFOOTER();
    exit(0);
}

37
#
38 39
# Hacks -- unset $pid and $eid vars if they are '' before we check args!
# (needed so we can let users drop a previous pid/eid filter).
40 41 42 43 44 45 46 47
#
if (isset($_REQUEST['pid']) && $_REQUEST['pid'] == '') {
    unset($_REQUEST['pid']);
}
if (isset($_REQUEST['eid']) && $_REQUEST['eid'] == '') {
    unset($_REQUEST['eid']);
}

48
# Do similar for other arguments.
49 50 51
if (isset($_REQUEST['upnodefilter']) && $_REQUEST['upnodefilter'] == '') {
    unset($_REQUEST['upnodefilter']);
}
52 53 54
if (isset($_REQUEST['userquery']) && $_REQUEST['userquery'] == '') {
    unset($_REQUEST['userquery']);
}
55

56
# Make sure to add any new args here to pm_buildurl() as well.
57 58
$optargs = OptionalPageArguments( "experiment", PAGEARG_EXPERIMENT,
				  "cols",   PAGEARG_ALPHALIST,
59 60 61 62 63
				  "offset", PAGEARG_INTEGER,
				  "limit",  PAGEARG_INTEGER,
				  "upnodefilter", PAGEARG_STRING,
				  "pagelayout", PAGEARG_BOOLEAN,
				  "sortcols", PAGEARG_ALPHALIST,
64
				  "sortdir", PAGEARG_STRING,
65 66

                                  # Never fear.  We check this ourself since
67
                                  # it could contain either an alphanumeric
68 69 70
                                  # string, or a perl5 regex. NOTE:
                                  # only alphanum strings go in db queries;
                                  # regexes are handled within this page.
71
				  "hostfilter", PAGEARG_ANYTHING,
72

73 74
				  "selectable", PAGEARG_BOOLEAN,
				  "selectionlist", PAGEARG_ALPHALIST,
75 76
				  "newpgsel", PAGEARG_ARRAY,
				  "pgsel", PAGEARG_ARRAY,
77

78 79 80 81
                                  # We check this *extremely* copiously.
                                  # It actually gets tokenized and parsed and
                                  # we verify every last token against known 
                                  # or numeric values.
82 83 84 85 86 87 88
				  "userquery", PAGEARG_ANYTHING,

				  "flexlabfilter", PAGEARG_BOOLEAN,
				  "flexlabfcsize", PAGEARG_INTEGER,
				  "flexlabsave", PAGEARG_BOOLEAN,
				  "flexlabrecompute", PAGEARG_BOOLEAN,
				  "flexlabfcnodes", PAGEARG_ALPHALIST );
89

90 91 92
                                  # Unimplemented.
                                  # Verified, but never leaves php
                                  #"colrank", PAGEARG_ANYTHING);
93

94 95
# Yet to be implemented:
# * basic options
96
#     add and pcvm links; other?
97 98 99 100
# * advanced options
#     build-your-own rank (based off column weights, normalization to max or 
#       user-chosen ceil; each col should be able to be minimized/maximized in 
#       its contribution to the rank)
101

102
#
103
# Argument checks.
104 105
#

106 107 108
# first of all, only post-filter the data if absolutely necessary.
$mustpostfilter = 0;

109 110 111
# access control -- everybody can see stats for all nodes, but only members
# of a pid can see which nodes an eid in that pid has.
if (isset($experiment)) {
112 113 114 115
    if (!$experiment->AccessCheck($this_user, $TB_EXPT_READINFO)) {
	USERERROR("You do not have permission to view this experiment!",1);
    }

116 117 118
    # need these later
    $eid = $experiment->eid();
    $pid = $experiment->pid();
119
}
120

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
# check hostfilter -- if it is an alphanum string, we can send to db; else,
# it is a perl5 regexp and we have to filter in php.  Note that we also
# allow common hostname chars -,.,_ since the db can accept these...
if (!isset($hostfilter)) {
    $hf_regexp = 0;
}
elseif ($hostfilter == '' || preg_match("/^(\s+)$/",$hostfilter) > 0) {
    $hf_regexp = 0;
    unset($hostfilter);
}
elseif (preg_match("/^([0-9a-zA-Z\-_\.]+)$/",$hostfilter) == 1) {
    $hf_regexp = 0;
}
else {
    $hf_regexp = 1;
136 137
    # setup post-filtering so that we can match using regexps.
    $mustpostfilter = 1;
138
    #echo "needed regexp and postfilter<br>\n";
139 140
}

141 142 143 144 145 146 147 148 149
#
# Flexlab filter options:
#
if (isset($flexlabfilter) && $flexlabfilter) {
    if (isset($flexlabsave) && $flexlabsave 
	&& isset($flexlabrecompute) && $flexlabrecompute) {
        # unset recompute; save is the winner...
	unset($flexlabrecompute);
    }
David Johnson's avatar
David Johnson committed
150

151 152 153 154 155 156 157 158
    $mustpostfilter = 1;
}
else {
    if (isset($flexlabfcnodes)) {
	unset($flexlabfcnodes);
    }
}

159 160 161 162 163 164 165 166 167 168
#
# If filtering on pid/eid AND upnodefilter == emulab, we must postfilter the
# data (initial query will get the list of pid/eid nodes, then we filter out
# upnodes).  This is because a plab node isup according to Emulab if its 
# node status is up and if it is in the emulab-ops/plabnodes experiment.
#
if (isset($experiment) 
    && (isset($upnodefilter) && $upnodefilter == 'emulab')) {
    $mustpostfilter = 1;
}
169

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
#
# Standard Testbed Header
#
# Cheat and use the page args to influence the view.
$view = array();
if (isset($_REQUEST['pagelayout']) && $_REQUEST['pagelayout'] == "minimal") {
    $view = array ('hide_banner' => 1, 'hide_sidebar' => 1 );
}

PAGEHEADER("PlanetLab Metrics",$view);

#
# Some javascript to help us out.
#
?>
<script type='text/javascript' src='<?php echo $TBBASE; ?>/dynselect.js'></script>
<script type="text/javascript" language="javascript">

var divstate = new Array();

function isdef(obj) {
    if (typeof(obj) != "undefined") {
	return true;
    }
    return false;
}

function divinit(id) {
    var obj = document.getElementById(id);
    if (!isdef(obj)) {
	return false;
    }
    divstate[id] = new Object();
    divstate[id].origheight = obj.offsetHeight;
    divstate[id].vis = obj.style.visibility;
}

function divflipvis(id,linktxtid) {
    var obj = document.getElementById(id);
    if (!isdef(obj)) {
	return false;
    }
    if (!isdef(divstate[id])) {
	return false;
    }
    if (divstate[id].vis == "visible") {
	obj.style.height = "0px";
	obj.style.position = "absolute";
	divstate[id].vis = obj.style.visibility = "hidden";
    }
    else {
	obj.style.height = divstate[id].origheight+"px";
	//obj.style.height = obj.offsetHeight+"px";
	obj.style.position = "static";
	divstate[id].vis = obj.style.visibility = "visible";
    }

    linktxt = "Show";
    if (divstate[id].vis == "visible") {
	linktxt = "Hide";
    }
    if (linktxtid != null) {
	var lobj = document.getElementById(linktxtid);
	if (lobj != null) {
	    lobj.innerHTML = linktxt;
	}
    }
}

function init() {
    divinit('searchdiv');
    //    divinit('selectiondiv');

    //divflipvis('searchdiv');
    //divflipvis('legenddiv');
    //divflipvis('selectiondiv');
}

248
addLoadFunction(init);
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307

function setElementValue(id,value) {
    if (id == null) {
	return false;
    }
    var obj = document.getElementById(id);
    if (!isdef(obj)) {
	return false;
    }

    obj.value = value;

    return true;
}

function setFormElementValue(form,id,value) {
    if (form == null || id == null) {
	return false;
    }
    var fobj = document.forms[form].elements[id];
    if (!isdef(fobj) ) {
    	return false;
    }
    
    fobj.value = value;

}

function setSelectionAllCheckboxes(form,selstate) {
    if (form == null) {
	return false;
    }

    var fobj = document.forms[form];
    for (var i = 0; i < fobj.elements.length; ++i) {
	if (fobj.elements[i].type == 'checkbox') {
	    fobj.elements[i].checked = selstate;
	}
    }

    return true;
}

function getFormElementValue(form,id) {
    if (form == null || id == null) {
	return '';
    }
    var fobj = document.forms[form].elements[id];
    if (!isdef(fobj)) {
	return '';
    }

    return fobj.value;
}
    

</script>
<?php

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
#
# Setup the viewable exp/pid mappings:
#

?>

<script type='text/javascript' language='javascript'>
/*
 * Some pid/eid array mappings:
 */
var exps = new Array();
// handle the case where user doesn't want a pid/eid
// also see later where we generate the javascript event that handles the
// pid selectbox change.
exps['No Project'] = new Array('No Experiment');

var expValues = new Array();
expValues['No Experiment'] = '';

<?php

# This is a sick query, but there does not appear to be anything in the 
# user, project, or experiment objects that will do the job.
331 332
# We want to find all pid/eids that have mapped plab nodes that the user 
# can read.
333 334 335 336 337
if ($isadmin) {
    $pequery = "
select e.pid,e.eid from experiments as e 
left join reserved as r on e.pid=r.pid and e.eid=r.eid 
left join nodes as n on r.node_id=n.node_id 
338 339 340
left join nodes as n2 on n.phys_nodeid=n2.node_id 
left join node_types as nt on n2.type=nt.type 
where nt.isplabphysnode=1 group by e.pid,e.eid";
341 342 343 344 345 346 347
}
else {
    $pequery = "
select e.pid,e.eid from group_membership as g 
left join experiments as e on g.pid=e.pid and g.pid=g.gid 
left join reserved as r on e.pid=r.pid and e.eid=r.eid 
left join nodes as n on r.node_id=n.node_id 
348 349 350 351
left join nodes as n2 on n.phys_nodeid=n2.node_id 
left join node_types as nt on n2.type=nt.type 
where g.uid='johnsond' and e.pid is not null and e.eid is not null 
  and nt.isplabphysnode=1 group by e.pid,e.eid";
352 353 354 355 356
}
$qres = DBQueryFatal($pequery);

$pidmap = array();

David Johnson's avatar
David Johnson committed
357
if (mysql_num_rows($qres) > 0) {
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    while ($row = mysql_fetch_array($qres)) {
	$tpid = $row['pid'];
	$teid = $row['eid'];

	if (!array_key_exists($tpid,$pidmap)) {
	    $pidmap[$tpid] = array($teid);
	}
	else {
	    array_push($pidmap[$tpid],$teid);
	}
    }

    # now dump to an acceptable format in javascript, sigh
    foreach ($pidmap as $tpid => $ta) {
	echo "exps['$tpid'] = new Array(";
	$elmstr = '';
	foreach ($ta as $teid) {
	    $elmstr .= "'$teid',";
	}
	$elmstr = rtrim($elmstr,",");
	echo "$elmstr);\n";
379 380 381
    }
}

382
# Close off the script we started above.
383 384 385 386 387 388
?>

</script>

<?php

389 390 391 392 393 394 395 396 397 398
#
# Setup data source information (i.e., fields in our database that 
# can be displayed).
#

# A map of colnames the user can refer to, to their name in queries we issue.
# This mapping handles rvalues with table prefixes or ' as ' aliases.
$colmap = array( 'node_id' => 'pm.node_id',
		 'hostname' => 'pm.hostname',
		 'plab_id' => 'pm.plab_id',
399
                 'plc_name' => 'ppi.plc_name',
400
		 'site' => 'psm.site_name',
401
		 'plab_nodegroup' => 'png.nodegrouplist',
David Johnson's avatar
David Johnson committed
402 403 404
                 # emulab columns
		 'nodestatus' => 'ns.status',
		 'nodestatustime' => 'ns.status_timestamp',
405
		 'nettype' => 'nat.type',
David Johnson's avatar
David Johnson committed
406 407 408 409
		 'unavail' => 'pnhs.unavail',
		 'jitdeduction' => 'pnhs.jitdeduct',
		 'successes' => 'pnhs.succnum',
		 'failures' => 'pnhs.failnum',
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
                 # comon columns
		 'resptime' => 'pcd.resptime','uptime' => 'pcd.uptime',
		 'lastcotop' => 'pcd.lastcotop',
		 'date' => 'from_unixtime(pcd.date) as codate',
		 'drift' => 'pcd.drift','cpuspeed' => 'pcd.cpuspeed',
		 'busycpu' => 'pcd.busycpu','syscpu' => 'pcd.syscpu',
		 'freecpu' => 'pcd.freecpu','1minload' => 'pcd.1minload',
		 '5minload' => 'pcd.5minload','numslices' => 'pcd.numslices',
		 'liveslices' => 'pcd.liveslices','connmax' => 'pcd.connmax',
		 'connavg' => 'pcd.connavg','timermax' => 'pcd.timermax',
		 'timeravg' => 'pcd.timeravg','memsize' => 'pcd.memsize',
		 'memact' => 'pcd.memact','freemem' => 'pcd.freemem',
		 'swapin' => 'pcd.swapin','swapout' => 'pcd.swapout',
		 'diskin' => 'pcd.diskin','diskout' => 'pcd.diskout',
		 'gbfree' => 'pcd.gbfree','swapused' => 'pcd.swapused',
		 'bwlimit' => 'pcd.bwlimit','txrate' => 'pcd.txrate',
David Johnson's avatar
David Johnson committed
426
		 'rxrate' => 'pcd.rxrate' );
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

# An array to help color data sources so users can more easily figure out
# what is what.
$colsrc = array( 'resptime' => 'CoMon','uptime' => 'CoMon',
                 'lastcotop' => 'CoMon','date' => 'CoMon','drift' => 'CoMon',
		 'cpuspeed' => 'CoMon','busycpu' => 'CoMon',
		 'syscpu' => 'CoMon','freecpu' => 'CoMon','1minload' =>'CoMon',
		 '5minload' => 'CoMon','numslices' => 'CoMon',
                 'liveslices' => 'CoMon','connmax' => 'CoMon',
                 'connavg' => 'CoMon','timermax' => 'CoMon',
		 'timeravg' => 'CoMon','memsize' => 'CoMon',
                 'memact' => 'CoMon','freemem' => 'CoMon',
                 'swapin' => 'CoMon','swapout' => 'CoMon',
                 'diskin' => 'CoMon','diskout' => 'CoMon',
                 'gbfree' => 'CoMon','swapused' => 'CoMon',
                 'bwlimit' => 'CoMon','txrate' => 'CoMon','rxrate' => 'CoMon',
		 
		 'node_id' => 'Emulab','hostname' => 'Emulab',
445
                 'plc_name' => 'Emulab',
446
		 'plab_id' => 'Emulab','unavail' => 'Emulab',
447
		 'site' => 'Emulab','nettype' => 'Emulab',
448 449
		 'jitdeduction' => 'Emulab','successes' => 'Emulab',
                 'failures' => 'Emulab','nodestatus' => 'Emulab',
450
                 'nodestatustime' => 'Emulab','plab_nodegroup' => 'Emulab' );
451

David Johnson's avatar
David Johnson committed
452 453 454 455 456
# The legend for fields.  The comon data is quoted from
# http://summer.cs.princeton.edu/status/legend.html .
$coldoc = array( 'node_id' => 'Emulab physical node id.',
		 'hostname' => 'Node hostname.',
		 'plab_id' => 'PLC node id number.',
457
		 'plc_name' => 'PLC name.',
458
		 'site' => 'Site with which the node is affiliated.',
David Johnson's avatar
David Johnson committed
459
		 'nodestatus' => 'Emulab node status; up or down.',
460 461 462
		 'nodestatustime' => 'Most recent heartbeat time from the ' . 
		   'node to Emulab.',
		 'nettype' => 'Type of network node lives on.',
David Johnson's avatar
David Johnson committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
		 'unavail' => 'Unavailability percentage as calculated by ' . 
		   'Emulab.  Is increased if the node is often up and down.',
		 'jitdeduction' => 'Amount added to total unavailability ' . 
		   'percent; depends on if the node is often up and down.',
		 'successes' => 'Number of recent Emulab sliver creation ' . 
		   'successes on the node.',
		 'failures' => 'Number of recent Emulab sliver creation ' . 
		   'failures on the node.',
		 'resptime' => '"How long it took for the node to respond ' . 
		   'with the CoMon data."',
		 'uptime' => '"How long the node has been up since last reboot."',
		 'lastcotop' => '"How long it\'s been since we were able ' . 
		   'to query the CoTop sensor on the node."',
		 'date' => '"Reflects what time the node thinks it is, ' . 
		   'slightly adjusted for skew."',
		 'drift' => '"How far off from the median node time is ' . 
		   'this node\'s time."',
		 'cpuspeed' => '"The speed of the processor in gigahertz."',
		 'busycpu' => '"Gives the % of time the CPU is utilized."',
		 'syscpu' => '"Percentage of time the CPU is spending in ' . 
		   'the OS"',
		 'freecpu' => '"Indicates how much of the CPU a spin-loop ' . 
		   'was able to obtain"',
		 '1minload,5minload' => '"Load averages (# of runnable ' . 
		   'processes) for the past 1 and and 5 minutes. The ' . 
		   '1-minute value is actually the maximum 1-minute value ' . 
		   'over the past 5 minutes."',
		 'numslices' => '"Indicates the maximum number of slices ' . 
		   'reported by slicestat over the past 5 minutes, measured ' .
		   'once every 30 seconds."',
		 'liveslices' => '"The count of how many slices used at ' . 
		   'least one-tenth of one percent of the CPU in the last 5 ' .
		   'minutes, measured at 30-second intervals."',
		 'connmax,connavg' => '"We [CoMon] have a small client and ' . 
		   'server demon that performs a TCP accept every second. ' . 
		   'The Conn Max and Conn Avg fields show the maximum and ' . 
		   'average times of the connect, over the past 5 minutes. ' . 
		   'These values are in milliseconds."',
		 'timermax,timeravg' => '"On each node, we [CoMon] run a ' . 
		   'timer that tries to sleep for about 1ms at a time. The ' . 
		   'Timer Max field shows the maximum interval between ' . 
		   'timers and the Timer Avg field gives the average ' . 
		   'interval, over the past 5 minutes. The hope is that ' . 
		   'this gives some insight into the scheduler behavior. ' . 
		   'These values are in milliseconds."',
		 'memsize' => '"The node\'s memory size in GB."',
		 'memact' => '"Percentage of the memory is in the operating ' .
		   'system\'s "active" pool."',
		 'freemem' => '"The output of a test program that tries to ' . 
		   'grab 100MB of memory on the node."',
		 'swapin,swapout' => '"The amount of memory swapped in/out ' . 
		   'of disk per second, as obtained from vmstat."',
		 'diskin,diskout' => '"The blocks in/out per second, as ' . 
		   'obtained from vmstat."',
		 'gbfree' => '"The amount of free space left on the disk, ' . 
		   'in GB."',
		 'swapused' => '"The percentage of the swap space consumed."',
		 'bwlimit' => 'Node bandwidth limit.',
		 'txrate,rxrate' => '"Transmit and receive rates (in Kbps), ' .
		   'taken as a sum of per-slice activity."' );

524 525 526
# An array containing colors for data sources.
$colcol = array( 'Emulab' => '#cde4f3', 'CoMon' => '#ffedd7' );

David Johnson's avatar
David Johnson committed
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561

# Some help info.
$help = array();
$help['project/experiment'] = 
    "Show only nodes in one of your swapped-in experiments.";
$help['alive nodes'] = 
    "Show only nodes that are considered alive by Emulab (had" . 
    " heartbeat in last 20min) or by CoMon (CoTop in last 10 mins).";
$help['hostname'] = "Show only nodes matching this alphanumeric string" . 
    " or Perl5 regular expression.";
$help['custom query'] = "Show only nodes whose data fields match this" . 
    " expression.  Expressions are simple boolean queries such as" . 
    " (nodestatus == up and unavail < 0.25).  You can use the operators" . 
    " '<','>','<=','>=','==','!='.  Barewords are considered strings; if" . 
    " you must encode whitespace, surround the text with quotes.";
$help['show/order fields'] = "Move fields from the left-side box to the" . 
    " right-side box; fields in the right-side box are displayed.  In the" . 
    " right-side box, move fields up and down to change left-to-right " . 
    " display order.";
$help['sort fields'] = "Sort returned data in either ascending or descending" .
    " order, in the order of the fields.  Move the fields up and down to" . 
    " change the ordering.";
$help['nodes per page'] = "Show only N nodes per page.";
$help['fully connected nodes from Flexlab data'] = 
    "Show N fully-connected nodes based on latest Flexlab NxN tests." . 
    " Possible nodes are chosen from the pool of nodes selected by any" . 
    " filters you have enabled.";
$help['save fully connected set without recomputing'] = 
    "Save the computed fully-connected set for additional filtering" . 
    "(saves slight computation delay, depending on size of set).";
$help['recompute fully-connected set'] = 
    "Force a recomputation of the fully-connected set based on the current" . 
    " filter set.";


562
# We have to fix which node id we select based on if we are querying the 
563
# reserved table (we need phys node id, but have to join reserved to nodes
564
# to get it).
565 566
if (isset($experiment)) {
    $colmap['node_id'] = 'n.phys_nodeid';
567 568
}

569 570 571 572 573 574
# Strip off the table prefixes or use 'as' aliases; mysql driver does not 
# seem to include them as column names in mysql_fetch_array (needed for 
# converting user-visible col names to db select-able col names).
#
# Also form a second array that DOES have table prefixes, but only has aliases
# (need this for the where clause).
575
$colmap_mysqlnames = array();
576 577
$colmap_mysqlnames_where = array();

578
foreach ($colmap as $k => $v) {
579
    $had_as = false;
580
    if (strpos($v," as ") != false) {
581
	$sa = explode(' as ',$v);
582
	$nv = $sa[1];
583
	$had_as = true;
584
    }
585 586 587 588 589 590 591 592 593
    else {
	$sa = explode('.',$v);
	$nv = $sa[0];
	if (count($sa) == 2) {
	    $nv = $sa[1];
	}
	elseif (count($sa) > 2) {
	    $nv = implode('.',array_slice($sa,1));
	}
594 595 596
    }

    $colmap_mysqlnames[$k] = $nv;
597 598 599 600 601 602
    if ($had_as) {
	$colmap_mysqlnames_where[$k] = $nv;
    }
    else {
	$colmap_mysqlnames_where[$k] = $v;
    }
603
}
604

605
# Default columns displayed, in this order.
606
$defcols = array( 'node_id','hostname','plab_nodegroup','nodestatus',
607 608
                  'nodestatustime','unavail','5minload','freemem',
                  'txrate','rxrate','date', );
609

610
# Default sort and included columns
611
$defsortcols = array( 'unavail','5minload' );
612

613
# Assign a default set of columns if unset.
614 615
if (!isset($cols) || count($cols) == 0) {
    $cols = $defcols;
616 617
}

618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
# If selecting nodes, make sure that node_id is included in our queries.
if (isset($selectable) && $selectable) {
    $foundit = false;
    foreach ($cols as $c) {
	if ($c == 'node_id') {
	    $foundit = true;
	}
    }
    if (!$foundit) {
	$ta = array('node_id');
	foreach ($cols as $c) {
	    array_push($ta,$c);
	}
	$cols = $ta;
    }
}

635
# Assign a default set of sort columns and sort order if unset.
636 637 638 639 640
if (!isset($sortcols)) {
    $sortcols = $defsortcols;
    $sortdir = 'asc';
}

641
# Ensure all cols (vis/order and sort) are valid.  Remove them if not.
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
$tmpcols = array();
foreach ($cols as $sc) {
    if (array_key_exists($sc,$colmap)) {
	array_push($tmpcols,$sc);
    }
    else {
	array_push($opterrs,"Invalid display column '$sc'.");
    }
}
$cols = $tmpcols;

$tmpcols = array();
foreach ($sortcols as $sc) {
    if (array_key_exists($sc,$colmap)) {
        array_push($tmpcols,$sc);
    }
    else {
        array_push($opterrs,"Invalid sort column '$sc'.");
    }
}
$sortcols = $tmpcols;
663

664
# Ensure sortdir is valid (asc/desc).
665 666 667 668
if (isset($sortdir) && $sortdir != 'asc' && $sortdir != 'desc') {
    array_push($opterrs,"Invalid sort dir '$sortdir'; changing to 'asc'!");
    $sortdir = 'asc';
}
669

670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
#
# Figure out if the user selected any new plab nodes, and update the
# selectionlist if so.
#
if (isset($selectionlist) && $selectionlist[0] == 'ALLINSEARCH') {
    ;
}
elseif ((isset($newpgsel) && count($newpgsel) > 0) 
	|| (isset($pgsel) && count($pgsel) > 0)) {
    if (!isset($pgsel)) {
	$pgsel = array();
    }
    if (!isset($newpgsel)) {
	$newpgsel = array();
    }
    if (!isset($selectionlist) || $selectionlist == '') {
	$selectionlist = array();
    }

    # compare newpgsel and pgsel; see which are new, and add new ones
    # to selectionlist
    $ra = array();
    $aa = array();
    foreach ($newpgsel as $i) {
	if (!in_array($i,$pgsel)) {
	    array_push($aa,$i);
	}
    }
    foreach ($pgsel as $os) {
	if ($os != '' && !in_array($os,$newpgsel)) {
	    array_push($ra,$os);
	}
    }
    foreach ($aa as $elm) {
	if (!in_array($elm,$selectionlist)) {
	    array_push($selectionlist,$elm);
	}
    }
    $da = array_diff($selectionlist,$ra);
    $selectionlist = $da;
}
711

712 713 714 715 716 717 718 719 720
#
# Grab the base types for all plc nodetypes.  Used in pm_buildqueryinfo below.
#
$basenodetypes = array();
$qres = DBQueryFatal("select type from node_types where isplabdslice=1");
while ($row = mysql_fetch_array($qres)) {
    array_push($basenodetypes,$row['type']);
}

721 722 723 724 725 726 727
if (empty($basenodetypes)) {
    echo "<p>This site has not added any PlanetLab-style node types.</p>";
    echo "<p>Email $TBMAILADDR_OPS if this confuses you!</p>";
    PAGEFOOTER();
    exit(0);
}

728 729 730 731 732 733 734
#
# Next, build query:
#

# Grab query parts
$qbits = pm_buildqueryinfo();
$select_s = $qbits[0]; $src_s = $qbits[1]; $filter_s = $qbits[2]; 
735
$sort_s = $qbits[3]; $group_s = $qbits[4]; $pag_s = $qbits[5];
736 737 738

# query that counts num data rows
$qcount = "select count(" . $colmap['node_id'] . ") as num" . 
739
    " from $src_s $filter_s $group_s";
740

741 742 743
# query that gets data (note that if we need to do postfiltering, we
# manually paginate the data!)
if ($mustpostfilter) {
744
    $q = "select $select_s from $src_s $filter_s $group_s $sort_s";
745 746
}
else {
747
    $q = "select $select_s from $src_s $filter_s $group_s $sort_s $pag_s";
748
}
749

750 751 752
#echo "qcount = $qcount<br><br>\n";
#echo "q = $q<br><br>\n";

753 754 755
$qres = DBQueryFatal($qcount);
if (mysql_num_rows($qres) != 1) {
    TBERROR("Unexpected number of rows in count query!");
756
}
757 758
$row = mysql_fetch_array($qres);
$totalrows = $row['num'];
759

760 761 762 763 764 765 766 767 768
#
# Grab the query data!  Hate to store it in memory, but because of the page
# layout we must.
#
$qres = DBQueryFatal($q);
$data = array();
while ($row = mysql_fetch_array($qres)) {
    array_push($data,$row);
}
769

770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
#
# Post-db filtering
#
if ($mustpostfilter) {
    $data = pm_filterdata($data);
    $totalrows = count($data);

    # Now we must manually paginate (i.e., we just do offset/limit ourself).
    $i = 0;
    $finaldata = array();
    foreach ($data as $row) {
	if ($i >= $offset && $i < ($offset+$limit)) {
	    array_push($finaldata,$row);
	}
	++$i;
    }

    $data = $finaldata;
}

David Johnson's avatar
David Johnson committed
790 791 792 793 794 795 796 797 798 799 800
#
# Select all if desired.
#
if (isset($selectionlist) && count($selectionlist) == 1 
    && $selectionlist[0] == 'ALLINSEARCH') {
    $selectionlist = array();    
    foreach ($data as $row) {
	array_push($selectionlist,$row[$colmap_mysqlnames['node_id']]);
    }
}

801
#
802
# Draw nondata part of page
803
#
804 805 806
pm_shownondata();

echo "<br><br>";
807

808
#
809
# Finally, execute main data query and display resulting data.
810
#
811
pm_showtable($totalrows,$data);
812

David Johnson's avatar
David Johnson committed
813 814 815 816 817
#
# Say what it all means.
#
pm_showlegend();

David Johnson's avatar
David Johnson committed
818 819 820 821 822
#
# Provide some help.
#
pm_showhelp();

823 824 825 826
#
# Standard Testbed Footer
#
PAGEFOOTER();
827

828 829 830 831 832

#
# Utility functions.
#

833 834 835 836 837 838 839
#
# Filters the rows returned by a sql query and returns a new set of
# rows that the filter allows though.
#
function pm_filterdata($data) {
    global $hostfilter,$hf_regexp;
    global $colmap_mysqlnames;
840
    global $upnodefilter,$experiment;
841 842 843
    global $flexlabfilter,$flexlabfcsize,$flexlabsave,$flexlabrecompute;
    global $flexlabfcnodes;
    global $FLEXLAB_XMLRPC_SRV,$FLEXLAB_XMLRPC_SRV_PORT;
844

845 846 847 848
    global $opterrs;

    # Array of node_ids; must be reduced after each filter.
    $remaining_nodes = array();
849 850 851 852

    #
    # Filter all rows based on regexp (expensive but no good way).
    #
853 854 855 856 857 858 859 860 861
    if ($hf_regexp) {
	$rnatmp = array();
	$rnodes = array();
	foreach ($data as $row) {
	    if (preg_match("/$hostfilter/",
			   $row[$colmap_mysqlnames['hostname']]) > 0) {
		array_push($rnatmp,$row);
		$rnodes[$row[$colmap_mysqlnames['node_id']]] = 1;
	    }
862
	}
863 864 865 866
	$remaining_nodes = $rnatmp;
    }
    else {
	$remaining_nodes = $data;
867 868
    }

869 870 871 872 873 874
    #
    # If filtering on $experiment AND $upnodefilter == 'emulab', we must 
    # postfilter to see if the nodes really were in emulab-ops/plabnodes.
    #
    if (isset($experiment) 
	&& (isset($upnodefilter) && $upnodefilter == 'emulab')) {
875
        #echo "FOO: rns = " . count($remaining_nodes) . "<br>\n";
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
	$upnodes = array();
	$pnq = "select n.node_id from nodes as n" . 
	    " left join reserved as r on n.node_id=r.node_id" . 
	    " where n.type='pcplabphys'" . 
	    "  and !(r.pid='emulab-ops' and r.eid='hwdown')";

	$pnqres = DBQueryFatal($pnq);
	while ($row = mysql_fetch_array($pnqres)) {
	    array_push($upnodes,$row['node_id']);
	}

	$rnatmp = array();
	foreach ($remaining_nodes as $row) {
	    if (in_array($row[$colmap_mysqlnames['node_id']],$upnodes)) {
		array_push($rnatmp,$row);
	    }
	}
	$remaining_nodes = $rnatmp;
894
        #echo "FOO: rns = " . count($remaining_nodes) . "<br>\n";
895 896
    }

897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
    #
    # Flexlab "goodnodes" filter.  Contacts xmlrpc server on ops and gets a 
    # list of fully-connected nodes.
    #
    $rnatmp = array();
    if (isset($flexlabfilter) && $flexlabfilter) {
	if (isset($flexlabsave) && $flexlabsave 
	    && isset($flexlabfcnodes)) {
            # Do not make another expensive xmlrpc call -- just filter 
            # against the current flexlab set.
	    foreach ($remaining_nodes as $row) {
		if (in_array($row[$colmap_mysqlnames['node_id']],
			     $flexlabfcnodes)) {
		    array_push($rnatmp,$row);
		}
	    }
	}
	else {
            # Make the call.
	    $xargs = array( 'size' => $flexlabfcsize );
	    $ta = array();
	    foreach ($remaining_nodes as $row) {
		array_push($ta,$row[$colmap_mysqlnames['node_id']]);
	    }
	    if (count($ta) > 0) {
		$xargs['nodefilter'] = $ta;
		$xargs['filtertype'] = True;
	    }
	    $response = do_xmlrpc($FLEXLAB_XMLRPC_SRV,$FLEXLAB_XMLRPC_SRV_PORT,
				  'flexlab.getFullyConnectedSet',$xargs);
927 928
	    if (is_array($response) && is_array($response[1]) && $response[0] 
		&& !xmlrpc_is_fault($response[1])) {
929 930 931 932 933 934 935 936 937 938 939 940
                #echo "xresp = '" . implode(',',$response[1]) . "'<br>\n";
		$rnatmp = array();
		$flexlabfcnodes = array();
		foreach ($remaining_nodes as $row) {
		    if (in_array($row[$colmap_mysqlnames['node_id']],
				 $response[1])) {
			array_push($rnatmp,$row);
			array_push($flexlabfcnodes,
				   $row[$colmap_mysqlnames['node_id']]);
		    }
		}
	    }
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
	    else {
		if (is_array($response) || !is_array($response[1])) {
		    array_push($opterrs,"XMLRPC Server Error: " . 
			       $response[1] . "!\n");
		}
		elseif (!$response[0]) {
		    array_push($opterrs,
			       "Error in XMLRPC transport: " . $response[1]);
		}
		elseif (xmlrpc_is_fault($response[1])) {
		    array_push($opterrs,
			       "XMLRPC fault: code=".$response[1]['faultCode'].
			       "; msg=".$response[1]['faultString']);
		}
		else {
		    array_push($opterrs,"Completely unknown XMLRPC error!");
		}
958 959 960
		$rnatmp = $remaining_nodes;
		$flexlabfcnodes = array();
	    }
David Johnson's avatar
David Johnson committed
961 962 963 964 965 966 967 968 969 970 971

            # Now make sure that the save checkbox gets marked, and that 
            # recompute is off (try to save computation and keep result set
            # stable).
	    if (isset($flexlabrecompute)) {
		$flexlabrecompute = False;
	    }

            # Auto-save if any nodes were returned.
	    if (count($flexlabfcnodes) > 0) {
		$flexlabsave = True;
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
	    }
	}
	$remaining_nodes = $rnatmp;
    }
    
    return $remaining_nodes;
}

function do_xmlrpc($host,$port,$method,$argdict) {
    $xreq = xmlrpc_encode_request($method,$argdict);
    #echo "xreq = '<!-- " . $xreq . " -->'<br>\n";
    $xheaders = array( 'Content-type: text/xml',
		       "Content-length: " . strlen($xreq) );
    $c = curl_init();
    curl_setopt($c,CURLOPT_URL,"http://$host:$port/");
    curl_setopt($c,CURLOPT_RETURNTRANSFER,1);
David Johnson's avatar
David Johnson committed
988
    curl_setopt($c,CURLOPT_TIMEOUT,120);
989 990 991 992 993 994 995 996 997 998 999 1000 1001
    curl_setopt($c,CURLOPT_HTTPHEADER,$xheaders);
    curl_setopt($c,CURLOPT_POSTFIELDS,$xreq);

    $xresp = curl_exec($c);
    if (curl_errno($c)) {
	$errstr = curl_error($c);
	curl_close($c);
	return array(False,$errstr);
    }
    else {
	curl_close($c);
	return array(True,xmlrpc_decode($xresp));
    }
1002 1003
}

David Johnson's avatar
David Johnson committed
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
function pm_showhelp() {
    global $help;

    echo "<h3><a name='help'>Search Help</a></h3>\n";
    echo "<p>Filters:</p>\n";
    echo "<p><ul>\n";
    foreach ($help as $hi => $hv) {
	$link_hi = str_replace(" ","_",$hi);
	echo "<li><b><a name='$link_hi'>$hi</a></b> $hv</li>\n";
    }
    echo "</ul></p>\n";
}

David Johnson's avatar
David Johnson committed
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
function pm_showlegend() {
    global $colsrc,$coldoc;

    echo "<h3><a name='legend'>Data Legend</a></h3>\n";
    echo "<p>Emulab data:</p>\n";
    echo "<p><ul>\n";
    foreach ($coldoc as $col => $doc) {
	$rcol = $col;
	if (strstr($col,',')) {
	    $ta = explode(',',$col);
	    $rcol = $ta[0];
	}
	if ($colsrc[$rcol] == 'Emulab') {
	    echo "<li><b>$col</b> " . $coldoc[$col];
	}
    }
    echo "</p></ul>\n";
    echo "<p>CoMon data (quotes are from <a href='" . 
	"http://summer.cs.princeton.edu/status/legend.html'>the CoMon " . 
	"legend</a>):</p>\n";
    echo "<p><ul>\n";
    foreach ($coldoc as $col => $doc) {
	$rcol = $col;
	if (strstr($col,',')) {
	    $ta = explode(',',$col);
	    $rcol = $ta[0];
	}
	if ($colsrc[$rcol] == 'CoMon') {
	    echo "<li><b>$col</b> " . $coldoc[$col];
	}
    }
    echo "</p></ul>\n";
}

1051 1052 1053 1054
#
# Show the non-data part of the page:
#
function pm_shownondata() {
1055
    global $pagelayout,$selectable;
1056 1057 1058 1059
    global $opterrs;

    echo "<table class='stealth'>\n";
    echo "<tr>\n";
1060
    echo "<td class='stealth' valign='top' width='1%'>\n";
1061
    SUBMENUSTART("Page Options");
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
    if (!isset($selectable) || !$selectable) {
	$url = pm_buildurl(array( 'selectable' => 'yes' ));
	WRITESUBMENUBUTTON("Select Nodes",$url);
    }
    else {
	$url = pm_buildurl(array( 'selectable' => 'no' ));
	WRITESUBMENUBUTTON("Hide Selection",$url);
    }
    WRITESUBMENUBUTTON("<span id='searchdivtw'>Hide</span> Search",
		       "javascript:divflipvis(\"searchdiv\",\"searchdivtw\")");
1072 1073 1074 1075 1076 1077 1078 1079
    if (isset($pagelayout) && $pagelayout == 'minimal') {
	$url = pm_buildurl(array('pagelayout' => ''));
	WRITESUBMENUBUTTON("Show Sidebar",$url);
    }
    else {
	$url = pm_buildurl(array('pagelayout' => 'minimal'));
	WRITESUBMENUBUTTON("Hide Sidebar",$url);
    }
1080 1081
    WRITESUBMENUDIVIDER();
    WRITESUBMENUBUTTON("Data Legend","#legend");
David Johnson's avatar
David Johnson committed
1082
    WRITESUBMENUBUTTON("Search Help","#help");
1083 1084
    # Ugh -- cannot use SUBMENUEND cause it inserts a weird <td></td> combo
    # SUBMENUEND();
1085 1086 1087 1088 1089 1090 1091
    #
    # XXX: commented out until I can figure this out...
    #
    #if (1) {
    #SideBarSubMenuRender();
    #}
    #else {
1092 1093 1094 1095
        echo "    </ul>\n";
        echo "    <!-- end submenu -->\n";
        echo "</td>\n";
        echo "<td class='stealth' halign='left' valign='top' width='99%'>\n";
1096
    #}
1097 1098 1099 1100 1101 1102 1103 1104
    if (count($opterrs) > 0) {
	echo "<span style='color: red'>Errors:<br>\n";
	foreach ($opterrs as $e) {
	    echo "&nbsp;&nbsp;$e<br>\n";
	}
	echo "</span><br>\n";
    }
    pm_showsearch();
1105 1106 1107 1108
    if (isset($selectable) && $selectable) {
	echo "<br>\n";
	pm_showselection();
    }
1109 1110 1111 1112 1113 1114 1115 1116 1117
    echo "</td>\n";
    echo "</tr>\n";
    echo "</table>\n";
}

#
# Show the selection box
#
function pm_showselection() {
1118 1119
    global $selectionlist;

1120 1121 1122
    echo "<div style='padding: 8px; padding-left: 12px; padding-right: 12px;";
    echo " border: 2px solid silver; visibility: visible; margin-left: 4px'";
    echo " id='selectiondiv'>\n";
1123 1124 1125 1126

    echo "<form name='nodeeditsel' id='nodeeditsel' action='plabmetrics.php3' method='post'>\n";
    echo pm_gethiddenfields('nodeeditsel');

1127
    echo "<b>Selected nodes</b>:<br>\n";
1128 1129 1130 1131 1132 1133

    if (!isset($selectionlist)) {
	echo "None.\n";
    }
    else {
	echo "<textarea name='selectionlist' rows='4' cols='64'>\n";
1134
	echo implode(', ',$selectionlist);
1135 1136 1137
	echo "</textarea>\n";
	echo "<br>\n";
	echo "<input type='submit' value='Save Selection Edits'>\n";
David Johnson's avatar
Bug.  
David Johnson committed
1138 1139 1140
	echo "<input type='button' value='Clear Selection'" . 
	    " onClick='setFormElementValue(\"nodeeditsel\"," .
	    " \"selectionlist\",\"\")'>\n";
1141
    }
1142
    echo "</form>\n";
1143

1144 1145 1146
    # We need a separate mini-form that submits to plab_ez so that we can
    # open a new window without using javascript (only the <form> element
    # can accept a target attribute).
David Johnson's avatar
David Johnson committed
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
    if (isset($selectionlist)) {
	echo "<form name='toplabez' id='toplabez' action='plab_ez.php3'" . 
	    " method='post' target='_blank'" . 
	    " onSubmit='setFormElementValue(" . 
	    "   \"toplabez\",\"formfields[nodelist]\"," . 
	    "   (getFormElementValue(\"nodeeditsel\",\"selectionlist\")).replace(/\\,\s*/g,\" \"))'>\n";
        # plab_ez in advanced mode expects to see all these vars.
	echo "<input type='hidden' name='formfields[nodelist]' value=''>\n";
	echo "<input type='hidden' name='advanced' value='yes'>\n";
        # This is a hack.  We allow the submit button value to be submitted
        # to plab_ez so that plab_ez will figure out that it is supposed to
        # be in advanced mode.  Before changing the value (name) of this 
        # button, make sure you understand how plab_ez decides if it is in 
        # advanced mode.
	echo "<input type='submit' name='submit' value='Create PlanetLab Slice'>\n";
	echo "</form>\n";
1163
    }    
1164

1165 1166 1167 1168 1169 1170 1171 1172 1173
    echo "</div>\n";
}

#
# Show the search box.
#
function pm_showsearch() {
    global $limit,$DEF_LIMIT,$offset;
    global $upnodefilter,$pid,$eid,$sortcols,$cols,$sortdir;
1174
    global $pidmap,$colmap,$colcol,$colsrc;
1175
    global $hostfilter;
1176 1177
    global $pagelayout;
    global $userquery;
1178 1179
    global $flexlabfilter,$flexlabfcsize,$flexlabsave,$flexlabrecompute;
    global $flexlabfcnodes;
1180

1181 1182 1183
    echo "<div style='padding: 8px; padding-left: 12px; padding-right: 12px;";
    echo " border: 2px solid silver; visibility: visible; margin-left: 4px'";
    echo " id='searchdiv'>\n";
1184 1185
    echo "<form name='plsearchform' id='plsearchform'" .
        " action='plabmetrics.php3' method='post' " .
1186 1187 1188 1189
        # We manually set a comma-delineated string comprised of the col names
        # in the display cols select box.  This way, we still only must handle
        # the cols var, instead of both the cols and selcols[] vars on 
        # search box post vs url get.
1190
        " onSubmit='setFormElementValue(\"plsearchform\",\"cols\",".
1191 1192
	"dynselect.getOptsAsDelimString(document.getElementById(\"selcols\"),".
	"\",\",false)); " . 
1193
	"setFormElementValue(\"plsearchform\",\"sortcols\",".
1194 1195 1196
	"dynselect.getOptsAsDelimString(document.getElementById(\"selsortcols\"),".
	"\",\",false));" . "'" . ">\n";
    
1197 1198
    # Also includes hidden vals for col selection and sorting.
    echo pm_gethiddenfields('plsearchform');
1199 1200 1201
    
    # Only draw pid/exp boxes if the user can access swapped-in experiments 
    # with pl nodes.
1202
    if (count($pidmap)) {
David Johnson's avatar
David Johnson committed
1203 1204 1205
	echo "Show <b>only nodes in project/experiment</b>";
	echo " <a href='#project/experiment'>[?]</a>";
	echo ":\n";
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
	echo "<select name='pid' id='pid'";
	echo " onChange='dynselect.setOptions(";
	echo "   document.getElementById(\"eid\"),";
	echo "   exps[dynselect.getSelectedOption(document.getElementById(\"pid\"))],";
	echo "   null,expValues)'>\n";
	echo "  <option value=''";
	$selpid = '';
	if (!isset($pid) || $pid == '') {
	    echo " selected";
	}
	echo ">No Project</option>\n";
	foreach (array_keys($pidmap) as $tpid) {
	    echo "  <option value='$tpid'";
	    if (isset($pid) && $pid == $tpid) {
		echo " selected";
		$selpid = $tpid;
	    }
	    echo ">$tpid</option>\n";
	}
	echo "</select>\n";
	echo "<select name='eid' id='eid'>\n";
	if ($selpid == '') {
	    echo "  <option value='' selected>No Experiment</option>\n";
	}
	else {
	    foreach ($pidmap[$selpid] as $teid) {
		echo "  <option value='$teid' ";
		if (isset($eid) && $eid == $teid) {
		    echo " selected";
		}
		echo ">$teid</option>\n";
	    }
	}
	echo "</select>\n";
	echo "<br>\n";
    }

    # handle basic upnodefiltering
David Johnson's avatar
David Johnson committed
1244 1245 1246
    echo "Show <b>only nodes considered alive</b> ";
    echo " <a href='#alive_nodes'>[?]</a>";
    echo " by ";
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
    echo "<input type='checkbox' name='upnodefilter' value='emulab'";
    if (isset($upnodefilter) && $upnodefilter == 'emulab') {
	echo " checked";
    }
    echo " /> Emulab ";
    echo "<input type='checkbox' name='upnodefilter' value='comon'";
    if (isset($upnodefilter) && $upnodefilter == 'comon') {
	echo " checked";
    }
    echo " /> CoMon\n";
    echo "<br>\n";

    # hostname filtering (not included in normal user query because
    # we support perl5 regexps for filtering on the data as post-db)
1261
    echo "Show only <b>hostnames that match</b> ";
David Johnson's avatar
David Johnson committed
1262
    echo " <a href='#hostname'>[?]</a> ";
1263 1264 1265 1266 1267 1268 1269
    $thf = '';
    if (isset($hostfilter)) {
	$thf = $hostfilter;
    }
    echo "<input type='text' value='$thf' name='hostfilter' size='25'>\n";
    echo "<br>\n";

1270
    # custom user query
David Johnson's avatar
David Johnson committed
1271 1272 1273
    echo "Filter with <b>custom query</b> ";
    echo " <a href='#custom_query'>[?]</a>";
    echo ": \n";
1274 1275 1276
    $tuq = '';
    if (isset($userquery)) {
	$tuq = $userquery;
1277
    }
1278 1279
    echo "<input type='text' value='$tuq' name='userquery' size='64'>\n";
    echo "<br>\n";
1280 1281 1282 1283 1284 1285 1286


    # choose which columns are shown and what the column order should be
    # first do the headers -- col order and col sort get munged here!
    echo "<table class='stealth'>\n";
    echo "<tr>\n";
    echo "<td class='stealth' colspan='4'>\n";
David Johnson's avatar
David Johnson committed
1287 1288 1289
    echo "<b>Show and reorder</b> ";
    echo " <a href='#show/order_fields'>[?]</a>";
    echo " fields:<br>\n";
1290 1291 1292
    echo "</td>\n";
    # space
    echo "<td class='stealth' colspan='4'>\n";
David Johnson's avatar
David Johnson committed
1293
    echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
1294 1295
    echo "</td>\n";
    echo "<td class='stealth' colspan='4'>\n";
David Johnson's avatar
David Johnson committed
1296 1297 1298
    echo "<b>Sort selected</b> ";
    echo " <a href='#sort_fields'>[?]</a>";
    echo " fields:\n";
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
    echo "</td>\n";
    echo "</tr>\n";
    
    echo "<tr>\n"; 
    echo "<td class='stealth'>\n";
    echo "<select name='acols' id='acols' multiple size='6'>\n";
    # Compute the diff between avail cols and the current col selection.
    # avail cols go in the left box, current shown cols in the right.
    $da = array_diff(array_keys($colmap),$cols);
    foreach ($da as $foo) {
1309 1310 1311
	$colstr = $colcol[$colsrc[$foo]];
	echo "  <option style='background-color: $colstr' value='$foo'>";
	echo "$foo</option>\n";
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
    }
    echo "</select>\n";
    echo "</td>\n";
    echo "<td class='stealth'>\n";
    echo "<input type='button' name='rb1' value='>>' " . 
	" onClick='dynselect.moveSelectedOptions(" . 
	"document.getElementById(\"acols\")," . 
	"document.getElementById(\"selcols\"))'" . ">\n";
    echo "<br>\n";
    echo "<input type='button' name='lb1' value='<<' " . 
	" onClick='dynselect.moveSelectedOptions(" . 
	"document.getElementById(\"selcols\")," . 
	"document.getElementById(\"acols\"))'" . ">\n";
    echo "<br>\n";
    echo "</td>\n";
    echo "<td class='stealth'>\n";
    echo "<select name='selcols' id='selcols' multiple size='6'>\n";
    foreach ($cols as $foo) {
1330 1331 1332
	$colstr = $colcol[$colsrc[$foo]];
	echo "  <option style='background-color: $colstr' value='$foo'>";
	echo "$foo</option>\n";
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
    }
    echo "</select>\n";
    echo "</td>\n";
    echo "<td class='stealth'>\n";
    echo "<input type='button' name='up1' value='up' " . 
	" onClick='dynselect.moveSelectedOptionsVertically(" . 
	"document.getElementById(\"selcols\"),0)'" . ">\n";
    echo "<br>\n";
    echo "<input type='button' name='down1' value='down' " . 
	" onClick='dynselect.moveSelectedOptionsVertically(" . 
	"document.getElementById(\"selcols\"),1)'" . ">\n";

    # space
    echo "<td class='stealth' colspan='4'>\n";
    echo "&nbsp;&nbsp;&nbsp;&nbsp;";
    echo "</td>\n";

    # now widgets for column sort (note that this is different from order)
    echo "<td class='stealth'>\n";
    echo "<select name='acols2' id='acols2' multiple size='6'>\n";
    # Compute the diff between avail cols and the current col selection.
    # avail cols go in the left box, current shown cols in the right.
    $da2 = array_diff(array_keys($colmap),$sortcols);
    foreach ($da2 as $foo) {
1357 1358 1359
	$colstr = $colcol[$colsrc[$foo]];
	echo "  <option style='background-color: $colstr' value='$foo'>";
	echo "$foo</option>\n";
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
    }
    echo "</select>\n";
    echo "</td>\n";
    echo "<td class='stealth'>\n";
    echo "<input type='button' name='rb2' value='>>' " . 
	" onClick='dynselect.moveSelectedOptions(" . 
	"document.getElementById(\"acols2\")," . 
	"document.getElementById(\"selsortcols\"))'" . ">\n";
    echo "<br>\n";
    echo "<input type='button' name='lb2' value='<<' " . 
	" onClick='dynselect.moveSelectedOptions(" . 
	"document.getElementById(\"selsortcols\")," . 
	"document.getElementById(\"acols2\"))'" . ">\n";
    echo "<br>\n";
    echo "</td>\n";
    echo "<td class='stealth'>\n";
    echo "<select name='selsortcols' id='selsortcols' multiple size='6'>\n";
    foreach ($sortcols as $foo) {
1378 1379 1380
	$colstr = $colcol[$colsrc[$foo]];
	echo "  <option style='background-color: $colstr' value='$foo'>";
	echo "$foo</option>\n";
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
    }
    echo "</select>\n";
    echo "</td>\n";
    echo "<td class='stealth'>\n";
    echo "<input type='button' name='up2' value='up' " . 
	" onClick='dynselect.moveSelectedOptionsVertically(" . 
	"document.getElementById(\"selsortcols\"),0)'" . ">\n";
    echo "<br>\n";
    echo "<input type='button' name='down2' value='down' " . 
	" onClick='dynselect.moveSelectedOptionsVertically(" . 
	"document.getElementById(\"selsortcols\"),1)'" . ">\n";
    echo "<br><br>\n";
    echo "<select name='sortdir' id='sortdir'>\n";
    echo "  <option value='asc' ";
    if ($sortdir == 'asc') {
	echo " selected";
    }
    echo ">asc</option>\n";
    echo "  <option value='desc' ";
    if ($sortdir == 'desc') {
	echo " selected";
    }
    echo ">desc</option>\n";
    echo "</select>\n";
    echo "</td>\n";

    echo "</tr>\n";

    echo "</table>\n";
1410 1411 1412 1413 1414 1415 1416 1417 1418

    # handle limit/offset
    $defLimits = array( '10' => 10,'20' => 20,'50' => 50,
		        '100' => 100,'All' => 0 );
    echo "Show ";
    echo "<select name='limit' id='limit'";
    # XXX
    # if the user changes the limit we change the offset to 0 so they
    # do not get funky page offset stuff