Quote:
Originally Posted by szboardstretcher
Code:
while true
do
R1=cat /sys/class/net/$1/statistics/rx_bytes
T1=cat /sys/class/net/$1/statistics/tx_bytes
sleep 1
R2=cat /sys/class/net/$1/statistics/rx_bytes
T2=cat /sys/class/net/$1/statistics/tx_bytes
TBPS=expr $T2 - $T1
RBPS=expr $R2 - $R1
TKBPS=expr $TBPS / 1024
RKBPS=expr $RBPS / 1024
echo "tx $1: $TKBPS kb/s rx $1: $RKBPS kb/s"
done
|
Rickshaw looks awesome
... bookmarked. Now on to your problems, I don't know much about Rickshaw but your data gathering script is incorrect for what I assume is your intention. When you're setting environment variables you're setting the literal text "cat /sys/class/net/$1/statistics/rx_bytes" rather than the output of that command which is what I assume you want. Read the
bash(1) man page and look up the section "Compound commands" and "Command Substitution". Basically your script should look something like this...
Code:
#Author szboardstretcher @ linuxquestions.org
#Contributor sag47 @ linuxquestions.org
#Wed Sep 18 09:16:15 EDT 2013
#GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)
#DESCRIPTION
# A simple bandwidth rate usage script for a device.
#USAGE
# ./bandmeter.sh eth0
#do some error checking
if [ -z "${1}" ];then
echo "Device not specified!" 1>&2
echo "Usage: $(basename ${0}) device" 1>&2
exit 1
elif [ ! -e "/sys/class/net/${1}" ];then
echo "Error: The device you specified does not exist!" 1>&2
echo "Usage: $(basename ${0}) device" 1>&2
exit 1
fi
R1=0
T1=0
while true;do
#R2 and T2 are now the old values from the last second
R2="${R1}"
T2="${T1}"
R1="$(cat /sys/class/net/${1}/statistics/rx_bytes)"
T1="$(cat /sys/class/net/${1}/statistics/tx_bytes)"
TBPS="$(expr ${T1} - ${T2})"
RBPS="$(expr ${R1} - ${R2})"
TKBPS="$(expr ${TBPS} / 1024)"
RKBPS="$(expr ${RBPS} / 1024)"
echo "tx ${1}: ${TKBPS} kb/s rx ${1}: ${RKBPS} kb/s"
sleep 1
done
I modified the logic of your script a little bit so that it is a little more clear what is going on. You should make use of comments as well as error checking for your logic because you never know when you need to open this script up years down the road and wonder what in the world you were trying to do.
As for the braces "${variable}" instead of "$variable" you should look up Parameter Expansion in the bash man page. It is important to note that using curly braces at all times (i.e. "${1}" instead of "$1") is recommended because the braces are a clear and well defined variable. I'll give you an example of why.
Code:
some=my
echo "file = $some_file.txt"
echo "file = ${some}_file.txt"
You should see the following output.
Code:
file = file.txt
file = my_file.txt
Variable names can have an underscore '_'. However, in this case it wasn't intended for the underscore to be part of the variable name "$some" but was interpreted as the variable "$some_". With curly brackets you explicitly define the variable name you want to use with no room for confusion.
EDIT: Added a little more to your script so your graph doesn't get swamped with data. Also added a date to your graph because you need to know the time frame of exactly when you experienced that bandwidth.
Uploaded to github.