checknodes_daemon.in 10 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2009-2018 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.class!='switch' and ".
208
	    "       t.isvirtnode=0 and ".
209 210 211 212 213 214 215
            "       (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)) {
216
	    logit("Cannot find object for $nodeid", 1);
217 218
	    next;
	}
219 220 221 222 223 224 225
	#
	# Skip nodes that are not imageable; we cannot load them
	# to test them.
	#
	next
	    if (!$node->imageable() ||
		!defined($node->default_imageid()));
226

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

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

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

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

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

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

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

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

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

306 307 308 309 310 311 312 313 314
	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);
	    }
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 340
	}
    }
    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");
	}
    }

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

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

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

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

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

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

397 398
    MarkDaemonStopped("checknodes_daemon");

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

403 404 405 406 407 408 409 410 411 412 413
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";
    }
}