2010-02-24 13:46:12 +00:00
|
|
|
<?php
|
|
|
|
|
2015-07-13 18:10:26 +00: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
|
|
|
|
*
|
2016-07-15 17:21:09 +00:00
|
|
|
* @param $rrd_process
|
|
|
|
* @param $rrd_pipes
|
|
|
|
* @global $config
|
2011-10-01 14:54:06 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2015-11-15 21:42:51 +00:00
|
|
|
function rrdtool_pipe_open(&$rrd_process, &$rrd_pipes)
|
|
|
|
{
|
2015-08-20 14:06:44 +00:00
|
|
|
global $config;
|
2015-07-13 18:10:26 +00: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);
|
|
|
|
|
2016-07-08 19:35:00 +00:00
|
|
|
stream_set_blocking($rrd_pipes[1], false);
|
|
|
|
stream_set_blocking($rrd_pipes[2], false);
|
2011-10-01 14:54:06 +00:00
|
|
|
|
2016-07-08 19:35:00 +00:00
|
|
|
return is_resource($rrd_process);
|
2010-02-24 13:46:12 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:10:26 +00:00
|
|
|
|
2011-10-01 14:54:06 +00:00
|
|
|
/**
|
|
|
|
* Closes the pipe to RRDTool
|
|
|
|
*
|
2016-07-15 17:21:09 +00:00
|
|
|
* @param resource $rrd_process
|
|
|
|
* @param array $rrd_pipes
|
2011-10-01 14:54:06 +00:00
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
|
2012-04-05 16:44:58 +00:00
|
|
|
|
2015-11-15 21:42:51 +00:00
|
|
|
function rrdtool_pipe_close($rrd_process, &$rrd_pipes)
|
|
|
|
{
|
2015-08-20 14:06:44 +00: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 18:10:26 +00:00
|
|
|
fclose($rrd_pipes[0]);
|
|
|
|
fclose($rrd_pipes[1]);
|
|
|
|
fclose($rrd_pipes[2]);
|
2010-07-21 18:29:04 +00:00
|
|
|
|
2015-07-13 18:10:26 +00: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 18:10:26 +00:00
|
|
|
return $return_value;
|
2012-01-12 12:51:21 +00:00
|
|
|
|
2010-02-24 13:46:12 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:10:26 +00:00
|
|
|
|
2011-10-01 14:54:06 +00:00
|
|
|
/**
|
|
|
|
* Generates a graph file at $graph_file using $options
|
|
|
|
* Opens its own rrdtool pipe.
|
|
|
|
*
|
2016-07-15 17:21:09 +00:00
|
|
|
* @param string $graph_file
|
|
|
|
* @param string $options
|
2011-10-01 14:54:06 +00:00
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2015-11-15 21:42:51 +00:00
|
|
|
function rrdtool_graph($graph_file, $options)
|
|
|
|
{
|
2015-07-13 18:10:26 +00:00
|
|
|
global $config, $debug;
|
2012-01-12 12:51:21 +00:00
|
|
|
|
2015-07-13 18:10:26 +00: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']) {
|
2016-07-25 21:24:09 +00:00
|
|
|
$options = str_replace(array($config['rrd_dir'].'/', $config['rrd_dir']), '', $options);
|
2012-01-12 12:51:21 +00:00
|
|
|
|
2015-07-13 18:10:26 +00: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 18:10:26 +00:00
|
|
|
fclose($rrd_pipes[0]);
|
|
|
|
|
2016-07-07 06:33:43 +00:00
|
|
|
$line = "";
|
|
|
|
$data = "";
|
2015-07-13 18:10:26 +00:00
|
|
|
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 18:10:26 +00:00
|
|
|
|
2010-02-24 13:46:12 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:10:26 +00:00
|
|
|
|
2011-10-01 14:54:06 +00:00
|
|
|
/**
|
|
|
|
* Generates and pipes a command to rrdtool
|
|
|
|
*
|
2016-07-15 17:21:09 +00:00
|
|
|
* @param string $command create, update, updatev, graph, graphv, dump, restore, fetch, tune, first, last, lastupdate, info, resize, xport, flushcached
|
|
|
|
* @param string $filename The full patth to the rrd file
|
|
|
|
* @param string $options rrdtool command options
|
|
|
|
* @param integer $timeout seconds give up waiting for output, default 0 usually results in empty output
|
2016-07-08 19:35:00 +00:00
|
|
|
* @return array the output of stdout and stderr in an array
|
2011-10-01 14:54:06 +00:00
|
|
|
* @global config
|
|
|
|
* @global debug
|
|
|
|
* @global rrd_pipes
|
|
|
|
*/
|
2016-07-25 21:24:09 +00:00
|
|
|
function rrdtool($command, $filename, $options, $timeout=1)
|
2015-11-15 21:42:51 +00:00
|
|
|
{
|
2015-07-13 18:10:26 +00:00
|
|
|
global $config, $debug, $rrd_pipes, $console_color;
|
|
|
|
|
2015-12-01 21:53:12 +00:00
|
|
|
if ($config['rrdcached'] &&
|
2016-07-08 19:35:00 +00:00
|
|
|
(version_compare($config['rrdtool_version'], '1.5.5', '>=') || // 1.5.5+ supports all commands
|
|
|
|
(version_compare($config['rrdtool_version'], '1.5', '>=') && $command != "tune") || // 1.5+ supports all except tune
|
|
|
|
($command != "create" && $command != "tune")) // older supports all except create and tune
|
2015-12-01 21:53:12 +00:00
|
|
|
) {
|
2015-07-13 18:10:26 +00:00
|
|
|
|
2016-07-25 21:24:09 +00:00
|
|
|
// only relative paths if using rrdcached
|
|
|
|
$filename = str_replace(array($config['rrd_dir'].'/', $config['rrd_dir']), '', $filename);
|
|
|
|
|
|
|
|
// using rrdcached, append --daemon
|
2015-07-13 18:10:26 +00:00
|
|
|
$cmd = "$command $filename $options --daemon ".$config['rrdcached'];
|
2016-07-08 19:35:00 +00:00
|
|
|
} else {
|
2015-07-13 18:10:26 +00:00
|
|
|
$cmd = "$command $filename $options";
|
|
|
|
}
|
|
|
|
|
2016-07-15 17:21:09 +00:00
|
|
|
// do not write rrd files, but allow read-only commands
|
|
|
|
if ($config['norrd'] && !in_array($command, array('graph', 'graphv', 'dump', 'fetch', 'first', 'last', 'lastupdate', 'info', 'xport'))) {
|
2015-07-13 18:10:26 +00:00
|
|
|
print $console_color->convert('[%rRRD Disabled%n]');
|
2016-07-15 17:21:09 +00:00
|
|
|
$output = array(null, null);
|
2016-07-08 19:35:00 +00:00
|
|
|
} else {
|
2016-07-21 16:27:19 +00:00
|
|
|
|
|
|
|
fwrite($rrd_pipes[0], $cmd . "\n");
|
2015-07-13 18:10:26 +00:00
|
|
|
|
2016-07-25 21:24:09 +00:00
|
|
|
// this causes us to block until we receive output for up to $timeout seconds
|
|
|
|
stream_select($r = $rrd_pipes, $w = null, $x = null, $timeout);
|
2016-07-21 16:27:19 +00:00
|
|
|
$output = array(stream_get_contents($rrd_pipes[1]), stream_get_contents($rrd_pipes[2]));
|
2016-07-09 03:58:36 +00:00
|
|
|
}
|
2016-07-08 19:35:00 +00:00
|
|
|
|
2015-07-13 18:10:26 +00:00
|
|
|
if ($debug) {
|
2016-07-15 17:21:09 +00:00
|
|
|
print $console_color->convert('RRD[%g'.$cmd."%n] \n");
|
2016-07-25 21:24:09 +00:00
|
|
|
echo 'rrdtool output: ';
|
|
|
|
var_dump($output);
|
2015-07-13 18:10:26 +00:00
|
|
|
}
|
2011-10-01 14:54:06 +00:00
|
|
|
|
2016-07-08 19:35:00 +00:00
|
|
|
return $output;
|
2010-02-24 13:46:12 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:10:26 +00:00
|
|
|
|
2011-10-01 14:54:06 +00:00
|
|
|
/**
|
|
|
|
* Generates an rrd database at $filename using $options
|
|
|
|
*
|
2016-07-07 06:33:43 +00:00
|
|
|
* @internal
|
2016-07-15 17:21:09 +00:00
|
|
|
* @param string $filename
|
|
|
|
* @param string $options
|
2016-07-07 06:33:43 +00:00
|
|
|
* @return array|bool
|
2011-10-01 14:54:06 +00:00
|
|
|
*/
|
2015-11-15 21:42:51 +00:00
|
|
|
function rrdtool_create($filename, $options)
|
2016-07-07 06:33:43 +00:00
|
|
|
{
|
|
|
|
if (rrdtool_check_rrd_exists($filename)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return rrdtool('create', $filename, str_replace(array("\r", "\n"), '', $options));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the rrd file exists on the server
|
|
|
|
* This will perform a remote check if using rrdcached and rrdtool >= 1.5
|
|
|
|
*
|
|
|
|
* @param $filename
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function rrdtool_check_rrd_exists($filename)
|
2015-11-15 21:42:51 +00:00
|
|
|
{
|
2015-10-03 16:14:23 +00:00
|
|
|
global $config;
|
2016-07-07 06:33:43 +00:00
|
|
|
if ($config['rrdcached'] && version_compare($config['rrdtool_version'], '1.5', '>=')) {
|
2016-07-09 03:58:36 +00:00
|
|
|
$chk = rrdtool('last', $filename, '', 5); // wait up to 5 seconds
|
2016-07-18 18:30:39 +00:00
|
|
|
return strpos(implode($chk), "$filename': No such file or directory") === false;
|
2016-07-07 06:33:43 +00:00
|
|
|
} else {
|
|
|
|
return is_file($filename);
|
2015-10-02 02:00:28 +00:00
|
|
|
}
|
2010-02-24 13:46:12 +00: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"
|
|
|
|
*
|
2016-07-07 06:33:43 +00:00
|
|
|
* @internal
|
|
|
|
* @param string $filename
|
|
|
|
* @param array $options
|
|
|
|
* @return array|string
|
2012-05-25 11:29:53 +00:00
|
|
|
*/
|
2015-11-15 08:18:13 +00:00
|
|
|
function rrdtool_update($filename, $options)
|
|
|
|
{
|
2015-07-13 18:10:26 +00:00
|
|
|
$values = array();
|
|
|
|
// Do some sanitisation on the data if passed as an array.
|
2015-08-18 16:26:55 +00:00
|
|
|
|
2015-07-13 18:10:26 +00: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 18:10:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-18 16:26:55 +00:00
|
|
|
$values[] = $v;
|
2015-07-13 18:10:26 +00: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-11-15 08:18:13 +00:00
|
|
|
} // rrdtool_update
|
2010-07-04 21:09:22 +00:00
|
|
|
|
2015-07-13 18:10:26 +00:00
|
|
|
|
2015-11-15 08:18:13 +00:00
|
|
|
function rrdtool_fetch($filename, $options)
|
|
|
|
{
|
2015-07-13 18:10:26 +00:00
|
|
|
return rrdtool('fetch', $filename, $options);
|
2015-11-15 08:18:13 +00:00
|
|
|
} // rrdtool_fetch
|
2015-07-13 18:10:26 +00:00
|
|
|
|
2010-07-04 21:09:22 +00:00
|
|
|
|
2015-11-15 08:18:13 +00:00
|
|
|
function rrdtool_last($filename, $options)
|
|
|
|
{
|
2015-07-13 18:10:26 +00:00
|
|
|
return rrdtool('last', $filename, $options);
|
2015-11-15 08:18:13 +00:00
|
|
|
} // rrdtool_last
|
2011-03-31 10:51:02 +00:00
|
|
|
|
2015-07-13 18:10:26 +00:00
|
|
|
|
2015-11-15 08:18:13 +00:00
|
|
|
function rrdtool_lastupdate($filename, $options)
|
|
|
|
{
|
2015-07-13 18:10:26 +00:00
|
|
|
return rrdtool('lastupdate', $filename, $options);
|
2015-11-15 08:18:13 +00:00
|
|
|
} // rrdtool_lastupdate
|
2010-02-24 13:46:12 +00:00
|
|
|
|
2015-07-13 18:10:26 +00:00
|
|
|
|
2012-05-23 10:37:23 +00:00
|
|
|
/**
|
2016-07-15 17:21:09 +00:00
|
|
|
* Escapes strings for RRDtool
|
|
|
|
*
|
|
|
|
* @param string $string the string to escape
|
|
|
|
* @param integer $length if passed, string will be padded and trimmed to exactly this length (after rrdtool unescapes it)
|
2012-05-23 10:37:23 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-07-15 17:21:09 +00:00
|
|
|
function rrdtool_escape($string, $length=null){
|
2015-07-13 18:10:26 +00:00
|
|
|
$result = shorten_interface_type($string);
|
2015-11-15 01:48:39 +00:00
|
|
|
$result = str_replace("'", '', $result); # remove quotes
|
|
|
|
$result = str_replace('%', '%%', $result); # double percent signs
|
2016-07-15 17:21:09 +00:00
|
|
|
if (is_numeric($length)) {
|
|
|
|
$extra = substr_count($string, ':', 0, $length);
|
|
|
|
$result = substr(str_pad($result, $length), 0, ($length + $extra));
|
2015-07-13 18:10:26 +00:00
|
|
|
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-11-15 01:48:39 +00:00
|
|
|
$result = str_replace(':', '\:', $result); # escape colons
|
2015-07-13 18:10:26 +00:00
|
|
|
return $result.' ';
|
2015-11-15 08:01:09 +00:00
|
|
|
} // rrdtool_escape
|
2015-07-13 18:10:26 +00:00
|
|
|
|
2015-11-15 08:01:09 +00:00
|
|
|
|
2016-07-07 06:33:43 +00:00
|
|
|
/**
|
|
|
|
* Generates a filename based on the hostname (or IP) and some extra items
|
|
|
|
*
|
|
|
|
* @param string $host Host name
|
|
|
|
* @param array|string $extra Components of RRD filename - will be separated with "-", or a pre-formed rrdname
|
|
|
|
* @param string $extension File extension (default is .rrd)
|
|
|
|
* @return string the name of the rrd file for $host's $extra component
|
2015-11-15 08:01:09 +00:00
|
|
|
*/
|
2016-07-07 06:33:43 +00:00
|
|
|
function rrd_name($host, $extra, $extension = ".rrd")
|
2015-11-15 21:42:51 +00:00
|
|
|
{
|
2015-11-15 08:01:09 +00:00
|
|
|
global $config;
|
|
|
|
$filename = safename(is_array($extra) ? implode("-", $extra) : $extra);
|
2016-07-07 06:33:43 +00:00
|
|
|
return implode("/", array($config['rrd_dir'], $host, $filename.$extension));
|
2015-11-15 08:18:13 +00:00
|
|
|
} // rrd_name
|
2015-11-17 10:20:23 +00:00
|
|
|
|
2016-07-15 17:21:09 +00:00
|
|
|
/**
|
|
|
|
* Modify an rrd file's max value and trim the peaks as defined by rrdtool
|
|
|
|
*
|
|
|
|
* @param string $type only 'port' is supported at this time
|
|
|
|
* @param string $filename the path to the rrd file
|
|
|
|
* @param integer $max the new max value
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-11-17 10:20:23 +00:00
|
|
|
function rrdtool_tune($type, $filename, $max) {
|
2015-12-08 15:17:53 +00:00
|
|
|
$fields = array();
|
2015-11-17 10:20:23 +00:00
|
|
|
if ($type === 'port') {
|
|
|
|
if ($max < 10000000) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$max = $max / 8;
|
|
|
|
$fields = array(
|
|
|
|
'INOCTETS','OUTOCTETS','INERRORS','OUTERRORS','INUCASTPKTS','OUTUCASTPKTS','INNUCASTPKTS','OUTNUCASTPKTS','INDISCARDS','OUTDISCARDS','INUNKNOWNPROTOS','INBROADCASTPKTS','OUTBROADCASTPKTS','INMULTICASTPKTS','OUTMULTICASTPKTS'
|
2015-12-08 13:53:38 +00:00
|
|
|
);
|
2015-11-17 10:20:23 +00:00
|
|
|
}
|
2015-12-08 15:17:53 +00:00
|
|
|
if (count($fields) > 0) {
|
|
|
|
$options = "--maximum " . implode(":$max --maximum ", $fields). ":$max";
|
|
|
|
rrdtool('tune', $filename, $options);
|
|
|
|
}
|
2015-11-15 08:01:09 +00:00
|
|
|
} // rrdtool_tune
|
|
|
|
|
2016-01-10 09:31:39 +00:00
|
|
|
|
2016-07-07 06:33:43 +00:00
|
|
|
/**
|
2016-01-10 09:31:39 +00:00
|
|
|
* rrdtool backend implementation of data_update
|
2016-07-07 06:33:43 +00:00
|
|
|
*
|
2016-07-15 17:21:09 +00:00
|
|
|
* Tags:
|
|
|
|
* rrd_def array|string: (required) an array of rrd field definitions example: "DS:dataName:COUNTER:600:U:100000000000"
|
|
|
|
* rrd_name array|string: the rrd filename, will be processed with rrd_name()
|
|
|
|
* rrd_oldname array|string: old rrd filename to rename, will be processed with rrd_name()
|
|
|
|
* rrd_step int: rrd step, defaults to 300
|
|
|
|
*
|
|
|
|
* @param array $device device array
|
|
|
|
* @param string $measurement the name of this measurement (if no rrd_name tag is given, this will be used to name the file)
|
|
|
|
* @param array $tags tags to pass additional info to rrdtool
|
|
|
|
* @param array $fields data values to update
|
2015-11-15 08:01:09 +00:00
|
|
|
*/
|
2016-01-10 09:31:39 +00:00
|
|
|
function rrdtool_data_update($device, $measurement, $tags, $fields)
|
2015-11-15 21:42:51 +00:00
|
|
|
{
|
2015-11-15 08:01:09 +00:00
|
|
|
global $config;
|
|
|
|
|
2016-07-07 06:33:43 +00:00
|
|
|
$rrd_name = $tags['rrd_name'] ?: $measurement;
|
|
|
|
$step = $tags['rrd_step'] ?: 300;
|
2016-01-12 10:59:02 +00:00
|
|
|
$oldname = $tags['rrd_oldname'];
|
|
|
|
if (isset($oldname) && !empty($oldname)) {
|
|
|
|
rrd_file_rename($device, $oldname, $rrd_name);
|
|
|
|
}
|
2016-01-10 09:31:39 +00:00
|
|
|
|
|
|
|
$rrd = rrd_name($device['hostname'], $rrd_name);
|
2016-07-07 06:33:43 +00:00
|
|
|
if (!rrdtool_check_rrd_exists($rrd) && $tags['rrd_def']) {
|
2016-01-12 11:26:42 +00:00
|
|
|
$rrd_def = is_array($tags['rrd_def']) ? $tags['rrd_def'] : array($tags['rrd_def']);
|
|
|
|
// add the --step and the rra definitions to the command
|
|
|
|
$newdef = "--step $step ".implode(' ', $rrd_def).$config['rrd_rra'];
|
2015-11-15 08:01:09 +00:00
|
|
|
rrdtool_create($rrd, $newdef);
|
|
|
|
}
|
|
|
|
|
2016-01-10 09:31:39 +00:00
|
|
|
rrdtool_update($rrd, $fields);
|
|
|
|
} // rrdtool_data_update
|
2015-11-15 08:01:09 +00:00
|
|
|
|
|
|
|
|
2016-07-15 17:21:09 +00:00
|
|
|
/**
|
|
|
|
* rename an rrdfile, can only be done on the LibreNMS server hosting the rrd files
|
|
|
|
*
|
|
|
|
* @param array $device Device object
|
|
|
|
* @param string $oldname RRD name array as used with rrd_name()
|
|
|
|
* @param string $newname RRD name array as used with rrd_name()
|
2015-11-15 08:01:09 +00:00
|
|
|
* @return bool indicating rename success or failure
|
|
|
|
*/
|
|
|
|
function rrd_file_rename($device, $oldname, $newname)
|
|
|
|
{
|
|
|
|
$oldrrd = rrd_name($device['hostname'], $oldname);
|
|
|
|
$newrrd = rrd_name($device['hostname'], $newname);
|
2016-01-10 09:31:39 +00:00
|
|
|
if (is_file($oldrrd) && !is_file($newrrd)) {
|
2016-01-15 11:28:56 +00:00
|
|
|
if (rename($oldrrd, $newrrd)) {
|
2016-01-15 12:03:23 +00:00
|
|
|
log_event("Renamed $oldrrd to $newrrd", $device, "poller");
|
2016-01-15 11:28:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
2016-01-15 12:03:23 +00:00
|
|
|
log_event("Failed to rename $oldrrd to $newrrd", $device, "poller");
|
2016-01-15 11:28:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-10 09:31:39 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-01-15 11:28:56 +00:00
|
|
|
// we don't need to rename the file
|
2016-01-10 09:31:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-11-15 08:01:09 +00:00
|
|
|
} // rrd_file_rename
|