Skip to content

Instantly share code, notes, and snippets.

@osamu
Created August 3, 2016 03:36
Show Gist options
  • Save osamu/a99e777115ab0aea42d3f3d757befb7e to your computer and use it in GitHub Desktop.
Save osamu/a99e777115ab0aea42d3f3d757befb7e to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Time::HiRes qw(usleep);
use POSIX qw/strftime/;
use Data::Dumper;
my $interval = 1000; # milisecond
my $active_conn = 0;
my $passive_conn = 0 ;
my $retrans = 0;
my $f_retrans = 0;
my ($prev_rx, $prev_tx) = (0,0);
$ENV{LANG} = 'C';
my $interface = $ARGV[0];
while ( 1 ) {
my ($active_conn_rate, $passive_conn_rate, $retrans_rate, $f_retrans_rate, $estab ,$rx,$tx) = (0,0,0,0,0,0);
open(my $stat ,"netstat -st 2>/dev/null|");
while ( my $response = <$stat> ) {
if ( $response =~ /(\d+) active connections openings/ ) {
$active_conn_rate = ($1 - $active_conn)/($interval/1000);
$active_conn = $1;
}
if ($response =~/(\d+) passive connection openings/){
$passive_conn_rate = ($1 - $passive_conn)/($interval/1000);
$passive_conn = $1;
}
if ($response =~ /(\d+) connections established/){
$estab = $1;
}
if ($response =~ /(\d+) segments retransmited/) {
$retrans_rate = ($1 - $retrans)/ ($interval/1000);
$retrans = $1;
}
if ($response =~ /TCPLostRetransmit:\s*(\d+)/) {
}
if ($response =~/(\d) fast retransmits/) {
$f_retrans_rate = ($1 - $f_retrans)/($interval/1000);
$f_retrans = $1;
}
#29337819 forward retransmits
#1042051300 retransmits in slow start
#15964638 SACK retransmits failed
}
open(my $ifconfig, "cat /proc/net/dev|");
while( my $line = <$ifconfig>) {
if ( $line =~ /$interface:(.*)/ ) {
my @param = split(/\s+/,$1);
$rx = ($param[1] -$prev_rx)*8.0/1024/1024;
$tx = ($param[9]- $prev_tx)*8.0/1024/1024;
$prev_rx = $param[1];
$prev_tx = $param[9];
}
}
printf "%s : ActiveConn: %d/sec, PassiveConn: %d/sec, RetransRate:%d/sec, FastRetrans:%d/sec, Estab:%d, Rx: %0.2fMbps, Tx:%0.2fMbps \n", strftime("%T",localtime), $active_conn_rate, $passive_conn_rate, $retrans_rate, $f_retrans_rate,$estab, $rx, $tx;
close $stat;
usleep($interval * 1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment