Creating pretty graphs with RRDTOOL

by Girish Venkatachalam

Girish Venkatachalam is a UNIX hacker with more than a decade of networking and crypto programming experience. His hobbies include yoga,cycling, cooking and he runs his own business. Details here:

The open source rrdtool project helps you create a graphical view of any statistic collected at regular intervals. This could be anything you can possibly conceive. Traffic loads on your network, hard disk DMA transfers, CPU temperature, SNMP monitoring data or even mundane stuff like your employee attrition rate or payroll details.

Somehow the commercial world is locked up in nice graphical displays like

Sample RRD graph

You may be wondering what goes into creating such graphs. Is there a way to do this with open source tools?

RRDTool is a project by Tobi Oetiker and it is 100% open source. It has several features one would expect from a tool like this and it has evolved over the years, matured and is quite stable to use. Moreover the most important aspect is that of creating rrd databases which use a compact binary format and is Round Robin in nature. You allocate the space necessary for storing your graphical data and there is never a risk of disks getting full.

I used RRDTool heavily for my SpamCheetah web interface and I can produce hourly, monthly, weekly or daily reports of all kinds of data.

On the Internet you will find several projects that use RRDTool for gleaning various data, presenting them and analyzing them. If not for anything intellectual at least you have a certain sex appeal in graphs!

So you can take a look at the Cacti project and view these graphs on a web page to monitor things like Network load and traffic statistics. People love statistics and it is not without reason. You want to know that all is well with your network and you may wish to identify bottlenecks as well.

You want to know whether your teenage employees if any are downloading audio or video files all the time or if there is a new worm or virus in town. All this can be easily figured out if you have access to sophisticated graphs with even more sophisticated backends which feed the data.

RRDTool gives you the ability to generate PDF output, PNG output or even plain text output from samples taken every few hours or minutes. It also has the capability to reduce its accuracy once the time increases. We are only interested in current data and previous trends. It is a very smart tool and knows what people want.

You have to run a cron job to update the tool at the interval you specify when creating RRD archives. It is this archive that you query to generate graphs.

Sample update commands


use Tie::File;
use File::Glob qw(:glob);

sub spamdwhite {
        my @spamdwhiteoutput = `/var/www/bin/myifstat`;

        my $whitenum = $#spamdwhiteoutput + 1;

        `$RRDTOOL update /var/rrd/spamdwhite.rrd N:$whitenum`;
        print "spamdwhite done\n";

sub spamdgrey {
        my @spamdgreyoutput = `/usr/sbin/spamdb`;
        my $greynum = $#spamdgreyoutput + 1;

        `$RRDTOOL update /var/rrd/spamdgrey.rrd N:$greynum`;
        print "spamdgrey done\n";

sub spamdtrapped {
        my @spamdtrapoutput = `/usr/sbin/spamdb|grep TRAPPED`;
        my $trapped = $#spamdtrapoutput + 1;

        `$RRDTOOL update /var/rrd/spamdtrapped.rrd N:$trapped`;
        print "spamdtrapped done\n";

A shell script version of this would be:



spamdwhite() {

        $RRDTOOL update /var/rrd/spamdwhite.rrd N:${whitenum};
	# Check return value to spot success/failure
	if [ $? = '0' ]; then 
        	echo "spamdwhite update done\n";
		echo "spamdwhite update failed\n";

spamdgrey() {
        greynum=`/usr/sbin/spamdb | wc -l`;

        $RRDTOOL update /var/rrd/spamdgrey.rrd N:${greynum};
	# Check return value to spot success/failure
	if [ $? = '0' ]; then 
        	echo "spamdgrey update done\n";
		echo "spamdgrey update failed\n";

spamdtrapped() {
        trapped=`/usr/sbin/spamdb | grep -c TRAPPED`;

        $RRDTOOL update /var/rrd/spamdtrapped.rrd N:${trapped};
	# Check return value to spot success/failure
	if [ $? = '0' ]; then 
        	echo "spamdtrapped update done\n";
		echo "spamdtrapped update failed\n";

There are many other details that you can learn from the tutorials below. I will explain the above.

Every rrd input parameter is either of type GAUGE, DERIVE, ABSOLUTE, COMPUTE or COUNTER. GAUGE values store a certain sample at a particular instant. Whereas COUNTER values store the difference from the previous value. COUNTERs are useful when you want to measure bandwidth utilization in which the values monotonically increase. Other types are not so commonly used. If you are curious read the man page of rrdcreate(1).

And in the case of floating point data you have to truncate or round it off and feed only integers as samples. In the above snippet, you find that I update the database with values I obtain from shell commands. Your creativity and ingenuity goes into what you want to measure and how you measure it. RRDTool after all cannot help you against your lack of creativity.

It can generate solid lines of varying thickness, supports a lot of colors and you can also draw AREAs which make the graph appear pretty. You have the choice of coloring the canvas also.

To wrap up, you should first create the Round Robin database with this RRDTool command.

$ rrdtool create /var/lib/rrd/eth0.rrd -s 300	\
         DS:in:DERIVE:600:0:12500000		\
         DS:out:DERIVE:600:0:12500000	\
         RRA:AVERAGE:0.5:1:576		\
         RRA:AVERAGE:0.5:6:672    		\
         RRA:AVERAGE:0.5:24:732   		\

Here we are defining two data sources given with the DS prefix and we are storing 4 archives that store average data. It is a lot more complex but I will leave that out.

Then you update using the command snippet you saw in my perl script cron job above.

Then you generate a graph with this command. This is usually run as a CGI script.

$ rrdtool graph mygraph.png -a PNG --title="TempTrax" \
--vertical-label "Deg F" \
'DEF:probe1=temptrax.rrd:probe1-temp:AVERAGE' \
'DEF:probe2=temptrax.rrd:probe2-temp:AVERAGE' \
'DEF:probe3=temptrax.rrd:probe3-temp:AVERAGE' \
'DEF:probe4=temptrax.rrd:probe4-temp:AVERAGE' \
'LINE1:probe1#ff0000:Switch Probe' \
'LINE1:probe2#0400ff:Server Probe' \
'AREA:probe3#cccccc:HVAC' \
'LINE1:probe4#35b73d:QA Lab Probe' \
'GPRINT:probe1:LAST:Switch Side Last Temp\: %2.1lf F' \
'GPRINT:probe3:LAST:HVAC Output Last Temp\: %2.1lf F\j' \
'GPRINT:probe2:LAST:Server Side Last Temp\: %2.1lf F' \
'GPRINT:probe4:LAST:QA Lab Last Temp\: %2.1lf F\j'

Most parameters are self explanatory. The DEF lines need to match with the original parameters defined while creating the archive. And you can draw lines or areas in colors given in '#ff0000' like HTML color codings. And you can name them with the last parameter say 'Switch Probe' or something. The GPRINTs print stuff on the graph and I never used it. Usually all you need are these lines.

$ rrdtool graph mygraph.png -a PNG --title="TempTrax" \
'DEF:probe1=temptrax.rrd:probe1-temp:AVERAGE' \
'LINE1:probe1#ff0000:Switch Probe' 

References and further reading

  1. Getting started with RRDtool
  2. RRDTool graphing open source tool homepage
  3. Cacti network graphing tool
  4. Network monitoring with rrdtool article
  5. Nice graphs produced by the tool

Got something to add? Send me email.

(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Creating pretty graphs with RRDTOOL


Increase ad revenue 50-250% with Ezoic

More Articles by © Girish Venkatachalam

Wed Aug 19 13:21:20 2009: 6770   BigDumbDinosaur

...even mundane stuff like your employee attrition rate or payroll details.

Dunno that I'd call employee attrition or payroll activities mundane. <Grin> They're kind of basic to a lot of businesses, eh?

Wed Sep 3 05:29:40 2014: http://www.varunchandak.in12523   Varun



Thanks for the tutorial. Can you explain the graph at the starting of the post ???

I have a mailing solution and I want to monitor SMTP/POP/IMAP IOPS on it. Any help from you.. I'd be very grateful.


Kerio Samepage

Have you tried Searching this site?

Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

The idea that Bill Gates has appeared like a knight in shining armour to lead all customers out of a mire of technological chaos neatly ignores the fact that it was he, by peddling second rate technology, led them into it in the first place, and continues to do so today. (Douglas Adams)

This post tagged: