[rrd-users] perl and lastupdate

Tobias Oetiker tobi at oetiker.ch
Mon Feb 21 23:16:58 CET 2011


Sean, Joshua,

note that RRDs::info gives you last update and all other meta
information you would ever want.

cheers
tobi
Today Joshua Keroes wrote:

> 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;
> }
>

-- 
Tobi Oetiker, OETIKER+PARTNER AG, Aarweg 15 CH-4600 Olten, Switzerland
http://it.oetiker.ch tobi at oetiker.ch ++41 62 775 9902 / sb: -9900



More information about the rrd-users mailing list