reportboot_daemon.in 9.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#!/usr/bin/perl -w
#
# Copyright (c) 2014-2016 University of Utah and the Flux Group.
# 
# {{{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/>.
# 
# }}}
#
use English;
use Getopt::Std;
use IO::Socket::UNIX;
use POSIX qw(strftime);
use Carp;

#
# This script is invoked on a dhcpd "commit" event.
#
# Intended to trigger Emulab node boot related activities.
#

sub usage()
{
    print "Usage: $0 [-dUDC]\n";
    print("Options:\n");
    print(" -d      - Turn on debugging\n");
41
    print(" -f      - Do not daemonize, as for daemon_wrapper\n");
42 43 44 45 46 47 48 49 50
    print(" -C      - Combine PXEBOOTING/BOOTING on PXE or U-boot boots;\n");
    print("           Send nothing on an OS boot.");
    print(" -D      - Record event times in a DB to moderate send rate\n");
    print(" -U      - Combine PXEBOOTING/BOOTING on U-boot boots only;\n");
    print("           Send nothing on an PXE or OS boot.");
    print("boot-type is one of OS, PXE, UBOOT.\n");
 
    exit(1);
}
51
my $optlist    = "dCDUnf";
52
my $debug      = 0;
53
my $nodaemon   = 0;
54 55 56 57 58
my $impotent   = 0;
my $combined   = 0;
my $ubootonly  = 0;
my $issubboss  = 0;
my $usedb      = 0;
59
my $event_handle;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

#
# Functions
#
sub report($$$);
sub findnode($$);
sub logit($);
sub sendevent($$);
sub fatal($);

#
# Configure variables
#
my $TBOPS            = "@TBOPSEMAIL@";
my $EVSERVER	     = "boss";
my $EVDB             = "reportboot";
my $NODEMAPDB        = "nodemap";
my $LOGFILE          = "reportboot.log";
my $CLIENT_VARDIR    = "@CLIENT_VARDIR@";
my $SOCKETFILE       = "/var/run/reportboot.sock";

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

# Protos
sub fatal($);
	  
#
# Minimum time between events.
# In bootinfo, this is 10 seconds!
#
my $EVMININT = 5;

#
# Turn off line buffering on output
#
$| = 1;

#
# We run this on boss *and* subboss so its config is a little cumbersome.
#
if (-e "/etc/emulab/issubboss") {
    require "/etc/emulab/paths.pm";
    import emulabpaths;
    require event;
    import event;
    require libtestbed;
    import libtestbed;

    $issubboss = 1;
    $EVDB      = "$CLIENT_VARDIR/db/$EVDB";
    $NODEMAPDB = "$CLIENT_VARDIR/db/$NODEMAPDB";
    $LOGFILE   = "$CLIENT_VARDIR/logs/$LOGFILE";
}
else {
    unshift(@INC, "@prefix@/lib");
    require event;
    import event;
    require libtestbed;
    import libtestbed;
    require Interface;

    $EVDB      = "@prefix@/db/$EVDB";
    $NODEMAPDB = "@prefix@/db/$NODEMAPDB";
    $LOGFILE   = "@prefix@/log/$LOGFILE";
}
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"C"})) {
    $combined = 1;
    $ubootonly = 0;
}
if (defined($options{"U"})) {
    $combined = 1;
    $ubootonly = 1;
}
if (defined($options{"D"})) {
    $usedb = 1;
}
if (defined($options{"d"})) {
    $debug = 1;
}
145 146 147
if (defined($options{"f"})) {
    $nodaemon = 1;
}
148 149 150 151
if (defined($options{"n"})) {
    $impotent = 1;
}

152
if (! ($debug || $nodaemon)) {
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    if (CheckDaemonRunning("reportboot_daemon")) {
	fatal("Not starting another reportboot daemon!");
    }
    # Go to ground.
    if (TBBackGround($LOGFILE)) {
	exit(0);
    }
    if (MarkDaemonRunning("reportboot_daemon")) {
	fatal("Could not mark daemon as running!");
    }
}
#
# Setup a signal handler for newsyslog.
#
sub handler()
{
    ReOpenLog($LOGFILE);
}
$SIG{HUP} = \&handler
172
    if (! ($debug || $nodaemon));
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

#
# We use a unix domain socket to talk to the client that is invoked
# by dhcpd. This provides the buffering so we do not block dhcpd.
#
unlink($SOCKETFILE)
    if (-e $SOCKETFILE);

my $socket = IO::Socket::UNIX->new(Type   => SOCK_STREAM(),
				   Local  => $SOCKETFILE,
				   Listen => 5);
if (!$socket) {
    fatal("Could not create socket $SOCKETFILE");
}

#
# Loop reading new messages from dhcpd, written with reportboot.
#
while (1)
{
    my $client = $socket->accept();
    my ($token) = <$client>;
    if (!$token) {
	print STDERR "Got nothing from $SOCKETFILE\n";
	next;
    }
    chomp($token);
    my ($ip,$mac,$boottype) = split(",", $token);
    if (!(defined($ip) && defined($mac) && defined($boottype))) {
	print STDERR "Invalid input from dhcpd: '$token'\n";
	next;
    }
    report($ip,$mac,$boottype);
}

sub report($$$)
{
    my ($IP,$MAC,$boottype) = @_;
    
    if ($IP !~ /^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$/) {
	logit("'IP' is not an IP address");
	return -1;
    }
    $MAC = lc($MAC);
    if ($MAC =~ /^([0-9a-f]+):([0-9a-f]+):([0-9a-f]+):([0-9a-f]+):([0-9a-f]+):([0-9a-f]+)$/) {
	# dhcpd will return an octet like "02" as "2", so we have to compensate
	$MAC = sprintf("%02x%02x%02x%02x%02x%02x",
		       hex($1), hex($2), hex($3), hex($4), hex($5), hex($6));
    }
    else {
	logit("'$MAC' is not a MAC address");
	return -1;
    }
    if ($boottype !~ /^(PXE|OS|UBOOT)$/) {
	logit("Invalid boot-type '$boottype'");
	return -1;
    }
    my $nodeid = findnode($IP, $MAC);
    if (!$nodeid) {
Leigh Stoller's avatar
Leigh Stoller committed
232
	logit("Ignoring unknown node $IP,$MAC");
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 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
	return -1;
    }

    #
    # See if we need to send events.
    # We won't send more than one event per second.
    #
    if ($usedb) {
	my %DB;
	my $lasttime = 0;
	my $now = time();
	my $key = "$IP/$boottype";

	if (!dbmopen(%DB, $EVDB, 0660)) {
	    logit("$nodeid: could not open $EVDB");
	    return -1;
	}

	my $needone = 1;
	if (defined($DB{$key})) {
	    $lasttime = $DB{$key};
	    # XXX watch for time going backward
	    if ($now >= $lasttime &&
		$now < ($lasttime + $EVMININT)) {
		$needone = 0;
	    }
	}
	if ($needone) {
	    $DB{$key} = $now;
	}
	dbmclose(%DB);

	if (!$needone) {
	    logit("$nodeid: NOT sending BOOTING event (too soon)");
	    return 0;
	}
    }

    #
    # Combine reporting of PXEBOOTING and BOOTING on the PXE event, reporting
    # nothing on the OS event. Use this if you have problems with later
    # OS-generated events happening before the BOOTING gets reported.
    # Note that this is essentially the same as what bootinfo does.
    #
    if ($combined) {
	if ($boottype eq "UBOOT" || ($boottype eq "PXE" && !$ubootonly)) {
	    if (sendevent($nodeid, "PXEBOOTING") ||
		sendevent($nodeid, "BOOTING")) {
		logit("$nodeid: could not send PXEBOOTING/BOOTING events");
		
	    }
	    logit("$nodeid: $boottype: sent PXEBOOTING and BOOTING events");
	}
	else {
	    logit("$nodeid: $boottype: NOT sending BOOTING event (combo mode)");
	}
    }
    else {
	my $event = ($boottype eq "OS") ? "BOOTING" : "PXEBOOTING";
	if (sendevent($nodeid, $event)) {
	    logit("$nodeid: $boottype: could not send $event event");
	}
	logit("$nodeid: $boottype: sent $event event");
    }
}

sub findnode($$)
{
    my ($IP,$mac) = @_;

    if ($issubboss) {
	my %DB;
	my $key = "$IP/$mac";
	my $node_id;

	if (!dbmopen(%DB, $NODEMAPDB, 0660)) {
	    logit("findnode($IP): could not open $NODEMAPDB");
	    return undef;
	}
	if (defined($DB{$key})) {
	    $node_id = $DB{$key};
	}
	dbmclose(%DB);
	return $node_id;
    }
    else {
	my $iface = Interface->LookupByIP($IP);
	return undef
	    if (!$iface);

	# XXX this should never happen since dhcpd looked up the IP by mac.
	if ($iface->mac() ne $mac) {
325 326 327 328 329 330 331 332 333 334 335 336
	    #
	    # XXX okay, one way it might happen is if someone changes the
	    # DB behind our back (the Interface module caches DB info).
	    # So we retry once after flushing the cache.
	    #
	    $iface->Flush();
	    $iface = Interface->LookupByIP($IP);
	    if (!$iface || $iface->mac() ne $mac) {
		logit("$IP: came in on $mac but expected " . $iface->mac())
		    if ($iface);
		return undef;
	    }
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	}

	return $iface->node_id();
    }
}

sub logit($)
{
    my $message = shift;

    # Time stamp log messages like:
    # Sep 20 09:36:00 $message
    my $tstamp = strftime("%b %e %H:%M:%S", localtime);

    print STDERR "$tstamp: $message\n";
}

sub sendevent($$)
{
    my ($node,$event) = @_;
    if ($impotent) {
	print "Would send event $event to $node\n";
	return;
    }
    my $URL = "elvin://$EVSERVER";

363 364 365 366 367 368 369
    # Connect to the event system. We might want to do our own failover.
    if (!defined($event_handle)) {
	$event_handle = event_register($URL, 0);
	if (!$event_handle) {
	    logit("$node: unable to register with event system");
	    return 1;
	}
370 371 372 373
    }
    my $tuple = address_tuple_alloc();
    if (!$tuple) {
	logit("$node: could not allocate an address tuple");
374 375
	event_unregister($event_handle);
	$event_handle = undef;
376 377 378 379 380 381 382 383
	return 1;
    }

    %$tuple = ( objtype => "TBNODESTATE",
		objname => $node,
		eventtype => $event,
		host => "boss");

384
    my $notification = event_notification_alloc($event_handle, $tuple);
385 386
    if (!$notification) {
	logit("$node: could not allocate notification");
387 388
	event_unregister($event_handle);
	$event_handle = undef;
389 390 391
	return 1;
    }

392
    if (!event_notify($event_handle, $notification)) {
393
	logit("$node: could not send $event notification");
394 395
	event_unregister($event_handle);
	$event_handle = undef;
396 397
	return 1;
    }
398
    event_notification_free($event_handle, $notification);
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    return 0;
}

# Flag for END block.
my $dying = 0;

sub fatal($)
{
    my ($msg) = @_;
    my $me = `hostname`;
    chomp($me);
    $dying = 1;
    
    if (! $debug) {
	#
	# Send a message to the testbed list. 
	#
	libtestbed::SENDMAIL($TBOPS,
		 "reportboot daemon died on $me",
		 $msg,
		 $TBOPS);
    }
421 422 423
    MarkDaemonStopped("reportboot_daemon")
	if (! ($debug || $nodaemon));
    
424 425 426 427 428 429 430 431 432 433 434
    die("*** $0:\n".
	"    $msg\n");
}

END {
    return
	if (!$? || $dying);
    my $saved_exitcode = $?;
    fatal("Exited abnormally, see the log for a traceback");
    $? = $saved_exitcode;
}