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

4 5 6 7 8
# macgrabber.pl, a new script to harvest MAC address (as printed out by Mike's
# 'showmac' kernel) from cature logs. It prints out SQL commands to insert the
# 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 15 16 17 18 19 20 21 22 23 24
if (@ARGV != 3) {
	die "Usage: $0 <start_node> <end_node> <start_file>\n";
}

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

# '#defines' - Change depeding on the machines you're harvesting MACs for
my $numCards = 5;

my $interfaceType = "fxp";
my $iface = "eth";
my $port = 1;
my $IPalias = "NULL";
my $currentSpeed = 100;
my $duplex = "full";

25
# Which of the interfaces (as printed by showmac) is the control net
26
my $controlInterface = 0;
27
# Subnet for the control net
28 29
my $subnet = "155.101.132.";

30 31 32 33 34

# Mapping of interface numbers, as printed by the showmac kernel, to 
# cannonical database order. For example, on Utah's pc850's, what
# showmac reports as eth0 is saved as eth2 in the database (since this
# is its name under Linux 2.4.x
35
my %cardmap = ( 0 => 2, 1 => 3, 2 => 4, 3 => 0, 4 => 1);
36
# end of '#defines'
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
$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
61
	my $filename = $filePrefix . $fileNum++ . $fileSuffix;
62
	my @MACs = parseMac($filename);
63
	if (!@MACs) {
64
		warn "No MACs found for ${nodeType}${i} in file $filename\n";
65 66 67 68 69 70 71 72
	} else {
		for (my $j = 0; $j < @MACs; $j++) {
			my $IP;
			if ($j == $controlInterface) {
				$IP = $subnet . $i;
			} else {
				$IP = "";
			}
73
			print qq|INSERT INTO interfaces VALUES ("${nodeType}${i}",$cardmap{$j}, | .
74
				qq|$port,"$MACs[$j]","$IP",$IPalias,"$interfaceType", | .
75
				qq|"${iface}$cardmap{$j}",$currentSpeed,"$duplex");\n|;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
		}
	}
}

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*$/) {
93
			my @tmpMACs = ();
94 95
			for (my $i = 0; $i < $numCards; $i++) {
				if (/^\s*eth\d\s+([\dabcdef]{12}?)\s*$/) {
96
					push @tmpMACs, $1;
97 98 99 100 101
					$_ = <LOG>;
				} else {
					warn "Incorrect or missing card MAC in file $filename\n";
					last;
				}
102 103 104
				if ($i == ($numCards -1)) {
					@MACs = @tmpMACs;
				}
105 106 107 108 109
			}
		}
	}
	return @MACs;
}