From 66dd6dbd61cdb76c0737b2ea1ce47fb0b86e2367 Mon Sep 17 00:00:00 2001 From: Adam Amstrong Date: Sat, 1 Oct 2011 14:54:06 +0000 Subject: [PATCH] use rrdtool pipe in poller. git-svn-id: http://www.observium.org/svn/observer/trunk@2631 61d68cd4-352d-0410-923a-c4978735b2b8 --- html/css/styles.css | 2 +- html/includes/functions.inc.php | 52 ------- html/includes/graphs/graph.inc.php | 14 +- includes/rrdtool.inc.php | 220 ++++++++++++++++++++++++----- poll-billing.php | 5 + poller.php | 4 +- 6 files changed, 203 insertions(+), 94 deletions(-) diff --git a/html/css/styles.css b/html/css/styles.css index 3628badf7..6bb1e4702 100644 --- a/html/css/styles.css +++ b/html/css/styles.css @@ -1281,7 +1281,7 @@ a.interface-upup:hover, a.interface-updown:hover, a.interface-admindown:hover { .tabBox .tabs { overflow: hidden; - padding: 0px 10px; + padding: 0px 8px; margin-bottom: -1px; } diff --git a/html/includes/functions.inc.php b/html/includes/functions.inc.php index ef51fb5e4..25f49eff4 100644 --- a/html/includes/functions.inc.php +++ b/html/includes/functions.inc.php @@ -1,57 +1,5 @@ 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("file", "/tmp/error-output.txt", "a") // stderr is a file to write to - ); - - $cwd = $config['rrd_dir']; - $env = array(); - - $process = proc_open($command, $descriptorspec, $pipes, $cwd, $env); - - if (is_resource($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']) - { - fwrite($pipes[0], "graph --daemon " . $config['rrdcached'] . " $graph_file $options"); - } else { - fwrite($pipes[0], "graph $graph_file $options"); - } - - fclose($pipes[0]); - fclose($pipes[1]); - - // It is important that you close any pipes before calling - // proc_close in order to avoid a deadlock - $return_value = proc_close($process); - - if ($debug) - { - echo("

"); - if ($debug) { echo("graph $graph_file $options"); } - echo("

"); - echo "command returned $return_value\n"; - echo("

"); - } - } -} - function generate_link($text, $vars, $new_vars = array()) { return ''.$text.''; diff --git a/html/includes/graphs/graph.inc.php b/html/includes/graphs/graph.inc.php index 9247f9d45..eecde75f7 100644 --- a/html/includes/graphs/graph.inc.php +++ b/html/includes/graphs/graph.inc.php @@ -25,6 +25,7 @@ include_once("../includes/common.php"); include_once("../includes/dbFacile.php"); include_once("../includes/rewrites.php"); include_once("includes/functions.inc.php"); +include_once("../includes/rrdtool.inc.php"); include_once("includes/authenticate.inc.php"); $from = mres($_GET['from']); @@ -54,8 +55,6 @@ preg_match('/^(?P[A-Za-z0-9]+)_(?P.+)/', mres($_GET['type']), $gr $type = $graphtype['type']; $subtype = $graphtype['subtype']; -if ($debug) {print_r($graphtype); } - if (is_file($config['install_dir'] . "/html/includes/graphs/$type/$subtype.inc.php")) { if (isset($config['allow_unauth_graphs_cidr']) && count($config['allow_unauth_graphs_cidr']) > 0) @@ -141,8 +140,11 @@ if ($error_msg) { } else { if ($rrd_options) { - if ($config['rrdcached']) { $rrd_switches = " --daemon ".$config['rrdcached'] . " "; } - rrdtool_graph($graphfile, " $rrd_options"); + + #rrdtool_graph($filename, $options) + + rrdtool_graph($graphfile, $rrd_options); + if ($debug) { echo("
".$rrd_cmd."
"); } if (is_file($graphfile)) { @@ -150,8 +152,10 @@ if ($error_msg) { { header('Content-type: image/png'); $fd = fopen($graphfile,'r');fpassthru($fd);fclose($fd); - unlink($graphfile); + } else { + echo(`ls -l $graphfile`); } + unlink($graphfile); } else { diff --git a/includes/rrdtool.inc.php b/includes/rrdtool.inc.php index 1a6038220..b5c8164ea 100644 --- a/includes/rrdtool.inc.php +++ b/includes/rrdtool.inc.php @@ -1,54 +1,204 @@ + * + */ -function rrdtool_create($rrdfile, $rrdupdate) +/** + * Opens up a pipe to RRDTool using handles provided + * + * @return boolean + * @global config + * @global debug + * @param &rrd_process + * @param &rrd_pipes + */ + +function rrdtool_pipe_open(&$rrd_process, &$rrd_pipes) { global $config, $debug; - $command = $config['rrdtool'] . " create $rrdfile $rrdupdate"; + $command = $config['rrdtool'] . " -"; - if ($debug) { echo($command."\n"); } +# $command = "/usr/bin/php /home/observium/dev/testpipe.php"; - return shell_exec($command); -} + $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 + ); -function rrdtool_fetch($rrdfile, $rrdupdate) -{ - return rrdtool("fetch", $rrdfile, $rrdupdate); -} + $cwd = $config['rrd_dir']; + $env = array(); -#function rrdtool_graph($rrdfile, $rrdupdate) -#{ -# return rrdtool("graph", $rrdfile, $rrdupdate); -#} + $rrd_process = proc_open($command, $descriptorspec, $rrd_pipes, $cwd, $env); -function rrdtool_last($rrdfile, $rrdupdate) -{ - return rrdtool("last", $rrdfile, $rrdupdate); -} + stream_set_blocking($rrd_pipes[1], 0); + stream_set_blocking($rrd_pipes[2], 0); -function rrdtool_lastupdate($rrdfile, $rrdupdate) -{ - return rrdtool("lastupdate", $rrdfile, $rrdupdate); -} - -function rrdtool($command, $file, $options) -{ - global $config, $debug; - - $command = $config['rrdtool'] . " $command $file $options"; - if ($config['rrdcached']) + if (is_resource($rrd_process)) { - $command .= " --daemon " . $config['rrdcached']; + // $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; + } +} + +/** + * Closes the pipe to RRDTool + * + * @return integer + * @param resource rrd_process + * @param array rrd_pipes + */ + +function rrdtool_pipe_close(&$rrd_process, &$rrd_pipes) +{ + + if($debug) + { + echo stream_get_contents($rrd_pipes[1]); + echo stream_get_contents($rrd_pipes[2]); } - if ($debug) { echo($command."\n"); } + fclose($rrd_pipes[0]); + fclose($rrd_pipes[1]); + fclose($rrd_pipes[2]); - return shell_exec($command); + // It is important that you close any pipes before calling + // proc_close in order to avoid a deadlock + + $return_value = proc_close($rrd_process); + if($debug) + { + echo $return_value; + } +} + +/** + * Generates a graph file at $graph_file using $options + * Opens its own rrdtool pipe. + * + * @return integer + * @param string graph_file + * @param string options + */ + +function rrdtool_graph($graph_file, $options) +{ + + global $config, $debug; + + 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']) + { + fwrite($rrd_pipes[0], "graph --daemon " . $config['rrdcached'] . " $graph_file $options"); + } else { + fwrite($rrd_pipes[0], "graph $graph_file $options"); + } + + rrdtool_pipe_close($rrd_process, $rrd_pipes); + + if($debug) + { + echo("

"); + if ($debug) { echo("graph $graph_file $options"); } + echo("

"); + echo "command returned $return_value\n"; + echo("

"); + } + } +} + + + +/** + * Generates and pipes a command to rrdtool + * + * @param string command + * @param string filename + * @param string options + * @global config + * @global debug + * @global rrd_pipes + */ + +function rrdtool($command, $filename, $options) +{ + global $config, $debug, $rrd_pipes; + + $cmd = "$command $filename $options"; + if ($command != "create" && $config['rrdcached']) + { + $cmd .= " --daemon " . $config['rrdcached']; + } + + fwrite($rrd_pipes[0], $cmd."\n"); + + if ($debug) + { + echo stream_get_contents($rrd_pipes[1]); + echo stream_get_contents($rrd_pipes[2]); + + echo("

"); + echo("\n".$cmd."\n"); + echo("

"); + } + +} + +/** + * Generates an rrd database at $filename using $options + * + * @param string filename + * @param string options + */ + +function rrdtool_create($filename, $options) +{ + return rrdtool("create", $filename, $options); +} + +function rrdtool_update($filename, $options) +{ + return rrdtool("update", $filename, $options); +} + +function rrdtool_fetch($filename, $options) +{ + return rrdtool("fetch", $filename, $options); +} + +function rrdtool_last($filename, $options) +{ + return rrdtool("last", $filename, $options); +} + +function rrdtool_lastupdate($filename, $options) +{ + return rrdtool("lastupdate", $filename, $options); } ?> diff --git a/poll-billing.php b/poll-billing.php index 07f33da74..8e542e3a2 100755 --- a/poll-billing.php +++ b/poll-billing.php @@ -7,6 +7,8 @@ include("includes/functions.php"); $iter = "0"; +rrdtool_pipe_open($rrd_process, $rrd_pipes); + echo("Starting Polling Session ... \n\n"); foreach (dbFetchRows("SELECT * FROM `bills`") as $bill_data) @@ -102,4 +104,7 @@ function CollectData($bill_id) if ($argv[1]) { CollectData($argv[1]); } +rrdtool_pipe_close($rrd_process, $rrd_pipes); + + ?> diff --git a/poller.php b/poller.php index 08e3e85e5..93624db85 100755 --- a/poller.php +++ b/poller.php @@ -85,6 +85,8 @@ if (isset($options['d'])) # ini_set('error_reporting', 0); } +rrdtool_pipe_open($rrd_process, $rrd_pipes); + echo("Starting polling run:\n\n"); $polled_devices = 0; if (!isset($query)) @@ -120,7 +122,7 @@ echo('MySQL: Cell['.($db_stats['fetchcell']+0).'/'.round($db_stats['fetchcell_se echo("\n"); logfile($string); - +rrdtool_pipe_close($rrd_process, $rrd_pipes); unset($config); ### Remove this for testing #print_r(get_defined_vars());