power.in 19 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3 4

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2011 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6 7 8
# All rights reserved.
#

9 10 11
#
# Testbed Power Control script
#
12
# power [on|off|cycle] <node> [<node>] ...
13 14 15
#
############################################################

16 17 18
#
# Configure variables
#
19 20 21 22 23 24
my $TB         = "@prefix@";
my $TBOPS      = "@TBOPSEMAIL@";
my $ELABINELAB = @ELABINELAB@;
my $RPCSERVER  = "@OUTERBOSS_NODENAME@";
my $RPCPORT    = "@OUTERBOSS_XMLRPCPORT@";
my $RPCCERT    = "@OUTERBOSS_SSLCERTNAME@";
25
my $WOL        = "$TB/sbin/whol";
26
my $TBLOG	= "@TBLOGFACIL@";
27

28
use lib "@prefix@/lib";
29
use libdb;
30
use libxmlrpc;
31
use power_ipmi;
32
use power_rpc27;
33
use power_sgmote;
34
use power_mail;
35
use power_whol;
36
use power_ilo;
37
use power_icebox;
38
use snmpit_apc;
39
use libtestbed;
40 41
use User;
use Node;
42
use NodeType;
43
use StateWait;
44
use strict;
45
use English;
46
use Getopt::Std;
47
use POSIX qw(strftime);
48
use Sys::Syslog;
49 50 51 52 53 54 55 56 57

sub usage() {
    print << "END";
Usage: $0 [-v n] [-e] <on|off|cycle> <node ...>
-e     Surpress sending of event - for use by scripts that have already sent it
-v n   Run with verbosity level n
END
    1;
}
58

59 60 61
#
# Un-taint path since this gets called from setuid scripts.
#
62
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:@prefix@/bin';
63 64
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

65 66 67 68 69 70
my $op = "";			#stores operation (on/off/cyc)
my @machines = ();		#stores machines to operate on
my $ip = "";			#stores IP of a power controller
my $outlet = 0;			#stores number of an outlet
my %IPList = ();		#holds machine/ip pairs
my %OutletList = ();		#holds machine/outlet pairs
71
my @wolnodes = ();
72
my $exitval = 0;
73
my $this_user;
74
my %nodes = ();
75

76 77
# Protos
sub dostatus(@);
78
sub dowol(@);
79
sub logit($);
80

81 82 83 84 85 86 87
#
# Process command-line arguments
#
my %opt = ();
getopts("v:he",\%opt);

if ($opt{h}) {
88
    exit usage();
89 90 91 92 93 94 95 96 97 98 99 100 101 102
}

# useful values are 0, 1, 2 and 3
my $verbose = 0;
if ($opt{v}) {
    $verbose = $opt{v};
}
print "VERBOSE ON: Set to level $verbose\n" if $verbose;

my $sendevent = 1;
if ($opt{e}) {
    $sendevent = 0;
}

103 104 105
#
# Must have at least an op and a machine, so at least 2 ARGV
#
106 107 108 109
if (@ARGV < 2) {
    exit &usage;
}

110

111 112 113
#
# Read in ARGV
#
114
$op = shift (@ARGV);
115
if ($op =~ /^(on|off|cycle|status)$/) {
116
    $op = $1;
117
} else {
118
    exit &usage;
119
}
120 121 122 123

#
# Untaint the arguments.
#
124
@machines = @ARGV;
125
foreach my $n (0..$#ARGV) {
126
    $machines[$n] =~ s/^([-\@\w.]+)$/$1/;
127
}
128 129 130 131

#
# Lowercase nodenames and remove duplicates
#
132 133
my %all_nodes = ();
foreach my $n (0..$#machines) {
134
    $all_nodes{"\L$machines[$n]"} = 1; # Lowercase it and use as hash key
135
}
136
@machines= sort keys %all_nodes;
137

138 139 140 141
#
# Dump the args
#
print "do \"$op\" to @machines\n" if $verbose > 1;
142

143 144 145
# Set up syslog
openlog("power", "pid", $TBLOG);

146 147 148 149 150 151 152 153 154 155
#
# Handle the status command which is not a per-node operation and not
# allowed by anyone except admins.
#
if ($op eq "status") {
    die("Only admins are allowed to query status\n")
	if ($UID != 0 && !TBAdmin($UID));
    exit(dostatus(@machines));
}

156 157 158 159 160 161 162 163 164 165 166
#
# This script can be run by root.
#
if ($UID) {
    $this_user = User->ThisUser();
    if (! defined($this_user)) {
	die("*** $0:\n".
	    "    You ($UID) do not exist!\n");
    }
}

167 168 169 170 171 172 173 174
#
# ElabinElab is special; Do local permission checks, build up a node list
# and then contact the proxy to do the actual work. No perl bindings yet,
# so have to use the python client program. 
#
if ($ELABINELAB) {
    my @nodelist = ();
    
175 176 177 178 179 180 181 182 183
    foreach my $nodeid (@machines) {
	my $node = Node->Lookup($nodeid);
	if (!defined($node)) {
	    die("*** $0:\n".
		"    No such node $nodeid\n");
	}
	if ($UID && !$node->AccessCheck($this_user,
					TB_NODEACCESS_POWERCYCLE)) {
	    warn "You are not authorized to control $nodeid. Skipping...\n";
184 185
	    next;
	}
186 187
	$nodes{$nodeid} = $node;
	push(@nodelist, $nodeid);
188 189 190 191
    }

    exit(0)
	if (! @nodelist);
192

193 194
    logit("$op: @nodelist\n");

195 196 197 198 199 200 201 202 203 204 205
    libxmlrpc::Config({"server"  => $RPCSERVER,
		       "verbose" => 1,
		       "cert"    => $RPCCERT,
		       "portnum" => $RPCPORT});

    my $rval = libxmlrpc::CallMethod("elabinelab", "power",
				     {"op"    => "$op",
				      "nodes" => join(",", @nodelist)});
    if (!defined($rval)) {
	exit(-1);
    }
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
    if ($sendevent) {
	foreach my $node (values(%nodes)) {
	    my $oldstate = $node->eventstate();
	    my $newstate;

	    # This should not happen.
	    $oldstate = ""
		if (!defined($oldstate));

	    if ($op eq "off") {
		$newstate = TBDB_NODESTATE_POWEROFF();
	    }
	    elsif ($op eq "cycle") {
		$newstate = TBDB_NODESTATE_SHUTDOWN();
	    }
	    elsif ($op eq "on") {
		#
		# What if the node is on? Nothing is going to
		# happen and changing the state is wrong.
		#
		$newstate = TBDB_NODESTATE_SHUTDOWN()
		    if ($oldstate eq TBDB_NODESTATE_POWEROFF());
	    }
	    print "$node: $oldstate, $newstate\n";
	    
	    #
	    # Avoid sending duplicate events; annoys stated.
	    #
	    $node->SetEventState($newstate)
		if (defined($newstate) && $newstate ne $oldstate);
236 237
	}
    }
238 239
    exit($rval);
}
240

241 242
my %timelimited = ();

243 244 245 246 247 248
#
# Though TBNodeAccessCheck can check all nodes at once, we do it one at
# a time, so that we can get a list of all nodes we have access to. This
# is primarily to preserve the pre-libification behavior of power
#
my %outlets = ();
249 250 251 252 253 254 255 256 257 258 259 260
foreach my $nodeid (@machines) {
    my $node = Node->Lookup($nodeid);
    if (defined($node)) {
	#
	# We allow root/admins to power cycle a non-existent node
	# (a new node that has not been added yet).
	#
	if (defined($this_user) &&
	    !$node->AccessCheck($this_user, TB_NODEACCESS_POWERCYCLE)) {
	    warn "You are not authorized to control $nodeid. Skipping...\n";
	    next;
	}
261
	$nodes{$nodeid} = $node;
262
    }
263

264 265 266 267 268 269 270
    #
    # Query DB directly since node might not exist yet.
    #
    my $result =
        DBQueryFatal("select power_id,outlet,UNIX_TIMESTAMP(last_power) ".
		     "  from outlets ".
		     "where node_id='$nodeid'");
271
    if ($result->num_rows() == 0) {
272
	warn "No outlets table entry found for $nodeid. Skipping...\n";
273
	SENDMAIL($TBOPS,
274 275
		 "No power outlet for $nodeid",
		 "Unable to power '$op' $nodeid; no outlets table entry!",
276
		 $TBOPS);
277
	next;
278
    }
279
    my ($power_id, $outlet, $last_power) = $result->fetchrow();
Mac Newbold's avatar
Mac Newbold committed
280

281 282 283
    #
    # Default power delay to 60 seconds if non-existent node.
    #
284
    my $power_delay = 60;
285 286 287 288 289 290 291 292
    my $postwol     = 0;
    if (defined($node)) {
	$power_delay = $node->NodeTypeInfo()->power_delay();

	if ($node->NodeAttribute("wakeonlan_afterpower", \$postwol) != 0) {
	    warn "Could not get wakeonlan_afterpower attr for $nodeid.\n";
	    $postwol = 0;
	}
293
    }
294
    my $time_ok = (time() - $power_delay > $last_power ? 1 : 0);
295 296 297

    #
    # Check for rate-limiting, and update the last power cycle time
298 299 300 301
    # if it's been long enough. Root gets to bypass the checks, and
    # we only update the timestamp if it is being turned on or cycled,
    # to allow off then on without waiting (unless the on is too close 
    # to a previos on/cycle command)
302
    #
303 304
    if ( $op ne "off" ) {
	if (! ($time_ok || ($UID == 0)) ) {
305
	    warn "$nodeid was power cycled recently. Skipping...\n";
306
	    next;
307
	} elsif ( $power_id ne "mail" ) {
308
	    DBQueryFatal("update outlets set last_power=CURRENT_TIMESTAMP " .
309
			 "where node_id = '$nodeid'");
310
	}
311
    }
312

313 314 315
    #
    # Associate this node with the power controller it is attached to
    #
316
    push @{$outlets{$power_id}}, [$nodeid, $outlet, $postwol];
317 318
}

319
print "machines= ",join(" ",@machines),"\n" if $verbose;
320 321 322 323 324 325 326 327 328
print "devices= ", join(" ",keys %outlets),"\n" if $verbose;

foreach my $power_id (keys %outlets) {

    #
    # Get the list of outlet numbers used on this power controller
    #
    my @outlets = ();
    my @nodes = ();
329
    my %postwol = ();
330
    foreach my $node (@{$outlets{$power_id}}) {
331
	my ($node_id, $outlet, $wol) = @$node;
332 333
	push @outlets, $outlet;
	push @nodes, $node_id;
334
	$postwol{$node_id} = $wol;
335 336 337
    }
    my $nodestr = join(",",@nodes);

338 339 340 341
    my $type;
    my $IP;
    my $class;

342
    if ($power_id eq "mail" || $power_id =~ /^whol-/ 
343
	|| $power_id=~ /^rmcp-/
344
	|| $power_id eq 'drac'
345
	|| $power_id eq 'ilo' || $power_id eq 'ilo2') {
346
	$type = $power_id;
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	$IP = "";
	$class = "";
    }
    else {
	#
	# Find out some information about this power controller
	#
	my $result = DBQueryFatal("select n.type, i.IP, t.class ". 
		"from nodes as n " .
		"left join interfaces as i on n.node_id=i.node_id " .
		"left join node_types as t on n.type=t.type " .
		"where n.node_id='$power_id'");
	if ($result->num_rows() == 0) {
	    warn "No entry found for power controller $power_id. Skipping " .
		"$nodestr\n";
	    $exitval++;
	    next;
	}
	($type, $IP, $class) = $result->fetchrow();
366
    }
367 368
    # Log now, and not worry about errors. Just want to know we tried.
    logit("$op: @nodes\n");
369 370 371 372 373 374

    #
    # Finally, we look at the controller type and construct the proper type
    # of object
    #
    my $errors = 0;
375 376 377 378 379 380 381 382 383 384 385 386 387
    if ($type eq "IPMI") {
        my $device = new power_ipmi($IP,$verbose);
	if (!defined $device) {
	    warn "Unable to contact controller for $nodestr. Skipping...\n";
	    next;
	} else {
	    print "Calling device->power($op,@outlets)\n" if $verbose > 1;
	    if ($device->power($op,@outlets)) {
		print "Control of $nodestr failed.\n";
		$errors++;
	    }
	}
    }
388 389 390 391 392 393 394 395 396 397 398 399 400
    elsif ($type eq "icebox") {
        my $device = new power_icebox($IP,$verbose);
	if (!defined $device) {
	    warn "Unable to contact controller for $nodestr. Skipping...\n";
	    next;
	} else {
	    print "Calling device->power($op,@outlets)\n" if $verbose > 1;
	    if ($device->power($op,@outlets)) {
		print "Control of $nodestr failed.\n";
		$errors++;
	    }
	}
    }
401
    elsif ($type eq "APC") {
402 403
	my $device = new snmpit_apc($IP,$verbose);
	if (!defined $device) {
404
	    warn "Unable to contact controller for $nodestr. Skipping...\n";
405
	    next;
406
	} else {
407 408 409 410 411 412 413
	    print "Calling device->power($op,@outlets)\n"
		if $verbose > 1;
	    if ($device->power($op,@outlets)) {
		print "Control of $nodestr failed.\n";
		$errors++;
	    }
	}
414
    } elsif ($type =~ "RPC") {
415 416
	if (rpc27ctrl($op,$power_id,@outlets)) {
	    print "Control of $nodestr failed.\n"; $exitval++;
417
	}
418 419 420 421 422 423 424
    } elsif (($class eq "sg") || ($type eq "garcia")) {
	# XXX: 'garcia' is temporary until stargates are subnodes of
	# garcias
	if (sgmotectrl($op,@nodes)) {
	    print "Control of $nodestr failed.\n"; $exitval++;
	    $errors++;
	}
425 426 427 428 429 430
    } elsif ($type =~ /whol-(\w+)/) {
	my $iface = $1;
	if (wholctrl($op,$iface,@nodes)) {
	    print "Control of $nodestr failed.\n"; $exitval++;
	    $errors++;
	}
431
    } elsif ($type =~ /rmcp-(\w+)/) {
432
	require power_rmcp;
433
	if (power_rmcp::rmcpctrl($1,$op,@nodes)) {
434 435 436
	    print "Control of $nodestr failed.\n"; ++$exitval;
	    ++$errors;
	}
437
    } elsif ($type eq 'ilo2' || $type eq 'ilo' || $type eq 'drac') {
438 439 440 441
	if (iloctrl($type,$op,@nodes)) {
	    print "Control of $nodestr failed.\n"; ++$exitval;
            ++$errors;
        }
442 443 444 445 446
    } elsif ($type eq "mail") {
	if (mailctrl($op,@nodes)) {
	    print "Control of $nodestr failed.\n"; $exitval++;
	    $errors++;
	}
447
	$sendevent = 0; # power_mail sends this itself.
448
    } else {
449 450 451 452 453
	print "power: Unknown power type '$type'\n";
	$errors++;
    }

    if (!$errors) {
454 455 456
	foreach my $nodeid (@nodes) {
	    my $node = $nodes{$nodeid};
	    print "$nodeid now ",($op eq "cycle" ? "rebooting" : $op),"\n";
457
	    if ($sendevent) {
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
		my $oldstate = $node->eventstate();
		my $newstate;

		if ($op eq "off") {
		    $newstate = TBDB_NODESTATE_POWEROFF();
		}
		elsif ($op eq "cycle") {
		    $newstate = TBDB_NODESTATE_SHUTDOWN();
		}
		elsif ($op eq "on") {
		    #
		    # What if the node is on? Nothing is going to
		    # happen and changing the state is wrong.
		    #
		    $newstate = TBDB_NODESTATE_SHUTDOWN()
			if ($oldstate eq TBDB_NODESTATE_POWEROFF());
		}
		#
		# Avoid sending duplicate events; annoys stated.
		#
		$node->SetEventState($newstate)
		    if (defined($newstate) && $newstate ne $oldstate);
480
	    }
481 482
	    push(@wolnodes, $nodeid)
		if ($postwol{$nodeid});
483
	}
484 485
    } else {
	$exitval += $errors;
486
    }
487
}
488

489 490 491 492 493 494 495 496 497 498 499 500
#
# Handle the postwol option. This is technically incorrect; we really
# needed to start the wait operation before we turned the node off since
# we could miss the transition by starting to wait afterwards. But, that
# would require a complete reorg of this code and besides, the whole idea
# that nodes that need wakeonlan are not going to actually come back alive
# on their own.
#
if (@wolnodes) {
    if (dowol(@wolnodes) != 0) {
	$exitval++;
    }
501
}
502 503 504

# Return 0 on success. Return non-zero number of nodes that failed.
exit $exitval;
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527

sub byname() {
    my ($as, $an, $bs, $bn);

    if ($a =~ /(.*[^\d])(\d+)$/) {
	$as = $1; $an = $2;
    } else {
	$as = $a;
    }
    if ($b =~ /(.*[^\d])(\d+)$/) {
	$bs = $1; $bn = $2;
    } else {
	$bs = $b;
    }
    $as cmp $bs || $an <=> $bn;
}

#
# Query the given controllers for their status
#
sub dostatus(@) {
    my @wanted = @_;
    my %ctrls = ();
528
    my %IPs = ();
529 530 531 532 533 534 535 536 537 538 539 540
    my $errors = 0;

    if ($ELABINELAB) {
	warn "Cannot get status from inner elab\n";
	return 1;
    }

    my $doall = (@wanted == 1 && $wanted[0] eq "all");

    #
    # Fetch all possible power controllers
    #
541 542 543 544 545 546
    my $result = DBQueryFatal("select n.node_id,t.type,i.IP ".
			"from nodes as n " .
			"left join node_types as t on n.type=t.type " .
			"left join interfaces as i on n.node_id=i.node_id " .
			"where n.role='powerctrl'");
    while (my ($ctrl, $type, $IP) = $result->fetchrow()) {
547
	$ctrls{$ctrl} = $type;
548
	$IPs{$ctrl} = $IP;
549 550 551 552 553
    }

    @wanted = sort byname keys(%ctrls)
	if ($doall);

Mike Hibler's avatar
Mike Hibler committed
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
    #
    # For anything that was specified that is not a power controller,
    # look it up as a node and discover its controller.
    # XXX this is not very efficient.
    #
    my @nwanted = ();
    for my $node (@wanted) {    
	my $ctrl;

	if (!defined($ctrls{$node})) {
	    $result = DBQueryFatal("select power_id,outlet from outlets ". 
				   "where node_id='$node'");
	    if (!$result || $result->numrows == 0) {
		warn "No such power controller '$node', ignored\n";
		$errors++;
		next;
	    } else {
		($ctrl, $outlet) = $result->fetchrow();
		print "$node is $ctrl outlet $outlet...\n";
	    }
	} else {
	    $ctrl = $node;
	}
	push(@nwanted, $ctrl);
    }

580 581 582
    #
    # Loop through desired controllers getting status
    #
Mike Hibler's avatar
Mike Hibler committed
583
    for my $ctrl (@nwanted) {
584 585
	my %status;

586
	if ($ctrls{$ctrl} eq 'icebox') {
587
  	    my $device = new power_icebox($IPs{$ctrl}, $verbose);
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
	    if (!defined $device) {
		warn "Unable to contact controller $ctrl.\n";
		$errors++;
		next;
	    } else {
		print "Calling device->status()\n"
		    if $verbose > 1;
		if ($device->status(\%status)) {
		    print "Could not get status for $ctrl.\n";
		    $errors++;
		    next;
		}
	        for my $outlet (1..24) {
		    my $ostr = "outlet$outlet";
		    print "$ctrl Outlet $outlet: ", $status{$ostr}, "\n"
		        if (defined($status{$ostr}));
	        }
	        print "\n";
	    }
	} elsif ($ctrls{$ctrl} eq "APC") {
608 609 610 611 612 613 614 615 616 617 618 619 620 621
	    my $device = new snmpit_apc($IPs{$ctrl}, $verbose);
	    if (!defined $device) {
		warn "Unable to contact controller $ctrl.\n";
		$errors++;
		next;
	    } else {
		print "Calling device->status()\n"
		    if $verbose > 1;
		if ($device->status(\%status)) {
		    print "Could not get status for $ctrl.\n";
		    $errors++;
		    next;
		}
	    }
622 623 624
	    print "$ctrl Current: ", $status{current}, " Amps\n"
		if defined($status{current});
	    for my $outlet (1..24) {
625 626 627 628 629 630
		my $ostr = "outlet$outlet";
		print "$ctrl Outlet $outlet: ", $status{$ostr}, "\n"
		    if (defined($status{$ostr}));
	    }
	    print "\n";
	} elsif ($ctrls{$ctrl} =~ /^RPC/) {
631 632 633 634 635
	    if (rpc27status($ctrl,\%status)) {
		print "Could not get status for $ctrl.\n";
		$errors++;
		next;
	    }
Mike Hibler's avatar
Mike Hibler committed
636
	    print "$ctrl Current: ", $status{current}, " Amps\n"
637
		if defined($status{current});
Mike Hibler's avatar
Mike Hibler committed
638
	    print "$ctrl Power: ", $status{power}, " Watts\n"
639
		if defined($status{power});
640 641 642 643 644
	    if (defined($status{tempF}) || defined($status{tempC})) {
		my $temp = $status{tempF};
		if (!defined($temp)) {
		    $temp = $status{tempC} * 9 / 5 + 32;
		}
Mike Hibler's avatar
Mike Hibler committed
645
		printf "$ctrl Temperature: %.1f F\n", $temp;
646
	    }
647
	    for my $outlet (1..24) {
648
		my $ostr = "outlet$outlet";
Mike Hibler's avatar
Mike Hibler committed
649
		print "$ctrl Outlet $outlet: ", $status{$ostr}, "\n"
650 651
		    if (defined($status{$ostr}));
	    }
Mike Hibler's avatar
Mike Hibler committed
652
	    print "\n";
653 654 655 656 657 658 659 660
	} elsif (!$doall) {
	    warn "Cannot get status for $ctrl (type " .
		$ctrls{$ctrl} . ") yet\n";
	    $errors++;
	}
    }
    return $errors;
}
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

#
# Do a normal wakeonlan after power cycle. This is for laptops that do
# come back on (no bios setting to control it either).
#
sub dowol(@)
{
    my (@nodeids) = @_;
    my %macs      = ();

    # XXX Must know the outgoing interface. Using the whol flag. Ick.
    my $query_result =
	DBQueryFatal("select iface from interfaces ".
		     "where node_id='boss' and whol=1");
    if ($query_result->numrows != 1) {
	warn "WOL: Could not get outgoing interface for boss node.\n";
	return -1;
    }
    my ($iface) = $query_result->fetchrow_array();

    #
    # Grab the MACs for all of the nodes.
    #
    foreach my $nodeid (@nodeids) {
	$query_result =
	    DBQueryFatal("select mac from interfaces  ".
			 "where node_id='$nodeid' and ".
			 "      role='" . TBDB_IFACEROLE_CONTROL() . "'");

	if ($query_result->numrows != 1) {
	    warn "WOL: Could not get control interface MAC for $nodeid.\n";
	    next;
	}
	my ($mac) = $query_result->fetchrow_array();
	$macs{$nodeid} = $mac;
    }
    @nodeids = keys(%macs);

    print "Doing a plain WOL to @nodeids via interface $iface\n";
700
    logit("WOL: @nodeids\n");
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 728 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 760 761 762 763 764 765

    #
    # This is going to take an arbitrary length of time; we have no idea
    # how long it takes for the node to initialize itself and get to a
    # point where a wakeonlan packet will do something. So, we use state
    # waiting to find out when it hits pxeboot. Keep sending wol packets
    # until we get there.
    #
    my @states = (TBDB_NODESTATE_PXEBOOTING);
    if (initStateWait(\@states, @nodeids) != 0) {
	print "WOL: Could not initialize state waiting!\n";
	return -1;
    }

    # We have to wait at least a few seconds for the node to transition from
    # off to its sleep mode.
    sleep(15);

    #
    # Loop no more then 15 times (at 10 seconds per loop).
    #
    my $maxloops = 15;

    while (keys(%macs) && $maxloops > 0) {
	foreach my $nodeid (keys(%macs)) {
	    my $mac = $macs{$nodeid};
	    
	    # Do this a few times since the packet could get lost and
	    # it seems to take a couple of packets to kick it.
	    for (my $i = 0; $i < 5; $i++) {
		system("$WOL $iface $mac");
		select(undef, undef, undef, 0.1);
	    }
	}
	my @done = ();
	my @fail = ();
	
	if (waitForState(\@done, \@fail, 10) != 0) {
	    print "WOL: waitForState returned non zero!\n";
	    endStateWait();
	    return -1;
	}
	if (@fail) {
	    print "WOL: waitForState failed on @fail!\n";
	    foreach my $failed (@fail) {
		delete($macs{$failed});
	    }
	}
	foreach my $nodeid (@done) {
	    print "$nodeid is alive after wakeonlan.\n";
	    delete($macs{$nodeid});
	}

	@nodeids = keys(%macs);
	print "Sending more wol packets to @nodeids ...\n"
	    if (@nodeids);
	$maxloops--;
    }
    endStateWait();
    if (@nodeids) {
	print "WOL: @nodeids did not power on after many wakeonlan packets!\n";
	return -1;
    }
    return 0;
}
766 767 768 769 770 771

sub logit($)
{
    my ($message) = @_;
    my ($me) = getpwuid($UID);

772 773 774 775 776
    syslog("info", "[$me] $message");
}

END {
    closelog();
777
}