Skip to content

Instantly share code, notes, and snippets.

@artyom-poptsov
Last active December 14, 2015 17:39
Show Gist options
  • Save artyom-poptsov/5124093 to your computer and use it in GitHub Desktop.
Save artyom-poptsov/5124093 to your computer and use it in GitHub Desktop.
A simply script that calculates bandwidth usage for a given interface. It is written for using with GNU Screen.
backtick 1 1 1 /home/$USER/bin/bwstat.sh eth0
hardstatus alwayslastline '%{= G}[%= %{= w}%?%-Lw%?%{= R}%n*%f %t%?%{= R}(%u)%?%{= w}%+Lw%?%= %{= g}][ %{y}%1`%{g} ][ %{y}L: %l %{g}]'
#!/bin/bash
# bwstat.sh -- Calculate bandwidth usage for a given interface.
#
# Copyright (C) 2013 Artyom V. Poptsov <poptsov.artyom@gmail.com>
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved. This file is offered as-is,
# without any warranty.
BWSTAT_FILE=/tmp/bwstat
IFACE=$1
RES=""
if [ -s $BWSTAT_FILE ]; then
TIME_OLD=$(cat $BWSTAT_FILE | cut -d ' ' -f 1)
RXB_OLD=$(cat $BWSTAT_FILE | cut -d ' ' -f 2)
TXB_OLD=$(cat $BWSTAT_FILE | cut -d ' ' -f 3)
RXB_NEW=$(</sys/class/net/"$IFACE"/statistics/rx_bytes)
TXB_NEW=$(</sys/class/net/"$IFACE"/statistics/tx_bytes)
TIME_NEW=`date +%s`
PERIOD=$((TIME_NEW - TIME_OLD))
RXDIF=$(echo $((RXB_NEW - RXB_OLD)))
TXDIF=$(echo $((TXB_NEW - TXB_OLD)))
RES="U$((TXDIF / 1024 / PERIOD)) D$((RXDIF / 1024 / PERIOD))"
echo "$TIME_NEW $RXB_NEW $TXB_NEW" > $BWSTAT_FILE
else
RXB=$(</sys/class/net/"$iface"/statistics/rx_bytes)
TXB=$(</sys/class/net/"$iface"/statistics/tx_bytes)
echo "$(date +%s) $RXB $TXB" > $BWSTAT_FILE
chmod 600 $BWSTAT_FILE
RES="U0 D0"
fi
echo -e "$RES"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment