From 586031cebd8083680f2422f75fd513d2a234a324 Mon Sep 17 00:00:00 2001 From: robert Date: Sat, 11 Mar 2006 18:45:49 +0000 Subject: [PATCH] step to get RRD stats to work perfectly with RRDtool 1.2 --- include/classes/rrdstat.php-class | 256 +++++++++++++++++++----------- 1 file changed, 159 insertions(+), 97 deletions(-) diff --git a/include/classes/rrdstat.php-class b/include/classes/rrdstat.php-class index 590fc3c..a87ea9c 100644 --- a/include/classes/rrdstat.php-class +++ b/include/classes/rrdstat.php-class @@ -75,6 +75,9 @@ class rrdstat { // set definitions based on given configuration // [intended for internal use, called by the constructor] // + // function rrd_version() { + // get RRDtool version string + // // function create() // create RRD file according to set config // @@ -267,6 +270,26 @@ class rrdstat { $this->config_all = $complete_conf; } + function rrd_version() { + // return RRDtool version + static $version; + if (!isset($version)) { + $create_cmd = 'rrdtool --version'; + $return = `$create_cmd 2>&1`; + if (strpos($return, 'ERROR') !== false) { + trigger_error($this->rrd_file.' - rrd version error: '.$return, E_USER_WARNING); + } + + if (preg_match('/^\s*RRDtool ([\d\.]+)\s+/', $return, $regs)) { + $version = $regs[1]; + } + else { + $version = '0.0'; + } + } + return $version; + } + function create() { // create RRD file @@ -605,16 +628,22 @@ class rrdstat { $srow['sType'] = isset($crow['sType'])?$crow['sType']:'COMMENT'; if ($grow['sType'] != 'COMMENT') { // XXX: use line below and remove cf var once we have rrdtol 1.2 - // $srow['name'] = $crow['name'].(isset($crow['cf'])?'_'.$crow['cf']:''); - $srow['name'] = $crow['name']; - $srow['cf'] = isset($crow['cf'])?$crow['cf']:'AVERAGE'; + if ($this->rrd_version() >= '1.2') { + $srow['name'] = $crow['name'].(isset($crow['cf'])?'_'.$crow['cf']:''); + } + else { + $srow['name'] = $crow['name']; + $srow['cf'] = isset($crow['cf'])?$crow['cf']:'AVERAGE'; + } if (isset($crow['cf'])) { - // XXX: use line below once we have rrdtol 1.2 - // $graphrows[] = array('dType'=>'VDEF', 'name'=>$srow['name'].'_'.$crow['cf'], 'rpn_expr'=>$srow['name'].','.$crow['cf']); + if ($this->rrd_version() >= '1.2') { + $graphrows[] = array('dType'=>'VDEF', 'name'=>$srow['name'].'_'.$crow['cf'], 'rpn_expr'=>$srow['name'].','.$crow['cf']); + } } elseif (isset($crow['rpn_expr'])) { - // XXX: does only work with rrdtool 1.2 - $graphrows[] = array('dType'=>'VDEF', 'name'=>$srow['name'], 'rpn_expr'=>$crow['rpn_expr']); + if ($this->rrd_version() >= '1.2') { + $graphrows[] = array('dType'=>'VDEF', 'name'=>$srow['name'], 'rpn_expr'=>$crow['rpn_expr']); + } } } $srow['text'] = isset($crow['text'])?$crow['text']:''; @@ -626,12 +655,19 @@ class rrdstat { foreach ($graphrows as $grow) { if (isset($grow['gType']) && strlen($grow['gType'])) { $textprefix = isset($grow['desc'])?$grow['desc']:(isset($grow['legend'])?$grow['legend']:$grow['name']); - // XXX: use lines below once we have rrdtol 1.2 - // $graphrows[] = array('dType'=>'VDEF', 'name'=>$grow['name'].'_last', 'rpn_expr'=>$grow['name'].',LAST'); - // $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'].'_last', 'text'=>'%3.2lf%s'); - $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'], 'cf'=>'MAX', 'text'=>$textprefix.'|'.dgettext($td, 'Maximum').'|%.2lf%s'); - $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'], 'cf'=>'AVERAGE', 'text'=>$textprefix.'|'.dgettext($td, 'Average').'|%.2lf%s'); - $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'], 'cf'=>'LAST', 'text'=>$textprefix.'|'.dgettext($td, 'Current').'|%.2lf%s'); + if ($this->rrd_version() >= '1.2') { + $graphrows[] = array('dType'=>'VDEF', 'name'=>$grow['name'].'_max', 'rpn_expr'=>$grow['name'].',MAXIMUM'); + $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'].'_max', 'text'=>$textprefix.'|'.dgettext($td, 'Maximum').'|%.2lf%s'); + $graphrows[] = array('dType'=>'VDEF', 'name'=>$grow['name'].'_avg', 'rpn_expr'=>$grow['name'].',AVERAGE'); + $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'].'_avg', 'text'=>$textprefix.'|'.dgettext($td, 'Average').'|%.2lf%s'); + $graphrows[] = array('dType'=>'VDEF', 'name'=>$grow['name'].'_last', 'rpn_expr'=>$grow['name'].',LAST'); + $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'].'_last', 'text'=>$textprefix.'|'.dgettext($td, 'Current').'|%.2lf%s'); + } + else { + $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'], 'cf'=>'MAX', 'text'=>$textprefix.'|'.dgettext($td, 'Maximum').'|%.2lf%s'); + $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'], 'cf'=>'AVERAGE', 'text'=>$textprefix.'|'.dgettext($td, 'Average').'|%.2lf%s'); + $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'], 'cf'=>'LAST', 'text'=>$textprefix.'|'.dgettext($td, 'Current').'|%.2lf%s'); + } } } } @@ -676,19 +712,27 @@ class rrdstat { } if (isset($grow['gType']) && strlen($grow['gType'])) { // XXX: change from STACK type to STACK flag once we have rrdtool 1.2 - if (isset($grow['stack']) && $grow['stack']) { $grow['gType'] = 'STACK'; } + if ($this->rrd_version() < '1.2') { + // rrdtool 1.0 only know STACK type + if (isset($grow['stack']) && $grow['stack']) { $grow['gType'] = 'STACK'; } + } $gGraphs .= ' '.$grow['gType'].':'.$grow['name'].$grow['color']; if (isset($grow['legend'])) { $gGraphs .= ':'.$this->text_quote($grow['legend']); } - // XXX: remove above STACK if-command and uncomment the one below once we have rrdtool 1.2 - //if (isset($grow['stack']) && $grow['stack']) { $gGraphs .= ':STACK'; } + if ($this->rrd_version() >= '1.2') { + // rrdtool 1.2 and above have STACK flag + if (isset($grow['stack']) && $grow['stack']) { $gGraphs .= ':STACK'; } + } } } foreach ($specialrows as $srow) { $addSpecial .= ' '.$srow['sType']; - // XXX: eliminate cf once we have rrdtool 1.2 - // $addSpecial .= ($grow['sType']!='COMMENT')?':'.$grow['name']:''); - $addSpecial .= (($srow['sType']!='COMMENT')?':'.$srow['name'].':'.$srow['cf']:''); + if ($this->rrd_version() >= '1.2') { + $addSpecial .= (($srow['sType']!='COMMENT')?':'.$srow['name']:''); + } + else { + $addSpecial .= (($srow['sType']!='COMMENT')?':'.$srow['name'].':'.$srow['cf']:''); + } $addSpecial .= ':'.$this->text_quote($srow['text']); } @@ -697,7 +741,11 @@ class rrdstat { if (strpos($return, 'ERROR') !== false) { trigger_error($this->rrd_file.' - rrd graph error: '.$return, E_USER_WARNING); - $return = $graph_cmd."\n\n".$return; + $return = 'command:'.$graph_cmd."\n\n".$return; + } + if (0) { + // debug output + $return = 'command:'.$graph_cmd."\n\n".$return; } $legendlines = ''; foreach ($graphrows as $grow) { @@ -715,11 +763,18 @@ class rrdstat { // create a RRD graph and return meta info as a ready-to-use array $gmeta = array('filename'=>null,'legends_long'=>false,'default_colorize'=>false); $ret = $this->graph($timeframe, $sub, $extra); - if (strpos($ret, "\n\n") !== false) { $gmeta['graph_cmd'] = substr($ret, 0, strpos($ret, "\n\n")); $ret = substr($ret, strpos($ret, "\n\n")+2); } - else { $gmeta['graph_cmd'] = null; } + if (0) { + // debug output + $gmeta['ret'] = $ret; + } +// if (preg_match('/\ncommand:(.*?)\n\n/', $ret, $regs)) { $gmeta['graph_cmd'] = $regs[1]; $ret = str_replace($regs[0], "\n",$ret); } +// else { $gmeta['graph_cmd'] = null; } $grout = explode("\n", $ret); foreach ($grout as $gline) { - if (preg_match('/^file:(.+)$/', $gline, $regs)) { + if (preg_match('/^command:(.+)$/', $gline, $regs)) { + $gmeta['graph_cmd'] = $regs[1]; + } + elseif (preg_match('/^file:(.+)$/', $gline, $regs)) { $gmeta['filename'] = $regs[1]; } elseif (preg_match('/^legend:([^\|]+)\|([^|]*)\|([^\|]*)\|(.*)$/', $gline, $regs)) { @@ -791,26 +846,26 @@ class rrdstat { $td = $this->mod_textdomain; $ptitle = isset($pconf['title_page'])?$pconf['title_page']:dgettext($td, 'RRD statistics index'); - $out = ''; - $out .= ''.$ptitle.''; - $out .= ''; - $out .= ''; - $out .= ''; + $out .= ''."\n"; + $out .= ''."\n"; + $out .= ''."\n"; - $out .= '

'.$ptitle.'

'; + $out .= '

'.$ptitle.'

'."\n"; if (isset($pconf['text_intro']) && strlen($pconf['text_intro'])) { - $out .= '

'.$pconf['text_intro'].'

'; + $out .= '

'.$pconf['text_intro'].'

'."\n"; } elseif (!isset($pconf['text_intro'])) { - $out .= '

'.dgettext($td, 'The following RRD stats are available:').'

'; + $out .= '

'.dgettext($td, 'The following RRD stats are available:').'

'."\n"; } $stats = $this->h_page_statsArray($pconf); @@ -821,7 +876,7 @@ class rrdstat { if (isset($pconf['stats_url_add'])) { $sURL_add = $pconf['stats_url_add']; } else { $sURL_add = '&sub=%s'; } - $out .= ''."\n"; $out .= $this->h_page_footer(); - $out .= ''; + $out .= ''."\n"; return $out; } @@ -852,22 +907,22 @@ class rrdstat { $td = $this->mod_textdomain; $ptitle = isset($pconf['title_page'])?$pconf['title_page']:dgettext($td, 'RRD statistics overview'); - $out = ''; - $out .= ''.$ptitle.''; - $out .= ''; - $out .= ''; - $out .= ''; + $out .= ''."\n"; + $out .= ''."\n"; + $out .= ''."\n"; - $out .= '

'.$ptitle.'

'; + $out .= '

'.$ptitle.'

'."\n"; if (isset($pconf['text_intro']) && strlen($pconf['text_intro'])) { $out .= '

'.$pconf['text_intro'].'

'; } $stats = $this->h_page_statsArray($pconf); @@ -881,12 +936,12 @@ class rrdstat { $num_rows = is_numeric($pconf['num_rows'])?$pconf['num_rows']:2; $num_cols = ceil(count($stats)/$num_rows); - $out .= ''; + $out .= '
'."\n"; for ($col = 0; $col < $num_cols; $col++) { - $out .= ''; + $out .= ''."\n"; for ($row = 0; $row < $num_rows; $row++) { $idx = $col * $num_rows + $row; - $out .= ''; + $out .= ''."\n"; } - $out .= ''; + $out .= ''."\n"; } - $out .= '
'; + $out .= ''."\n"; if ($idx < count($stats)) { @list($sname, $s_psub) = explode('|', $stats[$idx]['name'], 2); $s_psname = 'page'.(isset($s_psub)?'.'.$s_psub:''); @@ -902,7 +957,7 @@ class rrdstat { $s_ptitle = isset($s_psub)?sprintf(dgettext($td, '%s (%s) statistics'), $sname, $s_psub):sprintf(dgettext($td, '%s statistics'), $sname); } if (!isset($pconf['hide_titles']) || !$pconf['hide_titles']) { - $out .= '

'.$s_ptitle.'

'; + $out .= '

'.$s_ptitle.'

'."\n"; } $s_rrd = new rrdstat($this->config_all, $sname); @@ -925,23 +980,23 @@ class rrdstat { $out .= 'basename.(!is_null($g_sub)?' - '.$g_sub:'').' - '.$tframe.'" class="rrdgraph"'; if (isset($gmeta['width']) && isset($gmeta['height'])) { $out .= ' style="width:'.$gmeta['width'].'px;height:'.$gmeta['height'].'px;"'; } - $out .= '>'; + $out .= '>'."\n"; } else { - $out .= sprintf(dgettext($td, 'RRD error: status is "%s"'), $s_rrd->status); + $out .= sprintf(dgettext($td, 'RRD error: status is "%s"'), $s_rrd->status)."\n"; } } else { $out .= ' '; } - $out .= '
'; + $out .= ''."\n"; $out .= $this->h_page_footer(); - $out .= ''; + $out .= ''."\n"; return $out; } @@ -957,35 +1012,35 @@ class rrdstat { $gtitle['year'] = isset($pconf['title_year'])?$pconf['title_year']:dgettext($td, 'Year overview (scaling 1 day)'); $ltitle = isset($pconf['title_legend'])?$pconf['title_legend']:dgettext($td, 'Legend:'); - $out = ''; - $out .= ''.$ptitle.''; - $out .= ''; - $out .= ''; - $out .= ''; + $out .= ''."\n"; + $out .= ''."\n"; + $out .= ''."\n"; - $out .= '

'.$ptitle.'

'; - if (isset($pconf['text_intro']) && strlen($pconf['text_intro'])) { $out .= '

'.$pconf['text_intro'].'

'; } + $out .= '

'.$ptitle.'

'."\n"; + if (isset($pconf['text_intro']) && strlen($pconf['text_intro'])) { $out .= '

'.$pconf['text_intro'].'

'."\n"; } if (!isset($pconf['show_update']) || $pconf['show_update']) { $out .= '

'; if (is_null($this->last_update())) { $up_time = dgettext($td, 'unknown'); } elseif (class_exists('baseutils')) { $up_time = baseutils::dateFormat($this->last_update(), 'short'); } else { $up_time = date('Y-m-d H:i:s', $this->last_update()); } $out .= sprintf(dgettext($td, 'Last Update: %s'), $up_time); - $out .= '

'; + $out .= '

'."\n"; } $g_sub = isset($pconf['graph_sub'])?$pconf['graph_sub']:null; @@ -1001,16 +1056,23 @@ class rrdstat { else { $gURL = $gmeta['filename']; } - $out .= '
'; -// $out .= '

'.nl2br($ret).'

'; - $out .= '

'.$gtitle[$tframe].'

'; + $out .= '
'."\n"; + if (0) { + // debug output + ob_start(); + print_r($gmeta); + $buffer = ob_get_contents(); + ob_end_clean(); + $out .= '

'.nl2br($buffer).'

'; + } + $out .= '

'.$gtitle[$tframe].'

'."\n"; $out .= 'basename.(!is_null($g_sub)?' - '.$g_sub:'').' - '.$tframe.'" class="rrdgraph"'; if (isset($gmeta['width']) && isset($gmeta['height'])) { $out .= ' style="width:'.$gmeta['width'].'px;height:'.$gmeta['height'].'px;"'; } - $out .= '>'; + $out .= '>'."\n"; $colorize_data = (isset($pconf['data_colorize']) && $pconf['data_colorize']) || (!isset($pconf['data_colorize']) && $gmeta['default_colorize']); if (isset($gmeta['data']) && count($gmeta['data'])) { - $out .= ''; + $out .= '
'."\n"; foreach ($gmeta['data'] as $field=>$gdata) { $out .= '$gval) { $out .= ''; } - $out .= ''; + $out .= ''."\n"; } - $out .= '
'.$gkey.': '.$gval.'
'; + $out .= ''."\n"; } if (isset($gmeta['var']) && count($gmeta['var'])) { foreach ($gmeta['var'] as $gkey=>$gval) { - $out .= '

'.$gkey.': '.$gval.'

'; + $out .= '

'.$gkey.': '.$gval.'

'."\n"; } } if (isset($gmeta['info']) && count($gmeta['info'])) { foreach ($gmeta['info'] as $gval) { - $out .= '

'.$gval.'

'; + $out .= '

'.$gval.'

'."\n"; } } - $out .= '
'; + $out .= '
'."\n"; } if ($gmeta['legends_long'] && (!isset($pconf['show_legend']) || $pconf['show_legend'])) { - $out .= '
'; - $out .= '

'.$ltitle.'

'; - $out .= ''; + $out .= '
'."\n"; + $out .= '

'.$ltitle.'

'."\n"; + $out .= '
'."\n"; foreach ($gmeta['legend'] as $field=>$legend) { if (strlen($legend['desc_long'])) { $out .= ''; $out .= ''; - $out .= ''; + $out .= ''."\n"; } } - $out .= '
'.$legend['desc_long'].'
'; - $out .= '
'; + $out .= ''."\n"; + $out .= ''."\n"; } } else { - $out .= sprintf(dgettext($td, 'RRD error: status is "%s"'), $this->status); + $out .= sprintf(dgettext($td, 'RRD error: status is "%s"'), $this->status)."\n"; } $out .= $this->h_page_footer(); - $out .= ''; + $out .= ''."\n"; return $out; } @@ -1118,7 +1180,7 @@ class rrdstat { $out .= sprintf(dgettext($this->mod_textdomain, 'Statistics created with %s using a library created by %s.'), 'RRDtool', 'KaiRo.at'); - $out .= '

'; + $out .= '

'."\n"; return $out; } -- 2.35.3