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

#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
# 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
48
49
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);
50

51
getopts('vd',\%opt);
52
53


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

58
59
60
61
62
63
64
65
66
67
68
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"|;
	}
69

70
	&debug("Sending query $query\n");
71
72

	my $result = DBQueryFatal($query);
73
74

	if ($opt{v}) {
75
		while (my @row = $result->fetchrow) {
76
77
78
79
80
81
82
83
			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 {
84
		print $result->as_string;
85
86
	}
}
87

88
sub debug { warn @_,"\n" if $opt{d}; }
89
90

sub usage {
91
92
93
	print "Usage: $0 [-v] [-d] <node | node:if> ... \n";
	print "-v: Verbose, prose, output\n";
	print "-d: Enable debugging output\n";
94
95
	return -1;
}