Skip to content

Instantly share code, notes, and snippets.

@kazeburo
Created January 22, 2020 01:34
Show Gist options
  • Save kazeburo/57e7148efa190c97381ea08f2a675db5 to your computer and use it in GitHub Desktop.
Save kazeburo/57e7148efa190c97381ea08f2a675db5 to your computer and use it in GitHub Desktop.
#!/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