Hi Sean,<div><br></div><div>I agree that this functionality should be in RRDs.</div><div><br></div><div>That said, you can implement lastupdate() without the shell call thus.</div><div><br></div><div>-Joshua</div><div><font class="Apple-style-span" size="1"><br>
</font></div><div><div><font class="Apple-style-span" face="'courier new', monospace" size="1">use RRDs;</font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"><br></font></div>
<div><font class="Apple-style-span" face="'courier new', monospace" size="1">my $step = 300;</font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"><br></font></div><div>
<font class="Apple-style-span" face="'courier new', monospace" size="1">die "Usage: $0 file.rrd [file.rrd ...]" unless @ARGV;</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace" size="1"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1">for (@ARGV) {</font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"> print join ", ", lastupdate($_);</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace" size="1">}</font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1">exit;</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace" size="1"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"># Accepts: file.rrd, [--daemon hostname] (same args as rrdtool lastupdate)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace" size="1"># Returns: timestamp, data, [data, ...] (same return values, too)</font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1">sub lastupdate {</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace" size="1"> my ($file, @args) = @_;</font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"><br></font></div>
<div><font class="Apple-style-span" face="'courier new', monospace" size="1"> my $ts = RRDs::last(@_);</font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"> die "lastupdate() last error: " . RRDs::error if RRDs::error;</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace" size="1"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"> # Assume step is 300. If yours is different, fetch it with RRDs::info().</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace" size="1"> # The start value is earlier than last's timestamp because rrdtool stores</font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"> # deltas. I'm curious if we can set both -s and -e to $ts - 300 or if there's</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace" size="1"> # a possibility that we might be off by one. This is the safer solution.</font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"> #</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace" size="1"> my ($start, $step, $names, $data) = RRDs::fetch(</font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"> $file, </font></div>
<div><font class="Apple-style-span" face="'courier new', monospace" size="1"> 'AVERAGE', </font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"> '-s' => $ts - 300,</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace" size="1"> '-e' => $ts, </font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"> @args</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace" size="1"> );</font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"> die "lastupdate() fetch error: " . RRDs::error if RRDs::error;</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace" size="1"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"> # Find the last row with defined datapoints.</font></div>
<div><span class="Apple-style-span" style="font-family: 'courier new', monospace; font-size: x-small; "> #</span></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"> my @last_data;</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace" size="1"> for my $i (reverse 0..$#$data) {</font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"> if ( grep { defined } @{$data->[$i]} ) {</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace" size="1"> return ($start + $step * $i, @{$data->[$i]});</font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"> }</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace" size="1"> }</font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"> </font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1"> warn "No data found in $file";</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace" size="1"> return;</font></div><div><font class="Apple-style-span" face="'courier new', monospace" size="1">}</font></div></div><div><br>
</div>