bengmau has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'd like to layer two graph plots (in and out traffic) on the same graph using rrdtool::oo wrapper module you can enter multiple datapoints in one object but my second set of data may not have the same time keys. therefore, I decided to separate the entering of data into seperate loops. However when populating the outbound hash, the script crashes. I think I have to create a separate rrd object and then draw them both on the same graph however if anyone sees a quick fix to the code below that doesn't require that. I'm all ears. here's my code snippet:
sub nfgraph { my $hashref=shift; #my $hashrefout=shift; my $title=shift; my $start = timegm(0, 0, 0, 1, $args{month} - 1, $args{year} - 1900) +; my $end; if( 12 == $args{month} ) { $end = timegm(0, 0, 0, 1, 0, $args{year} - 1900 + 1) - 1; } else { $end = timegm(0, 0, 0, 1, $args{month}, $args{year} - 1900) - 1; } # create an RRD database my(undef, $tempfname) = tempfile( DIR => $tempdir, SUFFIX => '.rrd', UNLINK => 1, ); my $rrd = RRDTool::OO->new( file => $tempfname ) or LOGDIE("can't create RRDTool::OO object"); $rrd->create( start => $start, data_source => { name => $title, type => 'GAUGE', }, data_source => { name => 'out', type => 'GAUGE', }, archive => { rows => 500, cpoints => 12, cfunc => 'MAX', }, archive => { rows => 500, cpoints => 12, cfunc => 'MAX', }, ) or LOGDIE("can't create RRD archive"); #DEBUG("created RRD database $tempfname"); my $totalaccum=0; open (NFIN,">accum$title.csv"); for my $time( sort keys %$hashref ) { $totalaccum+=$hashref->{$time}; print NFIN "$time,$totalaccum\n"; $rrd->update( time => $time, value=$totalaccum ); } close(NFIN); my $totalaccumout=0; open (NFOUT,">accumout.csv"); for my $otime(sort keys %$hashrefout) { $totalaccumout+=$hashrefout->{$otime}; print NFOUT "$otime,$totalaccumout\n"; $rrd->update( time => $otime, values => {out => $totalaccumout +} ); } close(NFOUT);

Replies are listed 'Best First'.
Re: rrdtool:oo two separate datasets
by FitTrend (Pilgrim) on Feb 11, 2005 at 18:20 UTC

    We use RRD-Tool as part of our company products. The problem is that each RRD file requires a heartbeat which tracks the timeframe in which an value can be populated. Based on how you're describing it, RRD will already have a value populated on a certain epoch time. The second data point cannot be insert into it because the RRD file already has an entry within that heartbeat time frame.

    You will need to insert both values (in and out) at the same time.

    I recommend that you create a hash that contains both the in and out values, then loop through the hash. My example is showing this without the RRDTool::OO module. But I'm giving you a conceptual way to do it.

    use RRDs; ## FORMAT IN::OUT VALUES $rrdFile{'foo.rrd'} = "323::460"; $rrdFile{'bar.rrd'} = "322::110"; $RrdEpochTime = time(); foreach (keys (%rrdFile)) { ($in, $out) = split (/::/, $rrdFile{$_}); RRDs::update("$_","$RrdEpochTime:$in:$out"); }

    The goal here is to assemble a hash that contains your data, then loop once to store it in RRD.

    This should help get your gears moving. I've had to use this method before when polling a large amount of SNMP data.