remove interface
[dell-switch] / neighbours.pl
index e799ab3..e9d960b 100755 (executable)
@@ -2,11 +2,18 @@
 use warnings;
 use strict;
 use autodie;
+use Data::Dump qw(dump);
 
-# ./sw-name-mac.sh
-# ./sw-names | xargs -i ./dell-switch.pl {} 'show lldp neighbors'
+if ( $ARGV[0] eq 'update' ) {
+       print <<__SH__
+./sw-name-mac.sh
+./sw-names | xargs -i ./dell-switch.pl {} 'show lldp neighbors'
+/home/dpavlin/mikrotik-switch/m-neighbour
+__SH__
+       ;
+       exit 0;
+};
 
-use Data::Dump qw(dump);
 
 my $debug = $ENV{DEBUG} || 0;
 
@@ -18,11 +25,18 @@ while(<$f>) {
        #my ( $ip, $name, $mac ) = split(/ /,$_);
        my ( $name, $mac ) = split(/ /,$_);
        $mac = lc($mac);
-       $mac2name->{$mac} = $name;
+       if ( defined $mac2name->{$mac} ) {
+               if ( $mac2name->{$mac} ne $name ) {
+                       warn "ERROR: GOT $mac with $mac2name->{$mac} and now trying to overwrite it with $name\n";
+               }
+       } else {
+               $mac2name->{$mac} = $name;
+       }
 }
 
 sub mac2name {
        my ( $mac, $name ) = @_;
+       return unless defined $mac;
 
        $mac = lc($mac);
 
@@ -36,6 +50,10 @@ sub mac2name {
 
 warn "# mac2name = ",dump($mac2name) if $debug;
 
+open(my $n_fh, '>', '/dev/shm/neighbors.tab');
+open(my $html_fh, '>', '/var/www/neighbors.html');
+print $html_fh qq{<table>\n};
+
 # parse Dell switch lldp neighbors output
 
 foreach my $file ( glob('log/*lldp*') ) {
@@ -141,13 +159,21 @@ foreach my $file ( glob('log/*lldp*') ) {
        foreach my $p ( @ports ) {
                next if ( $p->[1] eq lc($p->[2]) && $p->[3] eq '' ); # FIXME hosts?
                print "$name ", join(' | ', @$p ), "\n";
+               print $n_fh "$name\t", join("\t", @$p ), "\n";
+               print $html_fh "<tr><td>$name</td><td>", join("</td><td>", @$p ), "</td></tr>\n";
        }
 }
 
 # prase MikroTik /ip neighbor print detail terse
 
-foreach my $file ( glob('../mikrotik-switch/out/*neighbor*'), glob('../tilera/out/*neighbor*') ) {
-       my $name = $1 if $file =~ m{out/([\w\-]+)\.ip neighbor};
+my $patt = 'ip neighbor print detail terse';
+$patt =~ s{\s}{\\ }g; # escape spaces
+my @files = ( map { $_ . '/*' . $patt } qw( ../mikrotik-switch/out/ ../tilera/out/ ));
+@files = map { glob $_ } @files;
+warn "XXX $patt files=",dump(\@files) if $debug;
+
+foreach my $file ( @files ) {
+       my $name = $1 if $file =~ m{out/+([\w\-]+)\.ip neighbor};
        print "## [$name] file $file\n" if $debug;
        open(my $f, '<', $file);
        while(<$f>) {
@@ -177,5 +203,9 @@ foreach my $file ( glob('../mikrotik-switch/out/*neighbor*'), glob('../tilera/ou
                ( $v[1], $v[3] ) = mac2name( $v[1], $v[3] );
 
                print "$name ", join(' | ', @v), $/;
+               print $n_fh "$name\t", join("\t", @v ), "\n";
+               print $html_fh "<tr><td>$name</td><td>", join("</td><td>", @v ), "</td></tr>\n";
        }
 }
+
+print $html_fh qq{</table>\n};