Created
January 22, 2020 01:34
-
-
Save kazeburo/57e7148efa190c97381ea08f2a675db5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
sub netstat { | |
my %stats; | |
{ | |
open my $fh, '<', '/proc/net/snmp' or die "$!\n"; | |
my @keys; | |
while (<$fh>) { | |
chomp; | |
if (/^Tcp\:/ && ! @keys) { | |
@keys = split / /; | |
} elsif ( /^Tcp\:/ ) { | |
for my $v (split / /) { | |
$stats{shift @keys} = $v; | |
} | |
} | |
} | |
} | |
{ | |
open my $fh, '<', '/proc/net/netstat' or die "$!\n"; | |
my @keys; | |
while (<$fh>) { | |
chomp; | |
if (/^TcpExt\:/ && ! @keys) { | |
@keys = split / /; | |
} elsif ( /^TcpExt\:/ ) { | |
for my $v (split / /) { | |
$stats{shift @keys} = $v; | |
} | |
} | |
} | |
} | |
return \%stats; | |
} | |
my $prev = netstat(); | |
my @keys = keys %$prev; | |
sleep 1; | |
while (1) { | |
my $cur = netstat(); | |
my @stats; | |
for my $k (@keys) { | |
next unless $k =~ m!(sack|retrans)!i; | |
push @stats, sprintf("%s:%s",$k, $cur->{$k} - $prev->{$k}); | |
} | |
print join("\t", @stats),"\n"; | |
$prev = $cur; | |
sleep 1; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment