#!/usr/bin/perl -w # vim: set sw=4 ts=4 si et: # Written by: Guido Socher, copyright: freeware, no warranty # # This software parses the output of the Linux command ifconfig. # # Version history: # end of 2016: fist version # 2017-04: add support for different versions of ifconfig # 2017-12: add packet size # use strict; use vars qw($opt_t $opt_h); use Getopt::Std; sub help(); my $t=5; # seconds to wait between measurements (int) # getopts("t:h")||exit 1; help() if ($opt_h); help() unless($ARGV[0]); my ($ptx,$prx,$ptxb,$prxb); my ($tx,$rx,$txb,$rxb); my ($txsize,$rxsize); $ptx=0; $prx=0; $ptxb=0; $prxb=0; $tx=0; $rx=0; $txb=0; $rxb=0; my $round=0; if ($opt_t && int($opt_t) >1){ $t=int($opt_t); } #eth2: flags=4163 mtu 1500 # inet 13.0.1.24 netmask 255.0.0.0 broadcast 13.255.255.255 # inet6 fe80::f816:3eff:fec9:2d6c prefixlen 64 scopeid 0x20 # ether fa:16:3e:c9:2d:6c txqueuelen 1000 (Ethernet) # RX packets 1298 bytes 333949 (326.1 KiB) # RX errors 0 dropped 0 overruns 0 frame 0 # TX packets 838 bytes 147957 (144.4 KiB) # TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 # # old format: #ens3 Link encap:Ethernet HWaddr fa:16:3e:20:09:24 # inet addr:1.1.1.8 Bcast:1.1.1.255 Mask:255.255.255.0 # inet6 addr: fe80::f816:3eff:fe20:924/64 Scope:Link # UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 # RX packets:29295707 errors:0 dropped:0 overruns:0 frame:0 # TX packets:260441288 errors:0 dropped:0 overruns:0 carrier:0 # collisions:0 txqueuelen:1000 # RX bytes:53551707969 (53.5 GB) TX bytes:374622075050 (374.6 GB) # print "monitoring interface $ARGV[0], crtl-c to end\n"; while(1){ open(PP,"ifconfig \"$ARGV[0]\"|")||die; while(){ if (/^\s+RX packets\s+(\d+)\s+bytes\s+(\d+)/){ $rx=$1; $rxb=$2; } if (/^\s+TX packets\s+(\d+)\s+bytes\s+(\d+)/){ $tx=$1; $txb=$2; } # old format: if (/^\s+RX packets:(\d+)/){ $rx=$1; } if (/^\s+TX packets:(\d+)/){ $tx=$1; } if (/^\s+RX bytes:(\d+).+TX bytes:(\d+)/){ $rxb=$1; $txb=$2; } } close(PP); if ($round > 0){ $txsize=0; $rxsize=0; if (($tx-$ptx)>0){ $txsize=($txb-$ptxb)/($tx-$ptx); } if (($rx-$prx)>0){ $rxsize=($rxb-$prxb)/($rx-$prx); } printf("tx: %10.6f Mbit/s, %10.3fK pps (avg pkt size=%.1f bytes)\n",($txb-$ptxb)*8/($t * 1000000),($tx-$ptx)/($t * 1000),$txsize); printf("rx: %10.6f Mbit/s, %10.3fK pps (avg pkt size=%.1f bytes)\n",($rxb-$prxb)*8/($t * 1000000),($rx-$prx)/($t * 1000),$rxsize); } $round++; $prx=$rx; $ptx=$tx; $prxb=$rxb; $ptxb=$txb; sleep($t); } # sub help(){ print "ifspeed.pl [-h] [-t intervaltime] interface The ifspeed.pl command runs periodically the command ifconfig and calculates transfer speeds in Mbit/s as well as packets per second (pps). OPTIONS: -h this help -t measurement interval in seconds EXAMPLE: ifspeed.pl -t 10 lo The default measurement interval is $t seconds. Note: you may see \"avg pkt size\" bigger than MTU and this is possible with network cards that support Segmentation Offload (TSO, LSO, GSO) and Receive Offload. This means is that the TCP stack sends a large chunk of data and the NIC breaks it into MSS (less than MTU) size packets. You can print those settings with the command: ethtool -k interface Ethtool command examples to enable offload features: ethtool -K eth0 lro on ethtool -K eth0 tso on \n"; exit; } __END__