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