Question

I am trying to create RRD graphs with the help of PHP in order to keep track of the inoctets,outoctets and counter of a server.

So far the script is operating as expected but my problems comes when I am trying to produce 2 or more separate graphs. I am trying to produce (hourly, weekly , etc) graphs. I thought by creating a loop would solve my problem, since I have split the RRA in hours and days. Unfortunately I end up having 2 graphs that updating simultaneously as expected but plotting the same thing. Has any one encounter similar problem? I have applied the same program in perl with RRD::Simple,where is extremely easy and everything is adjusted almost automatically.

Graph Output

I have supplied under a working example of my code with the minimum possible data because the code is a bit long:

<?php

$file = "snmp-2";
$rrdFile = dirname(__FILE__) . "/snmp-2.rrd";
$in = "ifInOctets";
$out = "ifOutOctets";
$count = "sysUpTime";
$step = 5;
$rounds = 1;
$output = array("Hourly","Daily");

while (1) { 
  sleep (6);

  $options = array(
           "--start","now -15s", // Now -10 seconds (default)
           "--step", "".$step."",
           "DS:".$in.":GAUGE:10:U:U",
           "DS:".$out.":GAUGE:10:U:U",
           "DS:".$count.":ABSOLUTE:10:0:4294967295",
           "RRA:MIN:0.5:12:60",
           "RRA:MAX:0.5:12:60",
           "RRA:LAST:0.5:12:60",
           "RRA:AVERAGE:0.5:12:60",
           "RRA:MIN:0.5:300:60",
           "RRA:MAX:0.5:300:60",
           "RRA:LAST:0.5:300:60",
           "RRA:AVERAGE:0.5:300:60",
           );


  if ( !isset( $create ) ) {

    $create = rrd_create(
             "".$rrdFile."",
             $options
             );

    if ( $create === FALSE ) {
      echo "Creation error: ".rrd_error()."\n";
    }
  }

  $t = time();

  $ifInOctets = rand(0, 4294967295);
  $ifOutOctets = rand(0, 4294967295);
  $sysUpTime = rand(0, 4294967295);

  $update = rrd_update(
               "".$rrdFile."",
               array(
                 "".$t.":".$ifInOctets.":".$ifOutOctets.":".$sysUpTime.""
                 )
               );

  if ($update === FALSE) {
    echo "Update error: ".rrd_error()."\n";
  }

  $start = $t - ($step * $rounds);

  foreach ($output as $test) {   

    $final = array(
           "--start","".$start." -15s",
           "--end", "".$t."",
           "--step","".$step."",
           "--title=".$file." RRD::Graph",
           "--vertical-label=Byte(s)/sec",
           "--right-axis-label=latency(min.)",
           "--alt-y-grid", "--rigid",
           "--width", "800", "--height", "500", 
           "--lower-limit=0",
           "--alt-autoscale-max",
           "--no-gridfit",
           "--slope-mode",
           "DEF:".$in."_def=".$file.".rrd:".$in.":AVERAGE",
           "DEF:".$out."_def=".$file.".rrd:".$out.":AVERAGE",
           "DEF:".$count."_def=".$file.".rrd:".$count.":AVERAGE",
           "CDEF:inbytes=".$in."_def,8,/",
           "CDEF:outbytes=".$out."_def,8,/",
           "CDEF:counter=".$count."_def,8,/",
           "COMMENT:\\n",
           "LINE2:".$in."_def#FF0000:".$in."",
           "COMMENT:\\n",
           "LINE2:".$out."_def#0000FF:".$out."",
           "COMMENT:\\n",
           "LINE2:".$count."_def#FFFF00:".$count."",
           );

    $outputPngFile = rrd_graph(
                   "".$test.".png",
                   $final
                   );

    if ($outputPngFile === FALSE) {
      echo "<b>Graph error: </b>".rrd_error()."\n";
    }

  } /* End of foreach function */

  $debug = rrd_lastupdate (
               "".$rrdFile.""
               );

  if ($debug === FALSE) {
    echo "<b>Graph result error: </b>".rrd_error()."\n";
   } 

  var_dump ($debug);

  $rounds++;

} /* End of while loop */

?>
Was it helpful?

Solution

A couple of issues.

Firstly, your definition of the RRD has a step of 5seconds and RRAs with steps of 12x5s=1min and 300x5s=25min. They also have a length of only 60 rows, so 1hr and 25hr respectively. You'll never get a weekly graph this way! You need to add more rows; also the step seems rather short, and you might need a smaller-step RRA for hourly graphs and a larger-step one for weekly graphs.

Secondly, it is not clear how you're calling the graph function. You seem to be specifying:

"--start","".$start." -15s",
"--end", "".$t."",
"--step","".$step."",

... which would force it to use the 5s interval (unavailable, so the 1min one would always get used) and for the graph to be only for the time window from the start to the last update, not a 'hourly' or 'daily' as you were asking.

Note that the RRA you have defined do not define the time window of the graph you are asking for. Also, just because you have more than one RRA defined, it doesnt mean you'll get more than one graph unless oyu call the graph function twice with different arguments.

If you want a daily graph, use

"--start","end - 1 hour",
"--end",$t,

Do not specify a step as the most appropriate available will be used anyway. For a daily graph, use

"--start","end - 1 day"
"--end",$t,

Similarly, no need to specify a step.

Hopefully this will make it a little clearer. Most of the RRD graph options have sensible defaults, and RRDTool is pretty good at picking the correct RRA to use based on the graph size, time window, and DEF statements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top