newnode-defs.php3 5.25 KB
Newer Older
1 2 3
<?PHP
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2003, 2005 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13
# All rights reserved.
#

#
# This is a simple library for use by the new-node pages, with functions that
# need to be called both by the initial checkin page and the adminstrator
# node-adding page.
#

14
function find_switch_macs(&$mac_list) {
15

16
    global $uid, $gid, $TBSUEXEC_PATH, $ELABINELAB;
17 18 19 20 21 22 23 24 25 26 27

    #
    # Avoid SIGPROF in child.
    #   
    set_time_limit(0); 
    
    $macs = popen("$TBSUEXEC_PATH $uid $gid switchmac 2>&1","r");

    #
    # XXX - error checking
    #
28
    $line = fgets($macs);
29
    while (!feof($macs)) {
30
        $line = rtrim($line);
31 32 33 34 35
	$exploded = explode(",",$line);
	$MAC = $exploded[0];
	$switchport = $exploded[1];
	$vlan = $exploded[2];
	$iface = $exploded[3];
36
	$class = $exploded[4];
37 38 39
	if (!preg_match("/^([\w-]+)\/(\d+)\.(\d+)$/",$switchport,$matches)) {
	    echo "<h3>Bad line from switchmac: $line\n";
	}
40 41 42
	$switch = $matches[1];
	$card = $matches[2];
	$port = $matches[3];
43 44 45 46
	if ($mac_list[$MAC] &&
	    (is_null($mac_list[$MAC]["class"]) ||
	     !isset($mac_list[$MAC]["class"]) ||
	     ($mac_list[$MAC]["class"] == $class))) {
47
	    $mac_list[$MAC]["switch"] = $switch;
48 49
	    $mac_list[$MAC]["switch_card"] = $card;
	    $mac_list[$MAC]["switch_port"] = $port;
50 51 52 53
	    if ($ELABINELAB) {
		# We let switchmac tell us.
		$mac_list[$MAC]["class"] = $class;
	    }
54
	}
55
	$line = fgets($macs);
56 57 58 59 60 61 62 63 64 65 66 67 68
    }

    pclose($macs);

    return 1;

}

function guess_node_type($proc,$disk) {

    #
    # Allow the reported speed to differ from the one in the database
    #
69
    $fudge_factor = .10;
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

    #
    # Convert disk size from megabytes to gigabtypes
    #
    $disk /= 1000.0;

    #
    # Go through node types and try to find a single one that matches
    # this node's processor speed. This is a totally bogus way to do this,
    # but it's the best we got for now.
    #
    $node_type = "";
    $query_result = DBQueryFatal("select type, speed, HD from node_types " .
	"where !isvirtnode and !isremotenode");
    while ($row = mysql_fetch_array($query_result)) {
        $speed = $row["speed"];
	$type = $row["type"];
	$HD = $row["HD"];
	echo "Checking $speed vs $proc, $HD vs $disk\n";
	if (($proc > ($speed * (1.0 - $fudge_factor))) &&
	    ($proc < ($speed * (1.0 + $fudge_factor))) &&
	    ($disk > ($HD * (1.0 - $fudge_factor))) &&
	    ($disk < ($HD * (1.0 + $fudge_factor)))) {
	    if ($node_type != "") {
	        # We found two potential matches, choose neither
		echo "Found a second match ($type), bailing\n";
		return "";
	    } else {
		echo "Found a first match ($type)\n";
	        $node_type = $type;
	    }
	}
    }

    echo "Returning $node_type\n";
    return $node_type;
}

108 109 110 111
#
# Create a new node type
# XXX - Right now, this is really only meant for inserting a node_types entry
# for ops. It misses doing a lot of important things, like setting the class and
112
# default OSID for types other than ops
113 114 115 116 117 118
#
function make_node_type($type,$proc,$disk) {

    #
    # Just insert a stub entry for this type
    #
119 120 121 122 123 124 125 126
    $class = "";
    $defosid = "";
    if (!strcmp($type,"ops")) {
	$class = "misc";
	$defosid = "emulab-ops-OPSNODE-BSD";
    }
    DBQueryFatal("insert into node_types set type='$type', speed='$speed', " .
	"HD='$disk', class='$class', osid='$defosid';");
127 128 129

}

130
function guess_IP ($prefix, $number) {
131
    global $CONTROL_NETWORK;
132 133 134 135

    $hostname = $prefix . $number;

    #
136
    # First, lets see if they've already added to to DNS - the PHP
137 138 139 140 141 142 143 144
    # gethostbyname has a really dumb way to return failure - it just
    # gives you the hostname back.
    #
    $IP = gethostbyname($hostname);
    if (strcmp($IP,$hostname)) {
    	return $IP;
    }

145
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
146 147
    # We want to be able to handle both numeric and character 'number's 
    # Figure out which we have
148
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
149
    if (! is_numeric($number)) {
150 151 152 153 154 155
	$using_char = 1;
	$number = ord($number);
    } else {
	$using_char = 0;
    }

156 157 158 159 160 161 162 163
    #
    # Okay, no such luck. We'll go backwards through the host list until
    # we find the previous node with an established IP, then add our offset
    # onto that
    #
    $i = $number - 1;
    $IP = "";
    while ($i > 0) {
164 165 166 167 168
        if ($using_char) {
	    $node = $prefix . chr($i);
	} else {
	    $node = $prefix . $i;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
169 170 171 172
        $query_result =
	    DBQueryFatal("select IP from interfaces as i " .
			 "where i.node_id='$node' and ".
			 "      i.role='" . TBDB_IFACEROLE_CONTROL . "'");
173 174 175 176 177 178 179 180
        if (mysql_num_rows($query_result)) {
	    $row = mysql_fetch_array($query_result);
	    $IP = $row[IP];
	    break;
	}

	# Try new_nodes too
        $query_result = DBQueryFatal("select IP from new_nodes " .
181
		"where node_id='$node'");
182 183 184 185 186 187 188 189 190
        if (mysql_num_rows($query_result)) {
	    $row = mysql_fetch_array($query_result);
	    $IP = $row[IP];
	    break;
	}

	$i--;
    }

191 192 193
    #
    # Lets start out as the first address on the given control network.
    # 
194
    if ($i <= 0) {
195
    	$IP = $CONTROL_NETWORK;
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    }

    #
    # Parse the IP we got into the last octet and everything else. Note that we
    # don't really do the IP address calcuation correctly - just an
    # approximation of the right thing.
    #
    list($oct1,$oct2,$oct3,$oct4) = explode(".",$IP);
    $oct4 += $number - $i;

    # We don't yet wrap - it may not be OK to do
    if ($oct4 > 254) {
        return 0;
    }

    return "$oct1.$oct2.$oct3.$oct4";
    
}


?>