contrib/php-collection: Add a PHP frontend for graph generation.
[collectd.git] / contrib / php-collection / functions.php
1 <?php // vim:fenc=utf-8:filetype=php:ts=4
2 /*
3  * Copyright (C) 2009  Bruno PrĂ©mont <bonbons AT linux-vserver.org>
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License as published by the Free Software
7  * Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18
19 define('REGEXP_HOST', '/^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/');
20 define('REGEXP_PLUGIN', '/^[a-zA-Z0-9_.-]+$/');
21
22 /**
23  * Read input variable from GET, POST or COOKIE taking
24  * care of magic quotes
25  * @name Name of value to return
26  * @array User-input array ($_GET, $_POST or $_COOKIE)
27  * @default Default value
28  * @return $default if name in unknown in $array, otherwise
29  *         input value with magic quotes stripped off
30  */
31 function read_var($name, &$array, $default = null) {
32         if (isset($array[$name])) {
33                 if (is_array($array[$name])) {
34                         if (get_magic_quotes_gpc()) {
35                                 $ret = array();
36                                 while (list($k, $v) = each($array[$name]))
37                                         $ret[stripslashes($k)] = stripslashes($v);
38                                 return $ret;
39                         } else
40                                 return $array[$name];
41                 } else if (is_string($array[$name]) && get_magic_quotes_gpc()) {
42                         return stripslashes($array[$name]);
43                 } else
44                         return $array[$name];
45         } else
46                 return $default;
47 }
48
49 /**
50  * Alphabetically compare host names, comparing label
51  * from tld to node name
52  */
53 function collectd_compare_host($a, $b) {
54         $ea = explode('.', $a);
55         $eb = explode('.', $b);
56         $i = count($ea) - 1;
57         $j = count($eb) - 1;
58         while ($i >= 0 && $j >= 0)
59                 if (($r = strcmp($ea[$i--], $eb[$j--])) != 0)
60                         return $r;
61         return 0;
62 }
63
64 /**
65  * Fetch list of hosts found in collectd's datadirs.
66  * @return Sorted list of hosts (sorted by label from rigth to left)
67  */
68 function collectd_list_hosts() {
69         global $config;
70
71         $hosts = array();
72         foreach($config['datadirs'] as $datadir)
73                 if ($d = @opendir($datadir)) {
74                         while (($dent = readdir($d)) !== false)
75                                 if ($dent != '.' && $dent != '..' && is_dir($datadir.'/'.$dent) && preg_match(REGEXP_HOST, $dent))
76                                         $hosts[] = $dent;
77                         closedir($d);
78                 }
79         $hosts = array_unique($hosts);
80         usort($hosts, 'collectd_compare_host');
81         return $hosts;
82 }
83
84 /**
85  * Fetch list of plugins found in collectd's datadirs for given host.
86  * @arg_host Name of host for which to return plugins
87  * @return Sorted list of plugins (sorted alphabetically)
88  */
89 function collectd_list_plugins($arg_host) {
90         global $config;
91
92         $plugins = array();
93         foreach ($config['datadirs'] as $datadir)
94                 if (preg_match(REGEXP_HOST, $arg_host) && ($d = @opendir($datadir.'/'.$arg_host))) {
95                         while (($dent = readdir($d)) !== false)
96                                 if ($dent != '.' && $dent != '..' && is_dir($datadir.'/'.$arg_host.'/'.$dent)) {
97                                         if ($i = strpos($dent, '-'))
98                                                 $plugins[] = substr($dent, 0, $i);
99                                         else
100                                                 $plugins[] = $dent;
101                                 }
102                         closedir($d);
103                 }
104         $plugins = array_unique($plugins);
105         sort($plugins);
106         return $plugins;
107 }
108
109 /**
110  * Fetch list of plugin instances found in collectd's datadirs for given host+plugin
111  * @arg_host Name of host
112  * @arg_plugin Name of plugin
113  * @return Sorted list of plugin instances (sorted alphabetically)
114  */
115 function collectd_list_pinsts($arg_host, $arg_plugin) {
116         global $config;
117
118         $pinsts = array();
119         foreach ($config['datadirs'] as $datadir)
120                 if (preg_match(REGEXP_HOST, $arg_host) && ($d = opendir($datadir.'/'.$arg_host))) {
121                         while (($dent = readdir($d)) !== false)
122                                 if ($dent != '.' && $dent != '..' && is_dir($datadir.'/'.$arg_host.'/'.$dent)) {
123                                         if ($i = strpos($dent, '-')) {
124                                                 $plugin = substr($dent, 0, $i);
125                                                 $pinst  = substr($dent, $i+1);
126                                         } else {
127                                                 $plugin = $dent;
128                                                 $pinst  = '';
129                                         }
130                                         if ($plugin == $arg_plugin)
131                                                 $pinsts[] = $pinst;
132                                 }
133                         closedir($d);
134                 }
135         $pinsts = array_unique($pinsts);
136         sort($pinsts);
137         return $pinsts;
138 }
139
140 /**
141  * Fetch list of types found in collectd's datadirs for given host+plugin+instance
142  * @arg_host Name of host
143  * @arg_plugin Name of plugin
144  * @arg_pinst Plugin instance
145  * @return Sorted list of types (sorted alphabetically)
146  */
147 function collectd_list_types($arg_host, $arg_plugin, $arg_pinst) {
148         global $config;
149
150         $types = array();
151         $my_plugin = $arg_plugin . (strlen($arg_pinst) ? '-'.$arg_pinst : '');
152         if (!preg_match(REGEXP_PLUGIN, $my_plugin))
153                 return $types;
154         foreach ($config['datadirs'] as $datadir)
155                 if (preg_match(REGEXP_HOST, $arg_host) && ($d = @opendir($datadir.'/'.$arg_host.'/'.$my_plugin))) {
156                         while (($dent = readdir($d)) !== false)
157                                 if ($dent != '.' && $dent != '..' && is_file($datadir.'/'.$arg_host.'/'.$my_plugin.'/'.$dent) && substr($dent, strlen($dent)-4) == '.rrd') {
158                                         $dent = substr($dent, 0, strlen($dent)-4);
159                                         if ($i = strpos($dent, '-'))
160                                                 $types[] = substr($dent, 0, $i);
161                                         else
162                                                 $types[] = $dent;
163                                 }
164                         closedir($d);
165                 }
166         $types = array_unique($types);
167         sort($types);
168         return $types;
169 }
170
171 /**
172  * Fetch list of type instances found in collectd's datadirs for given host+plugin+instance+type
173  * @arg_host Name of host
174  * @arg_plugin Name of plugin
175  * @arg_pinst Plugin instance
176  * @arg_type Type
177  * @return Sorted list of type instances (sorted alphabetically)
178  */
179 function collectd_list_tinsts($arg_host, $arg_plugin, $arg_pinst, $arg_type) {
180         global $config;
181
182         $tinsts = array();
183         $my_plugin = $arg_plugin . (strlen($arg_pinst) ? '-'.$arg_pinst : '');
184         if (!preg_match(REGEXP_PLUGIN, $my_plugin))
185                 return $types;
186         foreach ($config['datadirs'] as $datadir)
187                 if (preg_match(REGEXP_HOST, $arg_host) && ($d = @opendir($datadir.'/'.$arg_host.'/'.$my_plugin))) {
188                         while (($dent = readdir($d)) !== false)
189                                 if ($dent != '.' && $dent != '..' && is_file($datadir.'/'.$arg_host.'/'.$my_plugin.'/'.$dent) && substr($dent, strlen($dent)-4) == '.rrd') {
190                                         $dent = substr($dent, 0, strlen($dent)-4);
191                                         if ($i = strpos($dent, '-')) {
192                                                 $type  = substr($dent, 0, $i);
193                                                 $tinst = substr($dent, $i+1);
194                                         } else {
195                                                 $type  = $dent;
196                                                 $tinst = '';
197                                         }
198                                         if ($type == $arg_type)
199                                                 $tinsts[] = $tinst;
200                                 }
201                         closedir($d);
202                 }
203         $tinsts = array_unique($tinsts);
204         sort($tinsts);
205         return $tinsts;
206 }
207
208 /**
209  * Parse symlinks in order to get an identifier that collectd understands
210  * (e.g. virtualisation is collected on host for individual VMs and can be
211  *  symlinked to the VM's hostname, support FLUSH for these by flushing
212  *  on the host-identifier instead of VM-identifier)
213  * @host Host name
214  * @plugin Plugin name
215  * @pinst Plugin instance
216  * @type Type name
217  * @tinst Type instance
218  * @return Identifier that collectd's FLUSH command understands
219  */
220 function collectd_identifier($host, $plugin, $pinst, $type, $tinst) {
221         global $config;
222         $rrd_realpath    = null;
223         $orig_identifier = sprintf('%s/%s%s%s/%s%s%s', $host, $plugin, strlen($pinst) ? '-' : '', $pinst, $type, strlen($tinst) ? '-' : '', $tinst);
224         $identifier      = null;
225         foreach ($config['datadirs'] as $datadir)
226                 if (is_file($datadir.'/'.$orig_identifier.'.rrd')) {
227                         $rrd_realpath = realpath($datadir.'/'.$orig_identifier.'.rrd');
228                         break;
229                 }
230         if ($rrd_realpath) {
231                 $identifier   = basename($rrd_realpath);
232                 $identifier   = substr($identifier, 0, strlen($identifier)-4);
233                 $rrd_realpath = dirname($rrd_realpath);
234                 $identifier   = basename($rrd_realpath).'/'.$identifier;
235                 $rrd_realpath = dirname($rrd_realpath);
236                 $identifier   = basename($rrd_realpath).'/'.$identifier;
237         }
238
239         if (is_null($identifier))
240                 return $orig_identifier;
241         else
242                 return $identifier;
243 }
244
245 /**
246  * Tell collectd that it should FLUSH all data it has regarding the
247  * graph we are about to generate.
248  * @host Host name
249  * @plugin Plugin name
250  * @pinst Plugin instance
251  * @type Type name
252  * @tinst Type instance
253  */
254 function collectd_flush($identifier) {
255         global $config;
256
257         if (!$config['collectd_sock'])
258                 return false;
259         if (is_null($identifier) || (is_array($identifier) && count($identifier) == 0) || !(is_string($identifier) || is_array($identifier)))
260                 return false;
261
262         if (is_null($host) || !is_string($host) || strlen($host) == 0)
263                 return false;
264         if (is_null($plugin) || !is_string($plugin) || strlen($plugin) == 0)
265                 return false;
266         if (is_null($pinst) || !is_string($pinst))
267                 return false;
268         if (is_null($type) || !is_string($type) || strlen($type) == 0)
269                 return false;
270         if (is_null($tinst) || (is_array($tinst) && count($tinst) == 0) || !(is_string($tinst) || is_array($tinst)))
271                 return false;
272
273         $u_errno  = 0;
274         $u_errmsg = '';
275         if ($socket = @fsockopen($config['collectd_sock'], 0, $u_errno, $u_errmsg)) {
276                 $cmd = 'FLUSH plugin=rrdtool';
277                 if (is_array($identifier)) {
278                         foreach ($identifier as $val)
279                                 $cmd .= sprintf(' identifier="%s"', $val);
280                 } else
281                         $cmd .= sprintf(' identifier="%s"', $identifier);
282                 $cmd .= "\n";
283
284                 $r = fwrite($socket, $cmd, strlen($cmd));
285                 if ($r === false || $r != strlen($cmd))
286                         error_log(sprintf("graph.php: Failed to write whole command to unix-socket: %d out of %d written", $r === false ? -1 : $r, strlen($cmd)));
287
288                 $resp = fgets($socket);
289                 if ($resp === false)
290                         error_log(sprintf("graph.php: Failed to read response from collectd for command: %s", trim($cmd)));
291
292                 $n = (int)$resp;
293                 while ($n-- > 0)
294                         fgets($socket);
295
296                 fclose($socket);
297         } else
298                 error_log(sprintf("graph.php: Failed to open unix-socket to collectd: %d: %s", $u_errno, $u_errmsg));
299 }
300
301 class CollectdColor {
302         private $r = 0;
303         private $g = 0;
304         private $b = 0;
305
306         function __construct($value = null) {
307                 if (is_null($value)) {
308                 } else if (is_array($value)) {
309                         if (isset($value['r']))
310                                 $this->r = $value['r'] > 0 ? ($value['r'] > 1 ? 1 : $value['r']) : 0;
311                         if (isset($value['g']))
312                                 $this->g = $value['g'] > 0 ? ($value['g'] > 1 ? 1 : $value['g']) : 0;
313                         if (isset($value['b']))
314                                 $this->b = $value['b'] > 0 ? ($value['b'] > 1 ? 1 : $value['b']) : 0;
315                 } else if (is_string($value)) {
316                         $matches = array();
317                         if ($value == 'random') {
318                                 $this->randomize();
319                         } else if (preg_match('/([0-9A-Fa-f][0-9A-Fa-f])([0-9A-Fa-f][0-9A-Fa-f])([0-9A-Fa-f][0-9A-Fa-f])/', $value, $matches)) {
320                                 $this->r = ('0x'.$matches[1]) / 255.0;
321                                 $this->g = ('0x'.$matches[2]) / 255.0;
322                                 $this->b = ('0x'.$matches[3]) / 255.0;
323                         }
324                 } else if (is_a($value, 'CollectdColor')) {
325                         $this->r = $value->r;
326                         $this->g = $value->g;
327                         $this->b = $value->b;
328                 }
329         }
330
331         function randomize() {
332                 $this->r = rand(0, 255) / 255.0;
333                 $this->g = rand(0, 255) / 255.0;
334                 $this->b = 0.0;
335                 $min = 0.0;
336                 $max = 1.0;
337
338                 if (($this->r + $this->g) < 1.0) {
339                         $min = 1.0 - ($this->r + $this->g);
340                 } else {
341                         $max = 2.0 - ($this->r + $this->g);
342                 }
343                 $this->b = $min + ((rand(0, 255)/255.0) * ($max - $min));
344         }
345
346         function fade($bkgnd = null, $alpha = 0.25) {
347                 if (is_null($bkgnd) || !is_a($bkgnd, 'CollectdColor')) {
348                         $bg_r = 1.0;
349                         $bg_g = 1.0;
350                         $bg_b = 1.0;
351                 } else {
352                         $bg_r = $bkgnd->r;
353                         $bg_g = $bkgnd->g;
354                         $bg_b = $bkgnd->b;
355                 }
356
357                 $this->r = $alpha * $this->r + ((1.0 - $alpha) * $bg_r);
358                 $this->g = $alpha * $this->g + ((1.0 - $alpha) * $bg_g);
359                 $this->b = $alpha * $this->b + ((1.0 - $alpha) * $bg_b);
360         }
361
362         function as_array() {
363                 return array('r'=>$this->r, 'g'=>$this->g, 'b'=>$this->b);
364         }
365
366         function as_string() {
367                 $r = (int)($this->r*255);
368                 $g = (int)($this->g*255);
369                 $b = (int)($this->b*255);
370                 return sprintf('%02x%02x%02x', $r > 255 ? 255 : $r, $g > 255 ? 255 : $g, $b > 255 ? 255 : $b);
371         }
372 }
373
374
375 /**
376  * Helper function to strip quotes from RRD output
377  * @str RRD-Info generated string
378  * @return String with one surrounding pair of quotes stripped
379  */
380 function rrd_strip_quotes($str) {
381         if ($str[0] == '"' && $str[strlen($str)-1] == '"')
382                 return substr($str, 1, strlen($str)-2);
383         else
384                 return $str;
385 }
386
387 /**
388  * Determine useful information about RRD file
389  * @file Name of RRD file to analyse
390  * @return Array describing the RRD file
391  */
392 function rrd_info($file) {
393         $info = array('filename'=>$file);
394
395         $rrd = popen(RRDTOOL.' info '.escapeshellarg($file), 'r');
396         if ($rrd) {
397                 while (($s = fgets($rrd)) !== false) {
398                         $p = strpos($s, '=');
399                         if ($p === false)
400                                 continue;
401                         $key = trim(substr($s, 0, $p));
402                         $value = trim(substr($s, $p+1));
403                         if (strncmp($key,'ds[', 3) == 0) {
404                                 /* DS definition */
405                                 $p = strpos($key, ']');
406                                 $ds = substr($key, 3, $p-3);
407                                 if (!isset($info['DS']))
408                                         $info['DS'] = array();
409                                 $ds_key = substr($key, $p+2);
410
411                                 if (strpos($ds_key, '[') === false) {
412                                         if (!isset($info['DS']["$ds"]))
413                                                 $info['DS']["$ds"] = array();
414                                         $info['DS']["$ds"]["$ds_key"] = rrd_strip_quotes($value);
415                                 }
416                         } else if (strncmp($key, 'rra[', 4) == 0) {
417                                 /* RRD definition */
418                                 $p = strpos($key, ']');
419                                 $rra = substr($key, 4, $p-4);
420                                 if (!isset($info['RRA']))
421                                         $info['RRA'] = array();
422                                 $rra_key = substr($key, $p+2);
423
424                                 if (strpos($rra_key, '[') === false) {
425                                         if (!isset($info['RRA']["$rra"]))
426                                                 $info['RRA']["$rra"] = array();
427                                         $info['RRA']["$rra"]["$rra_key"] = rrd_strip_quotes($value);
428                                 }
429                         } else if (strpos($key, '[') === false) {
430                                 $info[$key] = rrd_strip_quotes($value);
431                         }
432                 }
433                 pclose($rrd);
434         }
435         return $info;
436 }
437
438 function rrd_get_color($code, $line = true) {
439         global $config;
440         $name = ($line ? 'f_' : 'h_').$code;
441         if (!isset($config['rrd_colors'][$name])) {
442                 $c_f = new CollectdColor('random');
443                 $c_h = new CollectdColor($c_f);
444                 $c_h->fade();
445                 $config['rrd_colors']['f_'.$code] = $c_f->as_string();
446                 $config['rrd_colors']['h_'.$code] = $c_h->as_string();
447         }
448         return $config['rrd_colors'][$name];
449 }
450
451 /**
452  * Draw RRD file based on it's structure
453  * @host
454  * @plugin
455  * @pinst
456  * @type
457  * @tinst
458  * @opts
459  * @return Commandline to call RRDGraph in order to generate the final graph
460  */
461 function collectd_draw_rrd($host, $plugin, $pinst = null, $type, $tinst = null, $opts = array()) {
462         global $config;
463         $timespan_def = null;
464         if (!isset($opts['timespan']))
465                 $timespan_def = reset($config['timespan']);
466         else foreach ($config['timespan'] as &$ts)
467                 if ($ts['name'] == $opts['timespan'])
468                         $timespan_def = $ts;
469
470         if (!isset($opts['rrd_opts']))
471                 $opts['rrd_opts'] = array();
472         if (isset($opts['logarithmic']) && $opts['logarithmic'])
473                 array_unshift($opts['rrd_opts'], '-o');
474
475         $rrdinfo = null;
476         $rrdfile = sprintf('%s/%s%s%s/%s%s%s', $host, $plugin, is_null($pinst) ? '' : '-', $pinst, $type, is_null($tinst) ? '' : '-', $tinst);
477         foreach ($config['datadirs'] as $datadir)
478                 if (is_file($datadir.'/'.$rrdfile.'.rrd')) {
479                         $rrdinfo = rrd_info($datadir.'/'.$rrdfile.'.rrd');
480                         if (isset($rrdinfo['RRA']) && is_array($rrdinfo['RRA']))
481                                 break;
482                         else
483                                 $rrdinfo = null;
484                 }
485
486         if (is_null($rrdinfo))
487                 return false;
488
489         $graph = array();
490         $has_avg = false;
491         $has_max = false;
492         $has_min = false;
493         reset($rrdinfo['RRA']);
494         $l_max = 0;
495         while (list($k, $v) = each($rrdinfo['RRA'])) {
496                 if ($v['cf'] == 'MAX')
497                         $has_max = true;
498                 else if ($v['cf'] == 'AVERAGE')
499                         $has_avg = true;
500                 else if ($v['cf'] == 'MIN')
501                         $has_min = true;
502         }
503         reset($rrdinfo['DS']);
504         while (list($k, $v) = each($rrdinfo['DS'])) {
505                 if (strlen($k) > $l_max)
506                         $l_max = strlen($k);
507                 if ($has_min)
508                         $graph[] = sprintf('DEF:%s_min=%s:%s:MIN', $k, $rrdinfo['filename'], $k);
509                 if ($has_avg)
510                         $graph[] = sprintf('DEF:%s_avg=%s:%s:AVERAGE', $k, $rrdinfo['filename'], $k);
511                 if ($has_max)
512                         $graph[] = sprintf('DEF:%s_max=%s:%s:MAX', $k, $rrdinfo['filename'], $k);
513         }
514         if ($has_min && $has_max || $has_min && $has_avg || $has_avg && $has_max) {
515                 $n = 1;
516                 reset($rrdinfo['DS']);
517                 while (list($k, $v) = each($rrdinfo['DS'])) {
518                         $graph[] = sprintf('LINE:%s_%s', $k, $has_min ? 'min' : 'avg');
519                         $graph[] = sprintf('CDEF:%s_var=%s_%s,%s_%s,-', $k, $k, $has_max ? 'max' : 'avg', $k, $has_min ? 'min' : 'avg');
520                         $graph[] = sprintf('AREA:%s_var#%s::STACK', $k, rrd_get_color($n++, false));
521                 }
522         }
523
524         reset($rrdinfo['DS']);
525         $n = 1;
526         while (list($k, $v) = each($rrdinfo['DS'])) {
527                 $graph[] = sprintf('LINE1:%s_avg#%s:%s ', $k, rrd_get_color($n++, true), $k.substr('                  ', 0, $l_max-strlen($k)));
528                 if (isset($opts['tinylegend']) && $opts['tinylegend'])
529                         continue;
530                 if ($has_avg)
531                         $graph[] = sprintf('GPRINT:%s_avg:AVERAGE:%%5.1lf%%s Avg%s', $k, $has_max || $has_min || $has_avg ? ',' : "\\l");
532                 if ($has_min)
533                         $graph[] = sprintf('GPRINT:%s_min:MIN:%%5.1lf%%s Max%s', $k, $has_max || $has_avg ? ',' : "\\l");
534                 if ($has_max)
535                         $graph[] = sprintf('GPRINT:%s_max:MAX:%%5.1lf%%s Max%s', $k, $has_avg ? ',' : "\\l");
536                 if ($has_avg)
537                         $graph[] = sprintf('GPRINT:%s_avg:LAST:%%5.1lf%%s Last\\l', $k);
538         }
539
540         $rrd_cmd = array(RRDTOOL, 'graph', '-', '-a', 'PNG', '-w', $config['rrd_width'], '-h', $config['rrd_height'], '-s', -1*$timespan_def['seconds'], '-t', $rrdfile);
541         $rrd_cmd = array_merge($rrd_cmd, $config['rrd_opts'], $opts['rrd_opts'], $graph);
542
543         $cmd = RRDTOOL;
544         for ($i = 1; $i < count($rrd_cmd); $i++)
545                 $cmd .= ' '.escapeshellarg($rrd_cmd[$i]);
546
547         return $cmd;
548 }
549
550 /**
551  * Draw RRD file based on it's structure
552  * @timespan
553  * @host
554  * @plugin
555  * @pinst
556  * @type
557  * @tinst
558  * @opts
559  * @return Commandline to call RRDGraph in order to generate the final graph
560  */
561 function collectd_draw_generic($timespan, $host, $plugin, $pinst = null, $type, $tinst = null) {
562         global $config, $GraphDefs;
563         $timespan_def = null;
564         foreach ($config['timespan'] as &$ts)
565                 if ($ts['name'] == $timespan)
566                         $timespan_def = $ts;
567         if (is_null($timespan_def))
568                 $timespan_def = reset($config['timespan']);
569
570         if (!isset($GraphDefs[$type]))
571                 return false;
572
573         $rrd_file = sprintf('%s/%s%s%s/%s%s%s', $host, $plugin, is_null($pinst) ? '' : '-', $pinst, $type, is_null($tinst) ? '' : '-', $tinst);
574         $rrd_cmd  = array(RRDTOOL, 'graph', '-', '-a', 'PNG', '-w', $config['rrd_width'], '-h', $config['rrd_height'], '-s', -1*$timespan_def['seconds'], '-t', $rrd_file);
575         $rrd_cmd  = array_merge($rrd_cmd, $config['rrd_opts']);
576         $rrd_args = $GraphDefs[$type];
577
578         foreach ($config['datadirs'] as $datadir) {
579                 $file = $datadir.'/'.$rrd_file.'.rrd';
580                 if (!is_file($file))
581                         continue;
582
583                 $file = str_replace(":", "\\:", $file);
584                 $rrd_args = str_replace('{file}', $file, $rrd_args);
585
586                 $rrdgraph = array_merge($rrd_cmd, $rrd_args);
587                 $cmd = RRDTOOL;
588                 for ($i = 1; $i < count($rrdgraph); $i++)
589                         $cmd .= ' '.escapeshellarg($rrdgraph[$i]);
590
591                 return $cmd;
592         }
593         return false;
594 }
595
596 /**
597  * Draw stack-graph for set of RRD files
598  * @opts Graph options like colors
599  * @sources List of array(name, file, ds)
600  * @return Commandline to call RRDGraph in order to generate the final graph
601  */
602 function collectd_draw_meta_stack(&$opts, &$sources) {
603         global $config;
604         $timespan_def = null;
605         if (!isset($opts['timespan']))
606                 $timespan_def = reset($config['timespan']);
607         else foreach ($config['timespan'] as &$ts)
608                 if ($ts['name'] == $opts['timespan'])
609                         $timespan_def = $ts;
610
611         if (!isset($opts['title']))
612                 $opts['title'] = 'Unknown title';
613         if (!isset($opts['rrd_opts']))
614                 $opts['rrd_opts'] = array();
615         if (!isset($opts['colors']))
616                 $opts['colors'] = array();
617         if (isset($opts['logarithmic']) && $opts['logarithmic'])
618                 array_unshift($opts['rrd_opts'], '-o');
619
620         $cmd = array(RRDTOOL, 'graph', '-', '-a', 'PNG', '-w', $config['rrd_width'], '-h', $config['rrd_height'], '-s', -1*$timespan_def['seconds'], '-t', $opts['title']);
621         $cmd = array_merge($cmd, $config['rrd_opts'], $opts['rrd_opts']);
622         $max_inst_name = 0;
623
624         foreach($sources as &$inst_data) {
625                 $inst_name = $inst_data['name'];
626                 $file      = $inst_data['file'];
627                 $ds        = isset($inst_data['ds']) ? $inst_data['ds'] : 'value';
628
629                 if (strlen($inst_name) > $max_inst_name)
630                         $max_inst_name = strlen($inst_name);
631
632                 if (!is_file($file))
633                         continue;
634
635                 $cmd[] = 'DEF:'.$inst_name.'_min='.$file.':'.$ds.':MIN';
636                 $cmd[] = 'DEF:'.$inst_name.'_avg='.$file.':'.$ds.':AVERAGE';
637                 $cmd[] = 'DEF:'.$inst_name.'_max='.$file.':'.$ds.':MAX';
638                 $cmd[] = 'CDEF:'.$inst_name.'_nnl='.$inst_name.'_avg,UN,0,'.$inst_name.'_avg,IF';
639         }
640         $inst_data = end($sources);
641         $inst_name = $inst_data['name'];
642         $cmd[] = 'CDEF:'.$inst_name.'_stk='.$inst_name.'_nnl';
643
644         $inst_data1 = end($sources);
645         while (($inst_data0 = prev($sources)) !== false) {
646                 $inst_name0 = $inst_data0['name'];
647                 $inst_name1 = $inst_data1['name'];
648
649                 $cmd[] = 'CDEF:'.$inst_name0.'_stk='.$inst_name0.'_nnl,'.$inst_name1.'_stk,+';
650                 $inst_data1 = $inst_data0;
651         }
652
653         foreach($sources as &$inst_data) {
654                 $inst_name = $inst_data['name'];
655                 $legend = sprintf('%s', $inst_name);
656                 while (strlen($legend) < $max_inst_name)
657                         $legend .= ' ';
658                 $number_format = isset($opts['number_format']) ? $opts['number_format'] : '%6.1lf';
659
660                 if (isset($opts['colors'][$inst_name]))
661                         $line_color = new CollectdColor($opts['colors'][$inst_name]);
662                 else
663                         $line_color = new CollectdColor('random');
664                 $area_color = new CollectdColor($line_color);
665                 $area_color->fade();
666
667                 $cmd[] = 'AREA:'.$inst_name.'_stk#'.$area_color->as_string();
668                 $cmd[] = 'LINE1:'.$inst_name.'_stk#'.$line_color->as_string().':'.$legend;
669                 if (!(isset($opts['tinylegend']) && $opts['tinylegend'])) {
670                         $cmd[] = 'GPRINT:'.$inst_name.'_min:MIN:'.$number_format.' Min,';
671                         $cmd[] = 'GPRINT:'.$inst_name.'_avg:AVERAGE:'.$number_format.' Avg,';
672                         $cmd[] = 'GPRINT:'.$inst_name.'_max:MAX:'.$number_format.' Max,';
673                         $cmd[] = 'GPRINT:'.$inst_name.'_avg:LAST:'.$number_format.' Last\\l';
674                 }
675         }
676
677         $rrdcmd = RRDTOOL;
678         for ($i = 1; $i < count($cmd); $i++)
679                 $rrdcmd .= ' '.escapeshellarg($cmd[$i]);
680         return $rrdcmd;
681 }
682
683 /**
684  * Draw stack-graph for set of RRD files
685  * @opts Graph options like colors
686  * @sources List of array(name, file, ds)
687  * @return Commandline to call RRDGraph in order to generate the final graph
688  */
689 function collectd_draw_meta_line(&$opts, &$sources) {
690         global $config;
691         $timespan_def = null;
692         if (!isset($opts['timespan']))
693                 $timespan_def = reset($config['timespan']);
694         else foreach ($config['timespan'] as &$ts)
695                 if ($ts['name'] == $opts['timespan'])
696                         $timespan_def = $ts;
697
698         if (!isset($opts['title']))
699                 $opts['title'] = 'Unknown title';
700         if (!isset($opts['rrd_opts']))
701                 $opts['rrd_opts'] = array();
702         if (!isset($opts['colors']))
703                 $opts['colors'] = array();
704         if (isset($opts['logarithmic']) && $opts['logarithmic'])
705                 array_unshift($opts['rrd_opts'], '-o');
706
707         $cmd = array(RRDTOOL, 'graph', '-', '-a', 'PNG', '-w', $config['rrd_width'], '-h', $config['rrd_height'], '-s', -1*$timespan_def['seconds'], '-t', $opts['title']);
708         $cmd = array_merge($cmd, $config['rrd_opts'], $opts['rrd_opts']);
709         $max_inst_name = 0;
710
711         foreach ($sources as &$inst_data) {
712                 $inst_name = $inst_data['name'];
713                 $file      = $inst_data['file'];
714                 $ds        = isset($inst_data['ds']) ? $inst_data['ds'] : 'value';
715
716                 if (strlen($inst_name) > $max_inst_name)
717                         $max_inst_name = strlen($inst_name);
718
719                 if (!is_file($file))
720                         continue;
721
722                 $cmd[] = 'DEF:'.$inst_name.'_min='.$file.':'.$ds.':MIN';
723                 $cmd[] = 'DEF:'.$inst_name.'_avg='.$file.':'.$ds.':AVERAGE';
724                 $cmd[] = 'DEF:'.$inst_name.'_max='.$file.':'.$ds.':MAX';
725         }
726
727         foreach ($sources as &$inst_data) {
728                 $inst_name = $inst_data['name'];
729                 $legend = sprintf('%s', $inst_name);
730                 while (strlen($legend) < $max_inst_name)
731                         $legend .= ' ';
732                 $number_format = isset($opts['number_format']) ? $opts['number_format'] : '%6.1lf';
733
734                 if (isset($opts['colors'][$inst_name]))
735                         $line_color = new CollectdColor($opts['colors'][$inst_name]);
736                 else
737                         $line_color = new CollectdColor('random');
738
739                 $cmd[] = 'LINE1:'.$inst_name.'_avg#'.$line_color->as_string().':'.$legend;
740                 if (!(isset($opts['tinylegend']) && $opts['tinylegend'])) {
741                         $cmd[] = 'GPRINT:'.$inst_name.'_min:MIN:'.$number_format.' Min,';
742                         $cmd[] = 'GPRINT:'.$inst_name.'_avg:AVERAGE:'.$number_format.' Avg,';
743                         $cmd[] = 'GPRINT:'.$inst_name.'_max:MAX:'.$number_format.' Max,';
744                         $cmd[] = 'GPRINT:'.$inst_name.'_avg:LAST:'.$number_format.' Last\\l';
745                 }
746         }
747
748         $rrdcmd = RRDTOOL;
749         for ($i = 1; $i < count($cmd); $i++)
750                 $rrdcmd .= ' '.escapeshellarg($cmd[$i]);
751         return $rrdcmd;
752 }
753
754 ?>