2010-02-24 13:46:12 +00:00
|
|
|
<?php
|
2016-08-22 10:32:05 -05:00
|
|
|
/**
|
|
|
|
* rrdtool.inc.php
|
2011-10-01 14:54:06 +00:00
|
|
|
*
|
2016-08-26 01:50:29 -05:00
|
|
|
* Helper for processing rrdtool requests efficiently
|
2012-05-25 11:29:53 +00:00
|
|
|
*
|
2016-08-22 10:32:05 -05:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @package LibreNMS
|
|
|
|
* @link http://librenms.org
|
2012-05-25 11:29:53 +00:00
|
|
|
* @copyright (C) 2006 - 2012 Adam Armstrong
|
2016-08-22 10:32:05 -05:00
|
|
|
* @copyright 2016 Tony Murray
|
|
|
|
* @author Tony Murray <murraytony@gmail.com>
|
2011-10-01 14:54:06 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-19 22:08:42 -05:00
|
|
|
use LibreNMS\Config;
|
|
|
|
|
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 12:21:09 -05:00
|
|
|
* @param string $graph_file
|
|
|
|
* @param string $options
|
2011-10-01 14:54:06 +00:00
|
|
|
* @return integer
|
|
|
|
*/
|
2015-11-16 07:42:51 +10:00
|
|
|
function rrdtool_graph($graph_file, $options)
|
|
|
|
{
|
2020-03-16 09:17:58 -05:00
|
|
|
return Rrd::graph($graph_file, $options);
|
2010-02-24 13:46:12 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 20:10:26 +02:00
|
|
|
|
2016-07-07 01:33:43 -05:00
|
|
|
/**
|
|
|
|
* Checks if the rrd file exists on the server
|
2016-08-22 10:32:05 -05:00
|
|
|
* This will perform a remote check if using rrdcached and rrdtool >= 1.5
|
2016-07-07 01:33:43 -05:00
|
|
|
*
|
2016-08-22 10:32:05 -05:00
|
|
|
* @param string $filename full path to the rrd file
|
|
|
|
* @return bool whether or not the passed rrd file exists
|
2016-07-07 01:33:43 -05:00
|
|
|
*/
|
|
|
|
function rrdtool_check_rrd_exists($filename)
|
2015-11-16 07:42:51 +10:00
|
|
|
{
|
2020-03-16 09:17:58 -05:00
|
|
|
return Rrd::checkRrdExists($filename);
|
2010-02-24 13:46:12 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 20:10:26 +02:00
|
|
|
|
2012-05-23 10:37:23 +00:00
|
|
|
/**
|
2016-07-15 12:21:09 -05:00
|
|
|
* Escapes strings for RRDtool
|
2016-08-09 09:31:26 -05:00
|
|
|
*
|
2016-07-15 12:21:09 -05:00
|
|
|
* @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-08-09 09:31:26 -05:00
|
|
|
function rrdtool_escape($string, $length = null)
|
|
|
|
{
|
2015-07-13 20:10:26 +02:00
|
|
|
$result = shorten_interface_type($string);
|
2015-11-15 11:48:39 +10:00
|
|
|
$result = str_replace("'", '', $result); # remove quotes
|
2020-01-07 14:06:55 -05:00
|
|
|
|
2016-07-15 12:21:09 -05:00
|
|
|
if (is_numeric($length)) {
|
2020-01-07 14:06:55 -05:00
|
|
|
# preserve original $length for str_pad()
|
|
|
|
|
|
|
|
# determine correct strlen() for substr_count()
|
|
|
|
$string_length=strlen($string);
|
|
|
|
$substr_count_length=$length;
|
|
|
|
|
|
|
|
if ($length > $string_length) {
|
|
|
|
$substr_count_length=$string_length; # If $length is greater than the haystack length, then substr_count() will produce a warning; fix warnings.
|
|
|
|
}
|
|
|
|
|
|
|
|
$extra = substr_count($string, ':', 0, $substr_count_length);
|
2016-07-15 12:21:09 -05:00
|
|
|
$result = substr(str_pad($result, $length), 0, ($length + $extra));
|
2015-07-13 20:10:26 +02: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 11:48:39 +10:00
|
|
|
$result = str_replace(':', '\:', $result); # escape colons
|
2020-01-07 14:06:55 -05:00
|
|
|
|
2015-07-13 20:10:26 +02:00
|
|
|
return $result.' ';
|
2015-11-15 18:01:09 +10:00
|
|
|
} // rrdtool_escape
|
2015-07-13 20:10:26 +02:00
|
|
|
|
2015-11-15 18:01:09 +10:00
|
|
|
|
2016-07-07 01:33:43 -05:00
|
|
|
/**
|
|
|
|
* Generates a filename based on the hostname (or IP) and some extra items
|
2016-08-09 09:31:26 -05:00
|
|
|
*
|
2016-07-07 01:33:43 -05:00
|
|
|
* @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 18:01:09 +10:00
|
|
|
*/
|
2016-07-07 01:33:43 -05:00
|
|
|
function rrd_name($host, $extra, $extension = ".rrd")
|
2015-11-16 07:42:51 +10:00
|
|
|
{
|
2020-03-16 09:17:58 -05:00
|
|
|
return Rrd::name($host, $extra, $extension);
|
2015-11-15 18:18:13 +10:00
|
|
|
} // rrd_name
|
2015-11-17 02:20:23 -08:00
|
|
|
|
2017-09-02 20:45:31 +02:00
|
|
|
/**
|
|
|
|
* Generates a path based on the hostname (or IP)
|
|
|
|
*
|
|
|
|
* @param string $host Host name
|
|
|
|
* @return string the name of the rrd directory for $host
|
|
|
|
*/
|
|
|
|
function get_rrd_dir($host)
|
|
|
|
{
|
|
|
|
$host = str_replace(':', '_', trim($host, '[]'));
|
2019-06-23 00:29:12 -05:00
|
|
|
return implode("/", [Config::get('rrd_dir'), $host]);
|
2017-09-02 20:45:31 +02:00
|
|
|
} // rrd_dir
|
|
|
|
|
2016-07-15 12:21:09 -05:00
|
|
|
/**
|
|
|
|
* rename an rrdfile, can only be done on the LibreNMS server hosting the rrd files
|
|
|
|
*
|
|
|
|
* @param array $device Device object
|
2018-02-05 07:39:13 -06:00
|
|
|
* @param string|array $oldname RRD name array as used with rrd_name()
|
|
|
|
* @param string|array $newname RRD name array as used with rrd_name()
|
2015-11-15 18:01:09 +10:00
|
|
|
* @return bool indicating rename success or failure
|
|
|
|
*/
|
|
|
|
function rrd_file_rename($device, $oldname, $newname)
|
|
|
|
{
|
2020-03-16 09:17:58 -05:00
|
|
|
return Rrd::renameFile($device, $oldname, $newname);
|
2015-11-15 18:01:09 +10:00
|
|
|
} // rrd_file_rename
|