Skip to content

Commit

Permalink
Try this one.
Browse files Browse the repository at this point in the history
Appears to work well for both "canned" and custom graphs.
  • Loading branch information
NOYB committed Jun 8, 2016
1 parent e99ba5e commit 3d5e745
Showing 1 changed file with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@
$rrd_location = "/var/db/rrd/";

//lookup end time based on resolution (ensure resolution interval)
$endLookup = array(
"60" => "-2min",
"300" => "-5min",
"3600" => "-1hour",
"86400" => "-1day"
$resolutionLookup = array(
"60" => "1min",
"300" => "5min",
"3600" => "1hour",
"86400" => "1day"
);

//TODO security/validation checks
Expand All @@ -82,32 +82,41 @@
$left_pieces = explode("-", $left);
$right_pieces = explode("-", $right);

$rrd_info_array = rrd_info($rrd_location . $left . ".rrd");
$left_last_updated = $rrd_info_array['last_update'];
// Get the last updated time of left and right axis RRD, or if not exist set to a very large value (decades into the future)
$left_rrd_info_array = rrd_info($rrd_location . $left . ".rrd");
$left_last_updated = ($left_rrd_info_array['last_update']) ? $left_rrd_info_array['last_update'] : 99999999999;

$rrd_info_array = rrd_info($rrd_location . $right . ".rrd");
$right_last_updated = $rrd_info_array['last_update'];
$right_rrd_info_array = rrd_info($rrd_location . $right . ".rrd");
$right_last_updated = ($right_rrd_info_array['last_update']) ? $right_rrd_info_array['last_update'] : 99999999999;

$last_updated = max($left_last_updated, $right_last_updated);
// Use the earlier (smaller) of left or right last updated time
$last_updated = min($left_last_updated, $right_last_updated);

if ($timePeriod === "custom") {
// Determine highest resolution available for requested time period
// Should be possible to determine programmaticly from the RRD header info array (rrd_info).
$rrd_options = array( 'AVERAGE', '-a', '-s', $start, '-e', $start );
$left_rrd_array = rrd_fetch($rrd_location . $left . ".rrd", $rrd_options);
$right_rrd_array = rrd_fetch($rrd_location . $right . ".rrd", $rrd_options);
$resolution = max($left_rrd_array['step'], $right_rrd_array['step']);

// make sure end time isn't later than last updated time entry
if( $end > $last_updated ) { $end = $last_updated; }

/* ensure resolution intreval */
$resolution = 60; //defaults to highest resolution available
$start = floor($start/$resolution) * $resolution;
$end = floor($end/$resolution) * $resolution;

$rrd_options = array( 'AVERAGE', '-r', $resolution, '-s', $start, '-e', $end );
// Minus resolution to prevent last value 0 (zero).
$end -= $resolution;

// make sure start time isn't later than end time
if ($start > $end) { $start = $end; }
} else {

$rrd_options = array( 'AVERAGE', '-r', $resolution, '-s', 'e'.$timePeriod, '-e', $endLookup[$resolution] );

// Use end time reference in 'start' to retain time period length.
$start = 'end' . $timePeriod . '+'.$resolutionLookup[$resolution];
// Use the RRD last updated time as end, minus resolution to prevent last value 0 (zero).
$end = $last_updated . '-'.$resolutionLookup[$resolution];
}

$rrd_options = array( 'AVERAGE', '-a', '-r', $resolution, '-s', $start, '-e', $end );

//Initialze
$left_unit_acronym = $right_unit_acronym = "";

Expand Down

0 comments on commit 3d5e745

Please sign in to comment.