Files

126 lines
3.9 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* rrdtool.inc.php
2011-10-01 14:54:06 +00:00
*
* Helper for processing rrdtool requests efficiently
2012-05-25 11:29:53 +00: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/>.
*
* @link http://librenms.org
2012-05-25 11:29:53 +00:00
* @copyright (C) 2006 - 2012 Adam Armstrong
* @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
2020-09-21 15:40:17 +02:00
* @return int
2011-10-01 14:54:06 +00:00
*/
2015-11-16 07:42:51 +10:00
function rrdtool_graph($graph_file, $options)
{
return Rrd::graph($graph_file, $options);
}
/**
* Checks if the rrd file exists on the server
* This will perform a remote check if using rrdcached and rrdtool >= 1.5
*
* @param string $filename full path to the rrd file
* @return bool whether or not the passed rrd file exists
*/
function rrdtool_check_rrd_exists($filename)
2015-11-16 07:42:51 +10:00
{
return Rrd::checkRrdExists($filename);
}
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
2020-09-21 15:40:17 +02:00
* @param int $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);
2020-09-21 15:40:17 +02:00
$result = str_replace("'", '', $result); // remove quotes
2016-07-15 12:21:09 -05:00
if (is_numeric($length)) {
2020-09-21 15:40:17 +02:00
// preserve original $length for str_pad()
2020-09-21 15:40:17 +02:00
// determine correct strlen() for substr_count()
$string_length = strlen($string);
$substr_count_length = $length;
if ($length > $string_length) {
2020-09-21 15:40:17 +02:00
$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
2020-09-21 15:40:17 +02:00
$result = str_replace(':', '\:', $result); // escape colons
2020-09-21 15:40:17 +02:00
return $result . ' ';
} // rrdtool_escape
2015-07-13 20:10:26 +02:00
/**
* Generates a filename based on the hostname (or IP) and some extra items
2016-08-09 09:31:26 -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
*/
2020-09-21 15:59:34 +02:00
function rrd_name($host, $extra, $extension = '.rrd')
2015-11-16 07:42:51 +10:00
{
return Rrd::name($host, $extra, $extension);
} // 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, '[]'));
2020-09-21 15:40:17 +02:00
2020-09-21 15:59:34 +02: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()
* @return bool indicating rename success or failure
*/
function rrd_file_rename($device, $oldname, $newname)
{
return Rrd::renameFile($device, $oldname, $newname);
} // rrd_file_rename