node_history.in 7.83 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
#
# EMULAB-COPYRIGHT
# Copyright (c) 2005 University of Utah and the Flux Group.
# All rights reserved.
#
use English;
use Getopt::Std;

#
# Drill down the node history data in the DB
#

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

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

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

use POSIX qw(ctime);

#
# Magic: Wed Jan 12 13:59:00 2005
# When Leigh implemented the history log.
#
my $epoch = 1105563540;

sub usage {
    print("Usage: $0 [-ARalrsvw] [node ...]\n".
37 38 39 40 41 42 43 44 45 46 47
    "  -A       print history of all nodes (you do NOT want to do this)\n".
    "  -R       print raw records (default is to combine some records)\n".
    "  -S field sort list by field (date, node, pideid, uid, elapsed)\n".
    "  -a       show only when allocated to experiment\n".
    "  -l       list records\n".
    "  -n num   report on only the first num records (after sorting)\n".
    "             use negative value for last num records\n".
    "  -r       reverse order (most recent first)\n".
    "  -s       show a summary of node's past usage\n".
    "  -v       verbose output\n".
    "  -w       print warnings about anomolous records\n");
48 49 50
    exit(1);
}

51
my $optlist = "ARS:aln:rswv";
52 53 54 55 56 57 58
my $warnme = 0;
my $verbose = 0;
my $showall = 0;
my $showalloconly = 0;
my $list = 0;
my $summary = 0;
my $raw = 0;
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
my $numrecs = 0;

#
# Sort stuff.  sortby value should correspond to record field format:
#
# sortby 0: node
# sortby 1: pideid
# sortby 2: uid
# sortby 3: date
# sortby 4: elapsed
#
my %sortmap = (
    "node" => 0,
    "pideid" => 1,
    "uid" => 2,
    "date" => 3,
    "elapsed" => 4
);
my $sortbydate = $sortmap{date};
my $sortby = $sortbydate;
79 80
my $revorder = 0;

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
# Sort function: first by indicated field, secondarily by date
sub byfield()
{
    # already sorted by date
    return 0 if ($sortby == $sortbydate);

    # int compare for elapsed
    if ($sortby == $sortmap{elapsed}) {
	return @$a[$sortby] <=> @$b[$sortby] ||
	    @$a[$sortbydate] <=> @$b[$sortbydate];
    }

    # string compare for everything else
    return @$a[$sortby] cmp @$b[$sortby] ||
	@$a[$sortbydate] <=> @$b[$sortbydate];
}


99 100 101 102 103 104 105 106 107 108 109 110 111
#
# Parse command arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"A"})) {
    $showall = 1;
}
if (defined($options{"R"})) {
    $raw = 1;
}
112 113 114 115 116 117 118
if (defined($options{"S"})) {
    if (!defined($sortmap{$options{"S"}})) {
	print STDERR "invalid sort field '$options{S}'\n";
	usage();
    }
    $sortby = $sortmap{$options{"S"}};
}
119 120 121 122 123 124
if (defined($options{"a"})) {
    $showalloconly = 1;
}
if (defined($options{"l"})) {
    $list = 1;
}
125 126 127
if (defined($options{"n"})) {
    $numrecs = $options{"n"};
}
128 129 130 131 132 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 158 159 160 161 162 163 164 165 166 167
if (defined($options{"r"})) {
    $revorder = 1;
}
if (defined($options{"s"})) {
    $summary = 1;
}
if (defined($options{"w"})) {
    $warnme = 1;
}
if (defined($options{"v"})) {
    $verbose = 1;
}
if (!$showall && @ARGV == 0) {
    usage();
}
my @nodes = @ARGV;

if (!$list && !$summary) {
    $list = 1;
}

#
# Common case: a single node.
# Just fetch data for it, otherwise fetch data for all.
# 
my $querymod = "";
if (@nodes == 1) {
    $querymod = " AND node_id='$nodes[0]'";
}

# XXX maybe it would be better to sort them here in perl, outside the DB?
my $orderby = " ORDER BY stamp ASC";

my $query_result =
    DBQueryFatal("SELECT node_id,stamp,op,uid,pid,eid ".
		 "FROM node_history,experiment_stats ".
		 "WHERE node_history.exptidx=experiment_stats.exptidx ".
		 "$querymod $orderby");

my %nodeinfo;	# [ expt, starttime, uid ]
168
my @records;
169 170 171 172 173 174 175 176 177 178 179 180

while (my %row = $query_result->fetchhash()) {
    my $pideid = "$row{pid}/$row{eid}";
    my $node = $row{node_id};
    my $stamp = $row{stamp};
    my $uid = $row{uid};

    #
    # XXX if this is the first record for a node, consider it as free
    # from the epoch til now.
    #
    if (!defined($nodeinfo{$node})) {
181
	$nodeinfo{$node} = [ "", $epoch, "root" ];
182 183 184 185 186 187 188 189 190 191
    }

    my ($opideid, $ostamp, $ouid) = @{$nodeinfo{$node}};
    my $elapsed = $stamp - $ostamp;

    #
    # Allocating node to experiment.
    # Should currently be free.
    #
    if ($row{op} eq "alloc") {
192 193
	if ($opideid ne "") {
	    print STDERR "$node: dup alloc: already allocated to $opideid\n"
194 195 196 197 198 199 200 201 202 203 204 205 206 207
		if ($warnme);
	    # XXX possibly missing state in the DB, treat as move
	}
	$nodeinfo{$node} = [ $pideid, $stamp, $uid ];
    }

    #
    # Free or move terminates a node's time in an experiment.
    #
    elsif ($row{op} eq "free") {
	if ($opideid ne $pideid) {
	    print STDERR "$node: mismatched alloc,free records: $opideid,$pideid\n"
		if ($warnme);
	}
208
	$nodeinfo{$node} = [ "", $stamp, $uid ];
209 210 211 212 213 214 215 216 217 218 219 220 221
    }
    elsif ($row{op} eq "move") {
	if (!$raw) {
	    # Moves from reloadpending to reloading are combined as reloading
	    if ($opideid eq "emulab-ops/reloadpending" &&
		$pideid eq "emulab-ops/reloading") {
		$nodeinfo{$node} = [ $pideid, $ostamp, $ouid ];
		next;
	    }
	}
	$nodeinfo{$node} = [ $pideid, $stamp, $uid ];
    }

222 223
    # save off the record
    push(@records, [ $node, $opideid, $ouid, $ostamp, $elapsed ]);
224 225
}

226
# Include the current state of nodes in a final record
227 228 229 230
my $stamp = time();
for $node (keys(%nodeinfo)) {
    my ($opideid, $ostamp, $ouid) = @{$nodeinfo{$node}};
    my $elapsed = $stamp - $ostamp;
231 232 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
    push(@records, [ $node, $opideid, $ouid, $ostamp, $elapsed ]);
}

# Prune the list based on date range (someday)

# Sort the list as desired
if ($sortby ne "date") {
    @records = sort byfield @records;
}
if ($revorder) {
    @records = reverse(@records);
}

# Prune to the proper number of entries (first/last $numrecs entries)
if ($numrecs && $numrecs < $#records) {
    if ($numrecs > 0) {
	@records = @records[0 .. $numrecs-1];
    } else {
	@records = @records[$numrecs .. -1 ];
    }
}

#
# Loop over the remaining records, computing summary stats
# and printing (if desired).
#
for my $rec (@records) {
    my ($node, $pideid, $uid, $stamp, $elapsed) = @{$rec};
259

260 261 262
    if (!defined($nodestats{$node})) {
	$nodestats{$node} = [ 0, 0, 0, 0 ];
    }
263 264
    my ($ftime, $atime, $rtime, $dtime) = @{$nodestats{$node}};
    my $isalloced = 0;
265
    if ($pideid eq "") {
266
	$ftime += $elapsed;
267 268
    } elsif ($pideid eq "emulab-ops/reloadpending" ||
	     $pideid eq "emulab-ops/reloading") {
269
	$rtime += $elapsed;
270
    } elsif ($pideid eq "emulab-ops/hwdown") {
271 272 273 274 275 276 277 278 279
	$dtime += $elapsed;
    } else {
	$atime += $elapsed;
	$isalloced = 1;
    }
    $nodestats{$node} = [ $ftime, $atime, $rtime, $dtime ];

    if ($list) {
	if ($verbose) {
280 281 282 283 284 285
	    my $start = ctime($stamp);
	    chomp($start);
	    my $end = ctime($stamp + $elapsed);
	    chomp($end);

	    print "$node: $pideid from $start til $end ($elapsed sec)\n"
286 287
		if (!$showalloconly || $isalloced);
	} else {
288 289 290 291 292 293 294
	    my ($pid, $eid);
	    if ($pideid eq "") {
		$pid = $eid = "<FREE>";
	    } else {
		($pid, $eid) = split("/", $pideid);
	    }
	    print "$node REC $stamp $elapsed $uid $pid $eid\n"
295 296 297 298 299
		if (!$showalloconly || $isalloced);
	}
    }
}

300 301 302
print scalar(@records) . " records\n"
    if ($list && $verbose);

303
#
304
# Print out summary information
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
for $node (sort keys(%nodestats)) {
    if ($summary && defined($nodestats{$node})) {
	my ($ftime, $atime, $rtime, $dtime) = @{$nodestats{$node}};
	my $ttime = $ftime + $atime + $rtime + $dtime;

	if ($verbose) {
	    my $pct;
	    print "$node SUMMARY\n";
	    if ($atime > 0) {
		$pct = ($atime * 100) / $ttime;
		printf("  Allocted:  %9d sec (%5.1f%%)\n", $atime, $pct);
	    }
	    if ($ftime > 0) {
		$pct = ($ftime * 100) / $ttime;
		printf("  Free:      %9d sec (%5.1f%%)\n", $ftime, $pct);
	    }
	    if ($rtime > 0) {
		$pct = ($rtime * 100) / $ttime;
		printf("  Reloading: %9d sec (%5.1f%%)\n", $rtime, $pct);
	    }
	    if ($dtime > 0) {
		$pct = ($dtime * 100) / $ttime;
		printf("  Down:      %9d sec (%5.1f%%)\n", $dtime, $pct);
	    }
	} else {
	    print "$node SUM $atime $ftime $rtime $dtime\n";
332 333 334 335 336
	}
    }
}

exit(0);