5e8cc6659c92b68f849e72bd2800c0a39b554ebf
[php-utility-classes.git] / include / classes / rrdstat.php-class
1 <?php
2 /* ***** BEGIN LICENSE BLOCK *****
3  *
4  * The contents of this file are subject to Austrian copyright reegulations
5  * ("Urheberrecht"); you may not use this file except in compliance with
6  * those laws.
7  * This contents and any derived work, if it gets distributed in any way,
8  * is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
9  * either express or implied.
10  *
11  * The Original Code is KaiRo's RRD statistics class.
12  *
13  * The Initial Developer of the Original Code is
14  * KaiRo - Robert Kaiser.
15  * Portions created by the Initial Developer are Copyright (C) 2005
16  * the Initial Developer. All Rights Reserved.
17  *
18  * Contributor(s): Robert Kaiser <kairo@kairo.at>
19  *
20  * ***** END LICENSE BLOCK ***** */
21
22 class rrdstat {
23   // rrdstat PHP class
24   // rrdtool statistics functions
25   //
26   // function rrdstat($rrdconfig, [$conf_id])
27   //   CONSTRUCTOR
28   //     if $conf_id is set, $rrdconfig is a total configuration set
29   //     else it's the configuration for this one RRD
30   //     currently only a config array is supported, XML config is planned
31   //
32   // var $rrd_file
33   // RRD file name
34   //
35   // var $basename
36   // base name for this RRD (usually file name without .rrd)
37   //
38   // var $basedir
39   // base directory for this RRD (with a trailing slash)
40   //   note that $rrd_file usually includes that path as well, but graph directory gets based on this value
41   //
42   // var $config_all
43   // complete, raw configuration array set
44   //
45   // var $config_raw
46   // configuration array set for current RRD
47   //
48   // var $config_graph
49   // configuration array set for default graph in this RRD
50   //
51   // var $config_page
52   // configuration array set for default page in this RRD
53   //
54   // var $rrd_fields
55   // definition of this RRD's fields
56   //
57   // var $rra_base
58   // definition of this RRD's base RRAs
59   //
60   // var $rrd_step
61   // basic stepping of this RRD in seconds (default: 300)
62   //
63   // var $rra_add_max
64   // should RRAs for MAX be added for every base RRA? (bool, default: true)
65   //
66   // var $status
67   // status of the RRD (unused/ok/readonly/graphonly)
68   //   note that most functions require certain status values
69   //   (e.g. update only works if status is ok, graph for ok/readonly/graphonly)
70   //
71   // function set_def($rrdconfig, [$conf_id])
72   //   set definitions based on given configuration
73   //   [intended for internal use, called by the constructor]
74   //
75   // function create()
76   //   create RRD file according to set config
77   //
78   // function update([$upArray])
79   //   feed new data into RRD (either use given array of values or use auto-update info from config)
80   //
81   // function fetch([$cf] = 'AVERAGE', $resolution = null, $start = null, $end = null)
82   //   fetch data from the defined RRD
83   //     using given consolidation function [default is AVERAGE],
84   //     resolution (seconds, default is the RRD's stepping),
85   //     start and end times (unix epoch, defaults are the RRD's last update time)
86   //
87   // function last_update()
88   //   fetch time of last update in this RRD file
89   //
90   // function graph([$timeframe], [$sub], [$extra])
91   //   create a RRD graph (and return all meta info in a flat string)
92   //     for given timeframe (day [default]/week/month/year),
93   //     sub-graph ID (if given) and extra config options (if given)
94   //
95   // function graph_plus([$timeframe], [$sub], [$extra])
96   //   create a RRD graph (see above) and return meta info as a ready-to-use array
97   //
98   // function page([$sub], [$page_extras], [$graph_extras])
99   //   create a (HTML) page and return it in a string
100   //     for given sub-page ID (if given, default is a simple HTML page)
101   //     and extra page and graph config options (if given)
102   //
103   // function simple_html([$sub], [$page_extras], [$graph_extras])
104   //   create a simple (MRTG-like) HTML page and return it in a string
105   //   XXX: this is here temporarily for compat only, it's preferred to use page()!
106   //
107   // function page_index($pconf)
108   //   create a bare, very simple index list HTML page and return it in a string
109   //   using given page config options
110   //   [intended for internal use, called by page()]
111   //
112   // function page_overview($pconf, [$graph_extras])
113   //   create an overview HTML page (including graphs) and return it in a string
114   //   using given page config options and extra graph options (if given)
115   //   [intended for internal use, called by page()]
116   //
117   // function page_simple($pconf, [$graph_extras])
118   //   create a simple (MRTG-like) HTML page and return it in a string
119   //   using given page config options and extra graph options (if given)
120   //   [intended for internal use, called by page()]
121   //
122   // function h_page_statsArray($pconf)
123   //   return array of stats to list on a page, using given page config options
124   //   [intended for internal use, called by page_*()]
125   //
126   // function h_page_footer()
127   //   return generic page footer
128   //   [intended for internal use, called by page_*()]
129   //
130   // function text_quote($text)
131   //   return a quoted/escaped text for use in rrdtool commandline text fields
132
133   var $rrd_file = null;
134   var $basename = null;
135   var $basedir = null;
136
137   var $config_all = null;
138   var $config_raw = null;
139   var $config_graph = null;
140   var $config_page = null;
141
142   var $rrd_fields = array();
143   var $rra_base = array();
144   var $rrd_step = 300;
145   var $rra_add_max = true;
146
147   var $status = 'unused';
148
149   function rrdstat($rrdconfig, $conf_id = null) {
150     // ***** init RRD stat module *****
151     $this->set_def($rrdconfig, $conf_id);
152
153     if (($this->status == 'unused') && !is_null($this->rrd_file)) {
154       if (!is_writeable($this->rrd_file)) {
155         if (!file_exists($this->rrd_file)) {
156           if (@touch($this->rrd_file)) { $this->create(); }
157           else { trigger_error('RRD file can not be created', E_USER_WARNING); }
158         }
159         else {
160           if (is_readable($this->rrd_file)) { $this->status = 'readonly'; }
161           else { trigger_error('RRD file is not readable', E_USER_WARNING); }
162         }
163       }
164       else {
165         $this->status = 'ok';
166       }
167     }
168   }
169
170   function set_def($rrdconfig, $conf_id = null) {
171     if (is_array($rrdconfig)) {
172       // we have an array in the format we like to have
173       $complete_conf =& $rrdconfig;
174     }
175     else {
176       // we have something else (XML data?), try to generate the iinfo aray from it
177       $complete_conf =& $rrdconfig;
178     }
179
180     if (!is_null($conf_id)) {
181       $iinfo = isset($complete_conf[$conf_id])?$complete_conf[$conf_id]:array();
182       if (isset($complete_conf['*'])) {
183         $iinfo = (array)$iinfo + (array)$complete_conf['*'];
184         if (isset($complete_conf['*']['graph'])) { $iinfo['graph'] = (array)$iinfo['graph'] + (array)$complete_conf['*']['graph']; }
185         if (isset($complete_conf['*']['page'])) { $iinfo['page'] = (array)$iinfo['page'] + (array)$complete_conf['*']['page']; }
186       }
187     }
188     else {
189       $iinfo = $complete_conf;
190     }
191
192     if (isset($iinfo['path']) && strlen($iinfo['path'])) {
193       $this->basedir = $iinfo['path'];
194       if (substr($this->basedir, -1) != '/') { $this->basedir .= '/'; }
195     }
196
197     if (isset($iinfo['graph-only']) && $iinfo['graph-only'] && !is_null($conf_id)) {
198       $this->basename = $conf_id;
199       $this->status = 'graphonly';
200     }
201     elseif (isset($iinfo['file'])) {
202       $this->rrd_file = (($iinfo['file']{0} != '/')?$this->basedir:'').$iinfo['file'];
203       $this->basename = (substr($this->rrd_file, -4) == '.rrd')?substr($this->rrd_file, 0, -4):$this->rrd_file;
204     }
205     elseif (!is_null($conf_id) && file_exists($conf_id.'.rrd')) {
206       $this->rrd_file = (($iinfo['file']{0} != '/')?$this->basedir:'').$conf_id.'.rrd';
207       $this->basename = $conf_id;
208     }
209     else {
210       $this->basename = !is_null($conf_id)?$conf_id:'xxx.unknown';
211     }
212
213     if (!is_null($this->rrd_file)) {
214       // fields (data sources, DS)
215       //  name - DS name
216       //  type - one of COUNTER, GAUGE, DERIVE, ABSOLUTE
217       //  heartbeat - if no sample recieved for that time, store UNKNOWN
218       //  min - U (unconstrained) or minimum value
219       //  max - U (unconstrained) or maximum value
220       //  update - this string will be fed into eval() for updating this field
221       if (isset($iinfo['fields']) && is_array($iinfo['fields'])) {
222         $this->rrd_fields = $iinfo['fields'];
223       }
224       else {
225         $this->rrd_fields[] = array('name' => 'ds0', 'type' => 'COUNTER', 'heartbeat' => 600, 'min' => 'U', 'max' => 'U');
226         $this->rrd_fields[] = array('name' => 'ds1', 'type' => 'COUNTER', 'heartbeat' => 600, 'min' => 'U', 'max' => 'U');
227       }
228
229
230       // MRTG-style RRD "database", see http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/tut/rrdtutorial.en.html
231       //
232       // archives (RRAs):
233       // 600 samples of 5 minutes  (2 days and 2 hours)
234       // 700 samples of 30 minutes (2 days and 2 hours, plus 12.5 days)
235       // 775 samples of 2 hours    (above + 50 days)
236       // 797 samples of 1 day      (above + 732 days, rounded up to 797)
237
238       $this->rrd_step = isset($iinfo['rrd_step'])?$iinfo['rrd_step']:300;
239
240       if (isset($iinfo['rra_base']) && is_array($iinfo['rra_base'])) {
241         $this->rra_base = $iinfo['rra_base'];
242       }
243       else {
244         $this->rra_base[] = array('step' => 1, 'rows' => 600);
245         $this->rra_base[] = array('step' => 6, 'rows' => 700);
246         $this->rra_base[] = array('step' => 24, 'rows' => 775);
247         $this->rra_base[] = array('step' => 288, 'rows' => 797);
248       }
249
250       $this->rra_add_max = isset($iinfo['rra_add_max'])?$iinfo['rra_add_max']:true;
251     }
252
253     if (isset($iinfo['graph'])) { $this->config_graph = $iinfo['graph']; }
254     if (isset($iinfo['page'])) { $this->config_page = $iinfo['page']; }
255     $this->config_raw = $iinfo;
256     $this->config_all = $complete_conf;
257   }
258
259   function create() {
260     // create RRD file
261
262     // compose create command
263     $create_cmd = 'rrdtool create '.$this->rrd_file.' --step '.$this->rrd_step;
264     foreach ($this->rrd_fields as $ds) {
265       if (!isset($ds['type'])) { $ds['type'] = 'COUNTER'; }
266       if (!isset($ds['heartbeat'])) { $ds['heartbeat'] = 2*$this->rrd_step; }
267       if (!isset($ds['min'])) { $ds['min'] = 'U'; }
268       if (!isset($ds['max'])) { $ds['max'] = 'U'; }
269       $create_cmd .= ' DS:'.$ds['name'].':'.$ds['type'].':'.$ds['heartbeat'].':'.$ds['min'].':'.$ds['max'];
270     }
271     foreach ($this->rra_base as $rra) {
272       if (!isset($rra['cf'])) { $rra['cf'] = 'AVERAGE'; }
273       if (!isset($rra['xff'])) { $rra['xff'] = 0.5; }
274       if (!isset($rra['step'])) { $rra['step'] = 1; }
275       if (!isset($rra['rows'])) { $rra['rows'] = 600; }
276       $create_cmd .= ' RRA:'.$rra['cf'].':'.$rra['xff'].':'.$rra['step'].':'.$rra['rows'];
277     }
278     if ($this->rra_add_max) {
279       foreach ($this->rra_base as $rra) {
280         if (!isset($rra['cf'])) {
281           // only rows that have no CF set will be looked at here
282           $rra['cf'] = 'MAX';
283           if (!isset($rra['xff'])) { $rra['xff'] = 0.5; }
284           if (!isset($rra['step'])) { $rra['step'] = 1; }
285           if (!isset($rra['rows'])) { $rra['rows'] = 600; }
286           $create_cmd .= ' RRA:'.$rra['cf'].':'.$rra['xff'].':'.$rra['step'].':'.$rra['rows'];
287         }
288       }
289     }
290     $return = `$create_cmd 2>&1`;
291     if (strpos($return, 'ERROR') !== false) {
292       trigger_error($this->rrd_file.' - rrd create error: '.$return, E_USER_WARNING);
293     }
294     else { $this->status = 'ok'; }
295   }
296
297   function update($upArray = null) {
298     // feed new data into RRD
299     if ($this->status != 'ok') { trigger_error('Cannot update non-writeable file', E_USER_WARNING); return false; }
300     $upvals = array();
301     if (isset($this->config_raw['update'])) {
302       if (preg_match('/^\s*function\s+{(.*)}\s*$/is', $this->config_raw['update'], $regs)) {
303         $upfunc = create_function('', $regs[1]);
304         $upvals = $upfunc();
305       }
306       else {
307         $evalcode = $this->config_raw['update'];
308         if (!is_null($evalcode)) {
309           ob_start();
310           eval($evalcode);
311           $ret = ob_get_contents();
312           if (strlen($ret)) { $upvals = explode("\n", $ret); }
313           ob_end_clean();
314         }
315       }
316     }
317     else {
318       foreach ($this->rrd_fields as $ds) {
319         if (is_array($upArray) && isset($upArray[$ds['name']])) { $val = $upArray[$ds['name']]; }
320         elseif (isset($ds['update'])) {
321           $val = null; $evalcode = null;
322           if (substr($ds['update'], 0, 4) == 'val:') {
323             $evalcode = 'function { return trim('.substr($ds['update'], 4).')); }';
324           }
325           elseif (substr($ds['update'], 0, 8) == 'snmp-if:') {
326             $snmphost = 'localhost'; $snmpcomm = 'public';
327             list($nix, $ifname, $valtype) = explode(':', $ds['update'], 3);
328             $iflist = explode("\n", `snmpwalk -v2c -c $snmpcomm $snmphost interfaces.ifTable.ifEntry.ifDescr`);
329             $ifnr = null;
330             foreach ($iflist as $ifdesc) {
331               if (preg_match('/ifDescr\.(\d+) = STRING: '.$ifname.'/', $ifdesc, $regs)) { $ifnr = $regs[1]; }
332             }
333             $oid = null;
334             if ($valtype == 'in') { $oid = '1.3.6.1.2.1.2.2.1.10.'.$ifnr; }
335             elseif ($valtype == 'out') { $oid = '1.3.6.1.2.1.2.2.1.16.'.$ifnr; }
336             if (!is_null($ifnr) && !is_null($oid)) {
337               $evalcode = 'function { return trim(substr(strrchr(`snmpget -v2c -c '.$snmpcomm.' '.$snmphost.' '.$oid.'`,":"),1)); }';
338             }
339           }
340           else { $evalcode = $ds['update']; }
341           if (preg_match('/^\s*function\s+{(.*)}\s*$/is', $evalcode, $regs)) {
342             $upfunc = create_function('', $regs[1]);
343             $val = $upfunc();
344           }
345           elseif (!is_null($evalcode)) {
346             ob_start();
347             eval($evalcode);
348             $val = ob_get_contents();
349             ob_end_clean();
350           }
351         }
352         else { $val = null; }
353         $upvals[$ds['name']] = $val;
354       }
355     }
356     $key_names = (!is_numeric(array_shift(array_keys($upvals))));
357     if (in_array('L', $upvals)) {
358       // for at least one value, we need to set the same as the last recorded value
359       $fvals = $this->fetch();
360       $rowids = array_shift($fvals);
361       $lastvals = array_shift($fvals);
362       foreach (array_keys($upvals, 'L') as $akey) {
363         $upvals[$akey] = $key_names?$lastvals[$akey]:$lastvals[$rowids[$akey]];
364       }
365     }
366     $walkfunc = create_function('&$val,$key', '$val = is_numeric(trim($val))?trim($val):"U";');
367     array_walk($upvals, $walkfunc);
368     $return = null;
369     if (count($upvals)) {
370       $update_cmd = 'rrdtool update '.$this->rrd_file.($key_names?' --template '.implode(':', array_keys($upvals)):'').' N:'.implode(':', $upvals);
371       $return = `$update_cmd 2>&1`;
372     }
373
374     if (strpos($return, 'ERROR') !== false) {
375       trigger_error($this->rrd_file.' - rrd update error: '.$return, E_USER_WARNING);
376       $success = false;
377     }
378     else { $success = true; }
379   return $success;
380   }
381
382   function fetch($cf = 'AVERAGE', $resolution = null, $start = null, $end = null) {
383     // fetch data from a RRD
384     if (!in_array($this->status, array('ok','readonly'))) { trigger_error('Error: rrd status is '.$this->status, E_USER_WARNING); return false; }
385
386     if (!in_array($cf, array('AVERAGE','MIN','MAX','LAST'))) { $cf = 'AVERAGE'; }
387     if (!is_numeric($resolution)) { $resolution = $this->rrd_step; }
388     if (!is_numeric($end)) { $end = $this->last_update(); }
389     elseif ($end < 0) { $end += $this->last_update(); }
390     $end = intval($end/$resolution)*$resolution;
391     if (!is_numeric($start)) { $start = $end; }
392     elseif ($start < 0) { $start += $end; }
393     $start = intval($start/$resolution)*$resolution;
394
395     $fetch_cmd = 'rrdtool fetch '.$this->rrd_file.' '.$cf.' --resolution '.$resolution.' --start '.$start.' --end '.$end;
396     $return = `$fetch_cmd 2>&1`;
397
398     if (strpos($return, 'ERROR') !== false) {
399       trigger_error($this->rrd_file.' - rrd fetch error: '.$return, E_USER_WARNING);
400       $fresult = false;
401     }
402     else {
403       $fresult = array();
404       $rows = explode("\n", $return);
405       $fields = preg_split('/\s+/', array_shift($rows));
406       if (array_shift($fields) == 'timestamp') {
407         $fresult[0] = $fields;
408         foreach ($rows as $row) {
409           if (strlen(trim($row))) {
410             $rvals = preg_split('/\s+/', $row);
411             $rtime = str_replace(':', '', array_shift($rvals));
412             $rv_array = array();
413             foreach ($rvals as $key=>$rval) {
414               $rv_array[$fields[$key]] = ($rval=='nan')?null:floatval($rval);
415             }
416             $fresult[$rtime] = $rv_array;
417           }
418         }
419       }
420     }
421   return $fresult;
422   }
423
424   function last_update() {
425     // fetch time of last update in this RRD file
426     static $last_update;
427     if (!isset($last_update) && in_array($this->status, array('ok','readonly'))) {
428       $last_cmd = 'rrdtool last '.$this->rrd_file;
429       $return = trim(`$last_cmd 2>&1`);
430       $last_update = is_numeric($return)?$return:null;
431     }
432   return isset($last_update)?$last_update:null;
433   }
434
435   function graph($timeframe = 'day', $sub = null, $extra = null) {
436     // create a RRD graph
437     static $gColors;
438     if (!isset($gColors)) {
439       $gColors = array('#00CC00','#0000FF','#000000','#FF0000','#00FF00','#FFFF00','#FF00FF','#00FFFF','#808080','#800000','#008000','#000080','#808000','#800080','#008080','#C0C0C0');
440     }
441
442     if (!in_array($this->status, array('ok','readonly','graphonly'))) { trigger_error('Error: rrd status is '.$this->status, E_USER_WARNING); return false; }
443
444     // assemble configuration
445     $gconf = (array)$extra;
446     if (!is_null($sub) && is_array($this->config_raw['graph.'.$sub])) {
447       $gconf = $gconf + $this->config_raw['graph.'.$sub];
448     }
449     $gconf = $gconf + (array)$this->config_graph;
450
451     if (isset($gconf['format']) && ($gconf['format'] == 'SVG')) {
452       $format = $gconf['format']; $fmt_ext = '.svg';
453     }
454     elseif (isset($gconf['format']) && ($gconf['format'] == 'EPS')) {
455       $format = $gconf['format']; $fmt_ext = '.eps';
456     }
457     elseif (isset($gconf['format']) && ($gconf['format'] == 'PDF')) {
458       $format = $gconf['format']; $fmt_ext = '.pdf';
459     }
460     else {
461       $format = 'PNG'; $fmt_ext = '.png';
462     }
463
464     if (isset($gconf['filename'])) { $fname = $gconf['filename']; }
465     else { $fname = $this->basename.(is_null($sub)?'':'-%s').'-%t%f'; }
466     $fname = str_replace('%s', strval($sub), $fname);
467     $fname = str_replace('%t', $timeframe, $fname);
468     $fname = str_replace('%f', $fmt_ext, $fname);
469     if (substr($fname, -strlen($fmt_ext)) != $fmt_ext) { $fname .= $fmt_ext; }
470     if (isset($gconf['path']) && ($fname{0} != '/')) { $fname = $gconf['path'].'/'.$fname; }
471     if ($fname{0} != '/') { $fname = $this->basedir.$fname; }
472     $fname = str_replace('//', '/', $fname);
473
474     $graphrows = array(); $specialrows = array(); $gC = 0;
475     $gDefs = ''; $gGraphs = ''; $addSpecial = '';
476
477     if ($timeframe == 'day') {
478       $duration = isset($gconf['duration'])?$gconf['duration']:30*3600; // 30 hours
479       $slice = isset($gconf['slice'])?$gconf['slice']:300; // 5 minutes
480       // vertical lines at day borders
481       $addSpecial .= ' VRULE:'.strtotime(date('Y-m-d')).'#FF0000';
482       $addSpecial .= ' VRULE:'.strtotime(date('Y-m-d').' -1 day').'#FF0000';
483       if (!isset($gconf['grid_x'])) { $gconf['grid_x'] = 'HOUR:1:HOUR:6:HOUR:2:0:%-H'; }
484     }
485     elseif ($timeframe == 'week') {
486       $duration = isset($gconf['duration'])?$gconf['duration']:8*86400; // 8 days
487       $slice = isset($gconf['slice'])?$gconf['slice']:1800; // 30 minutes
488       // vertical lines at week borders
489       $addSpecial .= ' VRULE:'.strtotime(date('Y-m-d').' '.(-date('w')+1).' day').'#FF0000';
490       $addSpecial .= ' VRULE:'.strtotime(date('Y-m-d').' '.(-date('w')-6).' day').'#FF0000';
491     }
492     elseif ($timeframe == 'month') {
493       $duration = isset($gconf['duration'])?$gconf['duration']:36*86400; // 36 days
494       $slice = isset($gconf['slice'])?$gconf['slice']:7200; // 2 hours
495       // vertical lines at month borders
496       $addSpecial .= ' VRULE:'.strtotime(date('Y-m-01')).'#FF0000';
497       $addSpecial .= ' VRULE:'.strtotime(date('Y-m-01').' -1 month').'#FF0000';
498     }
499     elseif ($timeframe == 'year') {
500       $duration = isset($gconf['duration'])?$gconf['duration']:396*86400; // 365+31 days
501       $slice = isset($gconf['slice'])?$gconf['slice']:86400; // 1 day
502       // vertical lines at month borders
503       $addSpecial .= ' VRULE:'.strtotime(date('Y-01-01 12:00:00')).'#FF0000';
504       $addSpecial .= ' VRULE:'.strtotime(date('Y-01-01 12:00:00').' -1 year').'#FF0000';
505     }
506     else {
507       $duration = isset($gconf['duration'])?$gconf['duration']:$this->rrd_step*500; // 500 steps
508       $slice = isset($gconf['slice'])?$gconf['slice']:$this->rrd_step; // whatever our step is
509     }
510
511     if (isset($gconf['rows']) && count($gconf['rows'])) {
512       foreach ($gconf['rows'] as $erow) {
513         if (isset($erow['name']) && strlen($erow['name'])) {
514           if (!isset($erow['scale']) && isset($gconf['scale'])) { $erow['scale'] = $gconf['scale']; }
515           $grow = array();
516           $grow['dType'] = isset($erow['dType'])?$erow['dType']:'DEF';
517           $grow['name'] = $erow['name'].(isset($erow['scale'])?'_tmp':'');
518           if ($grow['dType'] == 'DEF') {
519             $grow['dsname'] = isset($erow['dsname'])?$erow['dsname']:$erow['name'];
520             if (isset($erow['dsfile'])) { $grow['dsfile'] = $erow['dsfile']; }
521             $grow['cf'] = isset($erow['cf'])?$erow['cf']:'AVERAGE';
522           }
523           else {
524             $grow['rpn_expr'] = isset($erow['rpn_expr'])?$erow['rpn_expr']:'0';
525           }
526           if (isset($erow['scale'])) {
527             $graphrows[] = $grow;
528             $grow = array();
529             $grow['dType'] = 'CDEF';
530             $grow['name'] = $erow['name'];
531             $grow['rpn_expr'] = $erow['name'].'_tmp,'.$erow['scale'].',*';
532           }
533           $grow['gType'] = isset($erow['gType'])?$erow['gType']:'LINE1';
534           $grow['color'] = isset($erow['color'])?$erow['color']:$gColors[$gC++];
535           if ($gC >= count($gColors)) { $gC = 0; }
536           if (isset($erow['legend'])) {
537             $grow['legend'] = $erow['legend'];
538             if (!isset($gconf['show_legend'])) { $gconf['show_legend'] = true; }
539           }
540           if (isset($erow['stack'])) { $grow['stack'] = ($erow['stack'] == true); }
541           if (isset($erow['desc'])) { $grow['desc'] = $erow['desc']; }
542           $graphrows[] = $grow;
543         }
544       }
545     }
546     else {
547       foreach ($this->rrd_fields as $key=>$ds) {
548         $grow = array();
549         $grow['dType'] = 'DEF';
550         $grow['name'] = $ds['name'].(isset($gconf['scale'])?'_tmp':'');
551         $grow['dsname'] = $ds['name'];
552         $grow['cf'] = 'AVERAGE';
553         if (isset($gconf['scale'])) {
554           $graphrows[] = $grow;
555           $grow = array();
556           $grow['dType'] = 'CDEF';
557           $grow['name'] = $ds['name'];
558           $grow['rpn_expr'] = $ds['name'].'_tmp,'.$gconf['scale'].',*';
559         }
560         $grow['gType'] = ((count($this->rrd_fields)==2) && ($key==0))?'AREA':'LINE1';
561         $grow['color'] = $gColors[$gC++]; if ($gC >= count($gColors)) { $gC = 0; }
562         if (isset($ds['legend'])) {
563           $grow['legend'] = $ds['legend'];
564           if (!isset($gconf['show_legend'])) { $gconf['show_legend'] = true; }
565         }
566         $graphrows[] = $grow;
567       }
568     }
569
570     if (isset($gconf['special']) && count($gconf['special'])) {
571       foreach ($gconf['special'] as $crow) {
572         $srow = array();
573         $srow['sType'] = isset($crow['sType'])?$crow['sType']:'COMMENT';
574         if ($grow['sType'] != 'COMMENT') {
575           // XXX: use line below and remove cf var once we have rrdtol 1.2
576           // $srow['name'] = $crow['name'].(isset($crow['cf'])?'_'.$crow['cf']:'');
577           $srow['name'] = $crow['name'];
578           $srow['cf'] = isset($crow['cf'])?$crow['cf']:'AVERAGE';
579           if (isset($crow['cf'])) {
580             // XXX: use line below once we have rrdtol 1.2
581             // $graphrows[] = array('dType'=>'VDEF', 'name'=>$srow['name'].'_'.$crow['cf'], 'rpn_expr'=>$srow['name'].','.$crow['cf']);
582           }
583           elseif (isset($crow['rpn_expr'])) {
584             // XXX: does only work with rrdtool 1.2
585             $graphrows[] = array('dType'=>'VDEF', 'name'=>$srow['name'], 'rpn_expr'=>$crow['rpn_expr']);
586           }
587         }
588         $srow['text'] = isset($crow['text'])?$crow['text']:'';
589         $specialrows[] = $srow;
590       }
591     }
592     else {
593       foreach ($graphrows as $grow) {
594         if (isset($grow['gType']) && strlen($grow['gType'])) {
595           $textprefix = isset($grow['desc'])?$grow['desc']:(isset($grow['legend'])?$grow['legend']:$grow['name']);
596           // XXX: use lines below once we have rrdtol 1.2
597           // $graphrows[] = array('dType'=>'VDEF', 'name'=>$grow['name'].'_last', 'rpn_expr'=>$grow['name'].',LAST');
598           // $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'].'_last', 'text'=>'%3.2lf%s');
599           $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'], 'cf'=>'MAX', 'text'=>$textprefix.'|Maximum|%.2lf%s');
600           $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'], 'cf'=>'AVERAGE', 'text'=>$textprefix.'|Average|%.2lf%s');
601           $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'], 'cf'=>'LAST', 'text'=>$textprefix.'|Current|%.2lf%s');
602         }
603       }
604     }
605
606     $endtime = isset($gconf['time_end'])?$gconf['time_end']:(is_numeric($this->last_update())?$this->last_update():time());
607     $gOpts = ' --start '.($endtime-$duration).' --end '.$endtime.' --step '.$slice;
608     if (isset($gconf['label_top'])) { $gOpts .= ' --title '.$this->text_quote($gconf['label_top']); }
609     if (isset($gconf['label_y'])) { $gOpts .= ' --vertical-label '.$this->text_quote($gconf['label_y']); }
610     if (isset($gconf['width'])) { $gOpts .= ' --width '.$gconf['width']; }
611     if (isset($gconf['height'])) { $gOpts .= ' --height '.$gconf['height'];
612       if (($gconf['height'] <= 32) && isset($gconf['thumb']) && ($gconf['thumb'])) { $gOpts .= ' --only-graph'; }
613     }
614     if (!isset($gconf['show_legend']) || (!$gconf['show_legend'])) { $gOpts .= ' --no-legend'; }
615     if (isset($gconf['min_y'])) { $gOpts .= ' --lower-limit '.$gconf['min_y']; }
616     if (isset($gconf['max_y'])) { $gOpts .= ' --upper-limit '.$gconf['max_y']; }
617     if (isset($gconf['fix_scale_y']) && $gconf['fix_scale_y']) { $gOpts .= ' --rigid'; }
618     if (isset($gconf['grid_x'])) { $gOpts .= ' --x-grid '.$gconf['grid_x']; }
619     if (isset($gconf['grid_y'])) { $gOpts .= ' --y-grid '.$gconf['grid_y']; }
620     if (isset($gconf['units_exponent'])) { $gOpts .= ' --units-exponent '.$gconf['units_exponent']; }
621     if (isset($gconf['units_length'])) { $gOpts .= ' --units-length '.$gconf['units_length']; }
622     if (!isset($gconf['force_recreate']) || (!$gconf['force_recreate'])) { $gOpts .= ' --lazy'; }
623     if (isset($gconf['force_color']) && is_array($gconf['force_color'])) {
624       foreach ($gconf['force_color'] as $ctag=>$cval) { $gOpts .= ' --color '.$ctag.$cval; }
625     }
626     if (isset($gconf['force_font']) && is_array($gconf['force_font'])) {
627       foreach ($gconf['force_font'] as $ctag=>$cval) { $gOpts .= ' --font '.$ctag.$cval; }
628     }
629     if (isset($gconf['units_binary']) && $gconf['units_binary']) { $gOpts .= ' --base 1024'; }
630
631     foreach ($graphrows as $grow) {
632       if (isset($grow['dType']) && strlen($grow['dType'])) {
633         $gDefs .= ' '.$grow['dType'].':'.$grow['name'].'=';
634         if ($grow['dType'] == 'DEF') {
635           $gDefs .= isset($grow['dsfile'])?$grow['dsfile']:$this->rrd_file;
636           $gDefs .= ':'.$grow['dsname'].':'.$grow['cf'];
637         }
638         else { $gDefs .= $grow['rpn_expr']; }
639       }
640       if (isset($grow['gType']) && strlen($grow['gType'])) {
641         // XXX: change from STACK type to STACK flag once we have rrdtool 1.2
642         if (isset($grow['stack']) && $grow['stack']) { $grow['gType'] = 'STACK'; }
643         $gGraphs .= ' '.$grow['gType'].':'.$grow['name'].$grow['color'];
644         if (isset($grow['legend'])) { $gGraphs .= ':'.$this->text_quote($grow['legend']); }
645         // XXX: remove above STACK if-command and uncomment the one below once we have rrdtool 1.2
646         //if (isset($grow['stack']) && $grow['stack']) { $gGraphs .= ':STACK'; }
647       }
648     }
649
650     foreach ($specialrows as $srow) {
651       $addSpecial .= ' '.$srow['sType'];
652       // XXX: eliminate cf once we have rrdtool 1.2
653       // $addSpecial .= ($grow['sType']!='COMMENT')?':'.$grow['name']:'');
654       $addSpecial .= (($srow['sType']!='COMMENT')?':'.$srow['name'].':'.$srow['cf']:'');
655       $addSpecial .= ':'.$this->text_quote($srow['text']);
656     }
657
658     $graph_cmd = 'rrdtool graph '.str_replace('*', '\*', $fname.$gOpts.$gDefs.$gGraphs.$addSpecial);
659     $return = `$graph_cmd 2>&1`;
660
661     if (strpos($return, 'ERROR') !== false) {
662       trigger_error($this->rrd_file.' - rrd graph error: '.$return, E_USER_WARNING);
663       $return = $graph_cmd."\n\n".$return;
664     }
665     $return = 'file:'.$fname."\n".$return;
666   return $return;
667   }
668
669   function graph_plus($timeframe = 'day', $sub = null, $extra = null) {
670     // create a RRD graph and return meta info as a ready-to-use array
671     $gmeta = array('filename'=>null);
672     $ret = $this->graph($timeframe, $sub, $extra);
673     if (strpos($ret, "\n\n") !== false) { $gmeta['graph_cmd'] = substr($ret, 0, strpos($ret, "\n\n")); $ret = substr($ret, strpos($ret, "\n\n")+2); }
674     else { $gmeta['graph_cmd'] = null; }
675     $grout = explode("\n", $ret);
676     foreach ($grout as $gline) {
677       if (preg_match('/^file:(.+)$/', $gline, $regs)) {
678         $gmeta['filename'] = $regs[1];
679       }
680       elseif (preg_match('/^(\d+)x(\d+)$/', $gline, $regs)) {
681         $gmeta['width'] = $regs[1]; $gmeta['height'] = $regs[2];
682       }
683       elseif (preg_match('/^([^\|]+)\|([^|]+)\|([^\|]*)$/', $gline, $regs)) {
684         $gmeta['data'][$regs[1]][$regs[2]] = $regs[3];
685       }
686       elseif (preg_match('/^([^\|]+)\|([^\|]*)$/', $gline, $regs)) {
687         $gmeta['var'][$regs[1]] = $regs[2];
688       }
689       elseif (strlen(trim($gline))) {
690         $gmeta['info'][] = $gline;
691       }
692     }
693     if (is_null($gmeta['filename'])) {
694       $gmeta['filename'] = $this->basename.(!is_null($sub)?'-'.$sub:'').'-'.$timeframe.'.png';
695     }
696   return $gmeta;
697   }
698
699   function page($sub = null, $page_extras = null, $graph_extras = null) {
700     // create a (HTML) page and return it in a string
701
702     // assemble configuration
703     $pconf = (array)$page_extras;
704     if (!is_null($sub) && is_array($this->config_raw['page.'.$sub])) {
705       $pconf = $pconf + $this->config_raw['page.'.$sub];
706     }
707     $pconf = $pconf + (array)$this->config_page;
708
709     $return = null;
710     switch ($pconf['type']) {
711       case 'index':
712         $return = $this->page_index($pconf);
713         break;
714       case 'overview':
715         $return = $this->page_overview($pconf, $graph_extras);
716         break;
717       case 'simple':
718       default:
719         $return = $this->page_simple($pconf, $graph_extras);
720         break;
721     }
722   return $return;
723   }
724
725   function simple_html($sub = null, $page_extras = null, $graph_extras = null) {
726     // create a simple (MRTG-like) HTML page and return it in a string
727     // XXX: this is here temporarily for compat only, it's preferred to use page()!
728
729     // assemble configuration
730     $pconf = (array)$page_extras;
731     if (!is_null($sub) && is_array($this->config_raw['page.'.$sub])) {
732       $pconf = $pconf + $this->config_raw['page.'.$sub];
733     }
734     $pconf = $pconf + (array)$this->config_page;
735
736   return $this->page_simple($pconf, $graph_extras);
737   }
738
739   function page_index($pconf) {
740     // create a bare, very simple index list HTML page and return it in a string
741
742     $ptitle = isset($pconf['title_page'])?$pconf['title_page']:'RRD statistics index';
743
744     $out = '<html><head>';
745     $out .= '<title>'.$ptitle.'</title>';
746     $out .= '<style>';
747     if (isset($pconf['style_base'])) { $out .= $pconf['style_base']; }
748     else {
749       $out .= 'h1 { font-weight: bold; font-size: 1.5em; }';
750       $out .= '.footer { font-size: 0.75em; margin: 0.5em 0; }';
751       $out .= 'li.scanfile { font-style: italic; }';
752     }
753     if (isset($pconf['style'])) { $out .= $pconf['style']; }
754     $out .= '</style>';
755     $out .= '</head>';
756     $out .= '<body>';
757
758     $out .= '<h1>'.$ptitle.'</h1>';
759     if (isset($pconf['text_intro']) && strlen($pconf['text_intro'])) {
760       $out .= '<p class="intro">'.$pconf['text_intro'].'</p>';
761     }
762     elseif (!isset($pconf['text_intro'])) {
763       $out .= '<p class="intro">The following RRD stats are available:</p>';
764     }
765
766     $stats = $this->h_page_statsArray($pconf);
767
768     $out .= '<ul class="indexlist">';
769     foreach ($stats as $stat) {
770       $out .= '<li'.(isset($stat['class'])?' class="'.$stat['class'].'"':'').'>';
771       $surl = '?stat='.$stat['name'];
772       $out .= '<a href="'.$surl.'">'.$stat['name'].'</a>';
773       if (isset($stat['sub']) && count($stat['sub'])) {
774         $sprt = array();
775         foreach ($stat['sub'] as $ssub) { $sprt[] = '<a href="'.$surl.'&sub='.$ssub.'">'.$ssub.'</a>'; }
776         $out .= ' <span="subs">('.implode(', ', $sprt).')</span>';
777       }
778       $out .= '</li>';
779     }
780     $out .= '</ul>';
781
782     $out .= $this->h_page_footer();
783     $out .= '</body></html>';
784   return $out;
785   }
786
787   function page_overview($pconf, $graph_extras = null) {
788     // create an overview HTML page (including graphs) and return it in a string
789
790     $ptitle = isset($pconf['title_page'])?$pconf['title_page']:'RRD statistics overview';
791
792     $out = '<html><head>';
793     $out .= '<title>'.$ptitle.'</title>';
794     $out .= '<style>';
795     if (isset($pconf['style_base'])) { $out .= $pconf['style_base']; }
796     else {
797       $out .= 'h1 { font-weight: bold; font-size: 1.5em; }';
798       $out .= 'h2 { font-weight: bold; font-size: 1em; margin: 0.5em 0; }';
799       $out .= '.footer { font-size: 0.75em; margin: 0.5em 0; }';
800       $out .= 'img.rrdgraph { border: none; }';
801     }
802     if (isset($pconf['style'])) { $out .= $pconf['style']; }
803     $out .= '</style>';
804     $out .= '</head>';
805     $out .= '<body>';
806
807     $out .= '<h1>'.$ptitle.'</h1>';
808     if (isset($pconf['text_intro'])) { $out .= '<p class="intro">'.$pconf['text_intro'].'</p>'; }
809
810     $stats = $this->h_page_statsArray($pconf);
811
812     $num_rows = is_numeric($pconf['num_rows'])?$pconf['num_rows']:2;
813     $num_cols = ceil(count($stats)/$num_rows);
814
815     $out .= '<table class="overview">';
816     for ($col = 0; $col < $num_cols; $col++) {
817       $out .= '<tr>';
818       for ($row = 0; $row < $num_rows; $row++) {
819         $idx = $col * $num_rows + $row;
820         $out .= '<td>';
821         if ($idx < count($stats)) {
822           list($sname, $s_psub) = explode('|', $stats[$idx]['name'], 2);
823           $s_psname = 'page'.(isset($s_psub)?'.'.$s_psub:'');
824           $g_sub = $this->config_all[$sname][$s_psname]['graph_sub'];
825
826           if (isset($this->config_all[$sname][$s_psname]['title_page'])) {
827             $s_ptitle = $this->config_all[$sname][$s_psname]['title_page'];
828           }
829           elseif (isset($this->config_all[$sname]['page']['title_page'])) {
830             $s_ptitle = $this->config_all[$sname]['page']['title_page'];
831           }
832           else {
833             $s_ptitle = $sname.(isset($s_psub)?' ('.$s_psub.')':'').' statistics';
834           }
835           if (!isset($pconf['hide_titles']) || !$pconf['hide_titles']) {
836             $out .= '<h2>'.$s_ptitle.'</h2>';
837           }
838
839           $s_rrd = new rrdstat($this->config_all, $sname);
840           if (in_array($s_rrd->status, array('ok','readonly','graphonly'))) {
841             $tframe = isset($pconf['graph_timeframe'])?$pconf['graph_timeframe']:'day';
842             $gmeta = $s_rrd->graph_plus($tframe, $g_sub);
843             if (isset($pconf['graph_url'])) {
844               $gURL = $pconf['graph_url'];
845               $fname = str_replace('%f', basename($gmeta['filename']), $gURL);
846               $fname = str_replace('%p', $gmeta['filename'], $gURL);
847               if (substr($gURL, -1) == '/') { $gURL .= $gmeta['filename']; }
848             }
849             else {
850               $gURL = $gmeta['filename'];
851             }
852             $out .= '<a href="?stat='.$sname.(isset($s_psub)?'&sub='.$s_psub:'').'"';
853             $out .= '<img src="'.$gURL.'"';
854             $out .= ' alt="'.$s_rrd->basename.(!is_null($g_sub)?' - '.$g_sub:'').' - '.$tframe.'" class="rrdgraph"';
855             $out .= ' style="width:'.$gmeta['width'].'px;height:'.$gmeta['height'].'px;">';
856             $out .= '</a>';
857           }
858           else {
859             $out .= 'RRD error: status is "'.$s_rrd->status.'"';
860           }
861         }
862         else {
863           $out .= '&nbsp;';
864         }
865         $out .= '</td>';
866       }
867       $out .= '</tr>';
868     }
869     $out .= '</table>';
870
871     $out .= $this->h_page_footer();
872     $out .= '</body></html>';
873   return $out;
874   }
875
876   function page_simple($pconf, $graph_extras = null) {
877     // create a simple (MRTG-like) HTML page and return it in a string
878
879     $ptitle = isset($pconf['title_page'])?$pconf['title_page']:$this->basename.' - RRD statistics';
880     $gtitle = array();
881     $gtitle['day'] = isset($pconf['title_day'])?$pconf['title_day']:'Day overview (scaling 5 minutes)';
882     $gtitle['week'] = isset($pconf['title_week'])?$pconf['title_week']:'Week overview (scaling 30 minutes)';
883     $gtitle['month'] = isset($pconf['title_month'])?$pconf['title_month']:'Month overview (scaling 2 hours)';
884     $gtitle['year'] = isset($pconf['title_year'])?$pconf['title_year']:'Year overview (scaling 1 day)';
885
886     $out = '<html><head>';
887     $out .= '<title>'.$ptitle.'</title>';
888     $out .= '<style>';
889     if (isset($pconf['style_base'])) { $out .= $pconf['style_base']; }
890     else {
891       $out .= 'h1 { font-weight: bold; font-size: 1.5em; }';
892       $out .= 'h2 { font-weight: bold; font-size: 1em; }';
893       $out .= '.gdata, .gvar, .ginfo { font-size: 0.75em; margin: 0.5em 0; }';
894       $out .= 'table.gdata { border: 1px solid gray; border-collapse: collapse; }';
895       $out .= 'table.gdata td, table.gdata th { border: 1px solid gray; padding: 0.1em 0.2em; }';
896       $out .= '.footer { font-size: 0.75em; margin: 0.5em 0; }';
897     }
898     if (isset($pconf['style'])) { $out .= $pconf['style']; }
899     $out .= '</style>';
900     $out .= '</head>';
901     $out .= '<body>';
902
903     $out .= '<h1>'.$ptitle.'</h1>';
904     if (!isset($pconf['show_update']) || $pconf['show_update']) {
905       $out .= '<p class="last_up">Last Update: '.(is_null($this->last_update())?'unknown':date('Y-m-d H:i:s', $this->last_update())).'</p>';
906     }
907
908     $g_sub = isset($pconf['graph_sub'])?$pconf['graph_sub']:null;
909     if (in_array($this->status, array('ok','readonly','graphonly'))) {
910       foreach (array('day','week','month','year') as $tframe) {
911         $gmeta = $this->graph_plus($tframe, $g_sub, $graph_extras);
912         if (isset($pconf['graph_url'])) {
913           $gURL = $pconf['graph_url'];
914           $fname = str_replace('%f', basename($gmeta['filename']), $gURL);
915           $fname = str_replace('%p', $gmeta['filename'], $gURL);
916           if (substr($gURL, -1) == '/') { $gURL .= $gmeta['filename']; }
917         }
918         else {
919           $gURL = $gmeta['filename'];
920         }
921         $out .= '<div class="'.$tframe.'">';
922 //         $out .= '<p>'.nl2br($ret).'</p>';
923         $out .= '<h2>'.$gtitle[$tframe].'</h2>';
924         $out .= '<img src="'.$gURL.'"';
925         $out .= ' alt="'.$this->basename.(!is_null($g_sub)?' - '.$g_sub:'').' - '.$tframe.'" class="rrdgraph"';
926         $out .= ' style="width:'.$gmeta['width'].'px;height:'.$gmeta['height'].'px;">';
927         if (isset($gmeta['data']) && count($gmeta['data'])) {
928           $out .= '<table class="gdata">';
929           foreach ($gmeta['data'] as $field=>$gdata) {
930             $out .= '<tr><th>'.$field.'</th>';
931             foreach ($gdata as $gkey=>$gval) {
932               $out .= '<td><span class="gkey">'.$gkey.': </span>'.$gval.'</td>';
933             }
934             $out .= '</tr>';
935           }
936           $out .= '</table>';
937         }
938         if (isset($gmeta['var']) && count($gmeta['var'])) {
939           foreach ($gmeta['var'] as $gkey=>$gval) {
940             $out .= '<p class="gvar"><span class="gkey">'.$gkey.': </span>'.$gval.'</p>';
941           }
942         }
943         if (isset($gmeta['info']) && count($gmeta['info'])) {
944           foreach ($gmeta['info'] as $gval) {
945             $out .= '<p class="ginfo">'.$gval.'</p>';
946           }
947         }
948         $out .= '</div>';
949       }
950     }
951     else {
952       $out .= 'RRD error: status is "'.$this->status.'"';
953     }
954
955     $out .= $this->h_page_footer();
956     $out .= '</body></html>';
957   return $out;
958   }
959
960   function h_page_statsArray($pconf) {
961     // return array of stats to list on a page
962     $stats = array();
963     $snames = array(); $s_exclude = array(); $sfiles = array();
964     if (isset($pconf['index_ids'])) {
965       foreach (explode(',', $pconf['index_ids']) as $iid) {
966         if ($iid{0} == '-') { $s_exclude[] = substr($iid, 1); }
967         else { $snames[] = $iid; }
968       }
969     }
970     if (!isset($pconf['scan_config']) || $pconf['scan_config']) {
971       foreach ($this->config_all as $iname=>$rinfo) {
972         if (($iname != '*') && !(isset($rinfo['hidden']) && $rinfo['hidden']) &&
973             !(in_array($iname, $snames)) && !(in_array($iname, $s_exclude))) {
974           $snames[] = $iname;
975         }
976       }
977     }
978     foreach ($snames as $iname) {
979       $newstat = array('name'=>$iname);
980       $sfiles[] = isset($this->config_all[$iname]['file'])?$this->config_all[$iname]['file']:$iname.'.rrd';
981       if (is_array($this->config_all[$iname])) {
982         foreach ($this->config_all[$iname] as $key=>$val) {
983           if (substr($key, 0, 5) == 'page.') { $newstat['sub'][] = substr($key, 5); }
984         }
985       }
986       $stats[] = $newstat;
987     }
988     if (isset($pconf['scan_files']) && $pconf['scan_files']) {
989       $rrdfiles = glob('*.rrd');
990       foreach ($rrdfiles as $rrdfile) {
991         $iname = (substr($rrdfile, -4) == '.rrd')?substr($rrdfile, 0, -4):$rrdfile;
992         if (!in_array($rrdfile, $sfiles) && !(in_array($iname, $s_exclude))) {
993           $stats[] = array('name'=>$iname, 'class'=>'scanfile');
994         }
995       }
996     }
997   return $stats;
998   }
999
1000   function h_page_footer() {
1001     // return generic page footer
1002     $out = '<p class="footer">';
1003     $out .= 'Statistics created by <a href="http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/">RRDtool</a>';
1004     $out .= ' using a library created by <a href="http://www.kairo.at/">KaiRo.at</a>.';
1005     $out .= '</p>';
1006   return $out;
1007   }
1008
1009   function text_quote($text) { return '"'.str_replace('"', '\"', str_replace(':', '\:', $text)).'"'; }
1010 }
1011 ?>