newnode-defs.php3 5.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?PHP
#
# EMULAB-COPYRIGHT
# Copyright (c) 2003 University of Utah and the Flux Group.
# 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 17 18 19 20 21 22 23 24 25 26 27

    global $uid, $gid, $TBSUEXEC_PATH;

    #
    # 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 43 44
	$switch = $matches[1];
	$card = $matches[2];
	$port = $matches[3];
	if ($mac_list[$MAC] && (!$mac_list[$MAC]["class"] ||
	    ($mac_list[$MAC]["class"] == $class))) {
45
	    $mac_list[$MAC]["switch"] = $switch;
46 47
	    $mac_list[$MAC]["switch_card"] = $card;
	    $mac_list[$MAC]["switch_port"] = $port;
48
	}
49
	$line = fgets($macs);
50 51 52 53 54 55 56 57 58 59 60 61 62
    }

    pclose($macs);

    return 1;

}

function guess_node_type($proc,$disk) {

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

    #
    # 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;
}

102 103 104 105
#
# 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
106
# default OSID for types other than ops
107 108 109 110 111 112
#
function make_node_type($type,$proc,$disk) {

    #
    # Just insert a stub entry for this type
    #
113 114 115 116 117 118 119 120
    $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';");
121 122 123

}

124 125 126 127 128 129 130 131 132 133 134 135 136 137
function guess_IP ($prefix, $number) {

    $hostname = $prefix . $number;

    #
    # First, let's see if they've already added to to DNS - the PHP
    # 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;
    }

138 139 140 141 142 143 144 145 146 147 148
    #
    # We want to be able to handle both numeric and character 'number's - figure
    # out which we have
    #
    if (is_string($number)) {
	$using_char = 1;
	$number = ord($number);
    } else {
	$using_char = 0;
    }

149 150 151 152 153 154 155 156
    #
    # 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) {
157 158 159 160 161
        if ($using_char) {
	    $node = $prefix . chr($i);
	} else {
	    $node = $prefix . $i;
	}
162 163 164
        $query_result = DBQueryFatal("select IP from interfaces as i " .
		"left join nodes as n on i.node_id = n.node_id left join " .
		"node_types as nt on n.type = nt.type " .
165
		"where n.node_id='$node' and i.iface = nt.control_iface");
166 167 168 169 170 171 172 173
        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 " .
174
		"where node_id='$node'");
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
        if (mysql_num_rows($query_result)) {
	    $row = mysql_fetch_array($query_result);
	    $IP = $row[IP];
	    break;
	}

	$i--;
    }

    if ($i <= 0) {
    	return 0;
    }

    #
    # 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";
    
}


?>