1#!/usr/bin/perl
2
3#
4# Read mapfile
5#
6open(MAP, "< ibdiscover.map");
7
8while (<MAP>) {
9	($pre, $port, $desc) = split /\|/;
10	$val{$pre} = $desc;
11	#		print "Ack1 - $pre - $port - $desc\n";
12}
13close(MAP);
14
15#
16# Read old topo map in
17#
18open(TOPO, "< ibdiscover.topo");
19$topomap = 0;
20
21while (<TOPO>) {
22	$topomap = 1;
23	($localPort, $localGuid, $remotePort, $remoteGuid) = split /\|/;
24	chomp $remoteGuid;
25	$var = sprintf("%s|%2s|%2s|%s", $localGuid, $localPort, $remotePort,
26		$remoteGuid);
27	$topo{$var} = 1;
28	#	${$pre} = $desc;
29	#		print "Ack1 - $pre - $port - $desc\n";
30}
31close(TOPO);
32
33#
34# Read stdin and output enhanced output
35#
36# Search and replace =0x???? with value
37# Search and replace -000???? with value
38
39open(TOPO2, " >ibdiscover.topo.new");
40while (<STDIN>) {
41	($a, $b, $local, $d) = /([sh])([\s\S]*)=0x([a-f\d]*)([\s\S]*)/;
42	if ($local ne "") {
43		printf(
44			"\n%s GUID: %s  %s\n",
45			($a eq "s" ? "Switch" : "Host"),
46			$local, $val{$local}
47		);
48		chomp $local;
49		$localGuid = $local;
50	} else {
51		($localPort, $type, $remoteGuid, $remotePort) =
52		  /([\s\S]*)"([SH])\-000([a-f\d]*)"([\s\S]*)\n/;
53		($localPort)  = $localPort  =~ /\[(\d*)]/;
54		($remotePort) = $remotePort =~ /\[(\d*)]/;
55		if ($remoteGuid ne "" && $localPort ne "") {
56			printf(TOPO2 "%d|%s|%d|%s\n",
57				$localPort, $localGuid, $remotePort, $remoteGuid);
58			$var = sprintf("%s|%2s|%2s|%s",
59				$localGuid, $localPort, $remotePort, $remoteGuid);
60			$topo{$var} += 1;
61			printf(
62				"Local: %2s  Remote: %2s  %7s  GUID: %s  Location: %s\n",
63				$localPort,
64				$remotePort,
65				($type eq "H" ? "Host" : "Switch"),
66				$remoteGuid,
67				($val{$remoteGuid} ne "" ? $val{$remoteGuid} : $remoteGuid)
68			);
69		}
70	}
71}
72close(STDIN);
73close(TOPO2);
74
75printf("\nDelta change in topo (change between successive runs)\n\n");
76
77foreach $el (keys %topo) {
78	if ($topo{$el} < 2 || $topomap == 0) {
79		($lg, $lp, $rp, $rg) = split(/\|/, $el);
80		printf(
81"Link change:  Local/Remote Port %2d/%2d Local/Remote GUID: %s/%s\n",
82			$lp, $rp, $lg, $rg);
83		printf("\tLocations: Local/Remote\n\t\t%s\n\t\t%s\n\n",
84			$val{$lg}, $val{$rg});
85	}
86}
87