[rrd-users] how rrdtool calculate the average value on graph?

Jérôme ANGLES angles.jerome at wanadoo.fr
Tue Jul 8 11:31:33 CEST 2008


Hello, 

Maybe i ask a stupid question but i tried to calculate the average value for some of my graph generated in cacti, and i can't find the same value as the one on the graph. 

I wrote a simple perl script to extract value from the rrd file using rrd fetch, then add all of them multiplicated by the delta time for each. 

here is the code : 

Code:
  #!/usr/bin/perl 
  use strict; 
  use Getopt::Std; 
  use vars qw($opt_r); 

  getopts('r:'); 

  # extract the value for the last day (my polling time is 10) 
  my @values = `rrdtool fetch $opt_r AVERAGE -s -86400 -e -10`;  
  
  my $MAX_in = 0; 
  my $AVG_in = 0; 
  my $MAX_out = 0; 
  my $AVG_out = 0; 
  my $first_time = 0; 
  my $last_time = 0; 
  my $time_max_in = 0; 
  my $time_max_out = 0; 
    
  foreach my $value (@values) 
  { 
    my ($cur_time , $cur_AVG_in, $cur_AVG_out) = {0,0,0}; 
    
    ($cur_time , $cur_AVG_in, $cur_AVG_out) = split(" ", $value); 
    
    $cur_time =~ s/://; 
    
    if ( $cur_time <= 0 || $cur_AVG_in eq "nan" || $cur_AVG_out eq "nan" ) { 
      print "value (value error) : $value\n\n"; 
      next; 
    } 
    
    if ( $first_time == 0) { 
      $first_time = $cur_time; 
      $last_time = $cur_time; 
      next; 
    } 
    
    $AVG_in += $cur_AVG_in*8*($cur_time - $last_time); 
    $AVG_out += $cur_AVG_out*8*($cur_time - $last_time); 
    
    if ( $cur_AVG_in*8 > $MAX_in ) 
    { 
      $MAX_in = $cur_AVG_in*8; 
      $time_max_in = $cur_time; 
    } 
  
    if ( $cur_AVG_out*8 > $MAX_out ) 
    { 
      $MAX_out = $cur_AVG_out*8; 
      $time_max_out = $cur_time; 
    } 
    
    $last_time = $cur_time; 
  } 
  
  
  print "total in : $AVG_in\n"; 
  print "total out : $AVG_out\n"; 
  $AVG_in = $AVG_in / ( $last_time - $first_time ); 
  $AVG_out = $AVG_out / ( $last_time - $first_time );      
  
    
  print "result from $first_time to $last_time (".($last_time - $first_time).") \n"; 
  print "AVG_in = $AVG_in MAX_in = $MAX_in ($time_max_in)\n"; 
  print "AVG_out = $AVG_out MAX_out = $MAX_out ($time_max_out)\n";


my polling time is 10s for the first 24h. 

the maximum values are exact but the average value are always false... 
i have the good number of value (using wc or a counter in the script) and the times values are ok. 

Maybe someone know how rrdtool calculate the average value on graph, maybe there is something different... 

here is the rrd file generation command line : 

Code:
/usr/bin/rrdtool create \ 
file.rrd \ 
--step 10  \ 
DS:traffic_in:COUNTER:600:0:1000000000 \ 
DS:traffic_out:COUNTER:600:0:1000000000 \ 
RRA:AVERAGE:0.5:1:18000 \ 
RRA:AVERAGE:0.5:6:9000 \ 
RRA:MAX:0.5:1:18000 \ 
RRA:MAX:0.5:6:9000 \


here is the rrdtool graph command : 

Code:
/usr/bin/rrdtool graph - \ 
--imgformat=PNG \ 
--start=-86400 \ 
--end=-10 \ 
--title="device - Traffic - Slot x, Port x" \ 
--rigid \ 
--base=1000 \ 
--height=120 \ 
--width=500 \ 
--alt-autoscale-max \ 
--lower-limit=0 \ 
--vertical-label="bits per second" \ 
DEF:a="/local/home/Network/cacti/cacti/rra/file.rrd":traffic_in:MAX \ 
DEF:b="/local/home/Network/cacti/cacti/rra/file.rrd":traffic_out:MAX \ 
CDEF:cdefa=a,8,* \ 
CDEF:cdefe=b,8,* \ 
AREA:cdefa#00CF00:"Inbound"  \ 
GPRINT:cdefa:LAST:" Current\:%8.2lf %s"  \ 
GPRINT:cdefa:AVERAGE:"Average\:%8.2lf %s"  \ 
GPRINT:cdefa:MAX:"Maximum\:%8.2lf %s\n"  \ 
LINE1:cdefe#002A97:"Outbound"  \ 
GPRINT:cdefe:LAST:"Current\:%8.2lf %s"  \ 
GPRINT:cdefe:AVERAGE:"Average\:%8.2lf %s"  \ 
GPRINT:cdefe:MAX:"Maximum\:%8.2lf %s\n"


Thanks in advance for the help 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.oetiker.ch/pipermail/rrd-users/attachments/20080708/c216145b/attachment.html 


More information about the rrd-users mailing list