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};
48 warn "kill -HUP $$ # to dump stats\n";
50 open(my $fh, '>', "$dir/pid");
56 my $host_re = '[\w-]+';
57 my $port_re = '[\w/]+';
63 next if m/%PIX-/; # ignore PIX logs
66 if ( m/(\S+)\s%LINK-[IW]-(\w+):\s*(\w+)/ ) {
67 my ($host,$state,$port) = ($1,$2,$3);
68 $stat->{$host}->{$port} .= substr($state,0,1);
69 $stat->{$host}->{_count}->{$port} += $state =~ m/Up/ ? 1 : -1;
70 } elsif ( m/(\S+)\s%STP-W-PORTSTATUS:\s([\w\/]+): STP status (\w+)/ ) {
71 my ($host,$port,$state) = ($1,$2,$3);
72 $stat->{$host}->{$port} .= '-';
73 $stat->{$host}->{_count}->{$port} += $state =~ m/F/ ? 1 : -1;
77 } elsif ( m/LINK - (\w+) - Hostname: <($host_re)>, ($port_re)/ ) {
78 my ($state, $host, $port ) = ($1,$2,$3);
79 $stat->{$host}->{$port} .= substr($state,0,1);
80 $stat->{$host}->{_count}->{$port} += $state =~ m/U/ ? 1 : -1;
81 } elsif ( m/STP - PORTSTATUS - Hostname: <($host_re)>,($port_re): STP status (\w+)/ ) {
82 my ($host,$port,$state) = ($1,$2,$3);
83 $stat->{$host}->{$port} .= '-';
84 $stat->{$host}->{_count}->{$port} += $state =~ m/F/ ? 1 : -1;
87 } elsif ( m'==> /var/log/' ) {
90 warn "IGNORE: [$_]\n";
93 if ( -e "$dir/reset" && unlink "$dir/reset" ) {
95 warn "# reset stats\n";
96 } elsif ( -e "$dir/dump" ) {
97 print "### ",strftime("%Y-%m-%d %H:%M:%S",localtime(time)), "\n";
104 warn "# stat = ", dump($stat);