spewevents.in 4.48 KB
Newer Older
1 2 3 4
#!/usr/bin/perl -wT

#
# Copyright (c) 2000-2002, 2005, 2006 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{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/>.
# 
# }}}
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 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
#
use English;
use Getopt::Std;
use Fcntl;
use IO::Handle;
use strict;

#
# Spew event stream for an experiment.
#
sub usage()
{
    print STDOUT "Usage: spewevents <pid> <eid>\n";
    exit(-1);
}
my $optlist = "w";
my $fromweb = 0;
  
#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $TBLOGS      = "@TBLOGSEMAIL@";

#
# Load the Testbed support stuff. 
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use event;

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

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

# Protos
sub callbackFunc($$$);

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"w"})) {
    $fromweb = 1;
}
if (@ARGV != 2) {
    usage();
}
my $pid   = $ARGV[0];
my $eid   = $ARGV[1];

#
# Untaint the arguments.
#
if ($pid =~ /^([-\@\w]+)$/) {
    $pid = $1;
}
else {
    die("*** Bad data in pid: $pid\n");
}	
if ($eid =~ /^([-\@\w]+)$/) {
    $eid = $1;
}
else {
    die("*** Bad data in eid: $eid\n");
}

#
# Verify that this person is allowed to do this. 
#
if (!TBExptAccessCheck($UID, $pid, $eid, TB_EXPT_READINFO)) {
    die("*** $0:\n".
	"    You do not have permission to view events for $pid/$eid!\n");
}

# Obvious.
STDOUT->autoflush(1);

if (! EventRegister()) {
    die("*** $0:\n".
	"    Unable to register with event system\n");
}
my $handle = $event::EventSendHandle;

my $tuple  = address_tuple_alloc();
if (! $tuple) {
    die("*** $0:\n".
	"    Could not allocate an address tuple\n");
}

# All events for this experiment, sans scheduler events.
%$tuple = (expt => "$pid/$eid");

if (!event_subscribe($handle, \&callbackFunc, $tuple)) {
    die("*** $0:\n".
	"    Could not subscribe to event\n");
}

#
# Icky. Send out some stuff at the beginning to make the browser do
# something. This terrible, but not sure what else to do.
#
if ($fromweb) {
    for (my $i = 0; $i <= 1024; $i++) {
	print " ";
    }
    print "\n";
}

#
# Loop.
#
145 146
my $lastevent_time = time();

147 148 149 150
while (1) {
    # Quit when the experiment is no longer active.
    last
	if (ExpState($pid, $eid) ne EXPTSTATE_ACTIVE());
151 152

    # Quit if no events for a long time.
153
    if (time() - $lastevent_time > 900) {
154 155 156
	syswrite(STDOUT, "No events for a long time; stopping ...\n");
	last;
    }
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    
    event_poll_blocking($handle, 2000);
}
exit(0);

sub callbackFunc($$$) {
    my ($handle, $notification, $data) = @_;

    my $time      = time();
    my $site      = event_notification_get_site($handle, $notification);
    my $expt      = event_notification_get_expt($handle, $notification);
    my $group     = event_notification_get_group($handle, $notification);
    my $host      = event_notification_get_host($handle, $notification);
    my $objtype   = event_notification_get_objtype($handle, $notification);
    my $objname   = event_notification_get_objname($handle, $notification);
    my $eventtype = event_notification_get_eventtype($handle, $notification);
    my $arguments = event_notification_get_arguments($handle, $notification);

175 176 177 178 179 180 181 182 183 184 185 186
    my $string = 
	sprintf("%-12s %-10s %-15s %-12s %-15s %-15s",
		"$time", "$group", "$host", "$objtype",
		"$objname", "$eventtype");

    if (defined($arguments)) {
	# Trim extra whitespace.
	$arguments =~ s/^\s+//;
	$arguments =~ s/\s+$//;	

	$string .= "  $arguments";
    }
187
    $string .= "\n";
188 189 190

    my $rval = syswrite(STDOUT, $string);

191 192
    # If the web page stops this is only way to catch it and exit.
    # Hence, the use of syswrite so we can get status back.
193 194 195
    exit(0)
	if (!defined($rval));

196
    $lastevent_time = time();
197
}