checknodes_daemon.in 9.07 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# EMULAB-COPYRIGHT
4
# Copyright (c) 2009-2010 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18
# All rights reserved.
#
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);
}
19
my $optlist   = "dn";
20
my $debug     = 0;
21
my $impotent  = 0;
22 23 24 25 26 27 28 29

#
# Configure variables
#
my $TB		  = "@prefix@";
my $TBOPS         = "@TBOPSEMAIL@";
my $TBLOGS        = "@TBLOGSEMAIL@";
my $LOGFILE       = "$TB/log/checknodes.log";
30
my $PIDFILE       = "/var/run/checknodes.pid";
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
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($);
46 47
sub NodeIsDead($);
sub NodeIsOkay($);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	  
#
# 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"})) {
66
    $debug++;
67
}
68 69 70
if (defined($options{"n"})) {
    $impotent = 1;
}
71 72 73 74 75 76 77 78 79 80 81 82 83

# 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();

84
#
85
# Only one please.
86
#
87 88
if (CheckDaemonRunning("checknodes_daemon")) {
    fatal("Not starting another checknodes daemon!");
89 90
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
#
# 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 B. Stoller's avatar
Leigh B. Stoller committed
123 124 125 126 127 128 129 130
#
# 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.");
}

131 132 133 134 135 136
# Go to ground.
if (! $debug) {
    if (TBBackGround($LOGFILE)) {
	exit(0);
    }
}
137 138
if (MarkDaemonRunning("checknodes_daemon")) {
    fatal("Could not mark daemon as running!");
139
}
140 141 142 143 144 145 146 147 148 149 150
print "Check Nodes Daemon starting ... pid $$, at ".`date`;

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

#
# Setup a signal handler for newsyslog.
#
sub handler()
{
151 152 153
    my $SAVEEUID = $EUID;
    
    $EUID = 0;
154
    ReOpenLog($LOGFILE);
155
    $EUID = $SAVEEUID;
156 157 158 159 160
}
$SIG{HUP} = \&handler
    if (!$debug);

while (1) {
161 162 163
    my @informtbopsfatal = ();
    my @informtbopswarn  = ();
    my @tmp = ();
164
    my $disabled;
165

166 167 168
    print "Running at ".
	POSIX::strftime("20%y-%m-%d %H:%M:%S", localtime()) . "\n";

169 170 171 172 173
    if (! TBGetSiteVar("web/nologins", \$disabled) || $disabled) {
	print "  Skipping this loop cause of nologins\n";
	goto loop;
    }

174 175 176 177 178 179 180
    #
    # 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 ".
181
	    "left join node_types as t on t.type=n.type ".
182 183
	    "where (n.eventstate!='". TBDB_NODESTATE_ISUP ."' and ".
	    "       n.eventstate!='". TBDB_NODESTATE_PXEWAIT ."' and ".
184
	    "       n.eventstate!='". TBDB_NODESTATE_PXELIMBO ."' and ".
185 186 187
	    "       n.eventstate!='". TBDB_NODESTATE_ALWAYSUP ."' and ".
	    "       n.eventstate!='". TBDB_NODESTATE_POWEROFF ."') and ".
            "       r.pid is null and n.role='testnode' and ".
188
	    "       t.isvirtnode=0 and ".
189 190 191 192 193 194 195 196 197 198
            "       (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)) {
	    print STDERR "Cannot find object for $nodeid\n";
	    next;
	}
199 200 201 202 203 204 205
	#
	# Skip nodes that are not imageable; we cannot load them
	# to test them.
	#
	next
	    if (!$node->imageable() ||
		!defined($node->default_imageid()));
206 207 208 209 210

	print "Node in unknown state: $nodeid,$eventstate,$stamp\n";

	next
	    if ($impotent);
211
	
Leigh B. Stoller's avatar
Leigh B. Stoller committed
212 213 214 215 216
	#
	# The node is not in any experiment, so we have to explicitly
	# push it into the hwcheckup experiment.
	#
	if ($node->MoveReservation($experiment)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
217
	    print STDERR "Could not move $node to $experiment\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
218 219
	    next;
	}
220 221 222 223 224
	$node->InsertNodeLogEntry($elabman, TB_DEFAULT_NODELOGTYPE(),
				  "'Moved to hwcheckup by checknodes daemon; ".
				  "stuck in $eventstate since $stamp'");
    }

225 226 227 228 229 230 231
    $experiment->Flush();
    Node->FlushAll();

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

232 233 234 235 236
    if ($impotent) {
	print "Would check @nodelist\n";
	goto loop;
    }

237 238 239
    foreach my $node (@nodelist) {
	if ($node->ClearBootAttributes()) {
	    print STDERR "$node: Could not clear boot attributes.\n";
240
	    next;
241
	}
242 243 244 245 246 247 248
	if (! $node->imageable()) {
	    print STDERR "$node is not imageable.\n";
	    NodeIsDead($node);
	    push(@informtbopsfatal, $node->node_id());
	    next;
	}
	push(@tmp, $node);
249
    }
250
    @nodelist = @tmp;
251

Leigh B. Stoller's avatar
Leigh B. Stoller committed
252 253
    print "Checking nodes @nodelist\n";

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    if (@nodelist) {
	print "Running $GENTOPOFILE ...\n";
	if (system("$GENTOPOFILE $pid $eid")) {
	    print STDERR "$GENTOPOFILE failed\n";
	    next;
	}
	print "Running $EXPORTS_SETUP ...\n";
	if (system("$EXPORTS_SETUP")) {
	    print STDERR "$EXPORTS_SETUP failed\n";
	    next;
	}
	# The nodes will not boot locally unless there is a DNS
	# record.
	print "Running $NAMED_SETUP ...\n";
	if (system("$NAMED_SETUP")) {
	    print STDERR "$NAMED_SETUP failed\n";
	    next;
	}
	my @nodenames       = map { $_->node_id() } @nodelist;
	my %reload_args     = ();
	my %reload_results  = ();
275

276 277 278 279 280 281 282
	$reload_args{'debug'}     = $debug;
	$reload_args{'waitmode'}  = 2; # XXX Wait till reboot after reload.
	$reload_args{'nodelist'}  = [ @nodenames ];
	print "Running osload on @nodenames\n";
	my $failures = osload(\%reload_args, \%reload_results);
	if ($failures) {
	    print STDERR "osload returned $failures failures\n";
283 284
	}

285 286 287 288 289 290 291 292 293
	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);
	    }
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
	}
    }
    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");
	}
    }

    print "Running $GENTOPOFILE ...\n";
    if (system("$GENTOPOFILE $pid $eid")) {
	print STDERR "$GENTOPOFILE failed\n";
	next;
    }
    print "Running $EXPORTS_SETUP ...\n";
    if (system("$EXPORTS_SETUP")) {
	print STDERR "$EXPORTS_SETUP failed\n";
	next;
    }
    # The nodes will not boot locally unless there is a DNS record.
    print "Running $NAMED_SETUP ...\n";
    if (system("$NAMED_SETUP")) {
	print STDERR "$NAMED_SETUP failed\n";
	next;
    }
  loop:
337
    sleep(($debug ? 10 : 60));
338
}
339
MarkDaemonStopped("checknodes_daemon");
340 341
exit(0);

342 343 344 345
sub NodeIsDead($)
{
    my ($node) = @_;
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
346 347 348 349
    if ($node->MoveReservation($hwdown_experiment)) {
	print STDERR "Could not move $node to $hwdown_experiment\n";
	return;
    }
350 351 352 353 354 355 356 357 358 359 360 361 362 363
    print STDERR "$node is fatally ill; moving to hwdown.\n";
    $node->InsertNodeLogEntry($elabman, TB_DEFAULT_NODELOGTYPE(),
			      "Moved to hwdown by checknodes daemon");
}

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

    print STDERR "$node appears to be okay; releasing.\n";
    $node->InsertNodeLogEntry($elabman, TB_DEFAULT_NODELOGTYPE(),
			      "Released by checknodes daemon");
}

364 365 366 367 368 369 370 371 372 373 374 375
sub fatal($)
{
    my ($msg) = @_;

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

376 377
    MarkDaemonStopped("checknodes_daemon");

378 379 380
    die("*** $0:\n".
	"    $msg\n");
}
381