if2port.in 3.12 KB
Newer Older
1
#!/usr/bin/perl -w
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3 4

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2002, 2005 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6 7
# All rights reserved.
#
8
use Getopt::Std;
9 10
use strict;

11 12 13 14 15 16 17 18 19 20 21
use lib "@prefix@/lib";
use libdb;

#
# if2port - Given an interface (specified as node[:card]), find the
# port on the switches that it's connected to. With the -v switch, gives
# some human-readable information to help in following wires and diagnosing
# problems.
#

#
22 23
# IMPORTANT: This file contains some information specific to the Utah
# Network Testbed! If you want to use it in another enviroment, change
24
# the information below (%switches, %cards, %wires)
25

26
#
27 28
# Describes the location of each of the switches that nodes may be connected
# to
29
#
30 31 32
my %switches = ( cisco1 => 'top right', cisco2 => 'bottom right',
		 cisco3 => 'top left', cisco4 => 'bottom left');

33
#
34 35 36
# Description of the location of each chard on the back of a machine.
# A hash, indexed by node type. Each value is an array reference, ordered
# by the card order in the database 
37
#
38 39 40 41 42 43 44 45 46 47
my %cards = ( 'pc600' => [ 'second from left','third from left',
                          'second from right','rightmost','leftmost' ],
	      'pc850' => [ 'left port of dual card', 'right port of dual card',
	      		   'bottom builtin port', 'top builtin port',
			   'single port card' ] );

# Colors of wires, indexed by wire type
my %wires = ( Node => 'yellow', Control => 'red', Power => 'green',
	Serial => 'white' );

48
# Options: v = verbose, d = debug, m = show mac
49
my %opt = ( v => 0, d => 0, m => 0, I => 0);
50

51
getopts('vdmI',\%opt);
52
$opt{m} = 0 if ($opt{v});
53 54


55 56
if (@ARGV < 1) {
	exit &usage;
57 58
}

59 60 61 62 63
while (my $arg = shift @ARGV) {
	my ($node,$card) = split /:/,$arg;
	&debug("Node is $node");
	&debug("Card is $card") if (defined $card);

64
	my $query;
65 66 67 68 69 70 71 72 73 74
	if ($opt{m} || $opt{I}) {
	    $query = "SELECT w.node_id1,w.card1,";
	    $query .= "i.IP,"
		if ($opt{I});
	    $query .= "i.mac,"
		if ($opt{m});
	    $query .= "w.node_id2,w.card2,w.port2,w.cable,w.len,w.type,n.type".
		     " FROM wires AS w,nodes AS n,interfaces AS i".
		     " WHERE w.node_id1=n.node_id AND".
		     " w.node_id1=i.node_id AND w.card1=i.card";
75 76 77 78 79 80
	} else {
	    $query = "SELECT w.node_id1,w.card1,w.node_id2,w.card2,w.port2,".
		     "w.cable,w.len,w.type,n.type ".
		     "FROM wires AS w,nodes AS n WHERE w.node_id1=n.node_id";
	}
	$query .= " AND node_id1='$node'";
81
	if (defined $card) {
82
		$query .= " AND card1='$card'";
83
	}
84

85
	&debug("Sending query $query\n");
86 87

	my $result = DBQueryFatal($query);
88 89

	if ($opt{v}) {
90
		while (my @row = $result->fetchrow) {
91 92 93 94 95 96 97 98
			my $cardinfo = ${$cards{$row[8]}}[$row[1]];
			my $switchinfo = $switches{$row[2]};
			my $wireinfo = $wires{$row[7]};
			print "$row[0] port $row[1] ($cardinfo) connects to $row[2] " .
				"($switchinfo), module $row[3], port $row[4] via wire #" .
				"$row[5] (length $row[6], color $wireinfo)\n";
		}
	} else {
99
		print $result->as_string;
100 101
	}
}
102

103
sub debug { warn @_,"\n" if $opt{d}; }
104 105

sub usage {
106
	print "Usage: $0 [-vdm] <node | node:if> ... \n";
107 108
	print "-v: Verbose, prose, output\n";
	print "-d: Enable debugging output\n";
109
	print "-m: Show MAC addresses\n";
110
	print "-I: Show any assigned IP addresses\n";
111 112
	return -1;
}