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);
14 use POSIX qw(strftime);
17 $name =~ s/.*\/([^\/]+)$/$1/;
19 my $dir = "/dev/shm/$name";
20 mkdir $dir unless -e $dir;
25 open(my $fh, '>', "$dir/stats");
27 foreach my $host ( sort keys %$stat ) {
28 foreach my $port ( sort {
29 my $a1 = $a; $a1 =~ s/\D+//g;
30 my $b1 = $b; $b1 =~ s/\D+//g;
33 } keys %{ $stat->{$host} } ) {
34 next if $port =~ m/^_/;
35 my $out = sprintf "%s %s:%s %d\n", $host, $port, $stat->{$host}->{$port}, $stat->{$host}->{_count}->{$port};
45 if ( -e "$dir/reset" && unlink "$dir/reset" ) {
47 warn "# reset stats\n";
57 warn "kill -HUP $$ # to dump stats\n";
59 open(my $fh, '>', "$dir/pid");
66 my $host_re = '[\w-]+';
67 my $port_re = '[\w/]+';
76 next if m/%PIX-/; # ignore PIX logs
79 if ( m/(\S+)\s%LINK-[IW]-(\w+):\s*(\w+)/ ) {
80 my ($host,$state,$port) = ($1,$2,$3);
81 $stat->{$host}->{$port} .= substr($state,0,1);
82 $stat->{$host}->{_count}->{$port} += $state =~ m/Up/ ? 1 : -1;
83 } elsif ( m/(\S+)\s%STP-W-PORTSTATUS:\s([\w\/]+): STP status (\w+)/ ) {
84 my ($host,$port,$state) = ($1,$2,$3);
85 $stat->{$host}->{$port} .= '-';
86 $stat->{$host}->{_count}->{$port} += $state =~ m/F/ ? 1 : -1;
90 } elsif ( m/LINK - (\w+) - Hostname: <($host_re)>, ($port_re)/ ) {
91 my ($state, $host, $port ) = ($1,$2,$3);
92 $stat->{$host}->{$port} .= substr($state,0,1);
93 $stat->{$host}->{_count}->{$port} += $state =~ m/U/ ? 1 : -1;
94 } elsif ( m/STP - PORTSTATUS - Hostname: <($host_re)>,($port_re): STP status (\w+)/ ) {
95 my ($host,$port,$state) = ($1,$2,$3);
96 $stat->{$host}->{$port} .= '-';
97 $stat->{$host}->{_count}->{$port} += $state =~ m/F/ ? 1 : -1;
100 } elsif ( m'==> /var/log/' ) {
103 warn "IGNORE: [$_]\n";
106 if ( -e "$dir/dump" ) {
107 print "### ",strftime("%Y-%m-%d %H:%M:%S",localtime(time)), "\n";
114 warn "# stat = ", dump($stat);