17

Bandwidth Graph

Posted in Internet by kayelbe 2010 days ago

Bandwidth Graph

All credit goes to FafaG13

See his original post here (BPS Graph) http://www.macosxtips.co.uk/geeklets/internet/paranoid-1/

His tutorial was in french so I wanted to rewrite it in english for anyone else that wanted to use it and also clear up some things that I had troubles with.

Step 1: Install rrdtool by running the following command from the terminal.

  • My rrdtool install failed my first attempt so I had to update brew and try again. Probably best to just update first and then install rrdtool.

$ brew update

$ brew install rrdtool

Step 2: Create a folder that you will store the scripts and graphs.

Example (the ~ refers to you home directory):

mkdir ~/Tools/Geektool/NetworkGraph

cd ~/Tools/Geektool/NetworkGraph

Step 3: Run the following command to initialize the rrd database (creates a database file called bandwidthusage_db.rrd). You only need to run this command once.

$ rrdtool create bandwidthusage_db.rrd --step 10 DS:inbyts:COUNTER:20:0:U DS:oubyts:COUNTER:20:0:U RRA:MAX:0.5:1:900

Verify the file was created

$ ls
bandwidthusage_db.rrd

Step 4: Create the following script that will capture your interface statistics and populate your bandwidthusage_db.rrd file.

$ touch rrd_database.sh

$ chmod u+x rrd_database.sh

Use whatever text editor you are familiar with to add the following into this file.

IMPORTANT On my mac 'en0' is the wireless interface that I wanted to monitor. Change this in the script below to whichever interface you want. Use the command 'ifconfig' to see what interface you want to use.

#!/bin/bash
rrdtool="/usr/local/bin/rrdtool"
while :
do
    data=$(netstat -i -b | grep en0 | tail -n 1 | awk '{print $7" "$10}')
    inbytes=$( echo $data | awk '{print $1}')
    oubytes=$( echo $data | awk '{print $2}')
    $rrdtool update bandwidthusage_db.rrd N:$inbytes:$oubytes
    sleep 9
done

Step 5: Create the script that will generate the graph (in png format) from the information that is stored in the database.

$ touch create_graph.sh

$ chmod u+x create_graph.sh

Use your text editor to add the following into this file.

#!/bin/bash
/usr/local/bin/rrdtool graph bandwidth_graph.png \
-w 245 -h 80 -a PNG \
--start -1800 --end now \
--font DEFAULT:7:Menlo \
--title "Interface Bandwidth" \
--lower-limit 0 \
--upper-limit MAX \
--right-axis 1:0 \
--x-grid none \
--y-grid none \
--border 0 \
--color ARROW#ffffff00 \
--color AXIS#ffffff \
--color BACK#ffffff00 \
--color CANVAS#ffffff00 \
--color FONT#ffffff \
DEF:inbyts=bandwidthusage_db.rrd:inbyts:MAX \
DEF:oubyts=bandwidthusage_db.rrd:oubyts:MAX \
CDEF:IBNone=inbyts,0,0,LIMIT,UN,UNKN,INF,IF \
CDEF:OBNone=oubyts,0,0,LIMIT,UN,UNKN,INF,IF \
AREA:inbyts#FFFFFF:"inBytes" \
AREA:oubyts#4174d1:"ouBytes" \
GPRINT:inbyts:LAST:"Cur\: %5.2lf" \
GPRINT:inbyts:AVERAGE:"Avg\: %5.2lf" \
GPRINT:inbyts:MAX:"Max\: %5.2lf" \
GPRINT:inbyts:MIN:"Min\: %5.2lf"
sleep 9

Step 5: OSX uses plist files instead of cron jobs. We will need to set this up so that the scripts are ran to update the database and create the graph. This is how I did it on my mac (running Mavericks - v10.9.2).

Warning be careful what you do when running the commands when logged in as root and could cause problems with your system if you are careless.

Step 5.1: Creating the plist file that will launch the rrd_database.sh script at boot. This does not have to run at regular intervals as the script loops on it's own.

$ sudo -s

# cd /Library/LaunchDaemons

# touch com.myplistfile.rrdupdate.plist

Add the following to this file.

IMPORTANT you need to modify this file slightly first. Change USERNAME to your username and check that the paths are correct.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Debug</key>
<false/>
<key>GroupName</key>
<string>staff</string>
<key>Label</key>
<string>com.myplistfile.rrdupdate</string>
<key>ProgramArguments</key>
<array>
    <string>/bin/sh</string>
    <string>/Users/USERNAME/Tools/Geektool/NetworkGraph/rrd_database.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>USERNAME</string>
<key>WorkingDirectory</key>
<string>/Users/USERNAME/Tools/Geektool/NetworkGraph</string>
</dict>
</plist>

Step 5.2: Creating the plist file that update the graph (runs the create_graph.sh script). The script below is set to run every 15 seconds, feel free to change this to however often you want the png file created.

# touch com.myplistfile.rrdgraph.plist

IMPORTANT you need to modify this file slightly first. Change USERNAME to your username and check that the paths are correct.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>GroupName</key>
<string>staff</string>
<key>Label</key>
<string>com.myplistfile.rrdgraph</string>
<key>ProgramArguments</key>
<array>
    <string>/bin/sh</string>
    <string>/Users/USERNAME/Tools/Geektool/NetworkGraph/create_graph.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceDescription</key>
<string>rrdgraph</string>
<key>StartInterval</key>
<integer>15</integer>
<key>UserName</key>
<string>USERNAME</string>
<key>WorkingDirectory</key>
<string>/Users/USERNAME/Tools/Geektool/NetworkGraph</string>
</dict>
</plist>

Step 6: Run the 'launchctl' commands to load the plist files.

# launchctl load -w com.myplistfile.rrdupdate.plist

# launchctl load -w com.myplistfile.rrdgraph.plist

  • Note: if you ever want to stop these files from running you can unload/remove them by doing the following.

ONLY DO THIS TO REMOVE THE PLIST FILES

$ sudo -s

# cd /Library/LaunchDaemons

# launchctl unload com.myplistfile.rrdupdate.plist

# launchctl unload com.myplistfile.rrdgraph.plist

# rm com.myplistfile.rrdupdate.plist

# rm com.myplistfile.rrdgraph.plist

Step 7: After rebooting your system these scripts should now be running and updating the graph. Using Geektool you can now put an image on the screen (locate the png file that is being generated) and set it to automatically update every 10 seconds. I think this step should be pretty straight forward, but if you have questions let me know.

Hope this helps.


Geeklet files to download

Comments

User Avatar
BigBadBobD 2000 days ago
didn't work for me. I get this error, no clue on how to fix it.

Library not loaded: /usr/local/lib/libgobject-2.0.0.dylib

Referenced from: /usr/local/bin/rrdtool

Reason: Incompatible library version: rrdtool requires version 4001.0.0 or later, but libgobject-2.0.0.dylib provides version 2801.0.0
User Avatar
BigBadBobD 1999 days ago
this fixed it

sudo chown -R `whoami` /usr/local
User Avatar
kayelbe 1999 days ago
thanks for posting your fix!
User Avatar
FafaG13 1999 days ago
Someone interested about this :) Cooooooooool !
User Avatar
kayelbe 1999 days ago
Yeah, I was very interested in it! Great tutorial you posted! I hope others are interested too. :)
User Avatar
jerrychenglei 1981 days ago
Awesome thanks!!! that's cool
User Avatar
helioskk 1976 days ago
Hi, I was thinking instead of dirtying your hands with PLISTS, you could also make another shell script, put rrd_database.sh and create_graph.sh in them and call that script from shell plugin from geek tools?
User Avatar
kayelbe 1976 days ago
hey helioskk,

i think you're absolutely right.. i didn't know much about plists when i started this, but after some research i figured it out and that's what i went with.. but creating a simple script and calling it within geektools is much cleaner. ill give that a shot later and update this page once i have it working.
User Avatar
helioskk 1975 days ago
Thanks for giving it a thought! I am new to both mac and geek tools :) Hey! I learnt something about PLISTS! Great tutorial though!

Probably you can even club those shell scripts into one :)

make two functions, updateDb() has the contents of the first script and draw() has the contents of the second one. just call this one script from geek tools :)
User Avatar
michaelmoto 1968 days ago
Hi,

Thanks for this tutorial. How do I get geektool to call the script directly? Anyone who has an instruction? I'm new to using geektools and would love to be able to run this network graph.

Another thing is I think I'm having issue with the create_graph script. Every time I tried running it it give me this Error: 'M' is not a valid function name. Anyone who can enlighten me regarding this.

Appreciate the help.

Best regards,

michaelmoto
User Avatar
succube 1951 days ago
Hello,

I had the same error.

This is the fix:

Copy and paste the following into create_graph.sh

/usr/local/bin/rrdtool graph bandwidth_graph.png \

-w 500 -h 160 -a PNG \

--start -1800 --end now \

--font DEFAULT:7:Menlo \

--title "Interface Bandwidth " \

--lower-limit 0 \

--upper-limit MAX \

--right-axis 1:0 \

--x-grid none \

--y-grid none \

--border 0 \

--color ARROW#ffffff00 \

--color AXIS#ffffff \

--color BACK#ffffff00 \

--color CANVAS#ffffff00 \

--color FONT#ffffff \

DEF:inbyts=bandwidthusagedb.rrd:inbyts:MAX \

DEF:oubyts=bandwidthusagedb.rrd:oubyts:MAX \

CDEF:IBNone=inbyts,0,0,LIMIT,UN,UNKN,INF,IF \

CDEF:OBNone=oubyts,0,0,LIMIT,UN,UNKN,INF,IF \

AREA:inbyts#FFFFFF:"inBytes" \

AREA:oubyts#4174d1:"ouBytes" \

GPRINT:inbyts:LAST:"Cur\: %5.2lf" \

GPRINT:inbyts:AVERAGE:"Avg\: %5.2lf" \

GPRINT:inbyts:MAX:"Max\: %5.2lf" \

GPRINT:inbyts:MIN:"Min\: %5.2lf"

sleep 9
User Avatar
kayelbe 1948 days ago
Thanks succube. I see where the error was in the create_graph script. I've updated the posted version above.

michaelmoto, did you figure out how to call the script directly? i have been meaning to set this up without using plists, but havent had time lately. i'll give it a shot soon and if/when i get it working ill let you know.
User Avatar
dieteroo 1940 days ago
took me a while to figure this one out :-s

in Step 3 you create a file called bandwidthusagedb.rrd

$ rrdtool create bandwidthusagedb.rrd --step 10 DS:inbyts:COUNTER:20:0:U DS:oubyts:COUNTER:20:0:U RRA:MAX:0.5:1:900

in Step 5 you create a file called create_graph.sh

and use bandwidthusage_db.rrd

DEF:inbyts=bandwidthusage_db.rrd:inbyts:MAX \

DEF:oubyts=bandwidthusage_db.rrd:oubyts:MAX \

notice the extra underscore
User Avatar
kayelbe 1934 days ago
hey dieteroo,

thanks for pointing this out! not sure how i messed that up, but i've updated the post.
User Avatar
polinux 1907 days ago
Ho can you chenge that to show megabytes instead of bytes?
User Avatar
darkthemes 1535 days ago
jt would be easy to make it downloads instead of like writing the script but i usually don't put the script on it i just let them look also search jarvis and you will see mine
User Avatar
BenGrenoble 1472 days ago
Hello,

I always get an error: ERROR: option -u - Cannot convert 'MAX' to float

I'm not able to understand where's the error.

Someone could help me?
User Avatar
BenGrenoble 1471 days ago
Ok I've found the error,

--upper-limit MAX

doesn't work on my mac.

I put a value instead and now everything work well.

--upper-limit 200 for example.

Thanks.
User Avatar
thumper808 954 days ago
I wish the graph could be more aesthetic :) Very cool though.

Log in to comment or register here.