+ function fetch($cf = 'AVERAGE', $resolution = null, $start = null, $end = null) {
+ // fetch data from a RRD
+ if (!in_array($this->status, array('ok','readonly'))) { trigger_error('Error: rrd status is '.$this->status, E_USER_WARNING); return false; }
+
+ if (!in_array($cf, array('AVERAGE','MIN','MAX','LAST'))) { $cf = 'AVERAGE'; }
+ if (!is_numeric($resolution)) { $resolution = $this->rrd_step; }
+ if (!is_numeric($end)) { $end = $this->last_update(); }
+ elseif ($end < 0) { $end += $this->last_update(); }
+ $end = intval($end/$resolution)*$resolution;
+ if (!is_numeric($start)) { $start = $end; }
+ elseif ($start < 0) { $start += $end; }
+ $start = intval($start/$resolution)*$resolution;
+
+ $fetch_cmd = 'rrdtool fetch '.$this->rrd_file.' '.$cf.' --resolution '.$resolution.' --start '.$start.' --end '.$end;
+ $return = `$fetch_cmd 2>&1`;
+
+ if (strpos($return, 'ERROR') !== false) {
+ trigger_error('rrd fetch error: '.$return, E_USER_WARNING);
+ $fresult = false;
+ }
+ else {
+ $fresult = array();
+ $rows = explode("\n", $return);
+ $fields = preg_split('/\s+/', array_shift($rows));
+ if (array_shift($fields) == 'timestamp') {
+ $fresult[0] = $fields;
+ foreach ($rows as $row) {
+ if (strlen(trim($row))) {
+ $rvals = preg_split('/\s+/', $row);
+ $rtime = array_shift($rvals);
+ $rv_array = array();
+ foreach ($rvals as $key=>$rval) {
+ $rv_array[$fields[$key]] = ($rval=='nan')?null:floatval($rval);
+ }
+ $fresult[$rtime] = $rv_array;
+ }
+ }
+ }
+ }
+ return $fresult;
+ }