[rrd-users] perl and lastupdate

Joshua Keroes joshua at keroes.com
Mon Feb 21 22:57:57 CET 2011


Hi Sean,

I agree that this functionality should be in RRDs.

That said, you can implement lastupdate() without the shell call thus.

-Joshua

use RRDs;

my $step = 300;

die "Usage: $0 file.rrd [file.rrd ...]" unless @ARGV;

for (@ARGV) {
    print join ", ", lastupdate($_);
}

exit;


# Accepts: file.rrd, [--daemon hostname] (same args as rrdtool lastupdate)
# Returns: timestamp, data, [data, ...]  (same return values, too)
sub lastupdate {
    my ($file, @args) = @_;

    my $ts = RRDs::last(@_);
    die "lastupdate() last error: " . RRDs::error if RRDs::error;

    # Assume step is 300. If yours is different, fetch it with RRDs::info().
    # The start value is earlier than last's timestamp because rrdtool
stores
    # deltas. I'm curious if we can set both -s and -e to $ts - 300 or if
there's
    # a possibility that we might be off by one. This is the safer solution.
    #
    my ($start, $step, $names, $data) = RRDs::fetch(
        $file,
        'AVERAGE',
        '-s' => $ts - 300,
        '-e' => $ts,
        @args
    );
    die "lastupdate() fetch error: " . RRDs::error if RRDs::error;

    # Find the last row with defined datapoints.
    #
    my @last_data;
    for my $i (reverse 0..$#$data) {
        if ( grep { defined } @{$data->[$i]} ) {
            return ($start + $step * $i, @{$data->[$i]});
        }
    }

    warn "No data found in $file";
    return;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.oetiker.ch/pipermail/rrd-users/attachments/20110221/f6db15e0/attachment-0001.htm 


More information about the rrd-users mailing list