[mrtg] Re: rrdtool and php

Jeremy Shaffner jshaffner at dmisi.com
Thu Apr 10 18:16:20 MEST 2003

On Thursday, April 10, 2003, at 04:23 AM, Baker, Brendon wrote:

> Hi
> Does anyone have some example php code interacting with rrdtool 
> databases. I
> would like to start playing around with this a bit.

There was sample code included with rrd-php but here's what I've done 
with it...

In this example, my rrd file contains two DS's, 'foo' and 'bar'.


$opts = array("AVERAGE", "--start", "end-1month");

$rrd = rrd_fetch($rrdfile, $opts, count($opts));

if (!is_array($rrd)) {
     $err = rrd_error();
     echo "rrd_fetch() failed: $err\n";

$numcolumns = count($rrd[ds_namv]);
$numrows = count($rrd[data]) / $numcolumns;

# Put the data into arrays using variable variables so that the names 
for the arrays
# are created from the names of the DS's (columns).
for ($row = 0; $row < $numrows; $row++) {
     for ($column = 0; $column < $numcolumns; $column++) {
         $element = $row * $numcolumns + $column;
         ${$rrd[ds_namv][$column]}[$row] = round($rrd[data][$element]);

# If NaN then set it to 0
# I know ahead of time that there are two columns named foo and bar
for ($row = 0; $row < $numrows; $row++) {
     if (is_nan($foo[$row]) || is_nan($bar[$row])) {
         $foo[$row] = 0;
         $bar[$row] = 0;


$foo and $bar now contain all the values and you can step through them 
as you wish.  I plug them into JpGraph.

All this work isn't strictly necessary.  You can do what you need with 
the data during the nested for loop too.  I go through the trouble of 
creating arrays so that it's similar to dealing with SQL DB's.

> Also........is the
> rrd-php module installed by default.

No it's not.


Unsubscribe mailto:mrtg-request at list.ee.ethz.ch?subject=unsubscribe
Archive     http://www.ee.ethz.ch/~slist/mrtg
FAQ         http://faq.mrtg.org    Homepage     http://www.mrtg.org
WebAdmin    http://www.ee.ethz.ch/~slist/lsg2.cgi

More information about the mrtg mailing list