Skip to content

Instantly share code, notes, and snippets.

@joehillen
Last active August 29, 2015 14:07
Show Gist options
  • Save joehillen/ec8e613d4f20d94ae099 to your computer and use it in GitHub Desktop.
Save joehillen/ec8e613d4f20d94ae099 to your computer and use it in GitHub Desktop.
bandwidth monitor script for i3blocks
#!/bin/bash
if [ "$1" != "" ]; then
echo "Missing required argument: 'tx' or 'rx'" 1>&2
exit 1
fi
if [ "$1" != "rx" -a "$1" != "tx" ]; then
echo "Invalid argument: $1" 1>&2
echo "Usage: $0 (rx|tx)" 1>&2
exit 1
fi
DIRECTION=$1
TMPFILE=/dev/shm/i3blocks_bandwidth_$DIRECTION
touch $TMPFILE
PREV=$(cat $TMPFILE)
NETDIR=/sys/class/net
IFACE=$(ip r | grep default | cut -d' ' -f5)
FILE=$NETDIR/$IFACE/statistics/${DIRECTION}_bytes
CUR=$(cat $FILE)
DELTA=$(expr $CUR - $PREV)
echo $(expr $DELTA / 1000) kB/s
echo $CUR > $TMPFILE
@Chronophobe
Copy link

Two suggestions/remarks:

  1. Shouldn't line 3 check if $1 is equal to nothing, rather than unequal?
  2. It doesn't play nice if you have multiple interfaces. You could remove any extra line ip r | grep default returns. (I solved it quickly by appending | head -n 1, but perhaps there's a better way).

@Firescar96
Copy link

I believe there is a typo on line 3, it should be if [ "$1" == "" ]; then

@ThePenguin1140
Copy link

It is a type, it will never run with line 3 the way it is.
I would also suggest replacing line 15 with

if [ ! -f $TMPFILE ]; then
   echo '0' > $TMPFILE
fi

to avoid expr: syntax error warnings on the very first run.

Copy link

ghost commented Aug 19, 2015

Hey @joehillen,

In case you haven't heard, i3blocks is splitting into two repos! i3blocks will now go on to contain the core binary and a set of smallest usable blocklets to provide some beginner functionality. From i3blocks, i3blocks-contrib has now split off to gather the community contributed blocklets in a single repo to make perusing them easier and more convenient. We would be honoured if you would consider submitting your blocklet(s) to the contrib repo.

This change is the core change scheduled for the v1.5 release. Please note that as an artifact of this split, your entry for this blocklet in the core wiki has been removed as has everyone else's. If you would like to submit your blocklet, please review the contrib wiki to understand the workflow of i3blocks-contrib.

Please note that this change over is in its early stages, so the process for submission hasn't been completely nailed down. We're hard at work still compiling the two project wikis and documenting all changes and standards. It is very likely that things will still change and we value your input on how that change should occur.

Deepest regrads,

@Nycroth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment