Information Technology Grimoire

Version .0.0.1

IT Notes from various projects because I forget, and hopefully they help you too.

Network I/O

Network I/O

There are many great tools for this such as ntop, nload, iperf, netio, iptraf, dstat, iftop - but many vendors do not have these tools installed and your rights might be restricted to install useful tools. Sar might be installed, so you can try this:

sar

root@somedomain:~# sar -n DEV 1 3
Linux 5.15.0-71-generic (somedomain.com)    05/15/23        _x86_64_        (2 CPU)

12:31:09        IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s   %ifutil
12:31:10           lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
12:31:10         eth0      4.00      3.00      0.23      0.27      0.00      0.00      0.00      0.00
12:31:10         eth1      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

12:31:10        IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s   %ifutil
12:31:11           lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
12:31:11         eth0      2.00      2.00      0.13      0.92      0.00      0.00      0.00      0.00
12:31:11         eth1      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

12:31:11        IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s   %ifutil
12:31:12           lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
12:31:12         eth0      2.00      2.00      0.13      0.92      0.00      0.00      0.00      0.00
12:31:12         eth1      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

Average:        IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s   %ifutil
Average:           lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:         eth0      2.67      2.33      0.16      0.70      0.00      0.00      0.00      0.00
Average:         eth1      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

Bash

#!/bin/bash
IF=$1
if [ -z "$IF" ]; then 
IF=`ls -1 /sys/class/net/ | head -1` 
fi
RXPREV=-1
TXPREV=-1
echo "Listening $IF..."
while [ 1 == 1 ] ; do
RX=`cat /sys/class/net/${IF}/statistics/rx_bytes`
TX=`cat /sys/class/net/${IF}/statistics/tx_bytes`
if [ $RXPREV -ne -1 ] ; then
let BWRX=$RX-$RXPREV
let BWTX=$TX-$TXPREV
echo "Received: $BWRX B/s Sent: $BWTX B/s"
fi
RXPREV=$RX
TXPREV=$TX
sleep 1
done

it will show results like this:

Listening eth0...
Received: 248 B/s    Sent: 50 B/s
Received: 66 B/s    Sent: 142 B/s
Received: 66 B/s    Sent: 142 B/s
Received: 66 B/s    Sent: 142 B/s
Received: 166 B/s    Sent: 242 B/s
Received: 66 B/s    Sent: 142 B/s
Received: 66 B/s    Sent: 142 B/s
Received: 132 B/s    Sent: 236 B/s
Received: 132 B/s    Sent: 236 B/s
Received: 132 B/s    Sent: 236 B/s
Received: 132 B/s    Sent: 236 B/s
Received: 132 B/s    Sent: 236 B/s
Received: 132 B/s    Sent: 236 B/s

Perl

#!/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 @_ = <>;
    @_;
}

Which produces this:

collisions                     :               0       0       0       0       0
multicast                      :               0       0       0       0       0
rx_bytes                       :      4224015510     166      86     139     284
rx_compressed                  :               0       0       0       0       0
rx_crc_errors                  :               0       0       0       0       0
rx_dropped                     :               0       0       0       0       0
rx_errors                      :               0       0       0       0       0
rx_fifo_errors                 :               0       0       0       0       0
rx_frame_errors                :               0       0       0       0       0
rx_length_errors               :               0       0       0       0       0
rx_missed_errors               :               0       0       0       0       0
rx_nohandler                   :               0       0       0       0       0
rx_over_errors                 :               0       0       0       0       0
rx_packets                     :         5577418       3       1       2       3
tx_aborted_errors              :               0       0       0       0       0
tx_bytes                       :      8028771928    2258    2178    2188    2929
tx_carrier_errors              :               0       0       0       0       0
tx_compressed                  :               0       0       0       0       0
tx_dropped                     :               0       0       0       0       0
tx_errors                      :               0       0       0       0       0
tx_fifo_errors                 :               0       0       0       0       0
tx_heartbeat_errors            :               0       0       0       0       0
tx_packets                     :         4719851       3       1       2       3
tx_window_errors               :               0       0       0       0       0
eth0                           : 12:29:05.001168      1s      5s     15s     60s