LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-14-2013, 06:56 PM   #1
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Rickshaw graphing with live data


I have a project that is written in HTML/Bash/PHP.

Rickshaw is a Javascript graphing program: http://code.shutterstock.com/rickshaw/

It says that it is capable of live graphing data.

I would like to graph the current bandwidth of the web server that is serving the page. I am grabbing the data like so, from a script running on the server(this might not be the greatest idea, please chime in if you have a better idea):

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
Now, rickshaw is made up of JS, so all i have done so far is try a MOUNTAIN of examples and have personally gotten nowhere. This is not the fault of the examples or the coders behind them, it is me and my complete inability to grasp JS.

Can anyone put me on the path to Utilizing rickshaw to graph this time-series network data?

Any pointers are appreciated!
 
Old 09-18-2013, 08:07 AM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Quote:
Originally Posted by szboardstretcher View Post
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.

Last edited by sag47; 09-18-2013 at 08:30 AM.
 
1 members found this post helpful.
Old 09-18-2013, 03:03 PM   #3
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Original Poster
Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Thanks for the input. I had already ripped apart the script in a similar fashion to what you have explained here. I'm not sure where the backticks went in my original post,.. they were obviously there on my server.

This is a wonderful reply to my question, and I can't thank you enough.

hopefully other people will find this as useful as i have.
 
Old 09-18-2013, 08:00 PM   #4
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
If you look at the github script I added time to the data. When you graph it in Rickshaw you'll need to graph bandwidth over time. I wanted to point that out as I feel I didn't *completely* answer your question. That might be where you went wrong originally. Since the date is a unix timestamp (i.e. seconds since 1970) you might need to convert the unix timestamp to a date using JavaScript.

SAM

Last edited by sag47; 09-18-2013 at 08:03 PM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
suggest me Live Cd to recover the data. pinga123 Linux - Newbie 3 02-21-2011 10:23 PM
graphing data from mysql database fancylad Linux - Software 1 01-08-2009 12:49 AM
Help me rescue my data using a Live CD sk381 Linux - Newbie 4 04-05-2007 12:47 AM
What Live CD do you use for Data Recovery? pengu Linux - Distributions 3 09-14-2006 10:49 AM
Hiding data from a Live CD umeshbabu Linux - Software 3 08-05-2005 05:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration