5 # count Dell and Microtik syslog for port and stp events
8 # sudo cat /var/log/switch/sw-[a-z][0-9]*.log | ./syslog-count-link.pl | less -S
10 ## tail logs and report status on kill -HUP
11 # sudo tail -f /var/log/switch/sw-[a-z][0-9]*.log | ./syslog-count-link.pl &
13 use Data::Dump qw(dump);
16 $name =~ s/.*\/([^\/]+)$/$1/;
18 my $dir = "/dev/shm/$name";
19 mkdir $dir unless -e $dir;
24 open(my $fh, '>', "$dir/stats");
26 foreach my $host ( sort keys %$stat ) {
27 foreach my $port ( sort {
28 my $a1 = $a; $a1 =~ s/\D+//g;
29 my $b1 = $b; $b1 =~ s/\D+//g;
32 } keys %{ $stat->{$host} } ) {
33 next if $port =~ m/^_/;
34 my $out = sprintf "%s %s:%s %d\n", $host, $port, $stat->{$host}->{$port}, $stat->{$host}->{_count}->{$port};
47 warn "kill -HUP $$ # to dump stats\n";
49 open(my $fh, '>', "$dir/pid");
55 my $host_re = '[\w-]+';
56 my $port_re = '[\w/]+';
64 if ( m/(\S+)\s%LINK-[IW]-(\w+):\s*(\w+)/ ) {
65 my ($host,$state,$port) = ($1,$2,$3);
66 $stat->{$host}->{$port} .= substr($state,0,1);
67 $stat->{$host}->{_count}->{$port} += $state =~ m/Up/ ? 1 : -1;
68 } elsif ( m/(\S+)\s%STP-W-PORTSTATUS:\s([\w\/]+): STP status (\w+)/ ) {
69 my ($host,$port,$state) = ($1,$2,$3);
70 $stat->{$host}->{$port} .= '-';
71 $stat->{$host}->{_count}->{$port} += $state =~ m/F/ ? 1 : -1;
75 } elsif ( m/LINK - (\w+) - Hostname: <($host_re)>, ($port_re)/ ) {
76 my ($state, $host, $port ) = ($1,$2,$3);
77 $stat->{$host}->{$port} .= substr($state,0,1);
78 $stat->{$host}->{_count}->{$port} += $state =~ m/U/ ? 1 : -1;
79 } elsif ( m/STP - PORTSTATUS - Hostname: <($host_re)>,($port_re): STP status (\w+)/ ) {
80 my ($host,$port,$state) = ($1,$2,$3);
81 $stat->{$host}->{$port} .= '-';
82 $stat->{$host}->{_count}->{$port} += $state =~ m/F/ ? 1 : -1;
85 } elsif ( m'==> /var/log/' ) {
88 warn "IGNORE: [$_]\n";
94 warn "# stat = ", dump($stat);