checknodes_daemon.in 10 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2009-2011 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
#
use strict;
use English;
use Getopt::Std;

#
# Attempt to determine if nodes are really messed up.
#
sub usage()
{
    print "Usage: checknodes_daemon [-d]\n";
    exit(1);
}
36
my $optlist   = "dn";
37
my $debug     = 0;
38
my $impotent  = 0;
39 40 41 42 43 44 45 46

#
# Configure variables
#
my $TB		  = "@prefix@";
my $TBOPS         = "@TBOPSEMAIL@";
my $TBLOGS        = "@TBLOGSEMAIL@";
my $LOGFILE       = "$TB/log/checknodes.log";
47
my $PIDFILE       = "/var/run/checknodes.pid";
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
my $SUDO          = "/usr/local/bin/sudo";
my $PROTOUSER     = "elabman";
my $WAP           = "$TB/sbin/withadminprivs";
my $BATCHEXP      = "$TB/bin/batchexp";
my $NAMED_SETUP   = "$TB/sbin/named_setup";
my $EXPORTS_SETUP = "$TB/sbin/exports_setup";
my $GENTOPOFILE   = "$TB/libexec/gentopofile";
my $NFREE         = "$TB/bin/nfree";

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/usr/site/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

# Protos
sub fatal($);
63
sub logit($$);
64 65
sub NodeIsDead($);
sub NodeIsOkay($);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	  
#
# Turn off line buffering on output
#
$| = 1; 

if ($UID != 0) {
    die("Must be root to run this script\n");
}

#
# Check args early so we get the right DB.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
84
    $debug++;
85
}
86 87 88
if (defined($options{"n"})) {
    $impotent = 1;
}
89

90 91 92
# Set this to turn off tblog in libraries.
$ENV{'TBLOG_OFF'} = "yep";

93 94 95 96 97 98 99 100 101 102 103 104
# Load the Testbed support stuff.
use lib "@prefix@/lib";
use libdb;
use libosload;
use libtestbed;
use Experiment;
use Node;
use User;

my $NODEILL_PID = NODEILL_PID();
my $NODEILL_EID = NODEILL_EID();

105
#
106
# Only one please.
107
#
108 109
if (CheckDaemonRunning("checknodes_daemon")) {
    fatal("Not starting another checknodes daemon!");
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
#
# We need this user for running below.
#
my $elabman = User->Lookup($PROTOUSER);
if (!defined($elabman)) {
    fatal("Could not lookup $PROTOUSER user. Exiting ...");
}

#
# Grab the expeiment we use,
#
my $experiment = Experiment->Lookup($NODEILL_PID, $NODEILL_EID);
if (!defined($experiment)) {
    #
    # Create if it does not exist.
    #
    system("$SUDO -u $PROTOUSER $WAP $BATCHEXP ".
	   " -q -i -k -j -w -f -n -S 'System Experiment' ".
	   " -L 'System Experiment' ".
	   " -E 'Check failed nodes before moving to hwdown - DO NOT DELETE' ".
	   " -p $NODEILL_PID -e $NODEILL_EID");
    if ($?) {
 	fatal("Could not create experiment for $NODEILL_PID/$NODEILL_EID\n");
    }
    $experiment = Experiment->Lookup($NODEILL_PID, $NODEILL_EID);
}
if ($experiment->state() eq EXPTSTATE_SWAPPED()) {
    $experiment->SetState(EXPTSTATE_ACTIVE());
}
my $pid = $experiment->pid();
my $eid = $experiment->eid();

Leigh Stoller's avatar
Leigh Stoller committed
144 145 146 147 148 149 150 151
#
# We need the hwdown experiment below.
#
my $hwdown_experiment = Experiment->Lookup(NODEDEAD_PID(), NODEDEAD_EID());
if (!defined($hwdown_experiment)) {
    fatal("Cannot find the hwdown experiment.");
}

152 153 154 155 156 157
# Go to ground.
if (! $debug) {
    if (TBBackGround($LOGFILE)) {
	exit(0);
    }
}
158 159
if (MarkDaemonRunning("checknodes_daemon")) {
    fatal("Could not mark daemon as running!");
160
}
161
logit("Check Nodes Daemon starting ... pid $$", 0);
162 163 164 165 166 167 168 169 170 171

if ($elabman->FlipTo($experiment->unix_gid())) {
    fatal("Could not flipto $elabman ($experiment)");
}

#
# Setup a signal handler for newsyslog.
#
sub handler()
{
172 173 174
    my $SAVEEUID = $EUID;
    
    $EUID = 0;
175
    ReOpenLog($LOGFILE);
176
    $EUID = $SAVEEUID;
177 178 179 180 181
}
$SIG{HUP} = \&handler
    if (!$debug);

while (1) {
182 183 184
    my @informtbopsfatal = ();
    my @informtbopswarn  = ();
    my @tmp = ();
185
    my $disabled;
186

187
    if (! TBGetSiteVar("web/nologins", \$disabled) || $disabled) {
188
	logit("Skipping this loop because of nologins", 0);
189 190
	goto loop;
    }
191
    logit("Running", 0);
192

193 194 195 196 197 198 199
    #
    # Look for nodes in a wierd state. Lets test them.
    #
    my $query_result = 
	DBQueryWarn("select n.node_id,n.eventstate, ".
	    "   FROM_UNIXTIME(n.state_timestamp) from nodes as n ".
	    "left join reserved as r on r.node_id=n.node_id ".
200
	    "left join node_types as t on t.type=n.type ".
201 202
	    "where (n.eventstate!='". TBDB_NODESTATE_ISUP ."' and ".
	    "       n.eventstate!='". TBDB_NODESTATE_PXEWAIT ."' and ".
203
	    "       n.eventstate!='". TBDB_NODESTATE_PXELIMBO ."' and ".
204 205 206
	    "       n.eventstate!='". TBDB_NODESTATE_ALWAYSUP ."' and ".
	    "       n.eventstate!='". TBDB_NODESTATE_POWEROFF ."') and ".
            "       r.pid is null and n.role='testnode' and ".
207
	    "       t.isvirtnode=0 and ".
208 209 210 211 212 213 214
            "       (UNIX_TIMESTAMP(now()) - n.state_timestamp) > 600");
    goto loop
	if (!$query_result);

    while (my ($nodeid,$eventstate,$stamp) = $query_result->fetchrow_array()) {
	my $node = Node->Lookup($nodeid);
	if (!defined($node)) {
215
	    logit("Cannot find object for $nodeid", 1);
216 217
	    next;
	}
218 219 220 221 222 223 224
	#
	# Skip nodes that are not imageable; we cannot load them
	# to test them.
	#
	next
	    if (!$node->imageable() ||
		!defined($node->default_imageid()));
225

226
	logit("Node in unknown state: $nodeid,$eventstate,$stamp", 0);
227 228 229

	next
	    if ($impotent);
230
	
Leigh Stoller's avatar
Leigh Stoller committed
231 232 233 234 235
	#
	# The node is not in any experiment, so we have to explicitly
	# push it into the hwcheckup experiment.
	#
	if ($node->MoveReservation($experiment)) {
236
	    logit("Could not move $node to $experiment", 1);
Leigh Stoller's avatar
Leigh Stoller committed
237 238
	    next;
	}
239 240 241 242 243
	$node->InsertNodeLogEntry($elabman, TB_DEFAULT_NODELOGTYPE(),
				  "'Moved to hwcheckup by checknodes daemon; ".
				  "stuck in $eventstate since $stamp'");
    }

244 245 246 247 248 249 250
    $experiment->Flush();
    Node->FlushAll();

    my @nodelist = $experiment->NodeList();
    goto loop
	if (!@nodelist);

251
    if ($impotent) {
252
	logit("Would check @nodelist", 0);
253 254 255
	goto loop;
    }

256 257
    foreach my $node (@nodelist) {
	if ($node->ClearBootAttributes()) {
258
	    logit("$node: Could not clear boot attributes.", 1);
259
	    next;
260
	}
261
	if (! $node->imageable()) {
262
	    logit("$node is not imageable.", 1);
263 264 265 266 267
	    NodeIsDead($node);
	    push(@informtbopsfatal, $node->node_id());
	    next;
	}
	push(@tmp, $node);
268
    }
269
    @nodelist = @tmp;
270

271
    logit("Checking nodes @nodelist", 0);
Leigh Stoller's avatar
Leigh Stoller committed
272

273
    if (@nodelist) {
274
	logit("Running $GENTOPOFILE ...", 0);
275
	if (system("$GENTOPOFILE $pid $eid")) {
276
	    logit("$GENTOPOFILE failed", 1);
277 278
	    next;
	}
279
	logit("Running $EXPORTS_SETUP ...", 0);
280
	if (system("$EXPORTS_SETUP")) {
281
	    logit("$EXPORTS_SETUP failed", 1);
282 283 284 285
	    next;
	}
	# The nodes will not boot locally unless there is a DNS
	# record.
286
	logit("Running $NAMED_SETUP ...", 0);
287
	if (system("$NAMED_SETUP")) {
288
	    logit("$NAMED_SETUP failed", 1);
289 290 291 292 293
	    next;
	}
	my @nodenames       = map { $_->node_id() } @nodelist;
	my %reload_args     = ();
	my %reload_results  = ();
294

295 296
	$reload_args{'debug'}     = $debug;
	$reload_args{'waitmode'}  = 2; # XXX Wait till reboot after reload.
297
	$reload_args{'prepare'}   = 1; # XXX Clear out old disk metadata.
298
	$reload_args{'nodelist'}  = [ @nodenames ];
299
	logit("Running osload on @nodenames", 0);
300 301
	my $failures = osload(\%reload_args, \%reload_results);
	if ($failures) {
302
	    logit("osload returned $failures failures", 1);
303 304
	}

305 306 307 308 309 310 311 312 313
	foreach my $node (@nodelist) {
	    if ($reload_results{$node->node_id()}) {
		push(@informtbopsfatal, $node->node_id());
		NodeIsDead($node);
	    }
	    else {
		push(@informtbopswarn, $node->node_id());
		NodeIsOkay($node);
	    }
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
	}
    }
    if (@informtbopsfatal) {
	my $count = scalar(@informtbopsfatal);
	SENDMAIL($TBOPS, "$count nodes are down",
		 "Nodes:\n".
		 "  " . join(" ", @informtbopsfatal) . "\n".
		 "appear to be dead.\n\n".
		 "The nodes have been taken out of the pool.\n");
    }
    if (@informtbopswarn) {
	my $count = scalar(@informtbopswarn);

	system("$NFREE $pid $eid @informtbopswarn");
	if ($?) {
	    fatal("Could not free nodes: @informtbopswarn");
	}
	else {
	    SENDMAIL($TBOPS, "$count nodes appear to be okay",
		     "Nodes:\n".
		     "  " . join(" ", @informtbopswarn) . "\n".
		     "have reloaded and rebooted okay.\n\n".
		     "The nodes have been freed.\n");
	}
    }

340
    logit("Running $GENTOPOFILE ...", 0);
341
    if (system("$GENTOPOFILE $pid $eid")) {
342
	logit("$GENTOPOFILE failed", 1);
343 344
	next;
    }
345
    logit("Running $EXPORTS_SETUP ...", 0);
346
    if (system("$EXPORTS_SETUP")) {
347
	logit("$EXPORTS_SETUP failed", 1);
348 349 350
	next;
    }
    # The nodes will not boot locally unless there is a DNS record.
351
    logit("Running $NAMED_SETUP ...", 0);
352
    if (system("$NAMED_SETUP")) {
353
	logit("$NAMED_SETUP failed", 1);
354 355 356
	next;
    }
  loop:
357
    sleep(($debug ? 10 : 60));
358
}
359
MarkDaemonStopped("checknodes_daemon");
360 361
exit(0);

362 363 364 365
sub NodeIsDead($)
{
    my ($node) = @_;
    
Leigh Stoller's avatar
Leigh Stoller committed
366
    if ($node->MoveReservation($hwdown_experiment)) {
367
	logit("Could not move $node to $hwdown_experiment", 1);
Leigh Stoller's avatar
Leigh Stoller committed
368 369
	return;
    }
370
    logit("$node is fatally ill; moving to hwdown.", 1);
371 372 373 374 375 376 377 378
    $node->InsertNodeLogEntry($elabman, TB_DEFAULT_NODELOGTYPE(),
			      "Moved to hwdown by checknodes daemon");
}

sub NodeIsOkay($)
{
    my ($node) = @_;

379
    logit("$node appears to be okay; releasing.", 1);
380 381 382 383
    $node->InsertNodeLogEntry($elabman, TB_DEFAULT_NODELOGTYPE(),
			      "Released by checknodes daemon");
}

384 385 386 387 388 389 390 391 392 393 394 395
sub fatal($)
{
    my ($msg) = @_;

    #
    # Send a message to the testbed list. 
    #
    SENDMAIL($TBOPS,
	     "Check Nodes Daemon died",
	     $msg,
	     $TBOPS);

396 397
    MarkDaemonStopped("checknodes_daemon");

398 399 400
    die("*** $0:\n".
	"    $msg\n");
}
401

402 403 404 405 406 407 408 409 410 411 412
sub logit($$)
{
    my ($msg,$stderr) = @_;
    my $stamp = POSIX::strftime("20%y-%m-%d %H:%M:%S", localtime());

    if ($stderr) {
	print STDERR "$stamp: $msg\n";
    } else {
	print "$stamp: $msg\n";
    }
}