-
Notifications
You must be signed in to change notification settings - Fork 2
/
syslog-count-link.pl
executable file
·145 lines (111 loc) · 3.18 KB
/
syslog-count-link.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/perl
use warnings;
use strict;
# count Dell and Microtik syslog for port and stp events
## report all logs:
# sudo cat /var/log/switch/sw-[a-z][0-9]*.log | ./syslog-count-link.pl | less -S
#
## tail logs and report status on kill -HUP
# sudo tail -f /var/log/switch/sw-[a-z][0-9]*.log | ./syslog-count-link.pl &
use Data::Dump qw(dump);
use POSIX qw(strftime);
my $timeout = $ENV{TIMEOUT} || 60; # forget switch after sec
# timeout should be smaller than stp refresh inverval or
# stp messages will accumulate forever
my $name = $0;
$name =~ s/.*\/([^\/]+)$/$1/;
$name =~ s/\.pl$//;
my $dir = "/dev/shm/$name";
mkdir $dir unless -e $dir;
our $stat;
sub print_stats {
open(my $fh, '>', "$dir/stats");
foreach my $host ( sort keys %$stat ) {
foreach my $port ( sort {
my $a1 = $a; $a1 =~ s/\D+//g;
my $b1 = $b; $b1 =~ s/\D+//g;
no warnings;
$a1 <=> $b1;
} keys %{ $stat->{$host} } ) {
next if $port =~ m/^_/;
my $dt = time() - $stat->{$host}->{$port}->[0];
if ( $dt > $timeout ) {
delete $stat->{$host}->{$port};
next;
}
my $out = sprintf "%-12s %-8s %-2d %s\n", $host, $port, $dt, $stat->{$host}->{$port}->[1];
print $out;
print $fh $out;
}
}
close($fh);
}
sub reset_stats {
if ( -e "$dir/reset" && unlink "$dir/reset" ) {
$stat = {};
warn "# reset stats\n";
}
}
$SIG{HUP} = sub {
print_stats();
reset_stats;
};
warn "kill -HUP $$ # to dump stats\n";
{
open(my $fh, '>', "$dir/pid");
print $fh $$;
close($fh);
}
sub stat_host_port {
my ( $host, $port, $state ) = @_;
if ( ! exists $stat->{$host}->{$port} ) {
$stat->{$host}->{$port} = [-1, $state];
} else {
$stat->{$host}->{$port}->[1] .= $state;
}
$stat->{$host}->{$port}->[0] = time();
#warn "# stat_host_port ",dump($stat);
}
my $host_re = '[\w-]+';
my $port_re = '[\w/]+';
while(<>) {
chomp;
s/[\r\n]+//;
reset_stats;
next if m/^$/;
next if m/%PIX-/; # ignore PIX logs
## Dell old
if ( m/(\S+)\s%LINK-[IW]-(\w+):\s*(\w+)/ ) {
my ($host,$state,$port) = ($1,$2,$3);
stat_host_port( $host, $port, substr($state,0,1) );
} elsif ( m/(\S+)\s%STP-W-PORTSTATUS:\s([\w\/]+)(?: of instance \d+)?: STP status (\w+)/ ) {
my ($host,$port,$state) = ($1,$2,substr($3,0,1) );
stat_host_port( $host, $port, $state =~ m/f/i ? '-' : $state );
## Dell new
} elsif ( m/LINK - (\w+) - Hostname: <($host_re)>, ($port_re)/ ) {
my ($state, $host, $port ) = ($1,$2,$3);
stat_host_port( $host, $port, substr($state,0,1) );
} elsif ( m/STP - PORTSTATUS - Hostname: <($host_re)>,($port_re): STP status (\w+)/ ) {
my ($host,$port,$state) = ($1,$2,$3);
stat_host_port( $host, $port, '-' );
## Mikrotik
} elsif ( m/($host_re) \w+: ([\w\-]+) link (\w+)/ ) {
my ($host, $port, $state ) = ($1,$2,$3);
stat_host_port( $host, $port, substr($state,0,1) );
} elsif ( m/($host_re) TRAPMGR.* %% Spanning Tree Topology Change: (\d+)/ ) {
my ( $host, $state ) = ( $1, $2 );
stat_host_port( $host, 'STP', $2 );
} elsif ( m'==> /var/log/' ) {
# ignore tail output
next;
} else {
warn "IGNORE: [$_]\n";
next;
}
if ( -e "$dir/dump" || $ENV{DUMP} ) {
print "### ",strftime("%Y-%m-%d %H:%M:%S",localtime(time)), "\n";
print_stats;
}
}
warn "# stat = ", dump($stat);
print_stats;