checknodes_daemon.in 9.05 KB
Newer Older
1 2 3
#!/usr/bin/perl -w
#
# GENIPUBLIC-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 86 87 88 89 90 91 92 93 94 95 96
#
# Check for existing pidfile and running daemon. Mostly cause
# of devel tree versions.
#
if (-e $PIDFILE) {
    my $opid = `cat $PIDFILE`;
    chomp($opid);
    if (kill(0, $opid)) {
	fatal("Lockfile $PIDFILE exists, and process $opid is running");
    }
    unlink($PIDFILE);
}

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 123 124 125 126 127 128
#
# 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
129 130 131 132 133 134 135 136
#
# 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.");
}

137 138 139 140 141 142
# Go to ground.
if (! $debug) {
    if (TBBackGround($LOGFILE)) {
	exit(0);
    }
}
143 144 145 146 147
# This acts as a lockfile to prevent a devel version from running at
# the same time as the real version or another devel version.
if (system("echo '$PID' > $PIDFILE")) {
    fatal("Could not create $PIDFILE!");
}
148 149 150 151 152 153 154 155 156 157 158
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()
{
159 160 161
    my $SAVEEUID = $EUID;
    
    $EUID = 0;
162
    ReOpenLog($LOGFILE);
163
    $EUID = $SAVEEUID;
164 165 166 167 168
}
$SIG{HUP} = \&handler
    if (!$debug);

while (1) {
169 170 171 172
    my @informtbopsfatal = ();
    my @informtbopswarn  = ();
    my @tmp = ();

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

176 177 178 179 180 181 182
    #
    # 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 ".
183
	    "left join node_types as t on t.type=n.type ".
184 185 186 187 188
	    "where (n.eventstate!='". TBDB_NODESTATE_ISUP ."' and ".
	    "       n.eventstate!='". TBDB_NODESTATE_PXEWAIT ."' and ".
	    "       n.eventstate!='". TBDB_NODESTATE_ALWAYSUP ."' and ".
	    "       n.eventstate!='". TBDB_NODESTATE_POWEROFF ."') and ".
            "       r.pid is null and n.role='testnode' and ".
189
	    "       t.isvirtnode=0 and ".
190 191 192 193 194 195
            "       (UNIX_TIMESTAMP(now()) - n.state_timestamp) > 600");
    goto loop
	if (!$query_result);

    while (my ($nodeid,$eventstate,$stamp) = $query_result->fetchrow_array()) {
	print "Node in unknown state: $nodeid,$eventstate,$stamp\n";
196 197 198

	next
	    if ($impotent);
199 200 201 202 203 204
	
	my $node = Node->Lookup($nodeid);
	if (!defined($node)) {
	    print STDERR "Cannot find object for $nodeid\n";
	    next;
	}
205 206 207 208 209 210 211 212
	#
	# Skip nodes that are not imageable; we cannot load them
	# to test them.
	#
	next
	    if (!$node->imageable() ||
		!defined($node->default_imageid()));
	
Leigh Stoller's avatar
Leigh Stoller committed
213 214 215 216 217
	#
	# The node is not in any experiment, so we have to explicitly
	# push it into the hwcheckup experiment.
	#
	if ($node->MoveReservation($experiment)) {
Leigh Stoller's avatar
Leigh Stoller committed
218
	    print STDERR "Could not move $node to $experiment\n";
Leigh Stoller's avatar
Leigh Stoller committed
219 220
	    next;
	}
221 222 223 224 225
	$node->InsertNodeLogEntry($elabman, TB_DEFAULT_NODELOGTYPE(),
				  "'Moved to hwcheckup by checknodes daemon; ".
				  "stuck in $eventstate since $stamp'");
    }

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

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

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

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

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

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    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  = ();
276

277 278 279 280 281 282 283
	$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";
284 285
	}

286 287 288 289 290 291 292 293 294
	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);
	    }
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 337
	}
    }
    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:
338
    sleep(($debug ? 10 : 60));
339 340 341
}
exit(0);

342 343 344 345
sub NodeIsDead($)
{
    my ($node) = @_;
    
Leigh Stoller's avatar
Leigh 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 376 377 378
sub fatal($)
{
    my ($msg) = @_;

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

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