| 
									
										
										
										
											2010-02-24 13:46:12 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2012-05-25 11:29:53 +00:00
										 |  |  |  * Observium | 
					
						
							| 
									
										
										
										
											2011-10-01 14:54:06 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2012-05-25 11:29:53 +00:00
										 |  |  |  *   This file is part of Observium. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package    observium | 
					
						
							|  |  |  |  * @subpackage rrdtool | 
					
						
							|  |  |  |  * @author     Adam Armstrong <adama@memetic.org> | 
					
						
							|  |  |  |  * @copyright  (C) 2006 - 2012 Adam Armstrong | 
					
						
							| 
									
										
										
										
											2011-10-01 14:54:06 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Opens up a pipe to RRDTool using handles provided | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @return boolean | 
					
						
							|  |  |  |  * @global config | 
					
						
							|  |  |  |  * @global debug | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |  * @param  &rrd_process | 
					
						
							|  |  |  |  * @param  &rrd_pipes | 
					
						
							| 
									
										
										
										
											2011-10-01 14:54:06 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | function rrdtool_pipe_open(&$rrd_process, &$rrd_pipes) { | 
					
						
							| 
									
										
										
										
											2015-08-20 16:06:44 +02:00
										 |  |  |     global $config; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $command = $config['rrdtool'].' -'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $descriptorspec = array( | 
					
						
							|  |  |  |         0 => array( | 
					
						
							|  |  |  |             'pipe', | 
					
						
							|  |  |  |             'r', | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |         // stdin is a pipe that the child will read from
 | 
					
						
							|  |  |  |         1 => array( | 
					
						
							|  |  |  |             'pipe', | 
					
						
							|  |  |  |             'w', | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |         // stdout is a pipe that the child will write to
 | 
					
						
							|  |  |  |         2 => array( | 
					
						
							|  |  |  |             'pipe', | 
					
						
							|  |  |  |             'w', | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |         // stderr is a pipe that the child will write to
 | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $cwd = $config['rrd_dir']; | 
					
						
							|  |  |  |     $env = array(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $rrd_process = proc_open($command, $descriptorspec, $rrd_pipes, $cwd, $env); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     stream_set_blocking($rrd_pipes[1], 0); | 
					
						
							|  |  |  |     stream_set_blocking($rrd_pipes[2], 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (is_resource($rrd_process)) { | 
					
						
							|  |  |  |         // $pipes now looks like this:
 | 
					
						
							|  |  |  |         // 0 => writeable handle connected to child stdin
 | 
					
						
							|  |  |  |         // 1 => readable handle connected to child stdout
 | 
					
						
							|  |  |  |         // Any error output will be appended to /tmp/error-output.txt
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-01 14:54:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-24 13:46:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-01 14:54:06 +00:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Closes the pipe to RRDTool | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @return integer | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |  * @param  resource rrd_process | 
					
						
							|  |  |  |  * @param  array rrd_pipes | 
					
						
							| 
									
										
										
										
											2011-10-01 14:54:06 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-05 16:44:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | function rrdtool_pipe_close($rrd_process, &$rrd_pipes) { | 
					
						
							| 
									
										
										
										
											2015-08-20 16:06:44 +02:00
										 |  |  |     d_echo(stream_get_contents($rrd_pipes[1])); | 
					
						
							|  |  |  |     d_echo(stream_get_contents($rrd_pipes[2])); | 
					
						
							| 
									
										
										
										
											2010-07-21 18:29:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     fclose($rrd_pipes[0]); | 
					
						
							|  |  |  |     fclose($rrd_pipes[1]); | 
					
						
							|  |  |  |     fclose($rrd_pipes[2]); | 
					
						
							| 
									
										
										
										
											2010-07-21 18:29:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     // It is important that you close any pipes before calling
 | 
					
						
							|  |  |  |     // proc_close in order to avoid a deadlock
 | 
					
						
							|  |  |  |     $return_value = proc_close($rrd_process); | 
					
						
							| 
									
										
										
										
											2012-01-12 12:51:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     return $return_value; | 
					
						
							| 
									
										
										
										
											2012-01-12 12:51:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-24 13:46:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-01 14:54:06 +00:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Generates a graph file at $graph_file using $options | 
					
						
							|  |  |  |  * Opens its own rrdtool pipe. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @return integer | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |  * @param  string graph_file | 
					
						
							|  |  |  |  * @param  string options | 
					
						
							| 
									
										
										
										
											2011-10-01 14:54:06 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | function rrdtool_graph($graph_file, $options) { | 
					
						
							|  |  |  |     global $config, $debug; | 
					
						
							| 
									
										
										
										
											2012-01-12 12:51:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     rrdtool_pipe_open($rrd_process, $rrd_pipes); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (is_resource($rrd_process)) { | 
					
						
							|  |  |  |         // $pipes now looks like this:
 | 
					
						
							|  |  |  |         // 0 => writeable handle connected to child stdin
 | 
					
						
							|  |  |  |         // 1 => readable handle connected to child stdout
 | 
					
						
							|  |  |  |         // Any error output will be appended to /tmp/error-output.txt
 | 
					
						
							|  |  |  |         if ($config['rrdcached']) { | 
					
						
							|  |  |  |             if (isset($config['rrdcached_dir']) && $config['rrdcached_dir'] !== false) { | 
					
						
							|  |  |  |                 $options = str_replace($config['rrd_dir'].'/', './'.$config['rrdcached_dir'].'/', $options); | 
					
						
							|  |  |  |                 $options = str_replace($config['rrd_dir'], './'.$config['rrdcached_dir'].'/', $options); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2012-01-12 12:51:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |             fwrite($rrd_pipes[0], 'graph --daemon '.$config['rrdcached']." $graph_file $options"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             fwrite($rrd_pipes[0], "graph $graph_file $options"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2011-10-01 14:54:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         fclose($rrd_pipes[0]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         while (strlen($line) < 1) { | 
					
						
							|  |  |  |             $line  = fgets($rrd_pipes[1], 1024); | 
					
						
							|  |  |  |             $data .= $line; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $return_value = rrdtool_pipe_close($rrd_process, $rrd_pipes); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($debug) { | 
					
						
							|  |  |  |             echo '<p>'; | 
					
						
							|  |  |  |             if ($debug) { | 
					
						
							|  |  |  |                 echo "graph $graph_file $options"; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             echo '</p><p>'; | 
					
						
							|  |  |  |             echo "command returned $return_value ($data)\n"; | 
					
						
							|  |  |  |             echo '</p>'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $data; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2011-10-01 14:54:06 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-24 13:46:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-01 14:54:06 +00:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Generates and pipes a command to rrdtool | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |  * @param  string command | 
					
						
							|  |  |  |  * @param  string filename | 
					
						
							|  |  |  |  * @param  string options | 
					
						
							| 
									
										
										
										
											2011-10-01 14:54:06 +00:00
										 |  |  |  * @global config | 
					
						
							|  |  |  |  * @global debug | 
					
						
							|  |  |  |  * @global rrd_pipes | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | function rrdtool($command, $filename, $options) { | 
					
						
							|  |  |  |     global $config, $debug, $rrd_pipes, $console_color; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ($command != 'create' && $config['rrdcached']) { | 
					
						
							|  |  |  |         if (isset($config['rrdcached_dir']) && $config['rrdcached_dir'] !== false) { | 
					
						
							|  |  |  |             $filename = str_replace($config['rrd_dir'].'/', './'.$config['rrdcached_dir'].'/', $filename); | 
					
						
							|  |  |  |             $filename = str_replace($config['rrd_dir'], './'.$config['rrdcached_dir'].'/', $filename); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $cmd = "$command $filename $options --daemon ".$config['rrdcached']; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         $cmd = "$command $filename $options"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ($config['norrd']) { | 
					
						
							|  |  |  |         print $console_color->convert('[%rRRD Disabled%n]'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         fwrite($rrd_pipes[0], $cmd."\n"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ($debug) { | 
					
						
							|  |  |  |         echo stream_get_contents($rrd_pipes[1]); | 
					
						
							|  |  |  |         echo stream_get_contents($rrd_pipes[2]); | 
					
						
							|  |  |  |         print $console_color->convert('RRD[%g'.$cmd.'%n] '); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         $tmp = stream_get_contents($rrd_pipes[1]).stream_get_contents($rrd_pipes[2]); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-10-01 14:54:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-24 13:46:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-01 14:54:06 +00:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Generates an rrd database at $filename using $options | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param string filename | 
					
						
							|  |  |  |  * @param string options | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-01 19:29:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | function rrdtool_create($filename, $options) { | 
					
						
							| 
									
										
										
										
											2015-08-20 16:06:44 +02:00
										 |  |  |     global $config, $console_color; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if ($config['norrd']) { | 
					
						
							|  |  |  |         print $console_color->convert('[%gRRD Disabled%n] ', false); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         $command = $config['rrdtool']." create $filename $options"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-20 16:06:44 +02:00
										 |  |  |     d_echo($console_color->convert('RRD[%g'.$command.'%n] ')); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return shell_exec($command); | 
					
						
							| 
									
										
										
										
											2011-10-01 19:29:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-24 13:46:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-25 11:29:53 +00:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Updates an rrd database at $filename using $options | 
					
						
							|  |  |  |  * Where $options is an array, each entry which is not a number is replaced with "U" | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param string filename | 
					
						
							|  |  |  |  * @param array options | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | function rrdtool_update($filename, $options) { | 
					
						
							|  |  |  |     $values = array(); | 
					
						
							|  |  |  |     // Do some sanitisation on the data if passed as an array.
 | 
					
						
							| 
									
										
										
										
											2015-08-18 16:26:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     if (is_array($options)) { | 
					
						
							|  |  |  |         $values[] = 'N'; | 
					
						
							| 
									
										
										
										
											2015-08-18 16:26:55 +00:00
										 |  |  |         foreach ($options as $k => $v) { | 
					
						
							|  |  |  |             if (!is_numeric($v)) { | 
					
						
							|  |  |  |                 $v = U; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-18 16:26:55 +00:00
										 |  |  |             $values[] = $v; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $options = implode(':', $values); | 
					
						
							| 
									
										
										
										
											2015-08-18 16:26:55 +00:00
										 |  |  |         return rrdtool('update', $filename, $options); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         return 'Bad options passed to rrdtool_update'; | 
					
						
							| 
									
										
										
										
											2012-05-09 11:33:16 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-01 14:54:06 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-07-04 21:09:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | function rrdtool_fetch($filename, $options) { | 
					
						
							|  |  |  |     return rrdtool('fetch', $filename, $options); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-01 14:54:06 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-07-04 21:09:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | function rrdtool_last($filename, $options) { | 
					
						
							|  |  |  |     return rrdtool('last', $filename, $options); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-01 14:54:06 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-03-31 10:51:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | function rrdtool_lastupdate($filename, $options){ | 
					
						
							|  |  |  |     return rrdtool('lastupdate', $filename, $options); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-24 13:46:12 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-23 10:37:23 +00:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Escapes strings for RRDtool, | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @return string | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param string string to escape | 
					
						
							|  |  |  |  * @param integer if passed, string will be padded and trimmed to exactly this length (after rrdtool unescapes it) | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function rrdtool_escape($string, $maxlength=null){ | 
					
						
							|  |  |  |     $result = shorten_interface_type($string); | 
					
						
							|  |  |  |     $result = str_replace('%', '%%', $result); | 
					
						
							|  |  |  |     if (is_numeric($maxlength)) { | 
					
						
							|  |  |  |         $extra  = substr_count($string, ':', 0, $maxlength); | 
					
						
							|  |  |  |         $result = substr(str_pad($result, $maxlength), 0, ($maxlength + $extra)); | 
					
						
							|  |  |  |         if ($extra > 0) { | 
					
						
							|  |  |  |             $result = substr($result, 0, (-1 * $extra)); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-05-11 12:14:56 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2012-05-23 10:18:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $result = str_replace(':', '\:', $result); | 
					
						
							|  |  |  |     return $result.' '; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |