added reset and dump control files
[dell-switch] / syslog-count-link.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 # count Dell and Microtik syslog for port and stp events
6
7 ## report all logs:
8 # sudo cat /var/log/switch/sw-[a-z][0-9]*.log | ./syslog-count-link.pl | less -S
9 #
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 &
12
13 use Data::Dump qw(dump);
14 use POSIX qw(strftime);
15
16 my $name = $0;
17 $name =~ s/.*\/([^\/]+)$/$1/;
18 $name =~ s/\.pl$//;
19 my $dir = "/dev/shm/$name";
20 mkdir $dir unless -e $dir;
21
22 my $stat;
23
24 sub print_stats {
25         open(my $fh, '>', "$dir/stats");
26
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;
31                         no warnings;
32                         $a1 <=> $b1;
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};
36                         print $out;
37                         print $fh $out;
38                 }
39         }
40
41         close($fh);
42 }
43
44 $SIG{HUP} = sub {
45         print_stats();
46 };
47
48 warn "kill -HUP $$  # to dump stats\n";
49 {
50         open(my $fh, '>', "$dir/pid");
51         print $fh $$;
52         close($fh);
53 }
54
55
56 my $host_re = '[\w-]+';
57 my $port_re = '[\w/]+';
58
59 while(<>) {
60         chomp;
61         s/[\r\n]+//;
62         next if m/^$/;
63
64         ## Dell
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;
73
74
75         ## Mikrotik
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;
84
85
86         } elsif ( m'==> /var/log/' ) {
87                 # ignore tail output
88         } else {
89                 warn "IGNORE: [$_]\n";
90         }
91
92         if ( -e "$dir/reset" && unlink "$dir/reset" ) {
93                 $stat = {};
94                 warn "# reset stats\n";
95         } elsif ( -e "$dir/dump" ) {
96                 print "### ",strftime("%Y-%m-%d %H:%M:%S",localtime(time)), "\n";
97                 print_stats;
98         }
99 }
100
101
102
103 warn "# stat = ", dump($stat);
104 print_stats;
105
106