[rrd-users] rrdcgi

Simon Hobson linux at thehobsons.co.uk
Thu Aug 18 17:18:36 CEST 2011

fadwa salam wrote:

>i try to do examples of rrdcgi in the website : 
>i don't know, if i must save the script on  *.sh or *.cgi
>can smoone help me
>thank you .
>[root at inca-26 f_rrdtool]# ./web.sh
>./web.sh: line 2: HTML: No such file or directory
>./web.sh: line 3: syntax error near unexpected token `<'
>'/web.sh: line 3: ` <HEAD><TITLE>RRDCGI Demo</TITLE></HEAD>

It needs to be a cgi program - so either .cgi OR configure your web 
server to treat .sh as a cgi.

I've called my scripts .cgi, and they are structured along the lines of :

><deal with URL parameters>
><echo some headers>
><create an rrd script> | rrdcgi --filter - 2>/dev/null
><cat image file>

Some of the code from a script is :

>Etime=`/bin/date +%s`
>Etime=$(( ${Etime} / ${Step} * ${Step} ))
>PrintedTime=$(/bin/date -d "19700101 00:00 +0000 ${Etime}sec" 
>+"%H\:%M %a %d %b %Y")
>Exptime=$(( ${Etime} + ${Step} ))
>   echo "Content-Type: image/png
>Last-Modified: `date --rfc-2822 -d "19700101 00:00 +0000 ${Etime}sec"`
>Expires: `date --rfc-2822 -d "19700101 00:00 +0000 ${Exptime}sec"`
>if [ ${GType} = "simple" ]
>   DoSimple
>   DoStacked
>fi | /usr/bin/rrdcgi --filter - 2>&1 > /dev/null
>cat ${GraphPath}/${GrFile}-${Max}-${TimeScale}.png

and DoSimple is like this :
>DoSimple () {
>   echo "
>         <RRD::GRAPH ${GraphPath}/${GrFile}-${Max}-${TimeScale}.png
>         --title=\"${Heading} - ${TimeScaleHead}\" -v \"bytes/second\"
>         --end ${Etime} --start end-${Period}
>         --lazy
>         --width 600 --height 200
><rest of graph definition>

To use it, you'd have a static (or dynamic if you prefer) HTML page 
with stuff like this in it :

>   titles and stuff
>   <img src="myscript.cgi?period=day>
>   <img src="myscript.cgi?period=week>
>   <img src="myscript.cgi?period=month>
>   <img src="myscript.cgi?period=year>
>   other stuff

As an alternative, you can just generate a load of HTML which 
includes a graph definition enclosed in "<RRD::GRAPH" and ">" and 
pipe that through rrdcgi. I did that originally but it was less 
flexible. I had (for example) something like this :

><echo headers and stuff>
><do a graph - day>
><do a graph - week>
><do a graph - month>
><do a graph - year>
><echo footers and stuff>

The problem then is that each graph is created sequentially, you 
can't re-use a graph on a nother page, and I had some issues getting 
the path as seen by rrdcgi to match the path as seen by the web 

As to reusing a graph, by doing it the first way (the script is 
called as an image), you can re-use a graph in multiple places - eg 
having an overview page that shows data from multiple sources, but 
only one timescale.

At the end of the day, there are many ways to do this sort of stuff - 
above is just an outline of the methods I've used.

Simon Hobson

Visit http://www.magpiesnestpublishing.co.uk/ for books by acclaimed
author Gladys Hobson. Novels - poetry - short stories - ideal as
Christmas stocking fillers. Some available as e-books.

More information about the rrd-users mailing list