9973442ab5d9954a50e80d9aa7aecc85065fffbb
[dell-switch] / snmp-topology.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use autodie;
5
6 use Data::Dump qw(dump);
7
8 my $dir="/dev/shm/snmpbulkwalk";
9
10 my $stat;
11
12 my @dumps = @ARGV;
13 @dumps = glob("$dir/*") unless @dumps;
14
15 sub macfmt {
16         my $mac = shift;
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;
20         return $mac;
21 }
22
23 foreach my $file ( @dumps ) {
24
25         open(my $fh, '<', $file);
26         my $sw = $file; $sw =~ s/^.*\///;
27         while(<$fh>) {
28                 chomp;
29                 if ( m/^SNMPv2-MIB::(sysName|sysDescr)\.0 = STRING: (.+)/ ) {
30                         $stat->{$sw}->{$1} = $2;
31 =for xxx
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;
36 =cut
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;
48                 }
49         }
50         #warn "# $sw ",dump( $stat->{$sw} );
51 }
52 #warn "# stat = ",dump($stat);
53
54 open(my $fh, '>', '/dev/shm/mac2sw');
55 foreach my $mac ( keys %{ $stat->{_mac2sw} } ) {
56         print $fh macfmt($mac), " ", $stat->{_mac2sw}->{$mac}, "\n";
57 };
58
59 my $s = $stat->{_sw_mac_port_vlan};
60 foreach my $sw ( keys %$s ) {
61         foreach my $mac ( keys %{ $s->{$sw} } ) {
62                 if ( my $mac_name = $stat->{_mac2sw}->{ $mac } ) {
63                         next if $sw eq $mac_name; # mikrotik seems to see itself
64                         foreach my $port ( keys %{ $s->{$sw}->{$mac} } ) {
65                                 #$stat->{_sw_port_sw}->{$sw}->{$port}->{$mac_name} = $s->{$sw}->{$mac}->{$port};
66                                 push @{ $stat->{_sw_port_sw}->{$sw}->{$port} }, $mac_name;
67                         }
68                 }
69         }
70 }
71
72 warn "# _sw_port_sw = ",dump($stat->{_sw_port_sw});
73
74
75 my $s = $stat->{_sw_port_sw};
76 our $later;
77 my $last_later;
78
79 sub uniq {
80         my @visible = @_;
81         my $u; $u->{$_}++ foreach @visible;
82         @visible = sort keys %$u;
83         return @visible;
84 }
85
86 sub uniq_visible {
87         my @visible = uniq(@_);
88         @visible = grep { ! exists $stat->{_found}->{$_} } @visible;
89         return @visible;
90 }
91
92 sub to_later {
93         my $sw = shift;
94         my $port = shift;
95         my @visible = uniq_visible(@_);
96         warn "# to_later $sw $port visible = ", $#visible + 1, "\n";
97         $later->{$sw}->{$port} = [ @visible ];
98         return @visible;
99 }
100
101 while ( ref $s ) {
102 #warn "## s = ",dump($s);
103 foreach my $sw ( sort keys %$s ) {
104
105         #warn "## $sw s = ",dump($s->{$sw}),$/;
106
107         my @ports = sort { $a <=> $b } uniq( keys %{ $s->{$sw} } );
108
109         foreach my $port ( @ports ) {
110                 warn "## $sw $port => ",join(' ', @{$s->{$sw}->{$port}}),$/;
111         }
112         if ( $#ports == 0 ) {
113                 my $port = $ports[0];
114                 #print "$sw $port TRUNK\n";
115                 push @{$stat->{_trunk}->{$sw}}, $port; # FIXME multiple trunks?
116                 #warn "## _trunk = ",dump( $stat->{_trunk} ).$/;
117
118                 my @visible = uniq_visible( @{ $s->{$sw}->{$port} } );
119                 to_later( $sw, $port, @visible );
120                 next;
121         }
122
123         foreach my $port ( @ports ) {
124                 my @visible = uniq_visible( @{ $s->{$sw}->{$port} } );
125                 warn "### $sw $port visible=",dump(\@visible),$/;
126
127                 if ( $#visible == 0 ) {
128                         warn "++++ $sw $port $visible[0]\n";
129                         #print "$sw $port $visible[0]\n";
130                         $stat->{_found}->{$visible[0]} = "$sw $port";
131                 
132                 } elsif ( @visible ) {
133                         to_later( $sw, $port, @visible );
134                 } else {
135                         warn "#### $sw $port doesn't have anything visible\n";
136                 }
137                         
138         }
139         warn "## _found = ",dump( $stat->{_found} ),$/;
140 }
141
142 warn "NEXT later = ",dump($later),$/;
143 $s = $later;
144
145 my $d = dump($later);
146 if ( $d eq $last_later ) {
147         warn "FIXME later didn't change, last\n";
148         last;
149 }
150 $last_later = $d;
151
152 $later = undef;
153
154 } # while
155
156 warn "FINAL _found = ",dump( $stat->{_found} ),$/;
157 warn "FINAL _trunk = ",dump( $stat->{_trunk} ),$/;
158
159
160 my $node;
161 my @edges;
162
163 my $ports = $ENV{PORTS} || 0; # FIXME
164
165
166 open(my $dot, '>', '/tmp/snmp-topology.dot');
167
168 my $shape = $ports ? 'record' : 'ellipse';
169 my $rankdir = $ports ? 'TB' : 'LR';
170 print $dot <<"__DOT__";
171 digraph topology {
172 graph [ rankdir = $rankdir ]
173 node [ shape = $shape ]
174 edge [ color = "gray" ]
175 __DOT__
176
177 foreach my $to_sw ( keys %{ $stat->{_found} } ) {
178         my ($from_sw, $from_port) = split(/ /,$stat->{_found}->{$to_sw},2);
179         my @to_port = uniq(@{ $stat->{_trunk}->{$to_sw} });
180         my $to_port = $to_port[0];
181         warn "ERROR: $to_sw has ",dump(\@to_port), " ports instead of just one!" if $#to_port > 0;
182         printf "%s %s -> %s %s\n", $from_sw, $from_port, $to_sw, $to_port;
183         push @edges, [ $from_sw, $to_sw, $from_port, $to_port ];
184         push @{ $node->{$from_sw} }, [ $from_port, $to_sw ];
185         push @{ $node->{$to_sw} }, [ $to_port, $from_sw ]
186 }
187
188 warn "# edges = ",dump(\@edges);
189 warn "# node = ",dump($node);
190
191 if ( $ports ) {
192         foreach my $n ( keys %$node ) {
193                 my @port_sw =
194                         sort { $a->[0] <=> $b->[1] }
195                         @{ $node->{$n} };
196 warn "XXX $n ",dump( \@port_sw );
197                 print $dot qq!"$n" [ label="*$n*|! . join('|', map { sprintf "<%d>%2d %s", $_->[0], $_->[0], $_->[1] } @port_sw ) . qq!" ];\n!;
198         }
199 }
200
201 foreach my $e ( @edges ) {
202         if (! $ports) {
203                 print $dot sprintf qq{ "%s" -> "%s" [ taillabel="%s" ; headlabel="%s" ]\n}, @$e;
204         } else {
205                 print $dot sprintf qq{ "%s":%d -> "%s":%d\n}, $e->[0], $e->[2], $e->[1], $e->[3];
206         }
207 }
208
209 print $dot "}\n";
210