Commit efd167a0 authored by Robert Ricci's avatar Robert Ricci

Add support for Wisconsin's naming scheme, or more generally, any

naming scheme that ends in -A, where A is a letter, not a number.
parent 6ebb7efb
......@@ -114,6 +114,17 @@ function guess_IP ($prefix, $number) {
return $IP;
}
#
# 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;
}
#
# 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
......@@ -122,10 +133,15 @@ function guess_IP ($prefix, $number) {
$i = $number - 1;
$IP = "";
while ($i > 0) {
if ($using_char) {
$node = $prefix . chr($i);
} else {
$node = $prefix . $i;
}
$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 " .
"where n.node_id='$prefix$i' and i.iface = nt.control_iface");
"where n.node_id='$node' and i.iface = nt.control_iface");
if (mysql_num_rows($query_result)) {
$row = mysql_fetch_array($query_result);
$IP = $row[IP];
......@@ -134,7 +150,7 @@ function guess_IP ($prefix, $number) {
# Try new_nodes too
$query_result = DBQueryFatal("select IP from new_nodes " .
"where node_id='$prefix$i'");
"where node_id='$node'");
if (mysql_num_rows($query_result)) {
$row = mysql_fetch_array($query_result);
$IP = $row[IP];
......
......@@ -48,59 +48,61 @@ foreach ($HTTP_GET_VARS as $key => $value) {
# Use one of the interfaces to see if this node seems to have already checked
# in once
#
if (count($interfaces)) {
$testmac = $interfaces[0]["mac"];
#
# First, make sure it isn't a 'real boy' - we should let the operators know
# about this, because there may be some problem.
#
$query_result = DBQueryFatal("select n.node_id from " .
"nodes as n left join interfaces as i " .
"on n.node_id=i.node_id " .
"where i.mac='$testmac'");
if (mysql_num_rows($query_result)) {
$row = mysql_fetch_array($query_result);
$node_id = $row["node_id"];
echo "Node is already a real node, named $node_id\n";
TBMAIL($TBMAIL_OPS,"Node Checkin Error","A node attempted to check " .
"in as a new node, but it is already\n in the database as " .
"$node_id!");
exit;
}
#
# Next, try the new nodes
#
$query_result = DBQueryFatal("select n.new_node_id, n.node_id from " .
"new_nodes as n left join new_interfaces as i " .
"on n.new_node_id=i.new_node_id " .
"where i.mac='$testmac'");
if (mysql_num_rows($query_result)) {
$row = mysql_fetch_array($query_result);
$id = $row["new_node_id"];
$node_id = $row["node_id"];
echo "Node has already checked in as ID $id, name $node_id\n";
#
# Keep the temp. IP address around in case it's gotten a new one
#
DBQueryFatal("update new_nodes set temporary_IP='$tmpIP' " .
"where new_node_id=$id");
exit;
}
}
#if (count($interfaces)) {
# $testmac = $interfaces[0]["mac"];
#
# #
# # First, make sure it isn't a 'real boy' - we should let the operators know
# # about this, because there may be some problem.
# #
# $query_result = DBQueryFatal("select n.node_id from " .
# "nodes as n left join interfaces as i " .
# "on n.node_id=i.node_id " .
# "where i.mac='$testmac'");
# if (mysql_num_rows($query_result)) {
# $row = mysql_fetch_array($query_result);
# $node_id = $row["node_id"];
# echo "Node is already a real node, named $node_id\n";
# TBMAIL($TBMAIL_OPS,"Node Checkin Error","A node attempted to check " .
# "in as a new node, but it is already\n in the database as " .
# "$node_id!");
# exit;
# }
#
#
# #
# # Next, try the new nodes
# #
# $query_result = DBQueryFatal("select n.new_node_id, n.node_id from " .
# "new_nodes as n left join new_interfaces as i " .
# "on n.new_node_id=i.new_node_id " .
# "where i.mac='$testmac'");
#
# if (mysql_num_rows($query_result)) {
# $row = mysql_fetch_array($query_result);
# $id = $row["new_node_id"];
# $node_id = $row["node_id"];
# echo "Node has already checked in as ID $id, name $node_id\n";
#
# #
# # Keep the temp. IP address around in case it's gotten a new one
# #
# DBQueryFatal("update new_nodes set temporary_IP='$tmpIP' " .
# "where new_node_id=$id");
#
# exit;
# }
#}
#
# Attempt to come up with a node_id and an IP address for it
#
$node_num = find_free_id("pc");
$hostname = "pc$node_num";
$IP = guess_IP("pc",$node_num);
$name_info = find_free_id("pc");
$node_prefix = $name_info[0];
$node_num = $name_info[1];
$hostname = $node_prefix . $node_num;
$IP = guess_IP($node_prefix,$node_num);
#
# Make an educated guess as to what type it belongs to
......@@ -131,37 +133,76 @@ foreach ($interfaces as $interface) {
#
TBMAIL($TBMAIL_OPS,"New Node","A new node, $hostname, has checked in");
function check_node_exists($node_id) {
#
# Just check to see if this node already exists in one of the
# two tables - return 1 if it does, 0 if not
#
$query_result = DBQueryFatal("select node_id from nodes " .
"where node_id='$node_id'");
if (mysql_num_rows($query_result)) {
return 1;
}
$query_result = DBQueryFatal("select node_id from new_nodes " .
"where node_id='$node_id'");
if (mysql_num_rows($query_result)) {
return 1;
}
return 0;
}
function find_free_id($prefix) {
#
# Note, we lock the new_nodes table while we look to avoid races
# First, check to see if there's a recent entry in new_nodes we can name
# this node after
#
DBQueryFatal("lock tables nodes read, new_nodes write");
$node_number = 0;
$query_result = DBQueryFatal("select node_id from new_nodes " .
"order by created desc limit 1");
if (mysql_num_rows($query_result)) {
$row = mysql_fetch_array($query_result);
$old_node_id = $row[0];
#
# Try to figure out if this is in some format we can increment
#
if (preg_match("/^(.*[^\d])(\d+)$/",$old_node_id,$matches)) {
echo "Matches pcXXX format";
# pcXXX format
$base = $matches[1];
$number = $matches[2];
$potential_name = $base . ($number + 1);
if (!check_node_exists($potential_name)) {
return array($base,($number +1));
}
} elseif (preg_match("/^(.*)-([a-zA-Z])$/",$old_node_id,$matches)) {
# Something like WAIL's (type-rack-A) format
$base = $matches[1];
$lastchar = $matches[2];
$newchar = chr(ord($lastchar) + 1);
$potential_name = $base . '-' . $newchar;
if (!check_node_exists($potential_name)) {
return array($base . '-', $newchar);
}
}
}
#
# Okay, that didn't work.
# Just go through the nodes and new_nodes tables looking for one that
# hasn't been used yet - put in a silly little guard to prevent an
# infinite loop in case of bugs.
#
while ($node_number < 1000) {
$node_number = 0;
while ($node_number < 10000) {
$node_number++;
$potential_name = $prefix . $node_number;
$query_result = DBQueryFatal("select node_id from nodes " .
"where node_id='$potential_name'");
if (mysql_num_rows($query_result)) {
continue;
}
$query_result = DBQueryFatal("select node_id from new_nodes " .
"where node_id='$potential_name'");
if (mysql_num_rows($query_result)) {
continue;
if (!check_node_exists($potential_name)) {
break;
}
break;
}
DBQueryFatal("unlock tables");
return $node_number;
return array($prefix, $node_number);
}
......
......@@ -76,7 +76,8 @@ if ($calc) {
while ($row = mysql_fetch_array($query_result)) {
$id = $row["new_node_id"];
$name = $row["node_id"];
if (preg_match("/^(.*\D)(\d+)$/",$name,$matches)) {
if (preg_match("/^(.*\D)(\d+)$/",$name,$matches) ||
preg_match("/^(.*-)([a-zA-Z])$/",$name,$matches)) {
$prefix = $matches[1];
$number = $matches[2];
$newIP = guess_IP($prefix,$number);
......@@ -196,11 +197,16 @@ if ($newprefix || $addnumber) {
while ($row = mysql_fetch_array($query_result)) {
$id = $row['new_node_id'];
$name = $row['node_id'];
if (preg_match("/^(.*\D)(\d+)$/",$name,$matches)) {
if (preg_match("/^(.*\D)(\d+)$/",$name,$matches) ||
preg_match("/^(.*-)([a-zA-Z])$/",$name,$matches)) {
$prefix = $matches[1];
$number = $matches[2];
if ($addnumber) {
$number = $number + $addnumber;
if (is_string($number)) {
$number = chr(ord($number) + $addnumber);
} else {
$number = $number + $addnumber;
}
}
if ($newprefix) {
$prefix = $newprefix;
......@@ -245,7 +251,7 @@ if ($renumber) {
#
$query_result = DBQueryFatal("SELECT n.new_node_id, node_id, n.type, IP, " .
"DATE_FORMAT(created,'%M %e %H:%i:%s') as created, i.MAC, " .
"i.switch_id, i.switch_card, i.switch_port, n.temporary_IP " .
"i.switch_id, i.switch_card, i.switch_port, n.temporary_IP, n.dmesg " .
"FROM new_nodes AS n " .
"LEFT JOIN node_types AS t on n.type=t.type " .
"LEFT JOIN new_interfaces AS i ON n.new_node_id=i.new_node_id " .
......@@ -284,6 +290,7 @@ function deselectAll(form) {
<th>Interfaces</th>
<th>Temporary IP</th>
<th>Created</th>
<th>Warnings</th>
</tr>
<?
......@@ -296,6 +303,7 @@ while ($row = mysql_fetch_array($query_result)) {
$created = $row["created"];
$mac = $row["MAC"];
$tempIP = $row["temporary_IP"];
$dmesg = $row["dmesg"];
if ($row["switch_id"]) {
$port = "$row[switch_id].$row[switch_card]/$row[switch_port]";
} else {
......@@ -320,6 +328,11 @@ while ($row = mysql_fetch_array($query_result)) {
echo " <td>$interfaces</td>\n";
echo " <td>$tempIP</td>\n";
echo " <td>$created</td>\n";
if ($dmesg) {
echo " <td><img src=\"redball.gif\"></td>\n";
} else {
echo " <td><img src=\"greenball.gif\"></td>\n";
}
echo " </tr>\n";
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment