Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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