if2port.in 2.46 KB
Newer Older
1
#!/usr/bin/perl -w
2
use Getopt::Std;
3 4
use strict;

5 6 7 8 9 10 11 12 13 14 15
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.
#

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

20
#
21 22
# Describes the location of each of the switches that nodes may be connected
# to
23
#
24 25 26
my %switches = ( cisco1 => 'top right', cisco2 => 'bottom right',
		 cisco3 => 'top left', cisco4 => 'bottom left');

27
#
28 29 30
# 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 
31
#
32 33 34 35 36 37 38 39 40 41 42 43
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' );

# Options: v = verbose, d = debug
my %opt = ( v => 0, d => 0);
44

45
getopts('vd',\%opt);
46 47


48 49
if (@ARGV < 1) {
	exit &usage;
50 51
}

52 53 54 55 56 57 58 59 60 61 62
while (my $arg = shift @ARGV) {
	my ($node,$card) = split /:/,$arg;
	&debug("Node is $node");
	&debug("Card is $card") if (defined $card);

	my $query = qq|SELECT node_id1,card1,node_id2,card2,port2,cable,len| . 
		qq|,wires.type,nodes.type FROM wires LEFT JOIN nodes | .
		qq|ON node_id1 = node_id WHERE node_id1="$node"|;
	if (defined $card) {
		$query .= qq|AND card1="$card"|;
	}
63

64
	&debug("Sending query $query\n");
65 66

	my $result = DBQueryFatal($query);
67 68

	if ($opt{v}) {
69
		while (my @row = $result->fetchrow) {
70 71 72 73 74 75 76 77
			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 {
78
		print $result->as_string;
79 80
	}
}
81

82
sub debug { warn @_,"\n" if $opt{d}; }
83 84

sub usage {
85 86 87
	print "Usage: $0 [-v] [-d] <node | node:if> ... \n";
	print "-v: Verbose, prose, output\n";
	print "-d: Enable debugging output\n";
88 89
	return -1;
}