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