remove a lot of error suppression in favor of ?? in various utility classes
[php-utility-classes.git] / classes / rrdstat.php-class
CommitLineData
b7878f4d 1<?php
880bcb60
RK
2/* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
0ee9d2d0 5
b7878f4d 6class rrdstat {
0ee9d2d0 7 // rrdstat PHP class
8 // rrdtool statistics functions
9 //
6813be69 10 // function __construct($rrdconfig, [$conf_id])
0ee9d2d0 11 // CONSTRUCTOR
12 // if $conf_id is set, $rrdconfig is a total configuration set
13 // else it's the configuration for this one RRD
14 // currently only a config array is supported, XML config is planned
15 //
bc025bc8
RK
16 // private $rrdtool_bin
17 // RRDtool binary to use
18 //
6813be69 19 // private $rrd_file
0ee9d2d0 20 // RRD file name
21 //
6813be69 22 // private $basename
0ee9d2d0 23 // base name for this RRD (usually file name without .rrd)
24 //
6813be69 25 // private $basedir
2e28e4bd 26 // base directory for this RRD (with a trailing slash)
27 // note that $rrd_file usually includes that path as well, but graph directory gets based on this value
28 //
6813be69 29 // private $config_all
0ee9d2d0 30 // complete, raw configuration array set
31 //
6813be69 32 // private $config_raw
0ee9d2d0 33 // configuration array set for current RRD
34 //
6813be69 35 // private $config_graph
0ee9d2d0 36 // configuration array set for default graph in this RRD
37 //
6813be69 38 // private $config_page
0ee9d2d0 39 // configuration array set for default page in this RRD
40 //
6813be69 41 // private $rrd_fields
0ee9d2d0 42 // definition of this RRD's fields
43 //
6813be69 44 // private $rra_base
0ee9d2d0 45 // definition of this RRD's base RRAs
46 //
6813be69 47 // private $rrd_step
0ee9d2d0 48 // basic stepping of this RRD in seconds (default: 300)
49 //
6813be69 50 // private $rra_add_max
0ee9d2d0 51 // should RRAs for MAX be added for every base RRA? (bool, default: true)
52 //
6813be69 53 // private $status
0ee9d2d0 54 // status of the RRD (unused/ok/readonly/graphonly)
55 // note that most functions require certain status values
56 // (e.g. update only works if status is ok, graph for ok/readonly/graphonly)
57 //
6813be69 58 // private $mod_textdomain
724f6e78 59 // GNU gettext domain for this module
60 //
6813be69 61 // private function set_def($rrdconfig, [$conf_id])
0ee9d2d0 62 // set definitions based on given configuration
63 // [intended for internal use, called by the constructor]
64 //
6813be69 65 // public function rrd_version() {
586031ce 66 // get RRDtool version string
67 //
6813be69 68 // public function create()
0ee9d2d0 69 // create RRD file according to set config
70 //
6813be69 71 // public function update([$upArray])
0ee9d2d0 72 // feed new data into RRD (either use given array of values or use auto-update info from config)
73 //
6813be69 74 // public function fetch([$cf] = 'AVERAGE', $resolution = null, $start = null, $end = null)
0ee9d2d0 75 // fetch data from the defined RRD
76 // using given consolidation function [default is AVERAGE],
77 // resolution (seconds, default is the RRD's stepping),
78 // start and end times (unix epoch, defaults are the RRD's last update time)
79 //
6813be69 80 // public function last_update()
0ee9d2d0 81 // fetch time of last update in this RRD file
82 //
6813be69 83 // public function graph([$timeframe], [$sub], [$extra])
0ee9d2d0 84 // create a RRD graph (and return all meta info in a flat string)
85 // for given timeframe (day [default]/week/month/year),
86 // sub-graph ID (if given) and extra config options (if given)
87 //
6813be69 88 // public function graph_plus([$timeframe], [$sub], [$extra])
0ee9d2d0 89 // create a RRD graph (see above) and return meta info as a ready-to-use array
90 //
6813be69 91 // public function page([$sub], [$page_extras], [$graph_extras])
0ee9d2d0 92 // create a (HTML) page and return it in a string
93 // for given sub-page ID (if given, default is a simple HTML page)
94 // and extra page and graph config options (if given)
95 //
6813be69 96 // public function simple_html([$sub], [$page_extras], [$graph_extras])
0ee9d2d0 97 // create a simple (MRTG-like) HTML page and return it in a string
98 // XXX: this is here temporarily for compat only, it's preferred to use page()!
99 //
6813be69 100 // private function page_index($pconf)
0ee9d2d0 101 // create a bare, very simple index list HTML page and return it in a string
102 // using given page config options
103 // [intended for internal use, called by page()]
104 //
6813be69 105 // private function page_overview($pconf, [$graph_extras])
0ee9d2d0 106 // create an overview HTML page (including graphs) and return it in a string
107 // using given page config options and extra graph options (if given)
108 // [intended for internal use, called by page()]
109 //
6813be69 110 // private function page_simple($pconf, [$graph_extras])
0ee9d2d0 111 // create a simple (MRTG-like) HTML page and return it in a string
112 // using given page config options and extra graph options (if given)
113 // [intended for internal use, called by page()]
114 //
6813be69 115 // private function h_page_statsArray($pconf)
0ee9d2d0 116 // return array of stats to list on a page, using given page config options
117 // [intended for internal use, called by page_*()]
118 //
6813be69 119 // private function h_page_footer()
0ee9d2d0 120 // return generic page footer
121 // [intended for internal use, called by page_*()]
122 //
6813be69 123 // private function text_quote($text)
0ee9d2d0 124 // return a quoted/escaped text for use in rrdtool commandline text fields
b7878f4d 125
bc025bc8
RK
126 private $rrdtool_bin = '/usr/bin/rrdtool';
127
6813be69 128 private $rrd_file = null;
129 private $basename = null;
130 private $basedir = null;
b7878f4d 131
6813be69 132 private $config_all = null;
133 private $config_raw = null;
134 private $config_graph = null;
135 private $config_page = null;
a039a75c 136
6813be69 137 private $rrd_fields = array();
138 private $rra_base = array();
139 private $rrd_step = 300;
140 private $rra_add_max = true;
b7878f4d 141
6813be69 142 private $status = 'unused';
b7878f4d 143
6813be69 144 private $mod_textdomain;
724f6e78 145
6813be69 146 function __construct($rrdconfig, $conf_id = null) {
b7878f4d 147 // ***** init RRD stat module *****
724f6e78 148 $this->mod_textdomain = 'class_rrdstat';
ea29ba0c 149 $mod_charset = 'utf-8';
724f6e78 150
151 bindtextdomain($this->mod_textdomain, class_exists('baseutils')?baseutils::getDir('locale'):'locale/');
152 bind_textdomain_codeset($this->mod_textdomain, $mod_charset);
153
ebe03325 154 $this->set_def($rrdconfig, $conf_id);
b7878f4d 155
b1776944 156 if (($this->status == 'unused') && !is_null($this->rrd_file)) {
2a386f5a 157 if (!is_writeable($this->rrd_file)) {
158 if (!file_exists($this->rrd_file)) {
86ff8b91 159 if (@touch($this->rrd_file)) { $this->create(); }
2a386f5a 160 else { trigger_error('RRD file can not be created', E_USER_WARNING); }
161 }
162 else {
163 if (is_readable($this->rrd_file)) { $this->status = 'readonly'; }
164 else { trigger_error('RRD file is not readable', E_USER_WARNING); }
165 }
b7878f4d 166 }
167 else {
2a386f5a 168 $this->status = 'ok';
b7878f4d 169 }
170 }
b7878f4d 171 }
172
6813be69 173 private function set_def($rrdconfig, $conf_id = null) {
ebe03325 174 if (is_array($rrdconfig)) {
b7878f4d 175 // we have an array in the format we like to have
ebe03325 176 $complete_conf =& $rrdconfig;
b7878f4d 177 }
178 else {
179 // we have something else (XML data?), try to generate the iinfo aray from it
ebe03325 180 $complete_conf =& $rrdconfig;
181 }
182
183 if (!is_null($conf_id)) {
184 $iinfo = isset($complete_conf[$conf_id])?$complete_conf[$conf_id]:array();
e1727e7f 185 if (isset($complete_conf['*'])) {
186 $iinfo = (array)$iinfo + (array)$complete_conf['*'];
24883b0f
RK
187 if (isset($complete_conf['*']['graph'])) {
188 $iinfo['graph'] = (array)$iinfo['graph'] + (array)$complete_conf['*']['graph'];
189 }
190 if (isset($complete_conf['*']['page'])) {
191 $iinfo['page'] = (array)$iinfo['page'] + (array)$complete_conf['*']['page'];
192 }
e1727e7f 193 }
ebe03325 194 }
195 else {
196 $iinfo = $complete_conf;
b7878f4d 197 }
198
2e28e4bd 199 if (isset($iinfo['path']) && strlen($iinfo['path'])) {
200 $this->basedir = $iinfo['path'];
201 if (substr($this->basedir, -1) != '/') { $this->basedir .= '/'; }
202 }
203
b1776944 204 if (isset($iinfo['graph-only']) && $iinfo['graph-only'] && !is_null($conf_id)) {
205 $this->basename = $conf_id;
206 $this->status = 'graphonly';
207 }
099bd59f 208 elseif (isset($iinfo['file'])) {
cb2420c6 209 $this->rrd_file = (($iinfo['file'][0] != '/')?$this->basedir:'').$iinfo['file'];
82d064f4 210 $this->basename = basename((substr($this->rrd_file, -4) == '.rrd')?substr($this->rrd_file, 0, -4):$this->rrd_file);
b1776944 211 }
abe8eac1 212 elseif (!is_null($conf_id) && file_exists($conf_id.'.rrd')) {
cb2420c6 213 $this->rrd_file = (($iinfo['file'][0] != '/')?$this->basedir:'').$conf_id.'.rrd';
abe8eac1 214 $this->basename = $conf_id;
215 }
b7878f4d 216 else {
099bd59f 217 $this->basename = !is_null($conf_id)?$conf_id:'xxx.unknown';
218 }
219
abe8eac1 220 if (!is_null($this->rrd_file)) {
099bd59f 221 // fields (data sources, DS)
222 // name - DS name
223 // type - one of COUNTER, GAUGE, DERIVE, ABSOLUTE
224 // heartbeat - if no sample recieved for that time, store UNKNOWN
225 // min - U (unconstrained) or minimum value
226 // max - U (unconstrained) or maximum value
227 // update - this string will be fed into eval() for updating this field
228 if (isset($iinfo['fields']) && is_array($iinfo['fields'])) {
229 $this->rrd_fields = $iinfo['fields'];
230 }
231 else {
232 $this->rrd_fields[] = array('name' => 'ds0', 'type' => 'COUNTER', 'heartbeat' => 600, 'min' => 'U', 'max' => 'U');
233 $this->rrd_fields[] = array('name' => 'ds1', 'type' => 'COUNTER', 'heartbeat' => 600, 'min' => 'U', 'max' => 'U');
234 }
b7878f4d 235
236
24883b0f 237 // MRTG-style RRD "database", see http://oss.oetiker.ch/rrdtool/tut/rrdtutorial.en.html
099bd59f 238 //
239 // archives (RRAs):
240 // 600 samples of 5 minutes (2 days and 2 hours)
241 // 700 samples of 30 minutes (2 days and 2 hours, plus 12.5 days)
242 // 775 samples of 2 hours (above + 50 days)
243 // 797 samples of 1 day (above + 732 days, rounded up to 797)
b7878f4d 244
099bd59f 245 $this->rrd_step = isset($iinfo['rrd_step'])?$iinfo['rrd_step']:300;
b7878f4d 246
099bd59f 247 if (isset($iinfo['rra_base']) && is_array($iinfo['rra_base'])) {
248 $this->rra_base = $iinfo['rra_base'];
249 }
250 else {
251 $this->rra_base[] = array('step' => 1, 'rows' => 600);
252 $this->rra_base[] = array('step' => 6, 'rows' => 700);
253 $this->rra_base[] = array('step' => 24, 'rows' => 775);
254 $this->rra_base[] = array('step' => 288, 'rows' => 797);
255 }
b7878f4d 256
099bd59f 257 $this->rra_add_max = isset($iinfo['rra_add_max'])?$iinfo['rra_add_max']:true;
258 }
a039a75c 259
260 if (isset($iinfo['graph'])) { $this->config_graph = $iinfo['graph']; }
261 if (isset($iinfo['page'])) { $this->config_page = $iinfo['page']; }
262 $this->config_raw = $iinfo;
099bd59f 263 $this->config_all = $complete_conf;
b7878f4d 264 }
265
6813be69 266 public function rrd_version() {
586031ce 267 // return RRDtool version
268 static $version;
269 if (!isset($version)) {
bc025bc8 270 $create_cmd = $this->rrdtool_bin.' --version';
586031ce 271 $return = `$create_cmd 2>&1`;
272 if (strpos($return, 'ERROR') !== false) {
273 trigger_error($this->rrd_file.' - rrd version error: '.$return, E_USER_WARNING);
274 }
275
276 if (preg_match('/^\s*RRDtool ([\d\.]+)\s+/', $return, $regs)) {
277 $version = $regs[1];
278 }
279 else {
280 $version = '0.0';
281 }
282 }
283 return $version;
284 }
285
6813be69 286 public function create() {
b7878f4d 287 // create RRD file
288
289 // compose create command
bc025bc8 290 $create_cmd = $this->rrdtool_bin.' create '.$this->rrd_file.' --step '.$this->rrd_step;
b7878f4d 291 foreach ($this->rrd_fields as $ds) {
292 if (!isset($ds['type'])) { $ds['type'] = 'COUNTER'; }
293 if (!isset($ds['heartbeat'])) { $ds['heartbeat'] = 2*$this->rrd_step; }
294 if (!isset($ds['min'])) { $ds['min'] = 'U'; }
295 if (!isset($ds['max'])) { $ds['max'] = 'U'; }
296 $create_cmd .= ' DS:'.$ds['name'].':'.$ds['type'].':'.$ds['heartbeat'].':'.$ds['min'].':'.$ds['max'];
297 }
298 foreach ($this->rra_base as $rra) {
299 if (!isset($rra['cf'])) { $rra['cf'] = 'AVERAGE'; }
300 if (!isset($rra['xff'])) { $rra['xff'] = 0.5; }
301 if (!isset($rra['step'])) { $rra['step'] = 1; }
302 if (!isset($rra['rows'])) { $rra['rows'] = 600; }
303 $create_cmd .= ' RRA:'.$rra['cf'].':'.$rra['xff'].':'.$rra['step'].':'.$rra['rows'];
304 }
305 if ($this->rra_add_max) {
306 foreach ($this->rra_base as $rra) {
307 if (!isset($rra['cf'])) {
308 // only rows that have no CF set will be looked at here
309 $rra['cf'] = 'MAX';
310 if (!isset($rra['xff'])) { $rra['xff'] = 0.5; }
311 if (!isset($rra['step'])) { $rra['step'] = 1; }
312 if (!isset($rra['rows'])) { $rra['rows'] = 600; }
313 $create_cmd .= ' RRA:'.$rra['cf'].':'.$rra['xff'].':'.$rra['step'].':'.$rra['rows'];
314 }
315 }
316 }
b61757c1 317 $return = `$create_cmd 2>&1`;
318 if (strpos($return, 'ERROR') !== false) {
319 trigger_error($this->rrd_file.' - rrd create error: '.$return, E_USER_WARNING);
320 }
b7878f4d 321 else { $this->status = 'ok'; }
322 }
323
6813be69 324 public function update($upArray = null) {
b7878f4d 325 // feed new data into RRD
633b21af 326 if ($this->status != 'ok') { trigger_error('Cannot update non-writeable file', E_USER_WARNING); return false; }
b7878f4d 327 $upvals = array();
de093632 328 if (isset($this->config_raw['update'])) {
97472ddc
RK
329 if (is_object($this->config_raw['update'])) {
330 // We assume it's an anonymous function
331 $upvals = $this->config_raw['update']();
b5e79d08 332 }
333 else {
97472ddc
RK
334 if (preg_match('/^\s*function\s+{(.*)}\s*$/is', $this->config_raw['update'], $regs)) {
335 $evalcode = '$upfunc = function() {'."\n".$regs[1]."\n".'};'."\n".'print(implode("\n", $upfunc()));';
336 }
337 else {
338 $evalcode = $this->config_raw['update'];
339 }
b5e79d08 340 if (!is_null($evalcode)) {
341 ob_start();
342 eval($evalcode);
343 $ret = ob_get_contents();
344 if (strlen($ret)) { $upvals = explode("\n", $ret); }
345 ob_end_clean();
346 }
de093632 347 }
348 }
349 else {
350 foreach ($this->rrd_fields as $ds) {
351 if (is_array($upArray) && isset($upArray[$ds['name']])) { $val = $upArray[$ds['name']]; }
352 elseif (isset($ds['update'])) {
97472ddc
RK
353 $val = null;
354 if (is_object($ds['update'])) {
355 // We assume it's an anonymous function
356 $val = $ds['update']();
357 }
358 elseif (substr($ds['update'], 0, 4) == 'val:') {
359 $val = trim(substr($ds['update'], 4));
c5db3bd5 360 }
de093632 361 elseif (substr($ds['update'], 0, 8) == 'snmp-if:') {
bb9f2310
RK
362 if (substr_count($ds['update'], ':') >= 4) {
363 list($nix, $snmphost, $snmpcomm, $ifname, $valtype) = explode(':', $ds['update'], 5);
364 }
365 else {
366 $snmphost = 'localhost'; $snmpcomm = 'public';
367 list($nix, $ifname, $valtype) = explode(':', $ds['update'], 3);
368 }
de093632 369 $iflist = explode("\n", `snmpwalk -v2c -c $snmpcomm $snmphost interfaces.ifTable.ifEntry.ifDescr`);
370 $ifnr = null;
371 foreach ($iflist as $ifdesc) {
372 if (preg_match('/ifDescr\.(\d+) = STRING: '.$ifname.'/', $ifdesc, $regs)) { $ifnr = $regs[1]; }
373 }
374 $oid = null;
375 if ($valtype == 'in') { $oid = '1.3.6.1.2.1.2.2.1.10.'.$ifnr; }
376 elseif ($valtype == 'out') { $oid = '1.3.6.1.2.1.2.2.1.16.'.$ifnr; }
377 if (!is_null($ifnr) && !is_null($oid)) {
97472ddc 378 $val = trim(substr(strrchr(`snmpget -v2c -c $snmpcomm $snmphost $oid`,":"),1));
de093632 379 }
380 }
97472ddc
RK
381 else {
382 if (preg_match('/^\s*function\s+{(.*)}\s*$/is', $ds['update'], $regs)) {
383 $evalcode = '$upfunc = function() {'."\n".$regs[1]."\n".'};'."\n".'print($upfunc());';
384 }
385 else {
386 $evalcode = $ds['update'];
387 }
388 if (!is_null($evalcode)) {
389 ob_start();
390 eval($evalcode);
391 $val = ob_get_contents();
392 ob_end_clean();
393 }
c5db3bd5 394 }
395 }
de093632 396 else { $val = null; }
b5e79d08 397 $upvals[$ds['name']] = $val;
c5db3bd5 398 }
b7878f4d 399 }
0b7a8261
RK
400 $upval_keys = array_keys($upvals);
401 $keys_have_names = !is_numeric(array_shift($upval_keys));
82d064f4 402 if (in_array('L', $upvals, true)) {
f21e94d9 403 // for at least one value, we need to set the same as the last recorded value
31cb3fc4 404 $fvals = $this->fetch();
405 $rowids = array_shift($fvals);
406 $lastvals = array_shift($fvals);
f21e94d9 407 foreach (array_keys($upvals, 'L') as $akey) {
0b7a8261 408 $upvals[$akey] = $keys_have_names?$lastvals[$akey]:$lastvals[$rowids[$akey]];
f21e94d9 409 }
410 }
97472ddc 411 array_walk($upvals, function(&$val, $key) { $val = is_numeric(trim($val)) ? trim($val) : "U"; });
25b93a4d 412 $return = null;
413 if (count($upvals)) {
bc025bc8 414 $update_cmd = $this->rrdtool_bin.' update '.$this->rrd_file
0b7a8261 415 .($keys_have_names?' --template '.implode(':', array_keys($upvals)):'').' N:'.implode(':', $upvals);
25b93a4d 416 $return = `$update_cmd 2>&1`;
417 }
b61757c1 418
419 if (strpos($return, 'ERROR') !== false) {
420 trigger_error($this->rrd_file.' - rrd update error: '.$return, E_USER_WARNING);
421 $success = false;
422 }
423 else { $success = true; }
633b21af 424 return $success;
b7878f4d 425 }
426
6813be69 427 public function fetch($cf = 'AVERAGE', $resolution = null, $start = null, $end = null) {
a039a75c 428 // fetch data from a RRD
24883b0f
RK
429 if (!in_array($this->status, array('ok','readonly'))) {
430 trigger_error('Error: rrd status is '.$this->status, E_USER_WARNING); return false;
431 }
a039a75c 432
433 if (!in_array($cf, array('AVERAGE','MIN','MAX','LAST'))) { $cf = 'AVERAGE'; }
434 if (!is_numeric($resolution)) { $resolution = $this->rrd_step; }
435 if (!is_numeric($end)) { $end = $this->last_update(); }
436 elseif ($end < 0) { $end += $this->last_update(); }
437 $end = intval($end/$resolution)*$resolution;
c797f39a 438 if (!is_numeric($start)) { $start = $end-$resolution; }
a039a75c 439 elseif ($start < 0) { $start += $end; }
440 $start = intval($start/$resolution)*$resolution;
441
c797f39a 442 $fetch_cmd = 'LANG=C '.$this->rrdtool_bin.' fetch '.$this->rrd_file.' '.$cf.' --resolution '.$resolution
bc025bc8 443 .' --start '.$start.' --end '.$end;
a039a75c 444 $return = `$fetch_cmd 2>&1`;
445
446 if (strpos($return, 'ERROR') !== false) {
b61757c1 447 trigger_error($this->rrd_file.' - rrd fetch error: '.$return, E_USER_WARNING);
a039a75c 448 $fresult = false;
449 }
450 else {
451 $fresult = array();
452 $rows = explode("\n", $return);
453 $fields = preg_split('/\s+/', array_shift($rows));
6f3c5805
RK
454 if (in_array(array_shift($fields), array('timestamp', ''))) {
455 //$fresult[0] = $fields;
a039a75c 456 foreach ($rows as $row) {
457 if (strlen(trim($row))) {
458 $rvals = preg_split('/\s+/', $row);
31cb3fc4 459 $rtime = str_replace(':', '', array_shift($rvals));
a039a75c 460 $rv_array = array();
461 foreach ($rvals as $key=>$rval) {
462 $rv_array[$fields[$key]] = ($rval=='nan')?null:floatval($rval);
463 }
464 $fresult[$rtime] = $rv_array;
465 }
466 }
467 }
468 }
469 return $fresult;
470 }
b7878f4d 471
6813be69 472 public function last_update() {
f5e899df 473 // fetch time of last update in this RRD file
7edd708f
RK
474 static $last_update, $last_saved;
475 if (isset($last_update) && isset($last_saved) && ($last_saved <= (time() - 10))) { unset($last_update); }
f5e899df 476 if (!isset($last_update) && in_array($this->status, array('ok','readonly'))) {
bc025bc8 477 $last_cmd = $this->rrdtool_bin.' last '.$this->rrd_file;
f5e899df 478 $return = trim(`$last_cmd 2>&1`);
479 $last_update = is_numeric($return)?$return:null;
7edd708f 480 $last_saved = time();
f5e899df 481 }
482 return isset($last_update)?$last_update:null;
483 }
484
6813be69 485 public function graph($timeframe = 'day', $sub = null, $extra = null) {
a039a75c 486 // create a RRD graph
487 static $gColors;
b7878f4d 488 if (!isset($gColors)) {
24883b0f
RK
489 $gColors = array('#00CC00','#0000FF','#000000','#FF0000','#00FF00','#FFFF00','#FF00FF','#00FFFF',
490 '#808080','#800000','#008000','#000080','#808000','#800080','#008080','#C0C0C0');
b7878f4d 491 }
492
24883b0f
RK
493 if (!in_array($this->status, array('ok','readonly','graphonly'))) {
494 trigger_error('Error: rrd status is '.$this->status, E_USER_WARNING); return false;
495 }
a039a75c 496
497 // assemble configuration
e1727e7f 498 $gconf = (array)$extra;
2c30ff69 499 if (!is_null($sub) && is_array($this->config_raw['graph.'.$sub])) {
e1727e7f 500 $gconf = $gconf + $this->config_raw['graph.'.$sub];
a039a75c 501 }
e1727e7f 502 $gconf = $gconf + (array)$this->config_graph;
a039a75c 503
504 if (isset($gconf['format']) && ($gconf['format'] == 'SVG')) {
505 $format = $gconf['format']; $fmt_ext = '.svg';
506 }
507 elseif (isset($gconf['format']) && ($gconf['format'] == 'EPS')) {
508 $format = $gconf['format']; $fmt_ext = '.eps';
509 }
510 elseif (isset($gconf['format']) && ($gconf['format'] == 'PDF')) {
511 $format = $gconf['format']; $fmt_ext = '.pdf';
512 }
513 else {
514 $format = 'PNG'; $fmt_ext = '.png';
515 }
516
517 if (isset($gconf['filename'])) { $fname = $gconf['filename']; }
31df2e13 518 else { $fname = $this->basename.(is_null($sub)?'':'-%s').'-%t%f'; }
2c30ff69 519 $fname = str_replace('%s', strval($sub), $fname);
a039a75c 520 $fname = str_replace('%t', $timeframe, $fname);
521 $fname = str_replace('%f', $fmt_ext, $fname);
522 if (substr($fname, -strlen($fmt_ext)) != $fmt_ext) { $fname .= $fmt_ext; }
cb2420c6
RK
523 if (isset($gconf['path']) && ($fname[0] != '/')) { $fname = $gconf['path'].'/'.$fname; }
524 if ($fname[0] != '/') { $fname = $this->basedir.$fname; }
ebe03325 525 $fname = str_replace('//', '/', $fname);
b7878f4d 526
abe8eac1 527 $graphrows = array(); $specialrows = array(); $gC = 0;
4ba56977 528 $gDefs = ''; $gGraphs = ''; $addSpecial = '';
529
cd6b890c 530 // the default size for the graph area has a width of 400px, so use 400 slices by default
4ba56977 531 if ($timeframe == 'day') {
a039a75c 532 $slice = isset($gconf['slice'])?$gconf['slice']:300; // 5 minutes
cd6b890c 533 $duration = isset($gconf['duration'])?$gconf['duration']:400*$slice; // 33.33 hours
4ba56977 534 // vertical lines at day borders
535 $addSpecial .= ' VRULE:'.strtotime(date('Y-m-d')).'#FF0000';
536 $addSpecial .= ' VRULE:'.strtotime(date('Y-m-d').' -1 day').'#FF0000';
a039a75c 537 if (!isset($gconf['grid_x'])) { $gconf['grid_x'] = 'HOUR:1:HOUR:6:HOUR:2:0:%-H'; }
4ba56977 538 }
539 elseif ($timeframe == 'week') {
a039a75c 540 $slice = isset($gconf['slice'])?$gconf['slice']:1800; // 30 minutes
cd6b890c 541 $duration = isset($gconf['duration'])?$gconf['duration']:400*$slice; // 8.33 days
4ba56977 542 // vertical lines at week borders
543 $addSpecial .= ' VRULE:'.strtotime(date('Y-m-d').' '.(-date('w')+1).' day').'#FF0000';
544 $addSpecial .= ' VRULE:'.strtotime(date('Y-m-d').' '.(-date('w')-6).' day').'#FF0000';
545 }
546 elseif ($timeframe == 'month') {
a039a75c 547 $slice = isset($gconf['slice'])?$gconf['slice']:7200; // 2 hours
cd6b890c 548 $duration = isset($gconf['duration'])?$gconf['duration']:400*$slice; // 33.33 days
4ba56977 549 // vertical lines at month borders
550 $addSpecial .= ' VRULE:'.strtotime(date('Y-m-01')).'#FF0000';
551 $addSpecial .= ' VRULE:'.strtotime(date('Y-m-01').' -1 month').'#FF0000';
552 }
553 elseif ($timeframe == 'year') {
a039a75c 554 $slice = isset($gconf['slice'])?$gconf['slice']:86400; // 1 day
cd6b890c 555 $duration = isset($gconf['duration'])?$gconf['duration']:400*$slice; // 400 days
4ba56977 556 // vertical lines at month borders
dfe923be 557 $addSpecial .= ' VRULE:'.strtotime(date('Y-01-01 12:00:00')).'#FF0000';
558 $addSpecial .= ' VRULE:'.strtotime(date('Y-01-01 12:00:00').' -1 year').'#FF0000';
4ba56977 559 }
560 else {
a039a75c 561 $duration = isset($gconf['duration'])?$gconf['duration']:$this->rrd_step*500; // 500 steps
562 $slice = isset($gconf['slice'])?$gconf['slice']:$this->rrd_step; // whatever our step is
4ba56977 563 }
564
67c0bd08 565 $use_gcrows = (isset($gconf['rows']) && count($gconf['rows']));
566 if ($use_gcrows) { $grow_def =& $gconf['rows']; }
567 else { $grow_def =& $this->rrd_fields; }
568 foreach ($grow_def as $key=>$erow) {
569 if (isset($erow['name']) && strlen($erow['name'])) {
570 if (!isset($erow['scale']) && isset($gconf['scale'])) { $erow['scale'] = $gconf['scale']; }
24883b0f
RK
571 if (!isset($erow['scale_time_src']) && isset($gconf['scale_time_src'])) {
572 $erow['scale_time_src'] = $gconf['scale_time_src'];
573 }
574 if (!isset($erow['scale_time_tgt']) && isset($gconf['scale_time_tgt'])) {
575 $erow['scale_time_tgt'] = $gconf['scale_time_tgt'];
576 }
67c0bd08 577 foreach (array('scale_time_src','scale_time_tgt') as $st) {
578 if (!isset($erow[$st]) || !is_numeric($erow[$st])) {
0ffe81c3 579 switch ($erow[$st] ?? null) {
67c0bd08 580 case 'dyn':
581 case 'auto':
582 $erow[$st] = $slice;
583 break;
584 case 'day':
585 $erow[$st] = 24*3600;
586 break;
587 case '2hr':
588 case '2hours':
589 $erow[$st] = 7200;
590 break;
591 case 'hr':
592 case 'hour':
593 $erow[$st] = 3600;
594 break;
595 case '30min':
596 $erow[$st] = 1800;
597 break;
598 case '5min':
599 $erow[$st] = 300;
600 break;
601 case 'min':
602 $erow[$st] = 60;
603 break;
604 case 's':
605 case 'sec':
606 default:
607 $erow[$st] = 1;
608 break;
2304f1ba 609 }
610 }
b7878f4d 611 }
67c0bd08 612 $scale_time_factor = $erow['scale_time_tgt']/$erow['scale_time_src'];
613 if ($scale_time_factor != 1) { $erow['scale'] = (isset($erow['scale'])?$erow['scale']:1)*$scale_time_factor; }
b7878f4d 614 $grow = array();
67c0bd08 615 $grow['dType'] = ($use_gcrows && isset($erow['dType']))?$erow['dType']:'DEF';
616 $grow['name'] = $erow['name'].(isset($erow['scale'])?'_tmp':'');
617 if ($grow['dType'] == 'DEF') {
618 $grow['dsname'] = ($use_gcrows && isset($erow['dsname']))?$erow['dsname']:$erow['name'];
619 if ($use_gcrows && isset($erow['dsfile'])) { $grow['dsfile'] = $erow['dsfile']; }
620 $grow['cf'] = ($use_gcrows && isset($erow['cf']))?$erow['cf']:'AVERAGE';
621 }
622 else {
623 $grow['rpn_expr'] = isset($erow['rpn_expr'])?$erow['rpn_expr']:'0';
624 }
625 if (isset($erow['scale'])) {
5f42eda6 626 $graphrows[] = $grow;
627 $grow = array();
628 $grow['dType'] = 'CDEF';
67c0bd08 629 $grow['name'] = $erow['name'];
cf2bc478 630 $grow['rpn_expr'] = $erow['name'].'_tmp,'.sprintf('%F', $erow['scale']).',*';
5f42eda6 631 }
67c0bd08 632 if ($use_gcrows) { $grow['gType'] = isset($erow['gType'])?$erow['gType']:'LINE1'; }
633 else { $grow['gType'] = ((count($grow_def)==2) && ($key==0))?'AREA':'LINE1'; }
634 $grow['color'] = isset($erow['color'])?$erow['color']:$gColors[$gC++];
635 $grow['color_bg'] = isset($erow['color_bg'])?$erow['color_bg']:'';
636 if ($gC >= count($gColors)) { $gC = 0; }
637 if (isset($erow['legend'])) {
638 $grow['legend'] = $erow['legend'];
75124b94 639 if (!isset($gconf['show_legend'])) { $gconf['show_legend'] = true; }
640 }
67c0bd08 641 if (isset($erow['stack'])) { $grow['stack'] = ($erow['stack'] == true); }
642 if (isset($erow['desc'])) { $grow['desc'] = $erow['desc']; }
643 if (isset($erow['legend_long'])) { $grow['legend_long'] = $erow['legend_long']; }
b7878f4d 644 $graphrows[] = $grow;
645 }
646 }
647
16cc643c 648 if (isset($gconf['special']) && count($gconf['special'])) {
649 foreach ($gconf['special'] as $crow) {
650 $srow = array();
651 $srow['sType'] = isset($crow['sType'])?$crow['sType']:'COMMENT';
652 if ($grow['sType'] != 'COMMENT') {
653 // XXX: use line below and remove cf var once we have rrdtol 1.2
586031ce 654 if ($this->rrd_version() >= '1.2') {
655 $srow['name'] = $crow['name'].(isset($crow['cf'])?'_'.$crow['cf']:'');
656 }
657 else {
658 $srow['name'] = $crow['name'];
659 $srow['cf'] = isset($crow['cf'])?$crow['cf']:'AVERAGE';
660 }
16cc643c 661 if (isset($crow['cf'])) {
586031ce 662 if ($this->rrd_version() >= '1.2') {
24883b0f
RK
663 $graphrows[] = array('dType'=>'VDEF', 'name'=>$srow['name'].'_'.$crow['cf'],
664 'rpn_expr'=>$srow['name'].','.$crow['cf']);
586031ce 665 }
16cc643c 666 }
667 elseif (isset($crow['rpn_expr'])) {
586031ce 668 if ($this->rrd_version() >= '1.2') {
669 $graphrows[] = array('dType'=>'VDEF', 'name'=>$srow['name'], 'rpn_expr'=>$crow['rpn_expr']);
670 }
16cc643c 671 }
672 }
673 $srow['text'] = isset($crow['text'])?$crow['text']:'';
674 $specialrows[] = $srow;
675 }
676 }
677 else {
ecc732d5 678 $td = $this->mod_textdomain;
16cc643c 679 foreach ($graphrows as $grow) {
680 if (isset($grow['gType']) && strlen($grow['gType'])) {
fe34d2fe 681 $textprefix = isset($grow['desc'])?$grow['desc']:(isset($grow['legend'])?$grow['legend']:$grow['name']);
586031ce 682 if ($this->rrd_version() >= '1.2') {
3b68f7a1 683 $graphrows[] = array('dType'=>'VDEF', 'name'=>'_'.$grow['name'].'__max', 'rpn_expr'=>$grow['name'].',MAXIMUM');
24883b0f
RK
684 $specialrows[] = array('sType'=>'PRINT', 'name'=>'_'.$grow['name'].'__max',
685 'text'=>$textprefix.'|'.dgettext($td, 'Maximum').'|%.2lf%s');
3b68f7a1 686 $graphrows[] = array('dType'=>'VDEF', 'name'=>'_'.$grow['name'].'__avg', 'rpn_expr'=>$grow['name'].',AVERAGE');
24883b0f
RK
687 $specialrows[] = array('sType'=>'PRINT', 'name'=>'_'.$grow['name'].'__avg',
688 'text'=>$textprefix.'|'.dgettext($td, 'Average').'|%.2lf%s');
3b68f7a1 689 $graphrows[] = array('dType'=>'VDEF', 'name'=>'_'.$grow['name'].'__last', 'rpn_expr'=>$grow['name'].',LAST');
24883b0f
RK
690 $specialrows[] = array('sType'=>'PRINT', 'name'=>'_'.$grow['name'].'__last',
691 'text'=>$textprefix.'|'.dgettext($td, 'Current').'|%.2lf%s');
586031ce 692 }
693 else {
24883b0f
RK
694 $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'], 'cf'=>'MAX',
695 'text'=>$textprefix.'|'.dgettext($td, 'Maximum').'|%.2lf%s');
696 $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'], 'cf'=>'AVERAGE',
697 'text'=>$textprefix.'|'.dgettext($td, 'Average').'|%.2lf%s');
698 $specialrows[] = array('sType'=>'PRINT', 'name'=>$grow['name'], 'cf'=>'LAST',
699 'text'=>$textprefix.'|'.dgettext($td, 'Current').'|%.2lf%s');
586031ce 700 }
16cc643c 701 }
702 }
703 }
704
a039a75c 705 $endtime = isset($gconf['time_end'])?$gconf['time_end']:(is_numeric($this->last_update())?$this->last_update():time());
706 $gOpts = ' --start '.($endtime-$duration).' --end '.$endtime.' --step '.$slice;
707 if (isset($gconf['label_top'])) { $gOpts .= ' --title '.$this->text_quote($gconf['label_top']); }
708 if (isset($gconf['label_y'])) { $gOpts .= ' --vertical-label '.$this->text_quote($gconf['label_y']); }
709 if (isset($gconf['width'])) { $gOpts .= ' --width '.$gconf['width']; }
710 if (isset($gconf['height'])) { $gOpts .= ' --height '.$gconf['height'];
711 if (($gconf['height'] <= 32) && isset($gconf['thumb']) && ($gconf['thumb'])) { $gOpts .= ' --only-graph'; }
4ba56977 712 }
a039a75c 713 if (!isset($gconf['show_legend']) || (!$gconf['show_legend'])) { $gOpts .= ' --no-legend'; }
d6ad10a5 714 if (isset($gconf['logarithmic']) && $gconf['logarithmic']) { $gOpts .= ' --logarithmic'; }
a039a75c 715 if (isset($gconf['min_y'])) { $gOpts .= ' --lower-limit '.$gconf['min_y']; }
716 if (isset($gconf['max_y'])) { $gOpts .= ' --upper-limit '.$gconf['max_y']; }
717 if (isset($gconf['fix_scale_y']) && $gconf['fix_scale_y']) { $gOpts .= ' --rigid'; }
718 if (isset($gconf['grid_x'])) { $gOpts .= ' --x-grid '.$gconf['grid_x']; }
719 if (isset($gconf['grid_y'])) { $gOpts .= ' --y-grid '.$gconf['grid_y']; }
d6ad10a5 720 if (isset($gconf['gridfit']) && (!$gconf['gridfit'])) { $gOpts .= ' --no-gridfit'; }
721 if (isset($gconf['calc_scale_y']) && $gconf['calc_scale_y']) { $gOpts .= ' --alt-autoscale'; }
722 if (isset($gconf['calc_max_y']) && $gconf['calc_max_y']) { $gOpts .= ' --alt-autoscale-max'; }
a039a75c 723 if (isset($gconf['units_exponent'])) { $gOpts .= ' --units-exponent '.$gconf['units_exponent']; }
724 if (isset($gconf['units_length'])) { $gOpts .= ' --units-length '.$gconf['units_length']; }
e5574259 725 if (($this->rrd_version() < '1.2') || !count($specialrows)) {
726 // lazy graphics omit all print reporting in RRDtool 1.2!
727 // --> so don't use them there when we want to print stuff
728 if (!isset($gconf['force_recreate']) || (!$gconf['force_recreate'])) { $gOpts .= ' --lazy'; }
729 }
a039a75c 730 if (isset($gconf['force_color']) && is_array($gconf['force_color'])) {
731 foreach ($gconf['force_color'] as $ctag=>$cval) { $gOpts .= ' --color '.$ctag.$cval; }
4ba56977 732 }
a039a75c 733 if (isset($gconf['force_font']) && is_array($gconf['force_font'])) {
734 foreach ($gconf['force_font'] as $ctag=>$cval) { $gOpts .= ' --font '.$ctag.$cval; }
4ba56977 735 }
a039a75c 736 if (isset($gconf['units_binary']) && $gconf['units_binary']) { $gOpts .= ' --base 1024'; }
4ba56977 737
b7878f4d 738 foreach ($graphrows as $grow) {
4ba56977 739 if (isset($grow['dType']) && strlen($grow['dType'])) {
740 $gDefs .= ' '.$grow['dType'].':'.$grow['name'].'=';
b1776944 741 if ($grow['dType'] == 'DEF') {
742 $gDefs .= isset($grow['dsfile'])?$grow['dsfile']:$this->rrd_file;
743 $gDefs .= ':'.$grow['dsname'].':'.$grow['cf'];
744 }
745 else { $gDefs .= $grow['rpn_expr']; }
4ba56977 746 }
747 if (isset($grow['gType']) && strlen($grow['gType'])) {
16cc643c 748 // XXX: change from STACK type to STACK flag once we have rrdtool 1.2
586031ce 749 if ($this->rrd_version() < '1.2') {
750 // rrdtool 1.0 only know STACK type
751 if (isset($grow['stack']) && $grow['stack']) { $grow['gType'] = 'STACK'; }
752 }
4ba56977 753 $gGraphs .= ' '.$grow['gType'].':'.$grow['name'].$grow['color'];
754 if (isset($grow['legend'])) { $gGraphs .= ':'.$this->text_quote($grow['legend']); }
586031ce 755 if ($this->rrd_version() >= '1.2') {
756 // rrdtool 1.2 and above have STACK flag
757 if (isset($grow['stack']) && $grow['stack']) { $gGraphs .= ':STACK'; }
758 }
4ba56977 759 }
b7878f4d 760 }
761
16cc643c 762 foreach ($specialrows as $srow) {
763 $addSpecial .= ' '.$srow['sType'];
586031ce 764 if ($this->rrd_version() >= '1.2') {
765 $addSpecial .= (($srow['sType']!='COMMENT')?':'.$srow['name']:'');
766 }
767 else {
768 $addSpecial .= (($srow['sType']!='COMMENT')?':'.$srow['name'].':'.$srow['cf']:'');
769 }
16cc643c 770 $addSpecial .= ':'.$this->text_quote($srow['text']);
771 }
772
bc025bc8 773 $graph_cmd = $this->rrdtool_bin.' graph '.str_replace('*', '\*', $fname.$gOpts.$gDefs.$gGraphs.$addSpecial);
5dd242ef
RK
774 if ((file_exists($fname) && !is_writable($fname)) ||
775 (!file_exists($fname) && !is_writable(dirname($fname)))) {
461e89f0
RK
776 trigger_error($this->rrd_file.' - graph file not writable: '.$fname, E_USER_WARNING);
777 return 'command:'.$graph_cmd."\n\n".'unwritable file: '.$fname;
586031ce 778 }
461e89f0
RK
779 $graph_out = `$graph_cmd 2>&1`;
780
781 if (strpos($graph_out, 'ERROR') !== false) {
782 trigger_error($this->rrd_file.' - rrd graph error: '.$graph_out, E_USER_WARNING);
783 return 'command:'.$graph_cmd."\n\n".$graph_out;
b7878f4d 784 }
506711ee 785 $legendlines = '';
786 foreach ($graphrows as $grow) {
787 $legendline = isset($grow['desc'])?$grow['desc']:(isset($grow['legend'])?$grow['legend']:$grow['name']);
0ffe81c3 788 $legendline .= '|'.($grow['color'] ?? '');
506711ee 789 $legendline .= '|'.(isset($grow['color_bg'])?$grow['color_bg']:'');
790 $legendline .= '|'.(isset($grow['legend_long'])?$grow['legend_long']:'');
791 $legendlines .= 'legend:'.$legendline."\n";
792 }
461e89f0 793 $return = 'file:'.$fname."\n".$legendlines.$graph_out;
a039a75c 794 return $return;
b7878f4d 795 }
796
6813be69 797 public function graph_plus($timeframe = 'day', $sub = null, $extra = null) {
f5e899df 798 // create a RRD graph and return meta info as a ready-to-use array
506711ee 799 $gmeta = array('filename'=>null,'legends_long'=>false,'default_colorize'=>false);
f5e899df 800 $ret = $this->graph($timeframe, $sub, $extra);
586031ce 801 if (0) {
802 // debug output
803 $gmeta['ret'] = $ret;
804 }
f5e899df 805 $grout = explode("\n", $ret);
806 foreach ($grout as $gline) {
586031ce 807 if (preg_match('/^command:(.+)$/', $gline, $regs)) {
808 $gmeta['graph_cmd'] = $regs[1];
809 }
810 elseif (preg_match('/^file:(.+)$/', $gline, $regs)) {
f5e899df 811 $gmeta['filename'] = $regs[1];
812 }
48c82fbe 813 elseif (preg_match('/^legend:([^\|]+)\|([^|]*)\|([^\|]*)\|(.*)$/', $gline, $regs)) {
506711ee 814 $gmeta['legend'][$regs[1]] = array('color'=>$regs[2], 'color_bg'=>$regs[3], 'desc_long'=>$regs[4]);
815 if (strlen($regs[4])) { $gmeta['legends_long'] = true; }
816 if (strlen($regs[3]) || strlen($regs[4])) { $gmeta['default_colorize'] = true; }
817 }
f5e899df 818 elseif (preg_match('/^(\d+)x(\d+)$/', $gline, $regs)) {
819 $gmeta['width'] = $regs[1]; $gmeta['height'] = $regs[2];
820 }
821 elseif (preg_match('/^([^\|]+)\|([^|]+)\|([^\|]*)$/', $gline, $regs)) {
822 $gmeta['data'][$regs[1]][$regs[2]] = $regs[3];
823 }
824 elseif (preg_match('/^([^\|]+)\|([^\|]*)$/', $gline, $regs)) {
825 $gmeta['var'][$regs[1]] = $regs[2];
826 }
827 elseif (strlen(trim($gline))) {
828 $gmeta['info'][] = $gline;
829 }
830 }
831 if (is_null($gmeta['filename'])) {
832 $gmeta['filename'] = $this->basename.(!is_null($sub)?'-'.$sub:'').'-'.$timeframe.'.png';
833 }
834 return $gmeta;
835 }
836
6813be69 837 public function page($sub = null, $page_extras = null, $graph_extras = null) {
099bd59f 838 // create a (HTML) page and return it in a string
839
840 // assemble configuration
841 $pconf = (array)$page_extras;
842 if (!is_null($sub) && is_array($this->config_raw['page.'.$sub])) {
843 $pconf = $pconf + $this->config_raw['page.'.$sub];
844 }
845 $pconf = $pconf + (array)$this->config_page;
846
847 $return = null;
0ffe81c3 848 switch ($pconf['type'] ?? null) {
099bd59f 849 case 'index':
850 $return = $this->page_index($pconf);
851 break;
852 case 'overview':
853 $return = $this->page_overview($pconf, $graph_extras);
854 break;
855 case 'simple':
856 default:
857 $return = $this->page_simple($pconf, $graph_extras);
858 break;
859 }
860 return $return;
861 }
862
6813be69 863 public function simple_html($sub = null, $page_extras = null, $graph_extras = null) {
b7878f4d 864 // create a simple (MRTG-like) HTML page and return it in a string
099bd59f 865 // XXX: this is here temporarily for compat only, it's preferred to use page()!
6813be69 866 trigger_error(__CLASS__.'::'.__METHOD__.' is deprecated, use page() instead.', E_USER_NOTICE);
a039a75c 867
868 // assemble configuration
e1727e7f 869 $pconf = (array)$page_extras;
2c30ff69 870 if (!is_null($sub) && is_array($this->config_raw['page.'.$sub])) {
e1727e7f 871 $pconf = $pconf + $this->config_raw['page.'.$sub];
a039a75c 872 }
e1727e7f 873 $pconf = $pconf + (array)$this->config_page;
a039a75c 874
099bd59f 875 return $this->page_simple($pconf, $graph_extras);
876 }
877
6813be69 878 private function page_index($pconf) {
099bd59f 879 // create a bare, very simple index list HTML page and return it in a string
724f6e78 880 $td = $this->mod_textdomain;
881 $ptitle = isset($pconf['title_page'])?$pconf['title_page']:dgettext($td, 'RRD statistics index');
099bd59f 882
9efda7da
RK
883 $out = '<!DOCTYPE html>'."\n";
884 $out .= '<html><head>'."\n";
586031ce 885 $out .= '<title>'.$ptitle.'</title>'."\n";
886 $out .= '<style type="text/css">'."\n";
099bd59f 887 if (isset($pconf['style_base'])) { $out .= $pconf['style_base']; }
888 else {
586031ce 889 $out .= 'h1 { font-weight: bold; font-size: 1.5em; }'."\n";
890 $out .= '.footer { font-size: 0.75em; margin: 0.5em 0; }'."\n";
891 $out .= 'li.scanfile { font-style: italic; }'."\n";
099bd59f 892 }
893 if (isset($pconf['style'])) { $out .= $pconf['style']; }
586031ce 894 $out .= '</style>'."\n";
895 $out .= '</head>'."\n";
896 $out .= '<body>'."\n";
099bd59f 897
586031ce 898 $out .= '<h1>'.$ptitle.'</h1>'."\n";
f5e899df 899 if (isset($pconf['text_intro']) && strlen($pconf['text_intro'])) {
586031ce 900 $out .= '<p class="intro">'.$pconf['text_intro'].'</p>'."\n";
099bd59f 901 }
f5e899df 902 elseif (!isset($pconf['text_intro'])) {
586031ce 903 $out .= '<p class="intro">'.dgettext($td, 'The following RRD stats are available:').'</p>'."\n";
099bd59f 904 }
f5e899df 905
906 $stats = $this->h_page_statsArray($pconf);
907
82d064f4 908 if (isset($pconf['stats_url'])) { $sURL_base = $pconf['stats_url']; }
909 else { $sURL_base = '?stat=%i%a'; }
910
911 if (isset($pconf['stats_url_add'])) { $sURL_add = $pconf['stats_url_add']; }
9efda7da 912 else { $sURL_add = '&amp;sub=%s'; }
82d064f4 913
586031ce 914 $out .= '<ul class="indexlist">'."\n";
099bd59f 915 foreach ($stats as $stat) {
916 $out .= '<li'.(isset($stat['class'])?' class="'.$stat['class'].'"':'').'>';
82d064f4 917 $sURL = str_replace('%i', $stat['name'], $sURL_base);
918 $sURL = str_replace('%a', '', $sURL);
919 $sURL = str_replace('%s', '', $sURL);
920 $out .= '<a href="'.$sURL.'">'.$stat['name'].'</a>';
099bd59f 921 if (isset($stat['sub']) && count($stat['sub'])) {
922 $sprt = array();
82d064f4 923 foreach ($stat['sub'] as $ssub) {
924 $sURL = str_replace('%i', $stat['name'], $sURL_base);
925 $sURL = str_replace('%a', $sURL_add, $sURL);
926 $sURL = str_replace('%s', $ssub, $sURL);
927 $sprt[] = '<a href="'.$sURL.'">'.$ssub.'</a>';
928 }
9efda7da 929 $out .= ' <span class="subs">('.implode(', ', $sprt).')</span>';
099bd59f 930 }
586031ce 931 $out .= '</li>'."\n";
099bd59f 932 }
586031ce 933 $out .= '</ul>'."\n";
099bd59f 934
f5e899df 935 $out .= $this->h_page_footer();
586031ce 936 $out .= '</body></html>'."\n";
099bd59f 937 return $out;
938 }
939
6813be69 940 private function page_overview($pconf, $graph_extras = null) {
099bd59f 941 // create an overview HTML page (including graphs) and return it in a string
724f6e78 942 $td = $this->mod_textdomain;
943 $ptitle = isset($pconf['title_page'])?$pconf['title_page']:dgettext($td, 'RRD statistics overview');
f5e899df 944
9efda7da
RK
945 $out = '<!DOCTYPE html>'."\n";
946 $out .= '<html><head>'."\n";
586031ce 947 $out .= '<title>'.$ptitle.'</title>'."\n";
948 $out .= '<style type="text/css">'."\n";
f5e899df 949 if (isset($pconf['style_base'])) { $out .= $pconf['style_base']; }
950 else {
586031ce 951 $out .= 'h1 { font-weight: bold; font-size: 1.5em; }'."\n";
952 $out .= 'h2 { font-weight: bold; font-size: 1em; margin: 0.5em 0; }'."\n";
953 $out .= '.footer { font-size: 0.75em; margin: 0.5em 0; }'."\n";
954 $out .= 'img.rrdgraph { border: none; }'."\n";
f5e899df 955 }
956 if (isset($pconf['style'])) { $out .= $pconf['style']; }
586031ce 957 $out .= '</style>'."\n";
958 $out .= '</head>'."\n";
959 $out .= '<body>'."\n";
f5e899df 960
586031ce 961 $out .= '<h1>'.$ptitle.'</h1>'."\n";
24883b0f
RK
962 if (isset($pconf['text_intro']) && strlen($pconf['text_intro'])) {
963 $out .= '<p class="intro">'.$pconf['text_intro'].'</p>';
964 }
f5e899df 965
966 $stats = $this->h_page_statsArray($pconf);
967
ecc732d5 968 if (isset($pconf['stats_url'])) { $sURL_base = $pconf['stats_url']; }
969 else { $sURL_base = '?stat=%i%a'; }
970
971 if (isset($pconf['stats_url_add'])) { $sURL_add = $pconf['stats_url_add']; }
9efda7da 972 else { $sURL_add = '&amp;sub=%s'; }
ecc732d5 973
0ffe81c3
RK
974 $default_num_cols = $GLOBALS['ua']->isMobile() ? 1 : 2;
975 $num_cols = is_numeric($pconf['num_rows'] ?? null) ? $pconf['num_rows'] : $default_num_cols;
976 $num_rows = ceil(count($stats) / $num_cols);
f5e899df 977
586031ce 978 $out .= '<table class="overview">'."\n";
7ce4eead 979 for ($row = 0; $row < $num_rows; $row++) {
586031ce 980 $out .= '<tr>'."\n";
7ce4eead
RK
981 for ($col = 0; $col < $num_cols; $col++) {
982 $idx = $row * $num_cols + $col;
586031ce 983 $out .= '<td>'."\n";
f5e899df 984 if ($idx < count($stats)) {
2304f1ba 985 @list($sname, $s_psub) = explode('|', $stats[$idx]['name'], 2);
f5e899df 986 $s_psname = 'page'.(isset($s_psub)?'.'.$s_psub:'');
0ffe81c3 987 $g_sub = $this->config_all[$sname][$s_psname]['graph_sub'] ?? '';
f5e899df 988
989 if (isset($this->config_all[$sname][$s_psname]['title_page'])) {
990 $s_ptitle = $this->config_all[$sname][$s_psname]['title_page'];
991 }
992 elseif (isset($this->config_all[$sname]['page']['title_page'])) {
993 $s_ptitle = $this->config_all[$sname]['page']['title_page'];
994 }
995 else {
24883b0f
RK
996 $s_ptitle = isset($s_psub)
997 ?sprintf(dgettext($td, '%s (%s) statistics'), $sname, $s_psub)
998 :sprintf(dgettext($td, '%s statistics'), $sname);
f5e899df 999 }
1000 if (!isset($pconf['hide_titles']) || !$pconf['hide_titles']) {
586031ce 1001 $out .= '<h2>'.$s_ptitle.'</h2>'."\n";
f5e899df 1002 }
1003
1004 $s_rrd = new rrdstat($this->config_all, $sname);
1005 if (in_array($s_rrd->status, array('ok','readonly','graphonly'))) {
1006 $tframe = isset($pconf['graph_timeframe'])?$pconf['graph_timeframe']:'day';
1007 $gmeta = $s_rrd->graph_plus($tframe, $g_sub);
1008 if (isset($pconf['graph_url'])) {
1009 $gURL = $pconf['graph_url'];
ecc732d5 1010 $gURL = str_replace('%f', basename($gmeta['filename']), $gURL);
1011 $gURL = str_replace('%p', $gmeta['filename'], $gURL);
f5e899df 1012 if (substr($gURL, -1) == '/') { $gURL .= $gmeta['filename']; }
1013 }
1014 else {
1015 $gURL = $gmeta['filename'];
1016 }
ecc732d5 1017 $sURL = str_replace('%i', $sname, $sURL_base);
1018 $sURL = str_replace('%a', isset($s_psub)?$sURL_add:'', $sURL);
506711ee 1019 $sURL = str_replace('%s', isset($s_psub)?$s_psub:'', $sURL);
ecc732d5 1020 $out .= '<a href="'.$sURL.'">';
f5e899df 1021 $out .= '<img src="'.$gURL.'"';
1022 $out .= ' alt="'.$s_rrd->basename.(!is_null($g_sub)?' - '.$g_sub:'').' - '.$tframe.'" class="rrdgraph"';
24883b0f
RK
1023 if (isset($gmeta['width']) && isset($gmeta['height'])) {
1024 $out .= ' style="width:'.$gmeta['width'].'px;height:'.$gmeta['height'].'px;"';
1025 }
586031ce 1026 $out .= '></a>'."\n";
f5e899df 1027 }
1028 else {
586031ce 1029 $out .= sprintf(dgettext($td, 'RRD error: status is "%s"'), $s_rrd->status)."\n";
f5e899df 1030 }
1031 }
1032 else {
1033 $out .= '&nbsp;';
1034 }
586031ce 1035 $out .= '</td>'."\n";
f5e899df 1036 }
586031ce 1037 $out .= '</tr>'."\n";
f5e899df 1038 }
586031ce 1039 $out .= '</table>'."\n";
f5e899df 1040
1041 $out .= $this->h_page_footer();
586031ce 1042 $out .= '</body></html>'."\n";
f5e899df 1043 return $out;
099bd59f 1044 }
1045
6813be69 1046 private function page_simple($pconf, $graph_extras = null) {
099bd59f 1047 // create a simple (MRTG-like) HTML page and return it in a string
724f6e78 1048 $td = $this->mod_textdomain;
099bd59f 1049
724f6e78 1050 $ptitle = isset($pconf['title_page'])?$pconf['title_page']:sprintf(dgettext($td, '%s - RRD statistics'),$this->basename);
16cc643c 1051 $gtitle = array();
724f6e78 1052 $gtitle['day'] = isset($pconf['title_day'])?$pconf['title_day']:dgettext($td, 'Day overview (scaling 5 minutes)');
1053 $gtitle['week'] = isset($pconf['title_week'])?$pconf['title_week']:dgettext($td, 'Week overview (scaling 30 minutes)');
1054 $gtitle['month'] = isset($pconf['title_month'])?$pconf['title_month']:dgettext($td, 'Month overview (scaling 2 hours)');
1055 $gtitle['year'] = isset($pconf['title_year'])?$pconf['title_year']:dgettext($td, 'Year overview (scaling 1 day)');
506711ee 1056 $ltitle = isset($pconf['title_legend'])?$pconf['title_legend']:dgettext($td, 'Legend:');
b7878f4d 1057
9efda7da
RK
1058 $out = '<!DOCTYPE html>'."\n";
1059 $out .= '<html><head>'."\n";
586031ce 1060 $out .= '<title>'.$ptitle.'</title>'."\n";
1061 $out .= '<style type="text/css">'."\n";
16cc643c 1062 if (isset($pconf['style_base'])) { $out .= $pconf['style_base']; }
1063 else {
586031ce 1064 $out .= 'h1 { font-weight: bold; font-size: 1.5em; }'."\n";
1065 $out .= 'h2 { font-weight: bold; font-size: 1em; }'."\n";
1066 $out .= '.gdata, .gvar, .ginfo { font-size: 0.75em; margin: 0.5em 0; }'."\n";
1067 $out .= 'table.gdata, table.legend { border: 1px solid gray; border-collapse: collapse; }'."\n";
1068 $out .= 'table.gdata td, table.gdata th, '."\n";
1069 $out .= 'table.legend td, table.legend th { border: 1px solid gray; padding: 0.1em 0.2em; }'."\n";
1070 $out .= 'div.legend { font-size: 0.75em; margin: 0.5em 0; }'."\n";
1071 $out .= 'div.legend p { margin: 0; }'."\n";
1072 $out .= '.footer { font-size: 0.75em; margin: 0.5em 0; }'."\n";
16cc643c 1073 }
1074 if (isset($pconf['style'])) { $out .= $pconf['style']; }
586031ce 1075 $out .= '</style>'."\n";
1076 $out .= '</head>'."\n";
1077 $out .= '<body>'."\n";
16cc643c 1078
586031ce 1079 $out .= '<h1>'.$ptitle.'</h1>'."\n";
24883b0f
RK
1080 if (isset($pconf['text_intro']) && strlen($pconf['text_intro'])) {
1081 $out .= '<p class="intro">'.$pconf['text_intro'].'</p>'."\n";
1082 }
2c30ff69 1083 if (!isset($pconf['show_update']) || $pconf['show_update']) {
724f6e78 1084 $out .= '<p class="last_up">';
1085 if (is_null($this->last_update())) { $up_time = dgettext($td, 'unknown'); }
1086 elseif (class_exists('baseutils')) { $up_time = baseutils::dateFormat($this->last_update(), 'short'); }
1087 else { $up_time = date('Y-m-d H:i:s', $this->last_update()); }
1088 $out .= sprintf(dgettext($td, 'Last Update: %s'), $up_time);
586031ce 1089 $out .= '</p>'."\n";
2c30ff69 1090 }
4ba56977 1091
f5e899df 1092 $g_sub = isset($pconf['graph_sub'])?$pconf['graph_sub']:null;
b1776944 1093 if (in_array($this->status, array('ok','readonly','graphonly'))) {
b7878f4d 1094 foreach (array('day','week','month','year') as $tframe) {
f5e899df 1095 $gmeta = $this->graph_plus($tframe, $g_sub, $graph_extras);
253ead9f 1096 if (isset($pconf['graph_url'])) {
1097 $gURL = $pconf['graph_url'];
82d064f4 1098 $gURL = str_replace('%f', basename($gmeta['filename']), $gURL);
1099 $gURL = str_replace('%p', $gmeta['filename'], $gURL);
f5e899df 1100 if (substr($gURL, -1) == '/') { $gURL .= $gmeta['filename']; }
253ead9f 1101 }
1102 else {
f5e899df 1103 $gURL = $gmeta['filename'];
253ead9f 1104 }
586031ce 1105 $out .= '<div class="'.$tframe.'">'."\n";
1106 if (0) {
1107 // debug output
1108 ob_start();
1109 print_r($gmeta);
1110 $buffer = ob_get_contents();
1111 ob_end_clean();
1112 $out .= '<p>'.nl2br($buffer).'</p>';
1113 }
1114 $out .= '<h2>'.$gtitle[$tframe].'</h2>'."\n";
253ead9f 1115 $out .= '<img src="'.$gURL.'"';
31df2e13 1116 $out .= ' alt="'.$this->basename.(!is_null($g_sub)?' - '.$g_sub:'').' - '.$tframe.'" class="rrdgraph"';
24883b0f
RK
1117 if (isset($gmeta['width']) && isset($gmeta['height'])) {
1118 $out .= ' style="width:'.$gmeta['width'].'px;height:'.$gmeta['height'].'px;"';
1119 }
586031ce 1120 $out .= '>'."\n";
24883b0f
RK
1121 $colorize_data = (isset($pconf['data_colorize']) && $pconf['data_colorize']) ||
1122 (!isset($pconf['data_colorize']) && $gmeta['default_colorize']);
16cc643c 1123 if (isset($gmeta['data']) && count($gmeta['data'])) {
586031ce 1124 $out .= '<table class="gdata">'."\n";
16cc643c 1125 foreach ($gmeta['data'] as $field=>$gdata) {
506711ee 1126 $out .= '<tr><th';
1127 if ($colorize_data && isset($gmeta['legend'][$field])) {
1128 $out .= ' style="color:'.$gmeta['legend'][$field]['color'].';';
1129 if (strlen($gmeta['legend'][$field]['color_bg'])) {
1130 $out .= 'background-color:'.$gmeta['legend'][$field]['color_bg'].';';
1131 }
1132 $out .= '"';
1133 }
1134 $out .= '>'.$field.'</th>';
16cc643c 1135 foreach ($gdata as $gkey=>$gval) {
1136 $out .= '<td><span class="gkey">'.$gkey.': </span>'.$gval.'</td>';
1137 }
586031ce 1138 $out .= '</tr>'."\n";
16cc643c 1139 }
586031ce 1140 $out .= '</table>'."\n";
16cc643c 1141 }
1142 if (isset($gmeta['var']) && count($gmeta['var'])) {
1143 foreach ($gmeta['var'] as $gkey=>$gval) {
586031ce 1144 $out .= '<p class="gvar"><span class="gkey">'.$gkey.': </span>'.$gval.'</p>'."\n";
16cc643c 1145 }
1146 }
1147 if (isset($gmeta['info']) && count($gmeta['info'])) {
1148 foreach ($gmeta['info'] as $gval) {
586031ce 1149 $out .= '<p class="ginfo">'.$gval.'</p>'."\n";
16cc643c 1150 }
1151 }
586031ce 1152 $out .= '</div>'."\n";
b7878f4d 1153 }
506711ee 1154 if ($gmeta['legends_long'] && (!isset($pconf['show_legend']) || $pconf['show_legend'])) {
586031ce 1155 $out .= '<div class="legend">'."\n";
1156 $out .= '<p>'.$ltitle.'</p>'."\n";
1157 $out .= '<table class="legend">'."\n";
506711ee 1158 foreach ($gmeta['legend'] as $field=>$legend) {
d6ad10a5 1159 if (strlen($legend['desc_long'])) {
1160 $out .= '<tr><th';
1161 if ($colorize_data && isset($gmeta['legend'][$field])) {
1162 $out .= ' style="color:'.$gmeta['legend'][$field]['color'].';';
1163 if (strlen($gmeta['legend'][$field]['color_bg'])) {
1164 $out .= 'background-color:'.$gmeta['legend'][$field]['color_bg'].';';
1165 }
1166 $out .= '"';
506711ee 1167 }
d6ad10a5 1168 $out .= '>'.$field.'</th>';
1169 $out .= '<td>'.$legend['desc_long'].'</td>';
586031ce 1170 $out .= '</tr>'."\n";
506711ee 1171 }
506711ee 1172 }
586031ce 1173 $out .= '</table>'."\n";
1174 $out .= '</div>'."\n";
506711ee 1175 }
b7878f4d 1176 }
1177 else {
586031ce 1178 $out .= sprintf(dgettext($td, 'RRD error: status is "%s"'), $this->status)."\n";
b7878f4d 1179 }
c5db3bd5 1180
f5e899df 1181 $out .= $this->h_page_footer();
586031ce 1182 $out .= '</body></html>'."\n";
b7878f4d 1183 return $out;
1184 }
1185
6813be69 1186 private function h_page_statsArray($pconf) {
f5e899df 1187 // return array of stats to list on a page
1188 $stats = array();
1189 $snames = array(); $s_exclude = array(); $sfiles = array();
1190 if (isset($pconf['index_ids'])) {
1191 foreach (explode(',', $pconf['index_ids']) as $iid) {
cb2420c6 1192 if ($iid[0] == '-') { $s_exclude[] = substr($iid, 1); }
f5e899df 1193 else { $snames[] = $iid; }
1194 }
4ba56977 1195 }
f5e899df 1196 if (!isset($pconf['scan_config']) || $pconf['scan_config']) {
1197 foreach ($this->config_all as $iname=>$rinfo) {
1198 if (($iname != '*') && !(isset($rinfo['hidden']) && $rinfo['hidden']) &&
1199 !(in_array($iname, $snames)) && !(in_array($iname, $s_exclude))) {
1200 $snames[] = $iname;
1201 }
1202 }
1203 }
1204 foreach ($snames as $iname) {
1205 $newstat = array('name'=>$iname);
1206 $sfiles[] = isset($this->config_all[$iname]['file'])?$this->config_all[$iname]['file']:$iname.'.rrd';
0ffe81c3 1207 if (is_array($this->config_all[$iname] ?? null)) {
f5e899df 1208 foreach ($this->config_all[$iname] as $key=>$val) {
1209 if (substr($key, 0, 5) == 'page.') { $newstat['sub'][] = substr($key, 5); }
1210 }
1211 }
1212 $stats[] = $newstat;
1213 }
1214 if (isset($pconf['scan_files']) && $pconf['scan_files']) {
1215 $rrdfiles = glob('*.rrd');
1216 foreach ($rrdfiles as $rrdfile) {
1217 $iname = (substr($rrdfile, -4) == '.rrd')?substr($rrdfile, 0, -4):$rrdfile;
1218 if (!in_array($rrdfile, $sfiles) && !(in_array($iname, $s_exclude))) {
1219 $stats[] = array('name'=>$iname, 'class'=>'scanfile');
1220 }
1221 }
1222 }
1223 return $stats;
1224 }
1225
6813be69 1226 private function h_page_footer() {
f5e899df 1227 // return generic page footer
1228 $out = '<p class="footer">';
724f6e78 1229 $out .= sprintf(dgettext($this->mod_textdomain, 'Statistics created with %s using a library created by %s.'),
24883b0f 1230 '<a href="http://oss.oetiker.ch/rrdtool/">RRDtool</a>',
724f6e78 1231 '<a href="http://www.kairo.at/">KaiRo.at</a>');
586031ce 1232 $out .= '</p>'."\n";
f5e899df 1233 return $out;
4ba56977 1234 }
1235
6813be69 1236 private function text_quote($text) {
1237 $trans = array('"' => '\"', ':' => '\:');
1238 $qtext = '"'.strtr($text, $trans).'"';
1239 return $qtext;
1240 }
b7878f4d 1241}
1242?>