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