mirror of
https://github.com/stylersnico/librenms.git
synced 2026-07-26 00:18:03 +02:00
Fix coding style part 2
This commit is contained in:
+196
-165
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
/*
|
||||
* Observium
|
||||
*
|
||||
* This file is part of Observium.
|
||||
@@ -9,7 +9,6 @@
|
||||
* @subpackage rrdtool
|
||||
* @author Adam Armstrong <adama@memetic.org>
|
||||
* @copyright (C) 2006 - 2012 Adam Armstrong
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -18,170 +17,190 @@
|
||||
* @return boolean
|
||||
* @global config
|
||||
* @global debug
|
||||
* @param &rrd_process
|
||||
* @param &rrd_pipes
|
||||
* @param &rrd_process
|
||||
* @param &rrd_pipes
|
||||
*/
|
||||
|
||||
function rrdtool_pipe_open(&$rrd_process, &$rrd_pipes)
|
||||
{
|
||||
global $config, $debug;
|
||||
|
||||
$command = $config['rrdtool'] . " -";
|
||||
function rrdtool_pipe_open(&$rrd_process, &$rrd_pipes) {
|
||||
global $config, $debug;
|
||||
|
||||
$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
|
||||
);
|
||||
$command = $config['rrdtool'].' -';
|
||||
|
||||
$cwd = $config['rrd_dir'];
|
||||
$env = array();
|
||||
$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
|
||||
);
|
||||
|
||||
$rrd_process = proc_open($command, $descriptorspec, $rrd_pipes, $cwd, $env);
|
||||
$cwd = $config['rrd_dir'];
|
||||
$env = array();
|
||||
|
||||
stream_set_blocking($rrd_pipes[1], 0);
|
||||
stream_set_blocking($rrd_pipes[2], 0);
|
||||
$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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Closes the pipe to RRDTool
|
||||
*
|
||||
* @return integer
|
||||
* @param resource rrd_process
|
||||
* @param array rrd_pipes
|
||||
* @param resource rrd_process
|
||||
* @param array rrd_pipes
|
||||
*/
|
||||
|
||||
function rrdtool_pipe_close($rrd_process, &$rrd_pipes)
|
||||
{
|
||||
global $debug;
|
||||
|
||||
if ($debug)
|
||||
{
|
||||
echo stream_get_contents($rrd_pipes[1]);
|
||||
echo stream_get_contents($rrd_pipes[2]);
|
||||
}
|
||||
function rrdtool_pipe_close($rrd_process, &$rrd_pipes) {
|
||||
global $debug;
|
||||
|
||||
fclose($rrd_pipes[0]);
|
||||
fclose($rrd_pipes[1]);
|
||||
fclose($rrd_pipes[2]);
|
||||
if ($debug) {
|
||||
echo stream_get_contents($rrd_pipes[1]);
|
||||
echo stream_get_contents($rrd_pipes[2]);
|
||||
}
|
||||
|
||||
// It is important that you close any pipes before calling
|
||||
// proc_close in order to avoid a deadlock
|
||||
fclose($rrd_pipes[0]);
|
||||
fclose($rrd_pipes[1]);
|
||||
fclose($rrd_pipes[2]);
|
||||
|
||||
$return_value = proc_close($rrd_process);
|
||||
// It is important that you close any pipes before calling
|
||||
// proc_close in order to avoid a deadlock
|
||||
$return_value = proc_close($rrd_process);
|
||||
|
||||
return $return_value;
|
||||
return $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
|
||||
* @param string graph_file
|
||||
* @param string options
|
||||
*/
|
||||
|
||||
function rrdtool_graph($graph_file, $options)
|
||||
{
|
||||
global $config, $debug;
|
||||
|
||||
rrdtool_pipe_open($rrd_process, $rrd_pipes);
|
||||
function rrdtool_graph($graph_file, $options) {
|
||||
global $config, $debug;
|
||||
|
||||
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
|
||||
rrdtool_pipe_open($rrd_process, $rrd_pipes);
|
||||
|
||||
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);
|
||||
}
|
||||
fwrite($rrd_pipes[0], "graph --daemon " . $config['rrdcached'] . " $graph_file $options");
|
||||
} else {
|
||||
fwrite($rrd_pipes[0], "graph $graph_file $options");
|
||||
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);
|
||||
}
|
||||
|
||||
fwrite($rrd_pipes[0], 'graph --daemon '.$config['rrdcached']." $graph_file $options");
|
||||
}
|
||||
else {
|
||||
fwrite($rrd_pipes[0], "graph $graph_file $options");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates and pipes a command to rrdtool
|
||||
*
|
||||
* @param string command
|
||||
* @param string filename
|
||||
* @param string options
|
||||
* @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, $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";
|
||||
}
|
||||
function rrdtool($command, $filename, $options) {
|
||||
global $config, $debug, $rrd_pipes, $console_color;
|
||||
|
||||
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]);
|
||||
}
|
||||
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]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates an rrd database at $filename using $options
|
||||
*
|
||||
@@ -189,21 +208,26 @@ function rrdtool($command, $filename, $options)
|
||||
* @param string options
|
||||
*/
|
||||
|
||||
function rrdtool_create($filename, $options)
|
||||
{
|
||||
global $config, $debug, $console_color;
|
||||
|
||||
if ($config['norrd'])
|
||||
{
|
||||
print $console_color->convert("[%gRRD Disabled%n] ", false);
|
||||
} else {
|
||||
$command = $config['rrdtool'] . " create $filename $options";
|
||||
}
|
||||
if ($debug) { print $console_color->convert("RRD[%g".$command."%n] "); }
|
||||
function rrdtool_create($filename, $options) {
|
||||
global $config, $debug, $console_color;
|
||||
|
||||
if ($config['norrd']) {
|
||||
print $console_color->convert('[%gRRD Disabled%n] ', false);
|
||||
}
|
||||
else {
|
||||
$command = $config['rrdtool']." create $filename $options";
|
||||
}
|
||||
|
||||
if ($debug) {
|
||||
print $console_color->convert('RRD[%g'.$command.'%n] ');
|
||||
}
|
||||
|
||||
return shell_exec($command);
|
||||
|
||||
return shell_exec($command);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Updates an rrd database at $filename using $options
|
||||
* Where $options is an array, each entry which is not a number is replaced with "U"
|
||||
@@ -212,39 +236,46 @@ function rrdtool_create($filename, $options)
|
||||
* @param array options
|
||||
*/
|
||||
|
||||
function rrdtool_update($filename, $options)
|
||||
{
|
||||
$values = array();
|
||||
// Do some sanitisation on the data if passed as an array.
|
||||
if (is_array($options))
|
||||
{
|
||||
$values[] = "N";
|
||||
foreach ($options as $value)
|
||||
{
|
||||
if (!is_numeric($value)) { $value = U; }
|
||||
$values[] = $value;
|
||||
|
||||
function rrdtool_update($filename, $options) {
|
||||
$values = array();
|
||||
// Do some sanitisation on the data if passed as an array.
|
||||
if (is_array($options)) {
|
||||
$values[] = 'N';
|
||||
foreach ($options as $value) {
|
||||
if (!is_numeric($value)) {
|
||||
$value = U;
|
||||
}
|
||||
|
||||
$values[] = $value;
|
||||
}
|
||||
|
||||
$options = implode(':', $values);
|
||||
}
|
||||
$options = implode(':', $values);
|
||||
}
|
||||
|
||||
return rrdtool("update", $filename, $options);
|
||||
return rrdtool('update', $filename, $options);
|
||||
|
||||
}
|
||||
|
||||
function rrdtool_fetch($filename, $options)
|
||||
{
|
||||
return rrdtool("fetch", $filename, $options);
|
||||
|
||||
function rrdtool_fetch($filename, $options) {
|
||||
return rrdtool('fetch', $filename, $options);
|
||||
|
||||
}
|
||||
|
||||
function rrdtool_last($filename, $options)
|
||||
{
|
||||
return rrdtool("last", $filename, $options);
|
||||
|
||||
function rrdtool_last($filename, $options) {
|
||||
return rrdtool('last', $filename, $options);
|
||||
|
||||
}
|
||||
|
||||
function rrdtool_lastupdate($filename, $options)
|
||||
{
|
||||
return rrdtool("lastupdate", $filename, $options);
|
||||
|
||||
function rrdtool_lastupdate($filename, $options){
|
||||
return rrdtool('lastupdate', $filename, $options);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Escapes strings for RRDtool,
|
||||
*
|
||||
@@ -253,20 +284,20 @@ function rrdtool_lastupdate($filename, $options)
|
||||
* @param string string to escape
|
||||
* @param integer if passed, string will be padded and trimmed to exactly this length (after rrdtool unescapes it)
|
||||
*/
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
$result = str_replace(':','\:',$result);
|
||||
return $result." ";
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
$result = str_replace(':', '\:', $result);
|
||||
return $result.' ';
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user