node_defs.php 51 KB
Newer Older
1
2
<?php
#
3
# Copyright (c) 2006-2017 University of Utah and the Flux Group.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#
include_once("osinfo_defs.php");

#
# A cache to avoid lookups. Indexed by node_id.
#
$node_cache = array();

# Constants for Show() method.
define("SHOWNODE_NOFLAGS",	0);
define("SHOWNODE_SHORT",	1);
define("SHOWNODE_NOPERM",	2);

class Node
{
    var	$node;

    #
    # Constructor by lookup on unique index.
    #
    function Node($node_id) {
	$safe_node_id = addslashes($node_id);

	$query_result =
	    DBQueryWarn("select * from nodes where node_id='$safe_node_id'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->node = NULL;
	    return;
	}
	$this->node = mysql_fetch_array($query_result);
    }

    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->node);
    }

    # Lookup by node_id
    function Lookup($node_id) {
	global $node_cache;

        # Look in cache first
	if (array_key_exists("$node_id", $node_cache))
	    return $node_cache["$node_id"];

	$foo = new Node($node_id);

	if (! $foo->IsValid())
	    return null;

	# Insert into cache.
	$node_cache["$node_id"] =& $foo;
	return $foo;
    }

    # Lookup by IP
    function LookupByIP($ip) {
	$safe_ip = addslashes($ip);
	
	$query_result =
	    DBQueryFatal("select i.node_id from interfaces as i ".
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
			 "where i.IP='$safe_ip' and ".
			 "      i.role='" . TBDB_IFACEROLE_CONTROL . "'");

	if (mysql_num_rows($query_result) == 0) {
	    return null;
	}
	$row = mysql_fetch_array($query_result);
	return Node::Lookup($row["node_id"]);
    }

    # Lookup by Mac
    function LookupByMac($mac) {
	$safe_mac = addslashes($mac);
	
	$query_result =
	    DBQueryFatal("select i.node_id from interfaces as i ".
			 "where i.mac='$safe_mac' and ".
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
			 "      i.role='" . TBDB_IFACEROLE_CONTROL . "'");

	if (mysql_num_rows($query_result) == 0) {
	    return null;
	}
	$row = mysql_fetch_array($query_result);
	return Node::Lookup($row["node_id"]);
    }

    #
    # Refresh an instance by reloading from the DB.
    #
    function Refresh() {
	if (! $this->IsValid())
	    return -1;

	$node_id = $this->node_id();

	$query_result =
	    DBQueryWarn("select * from nodes where node_id='$node_id'");
    
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->node = NULL;
	    return -1;
	}
	$this->node = mysql_fetch_array($query_result);
	return 0;
    }

    #
    # Equality test.
    #
    function SameNode($node) {
	return $node->node_id() == $this->node_id();
    }

    # accessors
    function field($name) {
	return (is_null($this->node) ? -1 : $this->node[$name]);
    }
    function node_id()		{ return $this->field("node_id"); }
    function type() { return $this->field("type"); }
    function phys_nodeid() {return $this->field("phys_nodeid"); }
    function role() {return $this->field("role"); }
    function def_boot_osid() {return $this->field("def_boot_osid"); }
147
    function def_boot_osid_vers() {return $this->field("def_boot_osid_vers"); }
148
149
150
    function def_boot_path() {return $this->field("def_boot_path"); }
    function def_boot_cmd_line() {return $this->field("def_boot_cmd_line"); }
    function temp_boot_osid() {return $this->field("temp_boot_osid"); }
151
    function temp_boot_osid_vers() {return $this->field("temp_boot_osid_vers");}
152
    function next_boot_osid() {return $this->field("next_boot_osid"); }
153
    function next_boot_osid_vers() {return $this->field("next_boot_osid_vers");}
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    function next_boot_path() {return $this->field("next_boot_path"); }
    function next_boot_cmd_line() {return $this->field("next_boot_cmd_line"); }
    function pxe_boot_path() {return $this->field("pxe_boot_path"); }
    function rpms() {return $this->field("rpms"); }
    function deltas() {return $this->field("deltas"); }
    function tarballs() {return $this->field("tarballs"); }
    function startupcmd() {return $this->field("startupcmd"); }
    function startstatus() {return $this->field("startstatus"); }
    function ready() {return $this->field("ready"); }
    function priority() {return $this->field("priority"); }
    function bootstatus() {return $this->field("bootstatus"); }
    function status() {return $this->field("status"); }
    function status_timestamp() {return $this->field("status_timestamp"); }
    function failureaction() {return $this->field("failureaction"); }
    function routertype() {return $this->field("routertype"); }
    function eventstate() {return $this->field("eventstate"); }
    function state_timestamp() {return $this->field("state_timestamp"); }
    function op_mode() {return $this->field("op_mode"); }
    function op_mode_timestamp() {return $this->field("op_mode_timestamp"); }
    function allocstate() {return $this->field("allocstate"); }
    function update_accounts() {return $this->field("update_accounts"); }
    function next_op_mode() {return $this->field("next_op_mode"); }
    function ipodhash() {return $this->field("ipodhash"); }
    function osid() {return $this->field("osid"); }
    function ntpdrift() {return $this->field("ntpdrift"); }
    function ipport_low() {return $this->field("ipport_low"); }
    function ipport_next() {return $this->field("ipport_next"); }
    function ipport_high() {return $this->field("ipport_high"); }
    function sshdport() {return $this->field("sshdport"); }
    function jailflag() {return $this->field("jailflag"); }
    function jailip() {return $this->field("jailip"); }
    function sfshostid() {return $this->field("sfshostid"); }
    function stated_tag() {return $this->field("stated_tag"); }
    function rtabid() {return $this->field("rtabid"); }
    function cd_version() {return $this->field("cd_version"); }
    function boot_errno() {return $this->field("boot_errno"); }
    function reserved_pid() {return $this->field("reserved_pid"); }
191
    function taint_states() {return $this->field("taint_states"); }
192

193
194
195
196
197
198
199
200
201
    function def_boot_image() {
	return Image::Lookup($this->def_boot_osid(),
			     $this->def_boot_osid_vers());
    }
    function def_boot_osinfo() {
	return OSinfo::Lookup($this->def_boot_osid(),
			      $this->def_boot_osid_vers());
    }

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
248
249
250
251
252
253
254
255
256
257
258
259
    #
    # Access Check, determines if $user can access $this record.
    # 
    function AccessCheck($user, $access_type) {
	global $TB_NODEACCESS_READINFO;
	global $TB_NODEACCESS_MODIFYINFO;
	global $TB_NODEACCESS_LOADIMAGE;
	global $TB_NODEACCESS_REBOOT;
	global $TB_NODEACCESS_POWERCYCLE;
	global $TB_NODEACCESS_MIN;
	global $TB_NODEACCESS_MAX;
	global $TBDB_TRUST_USER;
	global $TBDB_TRUST_GROUPROOT;
	global $TBDB_TRUST_LOCALROOT;
	global $TBOPSPID;
	global $CHECKLOGIN_USER;
	$mintrust = $TBDB_TRUST_USER;

	if ($access_type < $TB_NODEACCESS_MIN ||
	    $access_type > $TB_NODEACCESS_MAX) {
	    TBERROR("Invalid access type: $access_type!", 1);
	}

	$uid = $user->uid();

	if (! ($experiment = $this->Reservation())) {
            #
	    # If the current user is in the emulab-ops project and has 
	    # sufficient privs, then he can muck with free nodes as if he 
	    # were an admin type.
	    #
	    if ($uid == $CHECKLOGIN_USER->uid() && OPSGUY()) {
		return(TBMinTrust(TBGrpTrust($uid, $TBOPSPID, $TBOPSPID),
				  $TBDB_TRUST_LOCALROOT));
	    }
	    return 0;
	}
	$pid = $experiment->pid();
	$gid = $experiment->gid();
	$eid = $experiment->eid();

	if ($access_type == $TB_NODEACCESS_READINFO) {
	    $mintrust = $TBDB_TRUST_USER;
	}
	else {
	    $mintrust = $TBDB_TRUST_LOCALROOT;
	}
	return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust) ||
	    TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_GROUPROOT);
    }

    #
    # Page header; spit back some html for the typical page header.
    #
    function PageHeader() {
	$node_id = $this->node_id();
	
	$html = "<font size=+2>Node <b>".
Leigh B Stoller's avatar
Leigh B Stoller committed
260
	    "<a href=shownode.php3?node_id=$node_id><b>$node_id</a>".
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
	    "</b></font>\n";

	return $html;
    }

    #
    # Get the bootlog for this node.
    #
    function BootLog(&$log, &$stamp) {
	$log     = null;
	$stamp   = null;
	$node_id = $this->node_id();
	
	$query_result =
	    DBQueryFatal("select * from node_bootlogs ".
			 "where node_id='$node_id'");

	if (mysql_num_rows($query_result) == 0)
	    return -1;

	$row = mysql_fetch_array($query_result);
	$log   = $row["bootlog"];
	$stamp = $row["bootlog_timestamp"];
	return 0;
    }

287
288
289
290
291
292
293
294
295
296
297
298
299
300
    function IsRemote() {
	$type = $this->type();

	$query_result =
	    DBQueryFatal("select isremotenode from node_types ".
			 "where type='$type'");
	
	if (mysql_num_rows($query_result) == 0) {
	    return 0;
	}
	$row = mysql_fetch_array($query_result);
	return $row["isremotenode"];
    }

301
302
303
304
305
306
307
308
309
310
311
    function IsVirtNode() {
	$type = $this->type();

	$query_result =
	    DBQueryFatal("select isvirtnode from node_types ".
			 "where type='$type'");
	
	if (mysql_num_rows($query_result) == 0) {
	    return 0;
	}
	$row = mysql_fetch_array($query_result);
Leigh B Stoller's avatar
Leigh B Stoller committed
312
	return $row["isvirtnode"];
313
314
    }

315
316
317
318
    function NodeStatus() {
	$node_id = $this->node_id();

	$query_result =
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
	    DBQueryFatal("select status from nodes where node_id='$node_id'");

	if (mysql_num_rows($query_result) == 0) {
	    return "";
	}
	$row = mysql_fetch_array($query_result);
	return $row["status"];
    }

    function RealNodeStatus() {
	$node_id = $this->node_id();

	$query_result =
	    DBQueryFatal("select status from node_status ".
			 "where node_id='$node_id'");
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385

	if (mysql_num_rows($query_result) == 0) {
	    return "";
	}
	$row = mysql_fetch_array($query_result);
	return $row["status"];
    }

    #
    # Get the experiment this node is reserved too, or null.
    #
    function Reservation() {
	$node_id = $this->node_id();
	
	$query_result =
	    DBQueryFatal("select pid,eid from reserved ".
			 "where node_id='$node_id'");

	if (mysql_num_rows($query_result) == 0) {
	    return null;
	}
	$row = mysql_fetch_array($query_result);
	$pid = $row["pid"];
	$eid = $row["eid"];

	return Experiment::LookupByPidEid($pid, $eid);
    }

    #
    # Get the raw reserved table info and return it, or null if no reservation
    #
    function ReservedTableEntry() {
	$node_id = $this->node_id();
	
	$query_result =
	    DBQueryFatal("select * from reserved ".
			 "where node_id='$node_id'");

	if (mysql_num_rows($query_result) == 0) {
	    return null;
	}
	return mysql_fetch_array($query_result);
    }

    #
    # See if this node has a serial console.
    #
    function HasSerialConsole() {
	$node_id = $this->node_id();
	
	$query_result =
	    DBQueryFatal("select tipname from tiplines ".
386
			 "where node_id='$node_id' and disabled=0");
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
	
	return mysql_num_rows($query_result);
    }

    #
    # Return the class of the node.
    #
    function TypeClass() {
	$this_type = $this->type();
	
	$query_result =
	    DBQueryFatal("select class from node_types ".
			 "where type='$this_type'");

	if (mysql_num_rows($query_result) == 0) {
	    return "";
	}

	$row = mysql_fetch_array($query_result);
	return $row["class"];
    }

    #
    # Return the virtual name of a reserved node.
    #
    function VirtName() {
	if (! ($row = $this->ReservedTableEntry())) {
	    return "";
	}
	if (! $row["vname"]) {
	    return "";
	}
	return $row["vname"];
    }

    var $lastact_query =
	"greatest(last_tty_act,last_net_act,last_cpu_act,last_ext_act)";

    #
    # Gets the time of idleness for a node, in hours by default (ie '2.75')
    #
    function IdleTime($format = 0) {
	$node_id = $this->node_id();
	$clause  = $this->lastact_query;
	
	$query_result =
	    DBQueryWarn("select (unix_timestamp(now()) - unix_timestamp( ".
			"        $clause)) as idle_time from node_activity ".
435
436
			"where node_id='$node_id' and ".
			"      UNIX_TIMESTAMP(last_report)!=0");
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480

	if (mysql_num_rows($query_result) == 0) {
	    return -1;
	}
	$row   = mysql_fetch_array($query_result);
	$t = $row["idle_time"];
        # if it is less than 5 minutes, it is not idle at all...
	$t = ($t < 300 ? 0 : $t);
	if (!$format) {
	    $t = round($t/3600,2);
	}
	else {
	    $t = date($format,mktime(0,0,$t));
	}
	return $t;
    }

    #
    # Find out if a node idle reports are stale
    #
    function IdleStale() {
	$node_id = $this->node_id();

	#
        # We currently have a 5 minute interval for slothd between reports
        # So give some slack in case a node reboots without reporting for
	# a while
	#
	# In Minutes;
	$staletime = 10;
	$stalesec  = 60 * $staletime;
	
	$query_result =
	    DBQueryWarn("select (unix_timestamp(now()) - ".
			"    unix_timestamp(last_report )) as t ".
			"from node_activity where node_id='$node_id'");

	if (mysql_num_rows($query_result) == 0) {
	    return -1;
	}
	$row   = mysql_fetch_array($query_result);
	return ($row["t"]>$stalesec);
    }

481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
    #
    # Check to see if node is tainted.
    #
    function IsTainted($instate = "") {
        $tstates = $this->taint_states();
        # No taint states set on this node?
	if (!isset($tstates) || !$tstates) {
	    return 0;
	}
        # Any taint will do if nothing was passed in to check.
	if (!$instate) {
	    return 1;
	}
	foreach (explode(",", $tstates) as $taint) {
	    if (strcmp($instate, $taint) == 0) {
	        return 1;
	    }
	}
	return 0;
    }

502
503
504
505
    #
    # Show node record.
    #
    function Show($flags = 0) {
506
507
	global $IPV6_ENABLED, $IPV6_SUBNET_PREFIX;
	
508
509
510
511
512
	$node_id = $this->node_id();
	$short   = ($flags & SHOWNODE_SHORT  ? 1 : 0);
	$noperm  = ($flags & SHOWNODE_NOPERM ? 1 : 0);
    
	$query_result =
513
	    DBQueryFatal("select n.*,na.*,r.vname,r.pid,r.eid,i.IP,i.mac, ".
514
515
			 "greatest(last_tty_act,last_net_act,last_cpu_act,".
			 "last_ext_act) as last_act, ".
516
517
			 "  t.isvirtnode,t.isremotenode,t.isplabdslice, ".
			 "  r.erole as rsrvrole, pi.IP as phys_IP, loc.*, ".
518
519
			 "  util.*,n.uuid as node_uuid, ".
			 "  mi.IP as mngmnt_IP ".
520
521
522
523
524
525
526
527
528
529
530
			 " from nodes as n ".
			 "left join reserved as r on n.node_id=r.node_id ".
			 "left join node_activity as na on ".
			 "     n.node_id=na.node_id ".
			 "left join node_types as t on t.type=n.type ".
			 "left join interfaces as i on ".
			 "     i.node_id=n.node_id and ".
			 "     i.role='" . TBDB_IFACEROLE_CONTROL . "' ".
			 "left join interfaces as pi on ".
			 "     pi.node_id=n.phys_nodeid and ".
			 "     pi.role='" . TBDB_IFACEROLE_CONTROL . "' ".
531
532
533
			 "left join interfaces as mi on ".
			 "     mi.node_id=n.node_id and ".
			 "     mi.role='" . TBDB_IFACEROLE_MANAGEMENT . "' ".
534
535
			 "left join location_info as loc on ".
			 "     loc.node_id=n.node_id ".
536
537
			 "left join node_utilization as util on ".
			 "     util.node_id=n.node_id ".
538
539
540
541
542
543
544
545
546
547
548
549
550
551
			 "where n.node_id='$node_id'");
	
	if (mysql_num_rows($query_result) == 0) {
	    TBERROR("The node $node_id is not a valid nodeid!", 1);
	}
		
	$row = mysql_fetch_array($query_result);

	$phys_nodeid        = $row["phys_nodeid"]; 
	$type               = $row["type"];
	$vname		    = $row["vname"];
	$pid 		    = $row["pid"];
	$eid		    = $row["eid"];
	$def_boot_osid      = $row["def_boot_osid"];
552
	$def_boot_osid_vers = $row["def_boot_osid_vers"];
553
554
	$def_boot_cmd_line  = $row["def_boot_cmd_line"];
	$next_boot_osid     = $row["next_boot_osid"];
555
	$next_boot_osid_vers= $row["next_boot_osid_vers"];
556
	$temp_boot_osid     = $row["temp_boot_osid"];
557
	$temp_boot_osid_vers= $row["temp_boot_osid_vers"];
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
	$next_boot_cmd_line = $row["next_boot_cmd_line"];
	$rpms               = $row["rpms"];
	$tarballs           = $row["tarballs"];
	$startupcmd         = $row["startupcmd"];
	$routertype         = $row["routertype"];
	$eventstate         = $row["eventstate"];
	$state_timestamp    = $row["state_timestamp"];
	$allocstate         = $row["allocstate"];
	$allocstate_timestamp= $row["allocstate_timestamp"];
	$op_mode            = $row["op_mode"];
	$op_mode_timestamp  = $row["op_mode_timestamp"];
	$IP                 = $row["IP"];
	$isvirtnode         = $row["isvirtnode"];
	$isremotenode       = $row["isremotenode"];
	$isplabdslice       = $row["isplabdslice"];
	$ipport_low	    = $row["ipport_low"];
	$ipport_next	    = $row["ipport_next"];
	$ipport_high	    = $row["ipport_high"];
Robert Ricci's avatar
Robert Ricci committed
576
	$sshdport	    = $row["sshdport"];
577
578
579
580
581
582
583
584
	$last_act           = $row["last_act"];
	$last_tty_act       = $row["last_tty_act"];
	$last_net_act       = $row["last_net_act"];
	$last_cpu_act       = $row["last_cpu_act"];
	$last_ext_act       = $row["last_ext_act"];
	$last_report        = $row["last_report"];
	$rsrvrole           = $row["rsrvrole"];
	$phys_IP	    = $row["phys_IP"];
585
	$mngmnt_IP	    = $row["mngmnt_IP"];
586
587
588
589
590
	$battery_voltage    = $row["battery_voltage"];
	$battery_percentage = $row["battery_percentage"];
	$battery_timestamp  = $row["battery_timestamp"];
	$boot_errno         = $row["boot_errno"];
	$reserved_pid       = $row["reserved_pid"];
591
592
593
	$inception          = $row["inception"];
	$alloctime          = $row["allocated"];
	$downtime           = $row["down"];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
594
	$uuid               = $row["node_uuid"];
595
	$mac		    	= $row["mac"];
596
	$taint_states       = $row["taint_states"];
597
598
599
600
601
602
603
604
605
606
607

	if (!$def_boot_cmd_line)
	    $def_boot_cmd_line = "&nbsp;";
	if (!$next_boot_cmd_line)
	    $next_boot_cmd_line = "&nbsp;";
	if (!$rpms)
	    $rpms = "&nbsp;";
	if (!$tarballs)
	    $tarballs = "&nbsp;";
	if (!$startupcmd)
	    $startupcmd = "&nbsp;";
608
609
	else
	    $startupcmd = CleanString($startupcmd);
610

Leigh B. Stoller's avatar
Leigh B. Stoller committed
611
612
613
614
615
616
	if ($node_id != $phys_nodeid) {
	    if (! ($phys_this = Node::Lookup($phys_nodeid))) {
		TBERROR("Cannot map physical node $phys_nodeid to object", 1);
	    }
	}

617
618
619
620
621
622
623
624
	if (!$short) {
            #
            # Location info.
            # 
	    if (isset($row["loc_x"]) && isset($row["loc_y"]) &&
		isset($row["floor"]) && isset($row["building"])) {
		$floor    = $row["floor"];
		$building = $row["building"];
625
		$room     = $row["room"];
626
627
628
		$loc_x    = $row["loc_x"];
		$loc_y    = $row["loc_y"];
		$orient   = $row["orientation"];
629
630
631
		$contact  = $row["contact"];
		$email    = $row["email"];
		$phone    = $row["phone"];
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
	
		$query_result =
		    DBQueryFatal("select * from floorimages ".
				 "where scale=1 and ".
				 "      floor='$floor' and ".
				 "      building='$building'");

		if (mysql_num_rows($query_result)) {
		    $row = mysql_fetch_array($query_result);
	    
		    if (isset($row["pixels_per_meter"]) &&
			($pixels_per_meter = $row["pixels_per_meter"]) != 0.0){
		
			$meters_x = sprintf("%.3f",
					    $loc_x / $pixels_per_meter);
			$meters_y = sprintf("%.3f",
					    $loc_y / $pixels_per_meter);

			if (isset($orient)) {
			    $orientation = sprintf("%.3f", $orient);
			}
		    }
		}
	    }
	}

	echo "<table border=2 cellpadding=0 cellspacing=2
                 align=center>\n";

	echo "<tr>
              <td>Node ID:</td>
              <td class=left>$node_id</td>
          </tr>\n";

	if ($isvirtnode) {
	    if (strcmp($node_id, $phys_nodeid)) {
		echo "<tr>
                      <td>Phys ID:</td>
                      <td class=left>
	   	          <a href='shownode.php3?node_id=$phys_nodeid'>
                             $phys_nodeid</a></td>
                  </tr>\n";
	    }
	}

	if (!$short && !$noperm) {
	    if ($vname) {
		echo "<tr>
                      <td>Virtual Name:</td>
                      <td class=left>$vname</td>
                  </tr>\n";
	    }

	    if ($pid) {
		echo "<tr>
                      <td>Project: </td>
                      <td class=\"left\">
                          <a href='showproject.php3?pid=$pid'>$pid</a></td>
                  </tr>\n";

		echo "<tr>
                      <td>Experiment:</td>
                      <td><a href='showexp.php3?pid=$pid&eid=$eid'>
                             $eid</a></td>
                  </tr>\n";
	    }
	}

	echo "<tr>
              <td>Node Type:</td>
              <td class=left>
  	          <a href='shownodetype.php3?node_type=$type'>$type</td>
          </tr>\n";

	$feat_result =
	    DBQueryFatal("select * from node_features ".
			 "where node_id='$node_id'");

	if (mysql_num_rows($feat_result) > 0) {
	    $features = "";
	    $count = 0;
	    while ($row = mysql_fetch_array($feat_result)) {
		if (($count > 0) && ($count % 2) == 0) {
		    $features .= "<br>";
		}
		$features .= " " . $row["feature"];
		$count += 1;
	    }

	    echo "<tr><td>Features:</td><td class=left>$features</td></tr>";
	}

	if (!$short && !$noperm) {
	    echo "<tr>
                  <td>Def Boot OS:</td>
                  <td class=left>";
728
	    SpitOSIDLink($def_boot_osid, $def_boot_osid_vers);
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
	    echo "    </td>
              </tr>\n";

	    if ($eventstate) {
		$when = strftime("20%y-%m-%d %H:%M:%S", $state_timestamp);
		echo "<tr>
                     <td>EventState:</td>
                     <td class=left>$eventstate ($when)</td>
                  </tr>\n";
	    }

	    if ($op_mode) {
		$when = strftime("20%y-%m-%d %H:%M:%S", $op_mode_timestamp);
		echo "<tr>
                     <td>Operating Mode:</td>
                     <td class=left>$op_mode ($when)</td>
                  </tr>\n";
	    }

	    if ($allocstate) {
		$when = strftime("20%y-%m-%d %H:%M:%S", $allocstate_timestamp);
		echo "<tr>
                     <td>AllocState:</td>
                     <td class=left>$allocstate ($when)</td>
                  </tr>\n";
	    }
	}
	if (!$short) {
            #
            # Location info.
            # 
760
761
762
763
764
765
766
767
768
769
770
771
772
	    if (isset($building)) {
		echo "<tr>
                      <td>Location (bldg/floor/room):</td>
                      <td class=left>$building";
		if (isset($floor)) {
		    echo "/$floor";
		}
		if (isset($room)) {
		    echo "/$room";
		}
		echo "</td>
                      </tr>\n";
	    }
773
774
	    if (isset($meters_x) && isset($meters_y)) {
		echo "<tr>
775
                      <td>Location Coordinates:</td>
776
777
778
779
780
781
782
                      <td class=left>x=$meters_x, y=$meters_y meters";
		if (isset($orientation)) {
		    echo " (o=$orientation degrees)";
		}
		echo      "</td>
                  </tr>\n";
	    }
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
	    if (OPSGUY() && (isset($contact) || isset($email))) {
		$lcstr = "";
		if (isset($contact)) {
		    $lcstr .= "$contact:";
		}
		if (isset($email)) {
		    $lcstr .= " <a href='mailto:$email'>$email</a>";
		}
		if (isset($phone)) {
		    $lcstr .= " $phone";
		}
		echo "<tr>
                      <td>Location Contact:</td>
                      <td class=left>$lcstr
                      </td>
                  </tr>\n";
	    }
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
	}
	
	if (!$short && !$noperm) {
             #
             # We want the last login for this node, but only if its *after* 
             # the experiment was created (or swapped in).
             #
	    if ($lastnodeuidlogin = TBNodeUidLastLogin($node_id)) {
		$foo = $lastnodeuidlogin["date"] . " " .
		    $lastnodeuidlogin["time"] . " " .
		    "(" . $lastnodeuidlogin["uid"] . ")";
	
		echo "<tr>
                      <td>Last Login:</td>
                      <td class=left>$foo</td>
                 </tr>\n";
	    }

	    if ($last_act) {
		echo "<tr>
                      <td>Last Activity:</td>
                      <td class=left>$last_act</td>
                  </tr>\n";

		$idletime = $this->IdleTime();
		echo "<tr>
                      <td>Idle Time:</td>
                      <td class=left>$idletime hours</td>
                  </tr>\n";

		echo "<tr>
                      <td>Last Act. Report:</td>
                      <td class=left>$last_report</td>
                  </tr>\n";

		echo "<tr>
                      <td>Last TTY Act.:</td>
                      <td class=left>$last_tty_act</td>
                  </tr>\n";

		echo "<tr>
                      <td>Last Net. Act.:</td>
                      <td class=left>$last_net_act</td>
                  </tr>\n";

		echo "<tr>
                      <td>Last CPU Act.:</td>
                      <td class=left>$last_cpu_act</td>
                  </tr>\n";

		echo "<tr>
                      <td>Last Ext. Act.:</td>
                      <td class=left>$last_ext_act</td>
                  </tr>\n";
	    }
	}

	if (!$short && !$noperm) {
	    if (!$isvirtnode && !$isremotenode) {
		echo "<tr>
                      <td>Def Boot Command&nbsp;Line:</td>
                      <td class=left>$def_boot_cmd_line</td>
                  </tr>\n";

		echo "<tr>
                      <td>Next Boot OS:</td>
                      <td class=left>";
    
		if ($next_boot_osid)
869
		    SpitOSIDLink($next_boot_osid, $next_boot_osid_vers);
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
		else
		    echo "&nbsp;";

		echo "    </td>
                  </tr>\n";

		echo "<tr>
                      <td>Next Boot Command Line:</td>
                      <td class=left>$next_boot_cmd_line</td>
                  </tr>\n";

		echo "<tr>
                      <td>Temp Boot OS:</td>
                      <td class=left>";
    
		if ($temp_boot_osid)
886
		    SpitOSIDLink($temp_boot_osid, $temp_boot_osid_vers);
887
888
889
890
891
892
893
894
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
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
		else
		    echo "&nbsp;";

		echo "    </td>
                  </tr>\n";
	    }
	    elseif ($isvirtnode) {
		if (!$isplabdslice) {
		    echo "<tr>
                          <td>IP Port Low:</td>
                          <td class=left>$ipport_low</td>
                      </tr>\n";

		    echo "<tr>
                          <td>IP Port Next:</td>
                          <td class=left>$ipport_next</td>
                      </tr>\n";

		    echo "<tr>
                          <td>IP Port High:</td>
                          <td class=left>$ipport_high</td>
                      </tr>\n";
		}
		echo "<tr>
                      <td>SSHD Port:</td>
                     <td class=left>$sshdport</td>
                  </tr>\n";
	    }

	    echo "<tr>
                  <td>Startup Command:</td>
                  <td class=left>$startupcmd</td>
              </tr>\n";

	    echo "<tr>
                  <td>Tarballs:</td>
                  <td class=left>$tarballs</td>
              </tr>\n";

	    echo "<tr>
                  <td>RPMs:</td>
                  <td class=left>$rpms</td>
              </tr>\n";

	    echo "<tr>
                  <td>Boot Errno:</td>
                  <td class=left>$boot_errno</td>
              </tr>\n";

	    if (!$isvirtnode && !$isremotenode) {
		echo "<tr>
                      <td>Router Type:</td>
                      <td class=left>$routertype</td>
                  </tr>\n";
	    }

	    if ($IP) {
		echo "<tr>
                      <td>Control Net IP:</td>
                      <td class=left>$IP</td>
                  </tr>\n";
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966

		if ($IPV6_ENABLED) {
		    $v = substr($mac, 1, 1);
		    $v = base_convert($v,16,2);
		    $v = str_pad($v, 4, '0', STR_PAD_LEFT);
		    if (substr($v,2,1) == '0')
			$v = substr_replace($v, '1', 2, 1);
		    else
			$v = substr_replace($v, '0', 2, 1);
		    $v = base_convert($v,2,16);
		    $IP6 = $IPV6_SUBNET_PREFIX . ':' . substr($mac,0,1) . $v .
			substr($mac,2,2) . ':' . substr($mac,4,2) . 'ff:fe' .
			substr($mac,6,2) . ':' . substr($mac,8,4) . "\n";

		    echo "<tr>
                           <td>Control Net IPv6:</td>
                           <td class=left>$IP6</td>
                          </tr>\n";
		}
967
968
969
970
971
972
		if ($mngmnt_IP) {
		    echo "<tr>
                          <td>Management IP:</td>
                          <td class=left>$mngmnt_IP</td>
                       </tr>\n";
		}
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
	    }
	    elseif ($phys_IP) {
		echo "<tr>
                      <td>Physical IP:</td>
                      <td class=left>$phys_IP</td>
                  </tr>\n";
	    }
	    if ($rsrvrole) {
		echo "<tr>
                      <td>Role:</td>
                      <td class=left>$rsrvrole</td>
                  </tr>\n";
	    }
	
	    if ($reserved_pid) {
		echo "<tr>
                      <td>Reserved Pid:</td>
                      <td class=left>
                          <a href='showproject.php3?pid=$reserved_pid'>
                               $reserved_pid</a></td>
                  </tr>\n";
	    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
995
996
997
998
999
1000
	    if ($uuid) {
		echo "<tr>
                      <td>UUID:</td>
                      <td class=left>$uuid</td>
                  </tr>\n";
	    }
1001
1002
1003
1004
1005
1006
	    if ($taint_states) {
		echo "<tr>
                      <td>Taint States:</td>
                      <td class=left>$taint_states</td>
                  </tr>\n";
	    }
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
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
	
            #
            # Show battery stuff
            #
	    if (isset($battery_voltage) && isset($battery_percentage)) {
		echo "<tr>
    	              <td>Battery Volts/Percent</td>
		      <td class=left>";
		printf("%.2f/%.2f ", $battery_voltage, $battery_percentage);

		if (isset($battery_timestamp)) {
		    echo "(" . date("m/d/y H:i:s", $battery_timestamp) . ")";
		}

		echo "    </td>
		  </tr>\n";
	    }

	    if ($isplabdslice) {
		$query_result = 
		    DBQueryFatal("select leaseend from plab_slice_nodes ".
				 "where node_id='$node_id'");
          
		if (mysql_num_rows($query_result) != 0) {
		    $row = mysql_fetch_array($query_result);
		    $leaseend = $row["leaseend"];
		    echo"<tr>
                     <td>Lease Expiration:</td>
                     <td class=left>$leaseend</td>
                 </tr>\n";
		}
	    }

	    if ($isremotenode) {
		if ($isvirtnode) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1042
		    $phys_this->ShowWideAreaNode(1);
1043
1044
		}
		else {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1045
		    $this->ShowWideAreaNode(1);
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
		}
	    }

            #
            # Show any auxtypes the node has
            #
	    $query_result =
		DBQueryFatal("select type, count from node_auxtypes ".
			     "where node_id='$node_id'");
    
	    if (mysql_num_rows($query_result) != 0) {
		echo "<tr>
                      <td align=center colspan=2>
                      Auxiliary Types
                      </td>
                  </tr>\n";

		echo "<tr><th>Type</th><th>Count</th>\n";

		while ($row = mysql_fetch_array($query_result)) {
		    $type  = $row["type"];
		    $count = $row["count"];
		    echo "<tr>
    	    	          <td>$type</td>
		          <td class=left>$count</td>
		      </td>\n";
		}
	    }
	}
    
	if (!$short) {
            #
            # Get interface info.
            #
	    echo "<tr>
                  <td align=center colspan=2>Interface Info</td>
              </tr>\n";
	    echo "<tr><th>Interface</th><th>Model; protocols</th>\n";
    
	    $query_result =
		DBQueryFatal("select i.*,it.*,c.*,s.capval as channel ".
			     "  from interfaces as i ".
			     "left join interface_types as it on ".
			     "     i.interface_type=it.type ".
			     "left join interface_capabilities as c on ".
			     "     i.interface_type=c.type and ".
			     "     c.capkey='protocols' ".
			     "left join interface_settings as s on ".
			     "     s.node_id=i.node_id and s.iface=i.iface and ".
			     "     s.capkey='channel' ".
			     "where i.node_id='$node_id' and ".
			     "      i.role='" . TBDB_IFACEROLE_EXPERIMENT . "'".
			     "order by iface");
    
	    while ($row = mysql_fetch_array($query_result)) {
		$iface     = $row["iface"];
		$type      = $row["type"];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1103
		$man       = $row["manufacturer"];
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
		$model     = $row["model"];
		$protocols = $row["capval"];
		$channel   = $row["channel"];

		if (isset($channel)) {
		    $channel = " (channel $channel)";
		}
		else
		    $channel = "";

		echo "<tr>
                      <td>$iface:&nbsp; $channel</td>
                      <td class=left>$type ($man $model; $protocols)</td>
                  </tr>\n";
	    }

1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
	    #
	    # Get subboss info.
	    #
	    echo "<tr>
              <td align=center colspan=2>Subboss Info</td>
            </tr>\n";
	    echo "<tr><th>Service</th><th>Subboss Node ID</th>\n";

	    $query_result =
	        DBQueryFatal("select service,subboss_id from subbosses ".
1130
			     "where node_id ='$node_id' and disabled=0");
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141

	    while ($row = mysql_fetch_array($query_result)) {
	        $service = $row["service"];
	        $subboss = $row["subboss_id"];

	        echo "<tr>
                  <td>$service</td>
                  <td class=left>$subboss</td>
                 </tr>\n";
	    }

1142
1143
1144
1145
1146
1147
            #
            # Switch info. Very useful for debugging.
            #
	    if (!$noperm) {
		$query_result =
		    DBQueryFatal("select i.*,w.* from interfaces as i ".
1148
1149
1150
				 "left join wires as w on ".
                                 "     i.node_id=w.node_id1 and ".
				 "     i.iface=w.iface1 ".
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
				 "where i.node_id='$node_id' and ".
				 "      w.node_id1 is not null ".
				 "order by iface");

		echo "<tr></tr><tr>
                    <td align=center colspan=2>Switch Info</td>
                  </tr>\n";
		echo "<tr><th>Iface:role &nbsp; card,port</th>
                      <th>Switch &nbsp; card,port</th>\n";
		
		while ($row = mysql_fetch_array($query_result)) {
		    $iface       = $row["iface"];
		    $role        = $row["role"];
		    $card        = $row["card1"];
		    $port        = $row["port1"];
		    $switch      = $row["node_id2"];
		    $switch_card = $row["card2"];
		    $switch_port = $row["port2"];

		    echo "<tr>
                      <td>$iface:$role &nbsp; $card,$port</td>
                      <td class=left>".
			"$switch: &nbsp; $switch_card,$switch_port</td>
                  </tr>\n";
		}
	    }
	}

        #
        # Spit out node attributes
        #
1182
1183
1184
1185
1186

	# Don't emit root password if node is tainted with "useronly" or
	# "blackbox".
	$noroot = $noperm || $this->IsTainted("useronly") || 
	    $this->IsTainted("blackbox");
1187
1188
	$query_result =
	    DBQueryFatal("select attrkey,attrvalue from node_attributes ".
1189
			 "where node_id='$node_id' ".
1190
			 ($noroot ? "and attrkey!='root_password'" : ""));
1191
			 
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
	if (!$short && mysql_num_rows($query_result)) {
	    echo "<tr>
                    <td align=center colspan=2>Node Attributes</td>
                  </tr>\n";
	    echo "<tr><th>Attribute</th><th>Value</th>\n";

	    while($row = mysql_fetch_array($query_result)) {
		$attrkey   = $row["attrkey"];
		$attrvalue = $row["attrvalue"];

		echo "<tr>
                        <td>$attrkey</td>
                        <td>$attrvalue</td>
                      </td>\n";
	    }
	}

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
	if (! ($short || $noperm || $isvirtnode)) {
	    $query_result =
		DBQueryFatal("select n.node_id,pid,eid,exptidx ".
			     " from nodes as n ".
			     "left join reserved as r on r.node_id=n.node_id ".
			     "where n.phys_nodeid='$node_id' and ".
			     "      n.node_id!=n.phys_nodeid");
	    if (mysql_num_rows($query_result)) {
		echo "<tr>
                       <td align=center colspan=2>Virtual/Sub Nodes</td>
                      </tr>\n";
		echo "<tr><th>Node ID</th><th>Experiment</th>\n";

		while($row = mysql_fetch_array($query_result)) {
		    $vnodeid = $row["node_id"];
		    $vpid    = $row["pid"];
		    $veid    = $row["eid"];
		    $vidx    = $row["exptidx"];
		    $url1    = CreateURL("shownode", URLARG_NODEID, $vnodeid);

		    echo "<tr>
                            <td><a href='$url1'>$vnodeid</a></td>\n";
		    
		    if (isset($veid)) {
			$url2 = CreateURL("showexp", URLARG_EID, $vidx);
			echo "<td><a href='$url2'>$vpid/$veid</a></td>\n";
		    }
		    else {
			echo "<td>No Experiment</a></td>\n";
		    }
		    echo "</td>\n";
		}
	    }
	}

1244
1245
1246
	echo "</table>\n";
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
    #
    # Show widearea node record. Just the widearea stuff, not the other.
    #
    function ShowWideAreaNode($embedded = 0) {
	$node_id = $this->node_id();
	
	$query_result =
	    DBQueryFatal("select * from widearea_nodeinfo ".
			 "where node_id='$node_id'");

	if (! mysql_num_rows($query_result)) {
	    return;
	}
	$row = mysql_fetch_array($query_result);
	$contact_uid	= $row["contact_uid"];
	$machine_type   = $row["machine_type"];
	$connect_type	= $row["connect_type"];
	$city		= $row["city"];
	$state		= $row["state"];
	$zip		= $row["zip"];
	$country	= $row["country"];
	$hostname	= $row["hostname"];
	$site		= $row["site"];
1270
1271
1272
	$boot_method    = $row["boot_method"];
	$gateway        = $row["gateway"];
	$dns            = $row["dns"];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335

	if (! ($user = User::Lookup($contact_uid))) {
            # This is not an error since the field is set to "nobody" when
            # there is no contact info. Why is that?
	    $showuser_url = CreateURL("showuser", URLARG_UID, $contact_uid);
	}
	else {
	    $showuser_url = CreateURL("showuser", $user);
	}

	if (! $embedded) {
	    echo "<table border=2 cellpadding=0 cellspacing=2
                         align=center>\n";
	}
	else {
	    echo "<tr>
                   <td align=center colspan=2>
                       Widearea Info
                   </td>
                 </tr>\n";
	}

	echo "<tr>
                  <td>Contact UID:</td>
                  <td class=left>
                      <a href='$showuser_url'>$contact_uid</a></td>
              </tr>\n";

	echo "<tr>
                  <td>Machine Type:</td>
                  <td class=left>$machine_type</td>
              </tr>\n";

	echo "<tr>
                  <td>connect Type:</td>
                  <td class=left>$connect_type</td>
              </tr>\n";

	echo "<tr>
                  <td>City:</td>
                  <td class=left>$city</td>
              </tr>\n";

	echo "<tr>
                  <td>State:</td>
                  <td class=left>$state</td>
              </tr>\n";

	echo "<tr>
                  <td>ZIP:</td>
                  <td class=left>$zip</td>
              </tr>\n";

	echo "<tr>
                  <td>Country:</td>
                  <td class=left>$country</td>
              </tr>\n";

        echo "<tr>
                  <td>Hostname:</td>
                  <td class=left>$hostname</td>
              </tr>\n";

1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
        echo "<tr>
                  <td>Boot Method:</td>
                  <td class=left>$boot_method</td>
              </tr>\n";

        echo "<tr>
                  <td>Gateway:</td>
                  <td class=left>$gateway</td>
              </tr>\n";

        echo "<tr>
                  <td>DNS:</td>
                  <td class=left>$dns</td>
              </tr>\n";

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
	echo "<tr>
                  <td>Site:</td>
                  <td class=left>$site</td>
              </tr>\n";

	if (! $embedded) {
	    echo "</table>\n";
	}
    }

1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
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
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
    #
    # Show log.
    # 
    function ShowLog() {
	$node_id = $this->node_id();

	$query_result =
	    DBQueryFatal("select * from nodelog where node_id='$node_id'".
			 "order by reported");

	if (! mysql_num_rows($query_result)) {
	    echo "<br>
                      <center>
                       There are no entries in the log for node $node_id.
                      </center>\n";
	    return 0;
	}

	echo "<br>
                  <center>
                    Log for node $node_id.
                  </center><br>\n";

	echo "<table border=1 cellpadding=2 cellspacing=2 align='center'>\n";
	echo "<tr>
                 <th>Delete?</th>
                 <th>Date</th>
                 <th>ID</th>
                 <th>Type</th>
                 <th>Reporter</th>
                 <th>Entry</th>
              </tr>\n";

	while ($row = mysql_fetch_array($query_result)) {
	    $type       = $row["type"];
	    $log_id     = $row["log_id"];
	    $reporter   = $row["reporting_uid"];
	    $date       = $row["reported"];
	    $entry      = $row["entry"];
	    $url        = CreateURL("deletenodelog", $this, "log_id", $log_id);

	    echo "<tr>
 	             <td align=center>
                      <a href='$url'>
                         <img alt='Delete Log Entry' src='redball.gif'>
                      </a></td>
                     <td>$date</td>
                     <td>$log_id</td>
                     <td>$type</td>
                     <td>$reporter</td>
                     <td>$entry</td>
                  </tr>\n";
	}
	echo "</table>\n";
    }
    
    #
    # Show one log entry.
    # 
    function ShowLogEntry($log_id) {
	$node_id = $this->node_id();
	$safe_id = addslashes($log_id);
	
	$query_result =
	    DBQueryFatal("select * from nodelog where ".
			 "node_id='$node_id' and log_id=$safe_id");

	if (! mysql_num_rows($query_result)) {
	    return 0;
	}

	echo "<table border=1 cellpadding=2 cellspacing=2 align='center'>\n";

	$row = mysql_fetch_array($query_result);
        $type       = $row["type"];
	$log_id     = $row["log_id"];
	$reporter   = $row["reporting_uid"];
	$date       = $row["reported"];
	$entry      = $row["entry"];

	echo "<tr>
                 <td>$date</td>
                 <td>$log_id</td>
                 <td>$type</td>
                 <td>$reporter</td>
                 <td>$entry</td>
               </tr>\n";
	echo "</table>\n";

	return 0;
    }

    #
    # Delete a node log entry.
    #
    function DeleteNodeLog($log_id) {
	$node_id = $this->node_id();
	$safe_id = addslashes($log_id);
	
	DBQueryFatal("delete from nodelog where ".
		     "node_id='$node_id' and log_id=$safe_id");

	return 0;
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479

    function sshurl($uid) {
	global $OURDOMAIN;
	$node_id = $this->node_id();

	if ($this->IsVirtNode()) {
	    $pnode_id = $this->phys_nodeid();
	    $sshdport = $this->sshdport();

	    return "ssh://${uid}@${pnode_id}.${OURDOMAIN}:$sshdport";
	}
	else {
	    return "ssh://${uid}@${node_id}.${OURDOMAIN}";
	}
    }
1480
1481
1482
1483
1484
}

#
# Show history.
#
1485
function ShowNodeHistory($node_id = null, $record = null,
1486
			 $count = 200, $showall = 0, $reverse = 0,
1487
1488
			 $date = null, $IP = null, $mac = null,
			 $node_opt = "") {
1489
    global $TBSUEXEC_PATH;
1490
    global $PROTOGENI;
1491
1492
1493
1494
1495
    $atime = 0;
    $ftime = 0;
    $rtime = 0;
    $dtime = 0;
    $nodestr = "";
1496
    $arg = "";
1497
    $opt = "-ls -n " . escapeshellarg($count);
1498
    if (!$showall) {
1499
	$opt .= " -a";
1500
    }
1501
1502
1503
    if ($reverse) {
	$opt .= " -r";
    }
1504
    if ($date) {
1505
	$date = date("Y-m-d H:i:s", strtotime($date));
1506
1507
	$opt .= " -d " . escapeshellarg($date);
    }
1508
1509
1510
    elseif ($record) {
	$opt .= " -x " . escapeshellarg($record);
    }
1511
    if ($node_id || $IP || $mac) {
1512
1513
1514
1515
	if ($IP) {
	    $opt .= " -i " . escapeshellarg($IP);
	    $nodestr = "<th>Node</th>";
	}
1516
1517
1518
1519
	if ($mac) {
	    $opt .= " -m " . escapeshellarg($mac);
	    $nodestr = "<th>Node</th>";
	}
1520
1521
1522
	else {
	    $arg = escapeshellarg($node_id);
	}
1523
1524
    }
    else {
1525
	$opt .= " -A";
1526
	$node_id = "";
1527
	$nodestr = "<th>Node</th>";
1528
1529
1530
1531
1532
1533
1534
	#
	# When supplying a date, we want a summary of all nodes at that
	# point in time, not a listing. 
	#
	if ($date) {
	    $opt .= " -c";
	}
1535
1536
    }
    if ($fp = popen("$TBSUEXEC_PATH nobody nobody ".
1537
		    "  webnode_history $opt $arg", "r")) {
1538
1539
1540
1541
1542
1543
	if (!$showall) {
	    $str = "Allocation";
	} else {
	    $str = "";
	}
	if ($node_id == "") {
1544
	    echo "<center><b>
1545
                  $str History for All Nodes.
1546
                  </b></center>\n";
1547
	} else {
1548
	    $node_url = CreateURL("shownode", URLARG_NODEID, $node_id);
1549
	    echo "<center><b>
1550
                  $str History for Node <a href='$node_url'>$node_id</a>.
1551
                  </b></center>\n";
1552
1553
	}

1554
1555
	# Keep track of history record bounds, for paging through.
	$max_history_id = 0;
1556
	$min_history_id = 1000000000;
1557

1558
1559
	# Build up table contents
	ob_start();
1560
1561
1562
1563
1564
1565
1566
1567

	$line = fgets($fp);
	while (!feof($fp)) {
	    #
	    # Formats:
	    # nodeid REC tstamp duration uid pid eid
	    # nodeid SUM alloctime freetime reloadtime downtime
	    #
1568
	    $results = preg_split("/[\s]+/", $line, 9, PREG_SPLIT_NO_EMPTY);
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
	    $nodeid = $results[0];
	    $type = $results[1];
	    if ($type == "SUM") {
		# Save summary info for later
		$atime = $results[2];
		$ftime = $results[3];
		$rtime = $results[4];
		$dtime = $results[5];
	    } elseif ($type == "REC") {
		$stamp = $results[2];
		$datestr = date("Y-m-d H:i:s", $stamp);
		$duration = $results[3];
		$durstr = "";
		if ($duration >= (24*60*60)) {
		    $durstr = sprintf("%dd", $duration / (24*60*60));
		    $duration %= (24*60*60);
		}
		if ($duration >= (60*60)) {
		    $durstr = sprintf("%s%dh", $durstr, $duration / (60*60));
		    $duration %= (60*60);
		}
		if ($duration >= 60) {
		    $durstr = sprintf("%s%dm", $durstr, $duration / 60);
		    $duration %= 60;
		}
		$durstr = sprintf("%s%ds", $durstr, $duration);
		$uid = $results[4];
		$pid = $results[5];
1597
		$thisid = intval($results[8]);
1598
		if ($thisid > $max_history_id) {
1599
1600
		    $max_history_id = $thisid;
		}
1601
1602
1603
		if ($thisid < $min_history_id) {
		    $min_history_id = $thisid;
		}
1604
		$slice = "--";
1605
		$expurl = null;
1606
1607
1608
1609
1610
1611
		if ($pid == "FREE") {
		    $pid = "--";
		    $eid = "--";
		    $uid = "--";
		} else {
		    $eid = $results[6];
1612
		    if ($results[7]) {
1613
			$experiment = Experiment::Lookup($results[7]);
1614
1615
1616
1617
1618
1619
1620
1621
1622
			$experiment_stats = ExperimentStats::Lookup($results[7]);
			if ($experiment_stats &&
			    $experiment_stats->slice_uuid()) {
			    $url = CreateURL("genihistory",
					     "slice_uuid",
					     $experiment_stats->slice_uuid());
			    $slice = "<a href='$url'>" .
				"<img src=\"greenball.gif\" border=0></a>";
			}
1623
1624
1625
1626
1627
1628
1629
1630
1631
			if ($experiment) {
			    $expurl = CreateURL("showexp",
						URLARG_EID, $experiment->idx());
			}
			else {
			    $expurl = CreateURL("showexpstats",
						"record",
						$experiment_stats->exptidx());
			}
1632
		    }
1633
		}
1634
		
1635
		if ($node_id == "") {
1636
1637
		    $nodeurl = CreateURL("shownodehistory",
					 URLARG_NODEID, $nodeid);
1638
		    echo "<tr>
1639
                          <td><a href='$nodeurl'>$nodeid</a></td>
1640
1641
1642
1643
1644
1645
1646
                          <td>$pid</td>";
		    if ($expurl) {
			echo "<td><a href='$expurl'>$eid</a></td>";
		    }
		    else {
			echo "<td>$eid</td>";
		    }
1647
1648
1649
1650
		    if ($PROTOGENI) {
			echo "<td>$slice</td>";
		    }
                    echo "<td>$uid</td>
1651
1652
1653
1654
1655
                          <td>$datestr</td>
                          <td>$durstr</td>
                          </tr>\n";
		} else {
		    echo "<tr>
1656
1657
1658
1659
1660
1661
1662
                          <td>$pid</td>";
		    if ($expurl) {
			echo "<td><a href='$expurl'>$eid</a></td>";
		    }
		    else {
			echo "<td>$eid</td>";
		    }
1663
1664
1665
1666
		    if ($PROTOGENI) {
			echo "<td>$slice</td>";
		    }
                    echo "<td>$uid</td>
1667
1668
1669
1670
1671
1672
1673
1674
                          <td>$datestr</td>
                          <td>$durstr</td>
                          </tr>\n";
		}
	    }
	    $line = fgets($fp, 1024);
	}
	pclose($fp);
1675
1676
1677
	$table_html = ob_get_contents();
	ob_end_clean();
	
1678
1679
1680
1681
1682
1683
	$hid = $max_history_id;
	if ($reverse) {
	    $hid = $min_history_id;
	}
	echo "<center><a href='shownodehistory.php3?record=$hid".
	    "&reverse=$reverse&count=$count&$node_opt'>Next $count records</a></center>\n";
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
	echo "<table border=1 cellpadding=2 cellspacing=2 align='center'>\n";
	echo "<tr>
	       $nodestr
               <th>Pid</th>
               <th>Eid</th>";
	if ($PROTOGENI) {
	    echo "<th>Slice</th>";
	}
        echo " <th>Allocated By</th>
               <th>Allocation Date</th>
	       <th>Duration</th>
              </tr>\n";

	echo $table_html;
1698
1699

	echo "</table>\n";
1700
1701
	echo "<center><a href='shownodehistory.php3?record=$hid".
	    "&reverse=$reverse&count=$count&$node_opt'>Next $count records</a></center>\n";
1702
1703
1704
1705

	$ttime = $atime + $ftime + $rtime + $dtime;
	if ($ttime) {
	    echo "<br>
Mike Hibler's avatar
Mike Hibler committed
1706
                  <center><b>
1707
                  Usage Summary for Node $node_id.
Mike Hibler's avatar
Mike Hibler committed
1708
                  </b></center><br>\n";
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732

	    echo "<table border=1 align=center>\n";

	    $str = "Allocated";
	    $pct = sprintf("%5.1f", $atime * 100.0 / $ttime);
	    echo "<tr><td>$str</td><td>$pct%</td></tr>\n";

	    $str = "Free";
	    $pct = sprintf("%5.1f", $ftime * 100.0 / $ttime);
	    echo "<tr><td>$str</td><td>$pct%</td></tr>\n";

	    $str = "Reloading";
	    $pct = sprintf("%5.1f", $rtime * 100.0 / $ttime);
	    echo "<tr><td>$str</td><td>$pct%</td></tr>\n";

	    $str = "Down";
	    $pct = sprintf("%5.1f", $dtime * 100.0 / $ttime);
	    echo "<tr><td>$str</td><td>$pct%</td></tr>\n";

	    echo "</table>\n";
	}
    }
}

1733
1734
1735
#
# Logged in users, show free node counts.
#
1736
function ShowFreeNodes($user, $group)
1737
1738
{
    $freecounts = array();
1739
1740
1741
1742
1743
    $perms      = array();
    $pid_idx    = $group->pid_idx();
    $pid        = $group->pid();
    $uid_idx    = $user->uid_idx();

1744
1745
1746
1747
    # Get typelist and set freecounts to zero.
    $query_result =
	DBQueryFatal("select n.type from nodes as n ".
		     "left join node_types as nt on n.type=nt.type ".
1748
1749
1750
1751
1752
                     "left join node_type_attributes as attr on ".
                     "     attr.type=n.type and ".
                     "     attr.attrkey='noshowfreenodes' ".
		     "where (role='testnode') and class='pc' and ".
                     "      attr.attrvalue is null");
1753
1754
1755
    while ($row = mysql_fetch_array($query_result)) {
	$type              = $row[0];
	$freecounts[$type] = 0;
1756
        $perms[$type]      = 1;
1757
1758
1759
1760
1761
    }

    if (!count($freecounts)) {
	return "";
    }
1762

1763
1764
1765
1766
1767
    # Get free totals by type.
    $query_result =
	DBQueryFatal("select n.eventstate,n.type,count(*) from nodes as n ".
		     "left join node_types as nt on n.type=nt.type ".
		     "left join reserved as r on r.node_id=n.node_id ".
1768
1769
1770
1771
1772
1773
                     "left join node_type_attributes as attr on ".
                     "     attr.type=n.type and ".
                     "     attr.attrkey='noshowfreenodes' ".
		     "where (role='testnode') and class='pc' and ".
		     "      r.pid is null and ".
                     "      attr.attrvalue is null and ".
1774
1775
		     "      (n.reserved_pid is null or ".
		     "       n.reserved_pid='$pid') ".
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
		     "group BY n.eventstate,n.type");

    while ($row = mysql_fetch_array($query_result)) {
	$type  = $row[1];
	$count = $row[2];
        # XXX Yeah, I'm a doofus and can't figure out how to do this in SQL.
	if (($row[0] == TBDB_NODESTATE_ISUP) ||
	    ($row[0] == TBDB_NODESTATE_PXEWAIT) ||
	    ($row[0] == TBDB_NODESTATE_ALWAYSUP) ||
	    ($row[0] == TBDB_NODESTATE_POWEROFF)) {
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797

            $policy_result =
                DBQueryFatal("select max(count) from group_policies as p ".
                             "where p.policy='type' and p.auxdata='$type' and ".
                             "      (p.pid='-' or p.pid='$pid')");
        
            if (mysql_num_rows($policy_result)) {
                $row = mysql_fetch_row($policy_result);
                if ($count > $row[0]) {
                    $count = $row[0];
                }
            }
1798
1799
1800
1801
1802
	    $freecounts[$type] = $count;
	}
    }
    $output = "";

1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
    #
    # Figure out how many node types are going to be printed out. These
    # are the nodes to which the user has access, no matter the count.
    #
    $pccount = 0;
    foreach($freecounts as $key => $value) {
	if ($perms[$key]) {
	    $pccount++;
	}
    }

1814
    $freepcs   = TBFreePCs($group);
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
    $reloading = TBReloadingPCs();

    $output .= "<table valign=top align=center width=100% height=100% border=1
		 cellspacing=1 cellpadding=0>
                 <tr><td nowrap colspan=6 class=usagefreenodes align=center>
 	           <b>$freepcs Free PCs, $reloading reloading</b></td></tr>\n";

    $newrow  = 1;
    $maxcols = (int) ($pccount / 3);
    if ($pccount % 3)
	$maxcols++;
    $cols    = 0;
    foreach($freecounts as $key => $value) {
	$freecount = $freecounts[$key];
1829
1830
1831
	if (!$perms[$key]) {
	    continue;
	}
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
	if ($newrow) {
	    $output .= "<tr>\n";
	}
	
	$output .= "<td class=usagefreenodes align=right>
                     <a target=_parent href=shownodetype.php3?node_type=$key>
                        $key</a></td>
                    <td class=usagefreenodes align=left>${freecount}</td>\n";

	$cols++;
	$newrow = 0;
	if ($cols == $maxcols || $pccount <= 3) {
	    $cols   = 0;
	    $newrow = 1;
	}

	if ($newrow) {
	    $output .= "</tr>\n";
	}
    }
    if (! $newrow) {
        # Fill out to $maxcols
	for ($i = $cols + 1; $i <= $maxcols; $i++) {
	    $output .= "<td class=usagefreenodes>&nbsp</td>";
	    $output .= "<td class=usagefreenodes>&nbsp</td>";
	}
	$output .= "</tr>\n";
    }
    # Fill in up to 3 rows.
    if ($pccount < 3) {
	for ($i = $pccount + 1; $i <= 3; $i++) {
	    $output .= "<tr><td class=usagefreenodes>&nbsp</td>
                            <td class=usagefreenodes>&nbsp</td></tr>\n";
	}
    }

    $output .= "</table>";
    return $output;
}
1871
?>