macgrabber.pl 4.04 KB
Newer Older
1
#!/usr/bin/perl -w
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3 4

#
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
24 25
#

26 27
use strict;

28
# macgrabber.pl, a new script to harvest MAC address (as printed out by Mike's
29
# 'whoami' kernel) from cature logs. It prints out SQL commands to insert the
30 31 32
# addresses to stdout. Change the values in the '#defines' section below
# depeding on the nodes you're harvesting info for.

33 34 35 36 37 38
if (@ARGV != 3) {
	die "Usage: $0 <start_node> <end_node> <start_file>\n";
}

my ($startNode, $endNode, $startFile) = @ARGV;

39
#
40
# '#defines' - Change depeding on the machines you're harvesting MACs for
41 42 43
#

# Number of cards (incl. control net) in each node.
44 45
my $numCards = 5;

46
# Interface type (from the interface_types) table of the cards
47 48
my $interfaceType = "fxp";

49
# Which of the interfaces (as printed by whoami) is the control net
50
my $controlInterface = 0;
51

52
# Subnet for the control net
53 54
my $subnet = "155.101.132.";

55 56 57 58
# Where the last octet of the nodes' IP addresses will start. For example,
# if pc1 is 155.101.132.1, this should be 1. If pc1 is 155.101.143.131, this
# should be 131.
my $base = 1;
59

60
# Mapping of interface numbers, as printed by the whoami kernel, to 
61 62
# cannonical database order. For example, on Utah's pc850's, what
# showmac reports as eth0 is saved as eth2 in the database (since this
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
# is its name under Linux 2.4.x . In general, the best way to get this
# mapping is to boot both the whoami kernel and a Linux kernel, and
# compare the ordering. There should be as many entries in this list
# as recorded in $numCards above.
my %cardmap = ( 0 => 2,
				1 => 3,
				2 => 4,
				3 => 0,
				4 => 1 );

# You can leave the following alone, unless you're sure you need to change them
my $iface = "eth";
my $port = 1;
my $IPalias = "NULL";
my $currentSpeed = 100;
my $duplex = "full";

80
# end of '#defines'
81

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
$startNode =~ /^(\D+)(\d+)$/;
my ($nodeType,$startNum) = ($1,$2);

if ((!defined $nodeType) || (!defined $startNum)) {
	die "Invalid start_node: $startNode\n";
}

$endNode =~ /^\D+(\d+)$/;
my $endNum = $1;

if (!defined $endNum) {
	die "Invalid end_node: $endNode\n";
}


$startFile =~ /^(\D*)(\d+)(\D*)$/;
my ($filePrefix,$fileNum,$fileSuffix) = ($1,$2,$3);

if (!defined $fileNum) {
	die "Invalid start_file: $startFile\n";
}

for (my $i = $startNum; $i <= $endNum; $i++) {
Robert Ricci's avatar
Robert Ricci committed
105
	my $filename = $filePrefix . $fileNum++ . $fileSuffix;
106
	my @MACs = parseMac($filename);
107
	if (!@MACs) {
108
		warn "No MACs found for ${nodeType}${i} in file $filename\n";
109 110 111 112
	} else {
		for (my $j = 0; $j < @MACs; $j++) {
			my $IP;
			if ($j == $controlInterface) {
113
				$IP = $subnet . ($base + $i -1);
114 115 116
			} else {
				$IP = "";
			}
117
			print qq|INSERT INTO interfaces VALUES ("${nodeType}${i}",$cardmap{$j}, | .
118
				qq|$port,"$MACs[$j]","$IP",$IPalias,"$interfaceType", | .
119
				qq|"${iface}$cardmap{$j}",$currentSpeed,"$duplex");\n|;
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
		}
	}
}

sub parseMac {
	my ($filename) = @_;

	if (!open(LOG,$filename)) {
		warn "Unable to open logfile $filename ... skipping\n";
		return ();
	}

	my @MACs = ();

	while (<LOG>) {
		chomp;
		if (/^\s*eth0\s+[\dabcdef]{12}?\s*$/) {
137
			my @tmpMACs = ();
138 139
			for (my $i = 0; $i < $numCards; $i++) {
				if (/^\s*eth\d\s+([\dabcdef]{12}?)\s*$/) {
140
					push @tmpMACs, $1;
141 142 143 144 145
					$_ = <LOG>;
				} else {
					warn "Incorrect or missing card MAC in file $filename\n";
					last;
				}
146 147 148
				if ($i == ($numCards -1)) {
					@MACs = @tmpMACs;
				}
149 150 151 152 153
			}
		}
	}
	return @MACs;
}