cab00a9eb9d8c064d32827d21947bbf76037337c
   1#!/bin/bash
   2# Copyright (C) 2012 Stefan Breunig <stefan+measure-net-speed@mathphys.fsk.uni-heidelberg.de>
   3# Copyright (C) 2014 kaueraal
   4# Copyright (C) 2015 Thiago Perrotta <perrotta dot thiago at poli dot ufrj dot br>
   5
   6# This program is free software: you can redistribute it and/or modify
   7# it under the terms of the GNU General Public License as published by
   8# the Free Software Foundation, either version 3 of the License, or
   9# (at your option) any later version.
  10
  11# This program is distributed in the hope that it will be useful,
  12# but WITHOUT ANY WARRANTY; without even the implied warranty of
  13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14# GNU General Public License for more details.
  15
  16# You should have received a copy of the GNU General Public License
  17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18
  19# Get custom IN and OUT labels if provided by command line arguments
  20while [[ $# -gt 1 ]]; do
  21    key="$1"
  22    case "$key" in
  23        -i|--inlabel)
  24            INLABEL="$2"
  25            shift;;
  26        -o|--outlabel)
  27            OUTLABEL="$2"
  28            shift;;
  29    esac
  30    shift
  31done
  32
  33#[[ -z $INLABEL ]] && INLABEL="IN "
  34#[[ -z $OUTLABEL ]] && OUTLABEL="OUT "
  35
  36# Use the provided interface, otherwise the device used for the default route.
  37if [[ -n $BLOCK_INSTANCE ]]; then
  38  INTERFACE=$BLOCK_INSTANCE
  39else
  40  INTERFACE=$(ip route | awk '/^default/ { print $5 ; exit }')
  41fi
  42
  43# Issue #36 compliant.
  44if ! [ -e "/sys/class/net/${INTERFACE}/operstate" ] || ! [ "`cat /sys/class/net/${INTERFACE}/operstate`" = "up" ]
  45then
  46    echo "$INTERFACE down"
  47    echo "$INTERFACE down"
  48    echo "#FF0000"
  49    exit 0
  50fi
  51
  52# path to store the old results in
  53path="/dev/shm/$(basename $0)-${INTERFACE}"
  54
  55# grabbing data for each adapter.
  56read rx < "/sys/class/net/${INTERFACE}/statistics/rx_bytes"
  57read tx < "/sys/class/net/${INTERFACE}/statistics/tx_bytes"
  58
  59# get time
  60time=$(date +%s)
  61
  62# write current data if file does not exist. Do not exit, this will cause
  63# problems if this file is sourced instead of executed as another process.
  64if ! [[ -f "${path}" ]]; then
  65  echo "${time} ${rx} ${tx}" > "${path}"
  66  chmod 0666 "${path}"
  67fi
  68
  69# read previous state and update data storage
  70read old < "${path}"
  71echo "${time} ${rx} ${tx}" > "${path}"
  72
  73# parse old data and calc time passed
  74old=(${old//;/ })
  75time_diff=$(( $time - ${old[0]} ))
  76
  77# sanity check: has a positive amount of time passed
  78[[ "${time_diff}" -gt 0 ]] || exit
  79
  80# calc bytes transferred, and their rate in byte/s
  81rx_diff=$(( $rx - ${old[1]} ))
  82tx_diff=$(( $tx - ${old[2]} ))
  83rx_rate=$(( $rx_diff / $time_diff ))
  84tx_rate=$(( $tx_diff / $time_diff ))
  85
  86# shift by 10 bytes to get KiB/s. If the value is larger than
  87# 1024^2 = 1048576, then display MiB/s instead
  88
  89# incoming
  90echo -n "$INLABEL"
  91rx_kib=$(( $rx_rate >> 10 ))
  92if hash bc 2>/dev/null && [[ "$rx_rate" -gt 1048576 ]]; then
  93
  94  if [ $rx_kib -lt 9900 ]; then
  95    printf '%sM' "`echo "scale=1; $rx_kib / 1024" | bc`"
  96  else
  97    if [ $rx_kib -lt 1022976 ]; then
  98      printf '%03dM' "`echo "scale=0; $rx_kib / 1024" | bc`"
  99    else
 100      printf "%sG" "`echo "scale=1; $rx_kib / 1024000" | bc`"
 101    fi
 102  fi
 103else
 104  printf %03d ${rx_kib} # leading zeroes so length is constant
 105  echo -n "K"
 106fi
 107
 108echo -n " "
 109
 110# outgoing
 111echo -n "$OUTLABEL"
 112tx_kib=$(( $tx_rate >> 10 ))
 113if hash bc 2>/dev/null && [[ "$tx_rate" -gt 1048576 ]]; then
 114  if [ $tx_kib -lt 9900 ]; then
 115    printf '%sM' "`echo "scale=1; $tx_kib / 1024" | bc`"
 116  else
 117    if [ $tx_kib -lt 1022976 ]; then
 118      printf '%03dM' "`echo "scale=0; $tx_kib / 1024" | bc`"
 119    else
 120      printf "%sG" "`echo "scale=1; $tx_kib / 1024000" | bc`"
 121    fi
 122  fi
 123else
 124  printf %03d ${tx_kib}  # leading zeroes so length is constant
 125  echo -n "K"
 126
 127fi