checknodes_daemon.in 9.11 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
	    "where (n.eventstate!='". TBDB_NODESTATE_ISUP ."' and ".
	    "       n.eventstate!='". TBDB_NODESTATE_PXEWAIT ."' and ".
186
	    "       n.eventstate!='". TBDB_NODESTATE_PXELIMBO ."' and ".
187 188 189
	    "       n.eventstate!='". TBDB_NODESTATE_ALWAYSUP ."' and ".
	    "       n.eventstate!='". TBDB_NODESTATE_POWEROFF ."') and ".
            "       r.pid is null and n.role='testnode' and ".
190
	    "       t.isvirtnode=0 and ".
191 192 193 194 195 196 197 198 199 200
            "       (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;
	}
201 202 203 204 205 206 207
	#
	# Skip nodes that are not imageable; we cannot load them
	# to test them.
	#
	next
	    if (!$node->imageable() ||
		!defined($node->default_imageid()));
208 209 210 211 212

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

	next
	    if ($impotent);
213
	
Leigh Stoller's avatar
Leigh Stoller committed
214 215 216 217 218
	#
	# 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
219
	    print STDERR "Could not move $node to $experiment\n";
Leigh Stoller's avatar
Leigh Stoller committed
220 221
	    next;
	}
222 223 224 225 226
	$node->InsertNodeLogEntry($elabman, TB_DEFAULT_NODELOGTYPE(),
				  "'Moved to hwcheckup by checknodes daemon; ".
				  "stuck in $eventstate since $stamp'");
    }

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

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

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

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

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

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

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

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

343 344 345 346
sub NodeIsDead($)
{
    my ($node) = @_;
    
Leigh Stoller's avatar
Leigh Stoller committed
347 348 349 350
    if ($node->MoveReservation($hwdown_experiment)) {
	print STDERR "Could not move $node to $hwdown_experiment\n";
	return;
    }
351 352 353 354 355 356 357 358 359 360 361 362 363 364
    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");
}

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

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

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