nodeutil.pl 4.19 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
#
# Copyright (c) 2007 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{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/>.
# 
# }}}
23 24 25
#
use English;

Leigh Stoller's avatar
Leigh Stoller committed
26
use lib "/usr/testbed/lib";
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
use libdb;
use libtestbed;
use Experiment;

$| = 1;

my %nodeids	= ();


# Need this for computing downtime.
my $downexp = Experiment->Lookup(NODEDEAD_PID(), NODEDEAD_EID());
if (!defined($downexp)) {
    die("Could not lookup hwdown experiment object\n");
}

#
# Cleanup some stuff ...
#
$query_result =
    DBQueryFatal("select e.pid,e.eid,s.rsrcidx,e.idx,e.creator_idx,s.created ".
		 " from experiments as e ".
		 "left join experiment_stats as s on s.exptidx=e.idx ".
		 "left join experiment_resources as r on r.idx=s.rsrcidx ".
		 "where r.exptidx is null");

while (my ($pid,$eid,$rsrcidx,$exptidx,$creator_idx,$created) =
       $query_result->fetchrow_array()) {
    DBQueryFatal("insert into experiment_resources ".
		"(idx, tstamp, exptidx, uid_idx) ".
		"values ($rsrcidx, '$created', $exptidx, $creator_idx)");
}

#
# Get the list of all current physical nodes.
#
$query_result =
    DBQueryFatal("select node_id from nodes as n ".
		 "left join node_types as t on t.type=n.type ".
		 "where t.class='pc' or t.class='pcplabphys'");

while (my ($node_id) = $query_result->fetchrow_array()) {
    $nodeids{$node_id} = {};
}

#
# Need to form an inception date. Use the first entry we find in the
# node_history table, or the current time. 
#
foreach my $nodeid (keys(%nodeids)) {
    $query_result =
	DBQueryFatal("select MIN(stamp) from node_history ".
		     "where node_id='$nodeid'");
    if (!$query_result->numrows) {
	$nodeids{$nodeid}->{'inception'} = time();
    }
    else {
	my ($stamp) = $query_result->fetchrow_array();
	$stamp = time()
	    if (!defined($stamp));

	$nodeids{$nodeid}->{'inception'} = $stamp;
    }
    $nodeids{$nodeid}->{'allocated'} = 0;
    $nodeids{$nodeid}->{'down'}      = 0;
}

#
# Now compute utilization.
#
foreach my $nodeid (keys(%nodeids)) {
    $query_result =
	DBQueryFatal("select op,exptidx,stamp from node_history ".
		     "where node_id='$nodeid' ".
		     "order by stamp");
    next
	if (!$query_result->numrows);

    my $alloctime = 0;
    my $downtime  = 0;
    my $alloced   = 0;
    my $lastexpt  = 0;

    while (my ($op,$exptidx,$stamp) = $query_result->fetchrow_array()) {
	#print "$op, $exptidx, $stamp\n";
	
	if ($op eq "alloc") {
	    $alloced  = $stamp;
	    $lastexpt = $exptidx;
	}
	elsif ($op eq "move") {
	    if (!$alloced) {
		$alloced  = $stamp;
		$lastexpt = $exptidx;
		next;
	    }
	    my $diff = $stamp - $alloced;

	    if ($lastexpt == $downexp->idx()) {
		$downtime += $diff;
	    }
	    else {
		$alloctime += $diff;
	    }
	    $alloced    = $stamp;
	    $lastexpt   = $exptidx;
	}
	else {
	    # Missing records ...
	    next
		if (!$alloced);
	    
	    my $diff = $stamp - $alloced;

	    if ($exptidx == $downexp->idx()) {
		$downtime += $diff;
	    }
	    else {
		$alloctime += $diff;
	    }
	    $alloced  = 0;
	    $lastexpt = 0;
	}
    }
    $nodeids{$nodeid}->{'allocated'} = $alloctime;
    $nodeids{$nodeid}->{'down'}      = $downtime;
}

foreach my $nodeid (keys(%nodeids)) {
    $inception = $nodeids{$nodeid}->{'inception'};
    $alloctime = $nodeids{$nodeid}->{'allocated'};
    $downtime  = $nodeids{$nodeid}->{'down'};

    print "$nodeid, $inception, $alloctime, $downtime\n";

    DBQueryFatal("update nodes set inception=FROM_UNIXTIME($inception) ".
		 "where node_id='$nodeid'");
    DBQueryFatal("replace into node_utilization set ".
		 "  node_id='$nodeid', allocated=$alloctime, down=$downtime");
}