6 use Data::Dump qw(dump);
8 my $dir="/dev/shm/snmp-topology";
13 @dumps = glob("$dir/*") unless @dumps;
17 $mac =~ s/^([0-9a-f]):/0$1:/i;
18 while ( $mac =~ s/:([0-9a-f]):/:0$1:/ig ) {};
19 $mac =~ s/:([0-9a-f])$/:0$1/i;
23 foreach my $file ( @dumps ) {
25 open(my $fh, '<', $file);
26 my $sw = $file; $sw =~ s/^.*\///;
29 if ( m/^SNMPv2-MIB::(sysName|sysDescr)\.0 = STRING: (.+)/ ) {
30 $stat->{$sw}->{$1} = $2;
32 } elsif ( m/^(IF-MIB)::(ifPhysAddress)\.0 = (\w+): (.+)/ ) {
33 my ($name,$oid,$type,$value) = ($1,$2,$3,$4);
34 #$value =~ s/\(\d+\)$// if $type eq 'INTEGER';
35 $stat->{$sw}->{$name}->{$oid} = $value;
37 } elsif ( m/^(IF-MIB)::(ifPhysAddress)\[(\d+)\] = (\w+): (.+)/ ) {
38 my ($name,$oid,$i,$type,$value) = ($1,$2,$3,$4,$5);
39 #warn "# $sw ",dump($name,$oid,$i,$type,$value),$/;
40 #$stat->{$sw}->{$name}->{$oid}->[$i] = $value;
41 $stat->{_mac2sw}->{$value} = $sw;
42 } elsif ( m/^BRIDGE-MIB::dot1dTpFdbPort\[STRING: ([^\]]+)\] = INTEGER: (\d+)/ ) {
43 my ( $mac, $port ) = ($1,$2);
44 push @{ $stat->{_sw_mac_port_vlan}->{$sw}->{$mac}->{$port} }, '';
45 } elsif ( m/^Q-BRIDGE-MIB::dot1qTpFdbPort\[(\d+)\]\[STRING: ([^\]]+)\] = INTEGER: (\d+)/ ) {
46 my ( $vlan, $mac, $port ) = ($1,$2,$3);
47 push @{ $stat->{_sw_mac_port_vlan}->{$sw}->{$mac}->{$port} }, $vlan;
50 #warn "# $sw ",dump( $stat->{$sw} );
52 #warn "# stat = ",dump($stat);
54 open(my $fh, '>', '/dev/shm/mac2sw');
55 foreach my $mac ( keys %{ $stat->{_mac2sw} } ) {
56 print $fh macfmt($mac), " ", $stat->{_mac2sw}->{$mac}, "\n";
59 # XXX inject additional mac in filter to include wap devices
60 my $mac_include = '/dev/shm/mac.wap';
61 if ( -e $mac_include ) {
63 open(my $fh, '<', $mac_include);
66 my ($mac,$host) = split(/\s+/,$_,2);
67 $mac =~ s/^0//; $mac =~ s/:0/:/g; # mungle mac to snmp format without leading zeros
68 $stat->{_mac2sw}->{$mac} = $host;
71 # warn "# $mac_include added to _mac2sw = ",dump($stat->{_mac2sw}),$/;
72 warn "# $mac_include added to _mac2sw $count hosts\n";
75 my $s = $stat->{_sw_mac_port_vlan};
76 foreach my $sw ( keys %$s ) {
77 foreach my $mac ( keys %{ $s->{$sw} } ) {
78 if ( my $mac_name = $stat->{_mac2sw}->{ $mac } ) {
79 next if $sw eq $mac_name; # mikrotik seems to see itself
80 foreach my $port ( keys %{ $s->{$sw}->{$mac} } ) {
81 #$stat->{_sw_port_sw}->{$sw}->{$port}->{$mac_name} = $s->{$sw}->{$mac}->{$port};
82 push @{ $stat->{_sw_port_sw}->{$sw}->{$port} }, $mac_name;
88 warn "# _sw_port_sw = ",dump($stat->{_sw_port_sw});
91 my $s = $stat->{_sw_port_sw};
97 my $u; $u->{$_}++ foreach @visible;
98 @visible = sort keys %$u;
103 my @visible = uniq(@_);
104 @visible = grep { ! exists $stat->{_found}->{$_} } @visible;
111 my @visible = uniq_visible(@_);
112 warn "# to_later $sw $port visible = ",dump( \@visible ),"\n";
113 $later->{$sw}->{$port} = [ @visible ];
118 #warn "## s = ",dump($s);
119 foreach my $sw ( sort keys %$s ) {
121 #warn "## $sw s = ",dump($s->{$sw}),$/;
123 my @ports = sort { $a <=> $b } uniq( keys %{ $s->{$sw} } );
125 foreach my $port ( @ports ) {
126 warn "## $sw $port => ",join(' ', @{$s->{$sw}->{$port}}),$/;
129 if ( $#ports == 0 ) {
130 my $port = $ports[0];
131 #print "$sw $port TRUNK\n";
132 push @{$stat->{_trunk}->{$sw}}, $port; # FIXME multiple trunks?
133 #warn "## _trunk = ",dump( $stat->{_trunk} ).$/;
135 my @visible = uniq_visible( @{ $s->{$sw}->{$port} } );
136 to_later( $sw, $port, @visible );
140 foreach my $port ( @ports ) {
141 my @visible = uniq_visible( @{ $s->{$sw}->{$port} } );
142 warn "### $sw $port visible=",dump(\@visible),$/;
144 if ( $#visible == 0 ) {
145 warn "++++ $sw $port $visible[0]\n";
146 #print "$sw $port $visible[0]\n";
147 $stat->{_found}->{$visible[0]} = "$sw $port";
149 } elsif ( @visible ) {
150 to_later( $sw, $port, @visible );
152 warn "#### $sw $port doesn't have anything visible\n";
156 warn "## _found = ",dump( $stat->{_found} ),$/;
159 warn "NEXT later = ",dump($later),$/;
163 my @single_sw_port_visible;
169 foreach my $sw ( keys %$later ) {
170 my @ports = sort keys %{ $later->{$sw} };
171 foreach my $port ( @ports ) {
172 my @visible = uniq_visible( @{ $later->{$sw}->{$port} } );
173 $s->{$sw}->{$port} = [ @visible ];
174 push @single_sw_port_visible, [ $sw, $port, $visible[0] ] if $#visible == 0; # single
180 if ( $d eq $last_later ) {
181 warn "FIXME later didn't change single_sw_port_visible = ",dump( \@single_sw_port_visible ),$/;
185 foreach my $single ( @single_sw_port_visible ) {
186 my ( $sw, $port, $visible ) = @$single;
187 foreach my $port ( keys %{ $s->{$visible} } ) {
188 # check back in original full map to see if it was visible
189 my @visible = @{ $stat->{_sw_port_sw}->{$visible}->{$port} };
190 if ( scalar grep(/$sw/,@visible) ) {
191 warn "PATCH $visible $port -> $sw ONLY";
192 $stat->{_found}->{$sw} = "$visible $port";
193 delete $s->{$visible}->{$port};
196 warn "PATCHED _found = ", dump($stat->{_found});
197 warn "PATCHED s = $d\n";
201 die "fatal inconsistency - dungeon colapses, you die";
205 warn "## applied $did_patch patches to unblock\n";
207 last if $d eq $last_later;
216 warn "FINAL _found = ",dump( $stat->{_found} ),$/;
217 warn "FINAL _trunk = ",dump( $stat->{_trunk} ),$/;
218 warn "FINAL later = ",dump( $later ),$/;
224 my $ports = $ENV{PORTS} || 1; # FIXME
227 open(my $dot, '>', '/tmp/snmp-topology.dot');
229 my $shape = $ports ? 'record' : 'ellipse';
230 my $rankdir = 'LR'; #$ports ? 'TB' : 'LR';
231 print $dot <<"__DOT__";
233 graph [ rankdir = $rankdir ]
234 node [ shape = $shape ]
235 edge [ color = "gray" ]
238 foreach my $to_sw ( keys %{ $stat->{_found} } ) {
239 my ($from_sw, $from_port) = split(/ /,$stat->{_found}->{$to_sw},2);
240 my @to_port = uniq(@{ $stat->{_trunk}->{$to_sw} });
241 my $to_port = $to_port[0];
242 warn "ERROR: $to_sw has ",dump(\@to_port), " ports instead of just one!" if $#to_port > 0;
243 printf "%s %s -> %s %s\n", $from_sw, $from_port, $to_sw, $to_port;
244 push @edges, [ $from_sw, $to_sw, $from_port, $to_port ];
245 push @{ $node->{$from_sw} }, [ $from_port, $to_sw ];
246 push @{ $node->{$to_sw} }, [ $to_port, $from_sw ]
249 warn "# edges = ",dump(\@edges);
250 warn "# node = ",dump($node);
253 foreach my $n ( keys %$node ) {
255 sort { $a->[0] <=> $b->[0] }
257 #warn "XXX $n ",dump( \@port_sw );
259 print $dot qq!"$n" [ label="!.uc($n).'|' . join('|', map { sprintf "<%d>%2d %s", $_->[0], $_->[0], $_->[1] } @port_sw ) . qq!" ];\n!;
263 foreach my $e ( @edges ) {
265 print $dot sprintf qq{ "%s" -> "%s" [ taillabel="%s" ; headlabel="%s" ]\n}, @$e;
268 print $dot sprintf qq{ "%s":%d -> "%s":%d\n}, $e->[0], $e->[2], $e->[1], $e->[3];