-
Notifications
You must be signed in to change notification settings - Fork 2
/
sbw-parse.pl
executable file
·382 lines (317 loc) · 11.2 KB
/
sbw-parse.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/perl
use warnings;
use strict;
use autodie;
# ./sw-name-mac.sh
# usage: NAME_MAC=/dev/shm/file-with-name-space-mac sbw-parse.pl [optional-switch-snmpbulkwalk-dump]
#
# cat /dev/shm/neighbors.tab | grep MikroTik | tee /dev/stderr | awk '{ print $7 "_" $8 " " $3 }' > /tmp/name-mac
# NAME_MAC=/tmp/name-mac ./sbw-parse.pl
use Storable;
use Data::Dump qw(dump);
$|=1; # flush stdout
my $debug = $ENV{DEBUG} || 0;
my @cols = qw( ifName ifHighSpeed ifAdminStatus ifOperStatus ifType dot1dStpPortPathCost ifAlias );
use lib 'lib';
use name2mac;
$name2mac::debug = 1;
my $mac2name = $name2mac::mac2name;
sub mac2name {
my ( $mac, $name ) = @_;
$mac = lc($mac);
if ( exists $mac2name->{$mac} ) {
my $mac_name = $mac2name->{$mac};
warn "ERROR: name different $name != $mac_name" if $name && $name ne $mac_name;
return ( $mac, $mac_name );
}
return ( $mac, $name );
}
my $mac_name_use;
my $sw;
sub fix_mac {
my $mac = shift;
$mac = lc($mac);
$mac =~ s/^([0-9a-f]):/0$1:/;
while ( $mac =~ s/:([0-9a-f]):/:0$1:/g ) {};
$mac =~ s/:([0-9a-f])$/:0$1/;
# warn "# $mac\n";
return $mac;
}
sub sw_name_mac_port {
my ( $name, $mac, $port ) = @_;
$mac = fix_mac($mac);
if ( exists $mac2name->{$mac} ) {
my $sw_name = $mac2name->{$mac};
$sw->{$name}->{port_for_switch}->{$port}->{ $sw_name }++;
#print "## $name $port $sw_name\n";
}
}
my $gv; # collect for graphviz
my @files = @ARGV;
@files = glob('snmpbulkwalk/*') unless @files;
foreach my $file ( @files ) {
my ( undef, $name ) = split(/\//, $file);
print "# $name $file\n" if $debug;
if ( -s "/dev/shm/$file" ) {
if ( -M $file < -M "/dev/shm/$file" ) {
warn "UPDATE $file\n";
system "cp -pv /dev/shm/$file $file";
}
$file="/dev/shm/$file";
} else {
warn "WARNING: using old file $file\n";
}
# dot1qPvid
open(my $f, '<', $file);
while(<$f>) {
chomp;
if ( m/::(sysName|sysLocation|ipDefaultTTL|dot1dStpPriority|dot1dStpTopChanges|dot1dStpDesignatedRoot|dot1dStpRootCost|dot1dStpRootPort|dot1qNumVlans|dot1dBaseBridgeAddress|dot1dStpRootCost|dot1dStpRootPort)\./ ) {
my $n = $1;
my ( undef, $v ) = split(/ = \S+: /,$_,2);
$v = fix_mac($v) if $n eq 'dot1dBaseBridgeAddress';
if ( $n eq 'dot1dStpDesignatedRoot' ) { # hex with spaces to mac
my @v = split(/ /, $v);
my $priority = '0x'; $priority .= shift @v; $priority .= shift @v; $priority .= ' ';
my $mac = fix_mac( join(':', @v) );
$v = $priority . $mac;
}
$sw->{$name}->{_}->{$n} = $v;
} elsif ( m/::(ifMtu|ifHighSpeed|ifSpeed)\[(\d\d?)\] = (?:INTEGER|Gauge32): (\d+)/ ) {
$sw->{$name}->{$1}->[$2] = $3;
# Dell PowerConnect 5548 doeesn't have ifHighSpeed
if ( $1 eq 'ifSpeed' && ! exists $sw->{$name}->{'ifHighSpeed'}->[$2] ) {
$sw->{$name}->{'ifHighSpeed'}->[$2] = $3 / 1_000_000;
}
} elsif ( m/::(ifPhysAddress)\[(\d\d?)\] = STRING: ([0-9a-f:]+)/ ) {
$sw->{$name}->{$1}->[$2] = fix_mac($3);
} elsif ( m/::(ifDescr|ifName|ifAlias)\[(\d\d?)\] = STRING: (.+)/ ) {
$sw->{$name}->{$1}->[$2] = $3;
if ( $1 eq 'ifName' ) {
my ( $if_name, $port ) = ($3,$2);
$sw->{$name}->{port_name_to_number}->{$if_name} = $port;
$sw->{$name}->{port_number_to_name}->{$port} = $if_name;
}
if ( $1 eq 'ifDescr' ) { # some switches miss ifName
my ( $if_name, $port ) = ($3,$2);
$if_name =~ s/gigabitethernet/ge/;
$if_name =~ s/tengigabitethernet/te/;
$sw->{$name}->{'ifName'}->[$port] = $if_name
if ( ! exists $sw->{name}->{'ifName'} );
$sw->{$name}->{port_name_to_number}->{$if_name} = $port
if ( ! exists $sw->{$name}->{port_name_to_number}->{$if_name} );
$sw->{$name}->{port_number_to_name}->{$port} = $if_name
if ( ! exists $sw->{$name}->{port_number_to_name}->{$port} );
}
} elsif ( m/::(ifAdminStatus|ifOperStatus|ifType|dot3StatsDuplexStatus)\[(\d\d?)\] = INTEGER: (\w+)\(/ ) {
$sw->{$name}->{$1}->[$2] = $3;
} elsif ( m/::(dot1dStpPortPathCost)\[(\d\d?)\] = INTEGER: (\d+)/ ) {
$sw->{$name}->{$1}->[$2] = $3;
} elsif ( m/::(dot1dTpFdbPort)\[STRING: ([0-9a-f:]+)\] = INTEGER: (\d+)/ ) {
$sw->{$name}->{$1}->{ fix_mac($2) } = $3;
sw_name_mac_port( $name, $2, $3 );
} elsif ( m/::(dot1qTpFdbPort)\[(\d+)\]\[STRING: ([0-9a-f:]+)\] = INTEGER: (\d+)/ ) {
$sw->{$name}->{$1}->{ fix_mac($3) } = [ $4, $2 ]; # port, vlan
sw_name_mac_port( $name, $3, $4 );
}
# dot1qVlanCurrentEgressPorts
# dot1qVlanCurrentUntaggedPorts
# dot1qVlanStaticName
# dot1qVlanStaticEgressPorts
# dot1qVlanStaticUntaggedPorts
# dot1qPvid
# entPhysicalDescr
# entPhysicalClass
#print "## $_<--\n";
}
warn "# sw $name = ",dump($sw->{$name}) if $debug;
foreach my $port ( 1 .. $#{ $sw->{$name}->{ifDescr} } ) {
print "$name $port";
foreach my $oid ( @cols ) {
if ( $oid eq 'ifAlias' ) {
if ( defined( $sw->{$name}->{$oid}->[$port] ) ) {
print " [",$sw->{$name}->{$oid}->[$port],"]";
}
} elsif ( defined $sw->{$name}->{$oid}->[$port] ) {
print " ", $sw->{$name}->{$oid}->[$port];
} else {
print " ?";
#warn "MISSING $name $oid $port\n";
}
}
if ( exists( $sw->{$name}->{port_for_switch}->{ $port } ) ) {
my @visible = sort keys %{ $sw->{$name}->{port_for_switch}->{ $port } };
print " ",join(',', @visible);
if ( scalar @visible == 1 ) {
$gv->{$name}->{$port}->{ $visible[0] }->{ 'no_port' } = [$port,0]; # no port
}
}
print "\n";
}}
# fix ifPhysAddress
# read neighbour visibility from lldp
sub port2number {
my ($name,$port) = @_;
return $port if $port =~ m/^\d+$/;
$port =~ s{bridge\d*/}{}; # remove mikrotik port prefix
$port =~ s{,bridge\d*$}{}; # remove mikrotik port suffix
$port =~ s{,bonding\d*$}{}; # remove mikrotik port suffix
if ( exists $sw->{$name}->{port_name_to_number}->{$port} ) {
return $sw->{$name}->{port_name_to_number}->{$port};
}
# gigabitethernet1/0/45 or gi1/0/45
if ( $port =~ m{(.+)1/0/(\d+)$} ) {
my ($type,$port) = ($1,$2);
if ( $type =~ m{gi}i ) {
return $port;
} elsif ( $type =~ m{te}i ) {
return $port <= 4 ? $port + 10000 : $port;
} else {
die "unknown [$type]";
}
}
# linux
if ( $port =~ m{eth(\d+)} ) {
return $1;
}
my @fuzzy = grep { m/^$port/ } keys %{ $sw->{$name}->{port_name_to_number} };
if ( scalar @fuzzy == 1 ) {
return $sw->{$name}->{port_name_to_number}->{$fuzzy[0]}
}
warn "ERROR [$_] can't find port $port for $name in ",dump( $sw->{$name}->{port_name_to_number} );
}
sub fix_sw_name {
my $name = shift;
if ( $name eq 'rack3-lib' ) {
$name = 'sw-lib-srv';
}
return $name;
}
open(my $n_fh, '<', '/dev/shm/neighbors.tab');
while(<$n_fh>) {
chomp;
my ( $sw1, $port1, undef, $port2, $sw2, undef ) = split(/\t/, $_, 6 );
next if $port2 =~ m/:/; # skip mac in port number (wap lldp leek)
next unless $sw2 && $port2;
next if $sw1 eq 'sw-core' || $sw2 eq 'sw-core'; # FIXME skip all sw-core
$sw1 = fix_sw_name($sw1);
my $port1_nr = port2number( $sw1, $port1 );
my $port2_nr = port2number( $sw2, $port2 );
if ( $sw->{$sw1}->{ifType}->[$port1_nr] ne 'ethernetCsmacd' ) {
warn "SKIP $sw1 $port1_nr $sw->{$sw1}->{ifType}->[$port1_nr]\n";
next;
} elsif ( $port1 =~ m{bridge} ) { # skip mikrotik bridges
# warn "SKIP $sw1 $port1_nr $port1\n";
# next;
}
$gv->{$sw1}->{$port1_nr}->{$sw2}->{$port2_nr} = [ $port1, $port2 ];
delete $gv->{$sw1}->{$port1_nr}->{$sw2}->{'no_port'} if exists $gv->{$sw1}->{$port1_nr}->{$sw2}->{'no_port'};
if ( $sw1 =~ /sw/ && $sw2 =~ /sw/ ) {
# ensure that connection between switches are bi-deirectional
$gv->{$sw2}->{$port2_nr}->{$sw1}->{$port1_nr} = [ $port2, $port1 ];
}
}
# FIXME sw-b101 doesn't have lldp so we insert data here manually from pictures
sub fake_gv {
my ($sw1, $p1, $sw2, $p2) = @_;
my $p1n = $sw->{$sw1}->{port_number_to_name}->{$p1};
my $p2n = $sw->{$sw2}->{port_number_to_name}->{$p2};
$gv->{$sw1}->{$p1}->{$sw2}->{$p2} = [ $p1n, $p2n ];
$gv->{$sw2}->{$p2}->{$sw1}->{$p1} = [ $p2n, $p1n ];
}
delete $gv->{'sw-b101'};
#fake_gv( 'sw-b101' => 3, 'sw-b101' => 4 ); # vlan1-to-vlan2 / vlan2-to-vlan1
#fake_gv( 'sw-b101' => 21, 'sw-rack2' => 50 );
#fake_gv( 'sw-b101' => 23, 'sw-lib-srv' => 48 );
#fake_gv( 'sw-b101' => 24, 'sw-rack1' => 48 );
#delete $gv->{'sw-ganeti'};
# spf-16 -> "sw-rack2" => { 2 => ["16-sfp-uplink-b101,bridge", "te1/0/2"] },
fake_gv( 'sw-core' => 2, 'sw-dpc-2' => 16 );
#fake_gv( 'sw-core' => 7, 'sw-b101' => 5 ); # 4 port lacp
#fake_gv( 'sw-core' => 8, 'sw-b101' => 6 );
#fake_gv( 'sw-core' => 9, 'sw-b101' => 7 );
#fake_gv( 'sw-core' => 10, 'sw-b101' => 8 );
# remove mikrotik multiple devices on same port
foreach my $sw1 ( keys %$gv ) {
foreach my $p1 ( keys %{ $gv->{$sw1} } ) {
my @sw2 = keys %{ $gv->{$sw1}->{$p1} };
if ( scalar @sw2 > 1 ) {
warn "XXX more than 1 port for $sw1 $p1 ", dump( $gv->{$sw1}->{$p1} );
}
}
}
print "# gv = ",dump( $gv );
Storable::store( $gv, '/tmp/gv.storable' );
open(my $dot_fh, '>', '/dev/shm/network.dot');
print $dot_fh qq|
digraph topology {
graph [ rankdir = LR ]
node [ shape = record; fontname="Helvetica" ]
edge [ color = "gray" ]
|;
my @edges;
my $node;
foreach my $sw1 ( sort keys %$gv ) {
foreach my $p1 ( sort { $a <=> $b } keys %{ $gv->{$sw1} } ) {
foreach my $sw2 ( sort keys %{ $gv->{$sw1}->{$p1} } ) {
if ( $sw1 eq $sw2 ) {
warn "SKIP same switch $sw1 == $sw2\n";
next;
}
#push @{ $node->{$sw1} }, [ $p1, $sw2 ];
foreach my $p2 ( sort { $a <=> $b } keys %{ $gv->{$sw1}->{$p1}->{$sw2} } ) {
push @edges, [ $sw1, $sw2, $p1, $p2 ];
##push @{ $node->{$sw2} }, [ $p2, $sw1 ];
push @{ $node->{$sw1} }, [ $p1, $sw2, $p2 ];
}
}
}
}
foreach my $n ( sort keys %$node ) {
no warnings;
my @port_sw =
sort { join(' ',@$a) <=> join(' ',@$b) }
@{ $node->{$n} };
print $dot_fh qq!"$n" [ label="!.uc($n).'|' . join('|', map {
$mac_name_use->{$_->[1]}++;
sprintf "<%d>%2d %s%s", $_->[0], $_->[0], $_->[1], $_->[2] eq 'no_port' ? '' : ' ' . $_->[2]
} @port_sw ) . qq!" ];\n!;
}
foreach my $e ( sort { join(' ',@$a) cmp join(' ', @$b) } @edges ) {
no warnings;
if ( $e->[3] == 0 ) {
#print $dot_fh sprintf qq{ "%s":%d -> "%s"\n}, $e->[0], $e->[2], $e->[1];
} else {
my $attr;
if ( $e->[2] == $sw->{$e->[0]}->{_}->{dot1dStpRootPort} ) {
$attr = "arrowtail=crow";
} elsif ( $e->[3] == $sw->{ $e->[1] }->{_}->{dot1dStpRootPort} ) {
$attr = "arrowhead=crow";
}
$attr = " [ $attr ]" if $attr;
print $dot_fh sprintf qq{ "%s":%d -> "%s":%d%s\n}, $e->[0], $e->[2], $e->[1], $e->[3], $attr;
}
}
sub git_commit {
return if $debug;
my $file = shift;
$file =~ s{/dev/shm/}{} && warn "## git_commit $file";
system qq{git -C /dev/shm add -f $file && git -C /dev/shm commit -m "\$( date +%Y-%m-%d ) $file" $file };
}
warn "## mac_name_use ",dump( $mac_name_use );
my @mac_name_use_zero = sort grep { $mac_name_use->{$_} == 0 } keys %$mac_name_use;
warn "# mac_name_use_zero=",dump( \@mac_name_use_zero );
open(my $zero_fh, '>', '/dev/shm/mac_name_use.0');
print $zero_fh "$_\n" foreach map {
s/^\w+\s//; $_; # remote msw_ prefix and leave only mac
} @mac_name_use_zero;
close($zero_fh);
git_commit 'mac_name_use.0';
print $dot_fh qq|
}
|;
close($dot_fh);
git_commit 'network.dot';
system "dot -Tsvg /dev/shm/network.dot > /var/www/network.svg";
#system 'git -C /dev/shm commit -m $( date +%Y-%m-%d ) network.dot' if ! $debug;
warn "# strange neihbours:";
system "grep -f /dev/shm/mac_name_use.0 /dev/shm/neighbors.tab";