Capturing Linux network statistics – perl script

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...
  • Capture Linux network statistics

Example capture output is below.

collisions                     :               0       0       0
multicast                      :            2101       0       0
rx_bytes                       :   1079497308853   15334   12768
rx_compressed                  :               0       0       0
rx_crc_errors                  :               0       0       0
rx_dropped                     :               0       0       0
rx_errors                      :               0       0       0
rx_fifo_errors                 :               0       0       0
rx_frame_errors                :               0       0       0
rx_length_errors               :               0       0       0
rx_missed_errors               :             897       0       0
rx_over_errors                 :               0       0       0
rx_packets                     :      1921006922     175     135
tx_aborted_errors              :               0       0       0
tx_bytes                       :   2413670939512   22024   21054
tx_carrier_errors              :               0       0       0
tx_compressed                  :               0       0       0
tx_dropped                     :               0       0       0
tx_errors                      :               0       0       0
tx_fifo_errors                 :               0       0       0
tx_heartbeat_errors            :               0       0       0
tx_packets                     :      2450565916     146     114
tx_window_errors               :               0       0       0
eth0                           : 00:11:17.002477      1s      5s     15s     60s

Perl script source is below

#!/usr/bin/perl
 
use strict;
use warnings;
 
use POSIX qw(strftime);
use Time::HiRes qw(gettimeofday usleep);
 
my $dev = @ARGV ? shift : 'eth0';
my $dir = "/sys/class/net/$dev/statistics";
my %stats = do {
    opendir +(my $dh), $dir;
    local @_ = readdir $dh;
    closedir $dh;
    map +($_, []), grep !/^\.\.?$/, @_;
};
 
if (-t STDOUT) {
    while (1) {
        print "\033[H\033[J", run();
        my ($time, $us) = gettimeofday();
        my ($sec, $min, $hour) = localtime $time;
        {
            local $| = 1;
            printf '%-31.31s: %02d:%02d:%02d.%06d%8s%8s%8s%8s',
            $dev, $hour, $min, $sec, $us, qw(1s 5s 15s 60s)
        }
        usleep($us ? 1000000 - $us : 1000000);
    }
}
else {print run()}
 
sub run {
    map {
        chomp (my ($stat) = slurp("$dir/$_"));
        my $line = sprintf '%-31.31s:%16.16s', $_, $stat;
        $line .= sprintf '%8.8s', int (($stat - $stats{$_}->[0]) / 1)
            if @{$stats{$_}} > 0;
        $line .= sprintf '%8.8s', int (($stat - $stats{$_}->[4]) / 5)
            if @{$stats{$_}} > 4;
        $line .= sprintf '%8.8s', int (($stat - $stats{$_}->[14]) / 15)
            if @{$stats{$_}} > 14;
        $line .= sprintf '%8.8s', int (($stat - $stats{$_}->[59]) / 60)
            if @{$stats{$_}} > 59;
        unshift @{$stats{$_}}, $stat;
        pop @{$stats{$_}} if @{$stats{$_}} > 60;
        "$line\n";
    } sort keys %stats;
}
 
sub slurp {
    local @ARGV = @_;
    local @_ = <>;
    @_;
}

Leave a Reply

avatar
3000
  Subscribe  
Notify of