count Dell and Microtick syslog port and stp changes
[dell-switch] / syslog-count-link.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use Data::Dump qw(dump);
6
7 my $stat;
8
9 my $host_re = '[\w-]+';
10 my $port_re = '[\w/]+';
11
12 while(<>) {
13         chomp;
14
15         ## Dell
16         if ( m/(\S+)\s%LINK-[IW]-(\w+):\s*(\w+)/ ) {
17                 my ($host,$state,$port) = ($1,$2,$3);
18                 $stat->{$host}->{$port} .= substr($state,0,1);
19                 $stat->{$host}->{_count}->{$port} += $state =~ m/Up/ ? 1 : -1;
20         } elsif ( m/(\S+)\s%STP-W-PORTSTATUS:\s([\w\/]+): STP status (\w+)/ ) {
21                 my ($host,$port,$state) = ($1,$2,$3);
22                 $stat->{$host}->{$port} .= '-';
23                 $stat->{$host}->{_count}->{$port} += $state =~ m/F/ ? 1 : -1;
24
25
26         ## Mikrotik
27         } elsif ( m/LINK - (\w+) - Hostname: <($host_re)>, ($port_re)/ ) {
28                 my ($state, $host, $port ) = ($1,$2,$3);
29                 $stat->{$host}->{$port} .= substr($state,0,1);
30                 $stat->{$host}->{_count}->{$port} += $state =~ m/U/ ? 1 : -1;
31         } elsif ( m/STP - PORTSTATUS - Hostname: <($host_re)>,($port_re): STP status (\w+)/ ) {
32                 my ($host,$port,$state) = ($1,$2,$3);
33                 $stat->{$host}->{$port} .= '-';
34                 $stat->{$host}->{_count}->{$port} += $state =~ m/F/ ? 1 : -1;
35
36
37         } else {
38                 warn "IGNORE: [$_]\n";
39         }
40 }
41
42 warn "# stat = ", dump($stat);
43
44 foreach my $host ( sort keys %$stat ) {
45         foreach my $port ( sort {
46                 my $a1 = $a; $a1 =~ s/\D+//g;
47                 my $b1 = $b; $b1 =~ s/\D+//g;
48                 $a1 <=> $b1;
49         } keys %{ $stat->{$host} } ) {
50                 next if $port =~ m/^_/;
51                 printf "%s %s:%s %d\n", $host, $port, $stat->{$host}->{$port}, $stat->{$host}->{_count}->{$port};
52         }
53 }