reportboot.in 6.63 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
#!/usr/bin/perl -w
#
# Copyright (c) 2014 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 POSIX qw(strftime);
use Sys::Syslog;

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

sub usage()
{
37
    print "Usage: $0 [-bdCD] IP MAC boot-type\n";
38 39 40
    print("Options:\n");
    print(" -b      - Run in the background\n");
    print(" -d      - Turn on debugging\n");
41
    print(" -C      - Combine PXEBOOTING/BOOTING on the PXE event\n");
42
    print(" -D      - Record event times in a DB to moderate send rate\n");
43 44 45
 
    exit(1);
}
46
my $optlist    = "bdCD";
47 48
my $background = 0;
my $debug      = 0;
49
my $combined   = 0;
50
my $usedb      = 0;
51 52 53 54

#
# Functions
#
55
sub findnode($$);
56
sub logit($);
57
sub sendevent($$);
58 59 60 61 62 63

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
64 65
my $EVPORT   = @BOSSEVENTPORT@;
my $EVSERVER = "boss";
66 67 68 69 70 71 72
my $EVDB     = "$TB/db/reportboot";

#
# Minimum time between events.
# In bootinfo, this is 10 seconds!
#
my $EVMININT = 5;
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

# Locals
my $logfile  = "$TB/log/reportboot.log";

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

#
# Untaint the path
# 
$ENV{'PATH'} = "$TB/bin:$TB/sbin:/bin:/usr/bin:/sbin:/usr/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libtestbed;
use libdb;
use libtblog;
95 96 97 98 99 100
use event;
use Interface;

if ($UID != 0) {
    die("Must be root to run this script\n");
}
101 102 103 104 105

my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
106 107 108
if (defined($options{"C"})) {
    $combined = 1;
}
109 110 111
if (defined($options{"D"})) {
    $usedb = 1;
}
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
if (defined($options{"b"})) {
    $background = 1;
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (@ARGV != 3) {
    usage();
}
my ($IP,$MAC,$boottype);
if ($ARGV[0] =~ /^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$/) {
    $IP = $1;
} else {
    print STDERR "'$ARGV[0]' is not an IP address\n";
    usage();
}
128 129 130 131 132
$MAC = lc($ARGV[1]);
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));
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
} else {
    print STDERR "'$ARGV[1]' is not a MAC address\n";
    usage();
}
if ($ARGV[2] =~ /^(PXE|OS)$/) {
    $boottype = $1;
} else {
    print STDERR "'$ARGV[2]' must be one of PXE or OS\n";
    usage();
}

if ($background) {
    my $cpid = fork();
    if (!defined($cpid)) {
	die "$0: could not fork!";
    }
    if ($cpid) {
	exit(0);
    }
    open(STDIN, "< /dev/null") or
	die("opening /dev/null for STDIN: $!");
    POSIX::setsid() or
	die("setsid failed: $!");
}

158 159 160 161 162 163
my $nodeid = findnode($IP, $MAC);
if (!$nodeid) {
    logit("Ignoring unknown node $IP");
    exit(0);
}

164 165 166 167 168 169 170 171 172 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
#
# 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");
	exit(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)");
	exit(0);
    }
}

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
#
# 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 "PXE") {
	if (sendevent($nodeid, "PXEBOOTING") ||
	    sendevent($nodeid, "BOOTING")) {
	    logit("$nodeid: could not send PXEBOOTING/BOOTING events");
	    exit(1);
	}
	logit("$nodeid: sent PXEBOOTING and BOOTING events");
    } else {
214
	logit("$nodeid: NOT sending BOOTING event (combo mode)");
215 216 217 218 219 220 221 222
    }
} else {
    my $event = ($boottype eq "OS") ? "BOOTING" : "PXEBOOTING";
    if (sendevent($nodeid, $event)) {
	logit("$nodeid: could not send $event event");
	exit(1);
    }
    logit("$nodeid: sent $event event");
223 224
}

225 226
exit(0);

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
sub findnode($$)
{
    my ($IP,$mac) = @_;

    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) {
	logit("$IP: came in on $mac but expected " . $iface->mac());
	return undef;
    }

    return $iface->node_id();
}

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
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);

    if (open(LOG, ">>$logfile")) {
	print LOG "$tstamp: $message\n";
	close(LOG);
    } else {
	print STDERR "Could not append to $logfile\n";
    }

    print STDERR "$message\n" if ($debug);
}
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

sub sendevent($$)
{
    my ($node,$event) = @_;

    my $URL = "elvin://$EVSERVER:$EVPORT";

    # Connect to the event system, and subscribe the the events we want
    my $handle = event_register($URL, 0);
    if (!$handle) {
	logit("$node: unable to register with event system");
	return 1;
    }
    my $tuple = address_tuple_alloc();
    if (!$tuple) {
	logit("$node: could not allocate an address tuple");
	return 1;
    }

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

    my $notification = event_notification_alloc($handle, $tuple);
    if (!$notification) {
	logit("$node: could not allocate notification");
	return 1;
    }

    if (!event_notify($handle, $notification)) {
	logit("$node: could not send $event notification");
	return 1;
    }

    event_notification_free($handle, $notification);

    if (event_unregister($handle) == 0) {
	logit("$node: WARNING: could not unregister with event system");
    }

    return 0;
}