dump stats on HUP signal
[dell-switch] / syslog-count-link.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use Data::Dump qw(dump);
6
7 my $stat;
8
9 sub print_stats {
10         foreach my $host ( sort keys %$stat ) {
11                 foreach my $port ( sort {
12                         my $a1 = $a; $a1 =~ s/\D+//g;
13                         my $b1 = $b; $b1 =~ s/\D+//g;
14                         no warnings;
15                         $a1 <=> $b1;
16                 } keys %{ $stat->{$host} } ) {
17                         next if $port =~ m/^_/;
18                         printf "%s %s:%s %d\n", $host, $port, $stat->{$host}->{$port}, $stat->{$host}->{_count}->{$port};
19                 }
20         }
21 }
22
23 $SIG{HUP} = sub {
24         print_stats();
25 };
26
27 warn "kill -HUP $$  # to dump stats\n";
28
29 my $host_re = '[\w-]+';
30 my $port_re = '[\w/]+';
31
32 while(<>) {
33         chomp;
34         s/[\r\n]+//;
35         next if m/^$/;
36
37         ## Dell
38         if ( m/(\S+)\s%LINK-[IW]-(\w+):\s*(\w+)/ ) {
39                 my ($host,$state,$port) = ($1,$2,$3);
40                 $stat->{$host}->{$port} .= substr($state,0,1);
41                 $stat->{$host}->{_count}->{$port} += $state =~ m/Up/ ? 1 : -1;
42         } elsif ( m/(\S+)\s%STP-W-PORTSTATUS:\s([\w\/]+): STP status (\w+)/ ) {
43                 my ($host,$port,$state) = ($1,$2,$3);
44                 $stat->{$host}->{$port} .= '-';
45                 $stat->{$host}->{_count}->{$port} += $state =~ m/F/ ? 1 : -1;
46
47
48         ## Mikrotik
49         } elsif ( m/LINK - (\w+) - Hostname: <($host_re)>, ($port_re)/ ) {
50                 my ($state, $host, $port ) = ($1,$2,$3);
51                 $stat->{$host}->{$port} .= substr($state,0,1);
52                 $stat->{$host}->{_count}->{$port} += $state =~ m/U/ ? 1 : -1;
53         } elsif ( m/STP - PORTSTATUS - Hostname: <($host_re)>,($port_re): STP status (\w+)/ ) {
54                 my ($host,$port,$state) = ($1,$2,$3);
55                 $stat->{$host}->{$port} .= '-';
56                 $stat->{$host}->{_count}->{$port} += $state =~ m/F/ ? 1 : -1;
57
58
59         } elsif ( m'==> /var/log/' ) {
60                 # ignore tail output
61         } else {
62                 warn "IGNORE: [$_]\n";
63         }
64 }
65
66
67
68 warn "# stat = ", dump($stat);
69 print_stats;
70
71