b5648972e6a75ed0c70fda766c3f373be5581c45
[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
15 my $name = $0;
16 $name =~ s/.*\/([^\/]+)$/$1/;
17 $name =~ s/\.pl$//;
18 my $dir = "/dev/shm/$name";
19 mkdir $dir unless -e $dir;
20
21 my $stat;
22
23 sub print_stats {
24         open(my $fh, '>', "$dir/stats");
25
26         foreach my $host ( sort keys %$stat ) {
27                 foreach my $port ( sort {
28                         my $a1 = $a; $a1 =~ s/\D+//g;
29                         my $b1 = $b; $b1 =~ s/\D+//g;
30                         no warnings;
31                         $a1 <=> $b1;
32                 } keys %{ $stat->{$host} } ) {
33                         next if $port =~ m/^_/;
34                         my $out = sprintf "%s %s:%s %d\n", $host, $port, $stat->{$host}->{$port}, $stat->{$host}->{_count}->{$port};
35                         print $out;
36                         print $fh $out;
37                 }
38         }
39
40         close($fh);
41 }
42
43 $SIG{HUP} = sub {
44         print_stats();
45 };
46
47 warn "kill -HUP $$  # to dump stats\n";
48 {
49         open(my $fh, '>', "$dir/pid");
50         print $fh $$;
51         close($fh);
52 }
53
54
55 my $host_re = '[\w-]+';
56 my $port_re = '[\w/]+';
57
58 while(<>) {
59         chomp;
60         s/[\r\n]+//;
61         next if m/^$/;
62
63         ## Dell
64         if ( m/(\S+)\s%LINK-[IW]-(\w+):\s*(\w+)/ ) {
65                 my ($host,$state,$port) = ($1,$2,$3);
66                 $stat->{$host}->{$port} .= substr($state,0,1);
67                 $stat->{$host}->{_count}->{$port} += $state =~ m/Up/ ? 1 : -1;
68         } elsif ( m/(\S+)\s%STP-W-PORTSTATUS:\s([\w\/]+): STP status (\w+)/ ) {
69                 my ($host,$port,$state) = ($1,$2,$3);
70                 $stat->{$host}->{$port} .= '-';
71                 $stat->{$host}->{_count}->{$port} += $state =~ m/F/ ? 1 : -1;
72
73
74         ## Mikrotik
75         } elsif ( m/LINK - (\w+) - Hostname: <($host_re)>, ($port_re)/ ) {
76                 my ($state, $host, $port ) = ($1,$2,$3);
77                 $stat->{$host}->{$port} .= substr($state,0,1);
78                 $stat->{$host}->{_count}->{$port} += $state =~ m/U/ ? 1 : -1;
79         } elsif ( m/STP - PORTSTATUS - Hostname: <($host_re)>,($port_re): STP status (\w+)/ ) {
80                 my ($host,$port,$state) = ($1,$2,$3);
81                 $stat->{$host}->{$port} .= '-';
82                 $stat->{$host}->{_count}->{$port} += $state =~ m/F/ ? 1 : -1;
83
84
85         } elsif ( m'==> /var/log/' ) {
86                 # ignore tail output
87         } else {
88                 warn "IGNORE: [$_]\n";
89         }
90 }
91
92
93
94 warn "# stat = ", dump($stat);
95 print_stats;
96
97