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);
16 my $timeout = $ENV{TIMEOUT} || 60; # forget switch after sec
19 $name =~ s/.*\/([^\/]+)$/$1/;
21 my $dir = "/dev/shm/$name";
22 mkdir $dir unless -e $dir;
27 open(my $fh, '>', "$dir/stats");
29 foreach my $host ( sort keys %$stat ) {
30 foreach my $port ( sort {
31 my $a1 = $a; $a1 =~ s/\D+//g;
32 my $b1 = $b; $b1 =~ s/\D+//g;
35 } keys %{ $stat->{$host} } ) {
36 next if $port =~ m/^_/;
37 my $dt = time() - $stat->{$host}->{$port}->[0];
38 if ( $dt > $timeout ) {
39 delete $stat->{$host}->{$port};
42 my $out = sprintf "%-12s %-4s %-2d %s\n", $host, $port, $dt, $stat->{$host}->{$port}->[1];
52 if ( -e "$dir/reset" && unlink "$dir/reset" ) {
54 warn "# reset stats\n";
64 warn "kill -HUP $$ # to dump stats\n";
66 open(my $fh, '>', "$dir/pid");
72 my ( $host, $port, $state ) = @_;
73 if ( ! exists $stat->{$host}->{$port} ) {
74 $stat->{$host}->{$port} = [-1, $state];
76 $stat->{$host}->{$port}->[1] .= $state;
78 $stat->{$host}->{$port}->[0] = time();
79 #warn "# stat_host_port ",dump($stat);
83 my $host_re = '[\w-]+';
84 my $port_re = '[\w/]+';
93 next if m/%PIX-/; # ignore PIX logs
96 if ( m/(\S+)\s%LINK-[IW]-(\w+):\s*(\w+)/ ) {
97 my ($host,$state,$port) = ($1,$2,$3);
98 stat_host_port( $host, $port, substr($state,0,1) );
99 } elsif ( m/(\S+)\s%STP-W-PORTSTATUS:\s([\w\/]+): STP status (\w+)/ ) {
100 my ($host,$port,$state) = ($1,$2,$3);
101 stat_host_port( $host, $port, '-' );
105 } elsif ( m/LINK - (\w+) - Hostname: <($host_re)>, ($port_re)/ ) {
106 my ($state, $host, $port ) = ($1,$2,$3);
107 stat_host_port( $host, $port, substr($state,0,1) );
108 } elsif ( m/STP - PORTSTATUS - Hostname: <($host_re)>,($port_re): STP status (\w+)/ ) {
109 my ($host,$port,$state) = ($1,$2,$3);
110 stat_host_port( $host, $port, '-' );
114 } elsif ( m/($host_re) \w+: ([\w\-]+) link (\w+)/ ) {
115 my ($host, $port, $state ) = ($1,$2,$3);
116 stat_host_port( $host, $port, substr($state,0,1) );
119 } elsif ( m'==> /var/log/' ) {
123 warn "IGNORE: [$_]\n";
127 if ( -e "$dir/dump" ) {
128 print "### ",strftime("%Y-%m-%d %H:%M:%S",localtime(time)), "\n";
135 warn "# stat = ", dump($stat);