Skip to content

Instantly share code, notes, and snippets.

@ruckuus
Last active August 29, 2015 14:06
Show Gist options
  • Save ruckuus/dc0f6243e128243ef2ab to your computer and use it in GitHub Desktop.
Save ruckuus/dc0f6243e128243ef2ab to your computer and use it in GitHub Desktop.
get_io_rate.sh
#!/usr/bin/env bash
[ -z $1 ] && echo "Gimme the PID" && exit 1
PIDNUM=$1
declare -A CURRENT
declare -A NEXT
export FLAG=0
get_io() {
myarr=($(cat /proc/$PIDNUM/io | awk '{print $1$2}'))
for i in "${myarr[@]}"
do
KEY=$(echo $i | cut -d : -f1)
VAL=$(echo $i | cut -d : -f2)
if [ $FLAG == 0 ]; then
CURRENT[$KEY]=$VAL
export FLAG=1
else
NEXT[$KEY]=$VAL
export FLAG=0
fi
done
}
get_rate() {
get_io
sleep 1
get_io
READ_VIRT=$(expr $(expr ${NEXT["rchar"]} - ${CURRENT["rchar"]}) / 1024)
WRITE_VIRT=$(expr $(expr ${NEXT["wchar"]} - ${CURRENT["wchar"]}) / 1024)
READ_PHYS=$(expr $(expr ${NEXT["read_bytes"]} - ${CURRENT["read_bytes"]}) / 1024)
WRITE_PHYS=$(expr $(expr ${NEXT["write_bytes"]} - ${CURRENT["write_bytes"]}) / 1024)
clear
echo "==================================================="
echo "Data read by the process : ${READ_VIRT#-} KB/s"
echo "Data written by the process : ${WRITE_VIRT#-} KB/s"
echo "Data read from storage : ${READ_PHYS#-} KB/s"
echo "Data written to storage : ${WRITE_PHYS#-} KB/s"
echo "==================================================="
}
while true
do
get_rate
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment