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/]+';
65 if ( m/(\S+)\s%LINK-[IW]-(\w+):\s*(\w+)/ ) {
66 my ($host,$state,$port) = ($1,$2,$3);
67 $stat->{$host}->{$port} .= substr($state,0,1);
68 $stat->{$host}->{_count}->{$port} += $state =~ m/Up/ ? 1 : -1;
69 } elsif ( m/(\S+)\s%STP-W-PORTSTATUS:\s([\w\/]+): STP status (\w+)/ ) {
70 my ($host,$port,$state) = ($1,$2,$3);
71 $stat->{$host}->{$port} .= '-';
72 $stat->{$host}->{_count}->{$port} += $state =~ m/F/ ? 1 : -1;
76 } elsif ( m/LINK - (\w+) - Hostname: <($host_re)>, ($port_re)/ ) {
77 my ($state, $host, $port ) = ($1,$2,$3);
78 $stat->{$host}->{$port} .= substr($state,0,1);
79 $stat->{$host}->{_count}->{$port} += $state =~ m/U/ ? 1 : -1;
80 } elsif ( m/STP - PORTSTATUS - Hostname: <($host_re)>,($port_re): STP status (\w+)/ ) {
81 my ($host,$port,$state) = ($1,$2,$3);
82 $stat->{$host}->{$port} .= '-';
83 $stat->{$host}->{_count}->{$port} += $state =~ m/F/ ? 1 : -1;
86 } elsif ( m'==> /var/log/' ) {
89 warn "IGNORE: [$_]\n";
92 if ( -e "$dir/reset" && unlink "$dir/reset" ) {
94 warn "# reset stats\n";
95 } elsif ( -e "$dir/dump" ) {
96 print "### ",strftime("%Y-%m-%d %H:%M:%S",localtime(time)), "\n";
103 warn "# stat = ", dump($stat);