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: Select all
#!/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";
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: Select all
/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 \
(PS : sorry for my english, i come from France, and my english is not very very good...)