Skip to content

Instantly share code, notes, and snippets.

@dalehamel
Created April 21, 2014 05:24
Show Gist options
  • Save dalehamel/11133050 to your computer and use it in GitHub Desktop.
Save dalehamel/11133050 to your computer and use it in GitHub Desktop.
Zero dependency linux IO usage checker
#!/bin/sh
INTERVAL=10
PARTITION=mmcblk0p2
DEVICE=mmcblk0
#PARTITION=sda2
#DEVICE=sda
S_SIZE=`cat /sys/block/$DEVICE/queue/hw_sector_size`
while getopts "h?i:" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
i) INTERVAL=$OPTARG
if [ $INTERVAL -lt 1 ];then
echo "You wanted an INTERVAL of $INTERVAL, but must be greater than
1"
exit 1
fi
;;
esac
done
show_help ()
{
echo "Usage: -i <INTERVAL in seconds>"
echo "Example: ./bw.sh -i 5"
echo "The longer the INTERVAL, the more accurate the average. Minimum is 1,
default is 10".
}
check_net ()
{
cat /proc/net/netstat | grep 'IpExt: ' | tail -n 1 | awk '{ print $8 "\t" $9 }'
}
check_disk ()
{
cat /proc/diskstats | grep $PARTITION | awk '{print $9 "\t" $13 }'
}
NET_START=`check_net`
NET_IN_START=`echo $NET_START | awk '{ print $1 }'`
NET_OUT_START=`echo $NET_START | awk '{ print $2 }'`
DISK_START=`check_disk`
DISK_IN_START=`echo $DISK_START | awk '{ print $1 }'`
DISK_OUT_START=`echo $DISK_START | awk '{ print $2 }'`
sleep $INTERVAL
NET_END=`check_net`
NET_IN_END=`echo $NET_END | awk '{ print $1 }'`
NET_OUT_END=`echo $NET_END | awk '{ print $2 }'`
DISK_END=`check_disk`
DISK_IN_END=`echo $DISK_END | awk '{ print $1 }'`
DISK_OUT_END=`echo $DISK_END | awk '{ print $2 }'`
NET_IN_BPS=`awk -v s=$NET_IN_START -v e=$NET_IN_END 'BEGIN { print ( ( e - s ) / 10^6 ) }'`
NET_OUT_BPS=`awk -v s=$NET_OUT_START -v e=$NET_OUT_END 'BEGIN { print ( ( e - s ) / 10^6 )
}'`
NET_TOTAL_BPS=`awk -v i=$NET_IN_BPS -v o=$NET_OUT_BPS 'BEGIN { print i + o }'`
DISK_IN_BPS=`awk -v s=$DISK_IN_START -v e=$DISK_IN_END 'BEGIN { print ( ( e - s ) / 10^6 ) }'`
DISK_OUT_BPS=`awk -v s=$DISK_OUT_START -v e=$DISK_OUT_END 'BEGIN { print ( ( e - s ) / 10^6 )
}'`
DISK_TOTAL_BPS=`awk -v i=$DISK_IN_BPS -v o=$DISK_OUT_BPS 'BEGIN { print i + o }'`
TOTAL_BPS=`awk -v i=$DISK_TOTAL_BPS -v o=$NET_TOTAL_BPS 'BEGIN { print i + o }'`
echo "Net In: " $NET_IN_BPS "MB/s";
echo "Net Out: " $NET_OUT_BPS "MB/s"
echo "Net Total: $NET_TOTAL_BPS MB/s"
echo "Disk In: " $DISK_IN_BPS "MB/s";
echo "Disk Out: " $DISK_OUT_BPS "MB/s"
echo "Disk Total: $DISK_TOTAL_BPS MB/s"
echo "Total: $TOTAL_BPS MB/s"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment