macgrabber.pl 3.36 KB
Newer Older
1
#!/usr/bin/perl -w
Leigh Stoller's avatar
Leigh Stoller committed
2 3 4 5 6 7 8

#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
# All rights reserved.
#

9 10
use strict;

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

16 17 18 19 20 21
if (@ARGV != 3) {
	die "Usage: $0 <start_node> <end_node> <start_file>\n";
}

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

22
#
23
# '#defines' - Change depeding on the machines you're harvesting MACs for
24 25 26
#

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

29
# Interface type (from the interface_types) table of the cards
30 31
my $interfaceType = "fxp";

32
# Which of the interfaces (as printed by whoami) is the control net
33
my $controlInterface = 0;
34

35
# Subnet for the control net
36 37
my $subnet = "155.101.132.";

38 39 40 41
# 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;
42

43
# Mapping of interface numbers, as printed by the whoami kernel, to 
44 45
# cannonical database order. For example, on Utah's pc850's, what
# showmac reports as eth0 is saved as eth2 in the database (since this
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
# 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";

63
# end of '#defines'
64

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
$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++) {
88
	my $filename = $filePrefix . $fileNum++ . $fileSuffix;
89
	my @MACs = parseMac($filename);
90
	if (!@MACs) {
91
		warn "No MACs found for ${nodeType}${i} in file $filename\n";
92 93 94 95
	} else {
		for (my $j = 0; $j < @MACs; $j++) {
			my $IP;
			if ($j == $controlInterface) {
96
				$IP = $subnet . ($base + $i -1);
97 98 99
			} else {
				$IP = "";
			}
100
			print qq|INSERT INTO interfaces VALUES ("${nodeType}${i}",$cardmap{$j}, | .
101
				qq|$port,"$MACs[$j]","$IP",$IPalias,"$interfaceType", | .
102
				qq|"${iface}$cardmap{$j}",$currentSpeed,"$duplex");\n|;
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
		}
	}
}

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*$/) {
120
			my @tmpMACs = ();
121 122
			for (my $i = 0; $i < $numCards; $i++) {
				if (/^\s*eth\d\s+([\dabcdef]{12}?)\s*$/) {
123
					push @tmpMACs, $1;
124 125 126 127 128
					$_ = <LOG>;
				} else {
					warn "Incorrect or missing card MAC in file $filename\n";
					last;
				}
129 130 131
				if ($i == ($numCards -1)) {
					@MACs = @tmpMACs;
				}
132 133 134 135 136
			}
		}
	}
	return @MACs;
}