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