macgrabber.pl 3.25 KB
Newer Older
1 2 3
#!/usr/bin/perl -w
use strict;

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

9 10 11 12 13 14
if (@ARGV != 3) {
	die "Usage: $0 <start_node> <end_node> <start_file>\n";
}

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

15
#
16
# '#defines' - Change depeding on the machines you're harvesting MACs for
17 18 19
#

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

22
# Interface type (from the interface_types) table of the cards
23 24
my $interfaceType = "fxp";

25
# Which of the interfaces (as printed by whoami) is the control net
26
my $controlInterface = 0;
27

28
# Subnet for the control net
29 30
my $subnet = "155.101.132.";

31 32 33 34
# 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;
35

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

56
# end of '#defines'
57

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

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