Revert "Refactor datastores to classes (#9179)" (#11273)

This reverts commit e5dad7a64e.
This commit is contained in:
Tony Murray
2020-03-11 07:52:52 -05:00
committed by GitHub
parent e5dad7a64e
commit 7fe895bd08
51 changed files with 1007 additions and 2523 deletions
+335 -4
View File
@@ -24,6 +24,71 @@
* @author Tony Murray <[email protected]>
*/
use LibreNMS\Config;
use LibreNMS\Exceptions\FileExistsException;
use LibreNMS\Proc;
/**
* Opens up a pipe to RRDTool using handles provided
*
* @param bool $dual_process start an additional process that's output should be read after every command
* @return bool the process(s) have been successfully started
*/
function rrdtool_initialize($dual_process = true)
{
global $rrd_sync_process, $rrd_async_process;
$command = Config::get('rrdtool') . ' -';
$descriptor_spec = 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::get('rrd_dir');
if (!rrdtool_running($rrd_sync_process)) {
$rrd_sync_process = new Proc($command, $descriptor_spec, $cwd);
}
if ($dual_process && !rrdtool_running($rrd_async_process)) {
$rrd_async_process = new Proc($command, $descriptor_spec, $cwd);
$rrd_async_process->setSynchronous(false);
}
return rrdtool_running($rrd_sync_process) && ($dual_process ? rrdtool_running($rrd_async_process) : true);
}
/**
* Checks if the variable is a running rrdtool process
*
* @param $process
* @return bool
*/
function rrdtool_running(&$process)
{
return isset($process) && $process instanceof Proc && $process->isRunning();
}
/**
* Close all open rrdtool processes.
* This should be done before exiting a script that has called rrdtool_initilize()
*/
function rrdtool_close()
{
global $rrd_sync_process, $rrd_async_process;
/** @var Proc $rrd_sync_process */
/** @var Proc $rrd_async_process */
if (rrdtool_running($rrd_sync_process)) {
$rrd_sync_process->close('quit');
}
if (rrdtool_running($rrd_async_process)) {
$rrd_async_process->close('quit');
}
}
/**
* Generates a graph file at $graph_file using $options
* Opens its own rrdtool pipe.
@@ -34,10 +99,123 @@
*/
function rrdtool_graph($graph_file, $options)
{
return Rrd::graph($graph_file, $options);
global $debug, $rrd_sync_process;
/** @var Proc $rrd_sync_process */
if (rrdtool_initialize(false)) {
$cmd = rrdtool_build_command('graph', $graph_file, $options);
$output = implode($rrd_sync_process->sendCommand($cmd));
if ($debug) {
echo "<p>$cmd</p>";
echo "<p>command returned ($output)</p>";
}
return $output;
} else {
return 0;
}
}
/**
* Generates and pipes a command to rrdtool
*
* @internal
* @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
* @return array the output of stdout and stderr in an array
* @throws FileExistsException thrown when a create command is set to rrdtool < 1.4 and the rrd already exists
* @throws Exception thrown when the rrdtool process(s) cannot be started
*/
function rrdtool($command, $filename, $options)
{
global $debug, $vdebug, $rrd_async_process, $rrd_sync_process;
/** @var Proc $rrd_sync_process */
/** @var Proc $rrd_async_process */
$start_time = microtime(true);
try {
$cmd = rrdtool_build_command($command, $filename, $options);
} catch (FileExistsException $e) {
c_echo('RRD[%g' . $filename . " already exists%n]\n", $debug);
return array(null, null);
}
c_echo("RRD[%g$cmd%n]\n", $debug);
// do not write rrd files, but allow read-only commands
$ro_commands = array('graph', 'graphv', 'dump', 'fetch', 'first', 'last', 'lastupdate', 'info', 'xport');
if (!empty(Config::get('norrd')) && !in_array($command, $ro_commands)) {
c_echo('[%rRRD Disabled%n]', !Config::get('hide_rrd_disabled'));
return array(null, null);
}
// send the command!
if ($command == 'last' && rrdtool_initialize(false)) {
// send this to our synchronous process so output is guaranteed
$output = $rrd_sync_process->sendCommand($cmd);
} elseif (rrdtool_initialize()) {
// don't care about the return of other commands, so send them to the faster async process
$output = $rrd_async_process->sendCommand($cmd);
} else {
throw new Exception('rrdtool could not start');
}
if ($vdebug) {
echo 'RRDtool Output: ';
echo $output[0];
echo $output[1];
}
recordRrdStatistic($command, $start_time);
return $output;
}
/**
* Build a command for rrdtool
* Shortens the filename as needed
* Determines if --daemon and -O should be used
*
* @internal
* @param string $command The base rrdtool command. Usually create, update, last.
* @param string $filename The full path to the rrd file
* @param string $options Options for the command possibly including the rrd definition
* @return string returns a full command ready to be piped to rrdtool
* @throws FileExistsException if rrdtool <1.4.3 and the rrd file exists locally
*/
function rrdtool_build_command($command, $filename, $options)
{
if ($command == 'create') {
// <1.4.3 doesn't support -O, so make sure the file doesn't exist
if (version_compare(Config::get('rrdtool_version', '1.4'), '1.4.3', '<')) {
if (is_file($filename)) {
throw new FileExistsException();
}
} else {
$options .= ' -O';
}
}
// no remote for create < 1.5.5 and tune < 1.5
$rrdtool_version = Config::get('rrdtool_version', '1.4');
if (Config::get('rrdcached') &&
!($command == 'create' && version_compare($rrdtool_version, '1.5.5', '<')) &&
!($command == 'tune' && Config::get('rrdcached') && version_compare($rrdtool_version, '1.5', '<'))
) {
// only relative paths if using rrdcached
$filename = str_replace([Config::get('rrd_dir') . '/', Config::get('rrd_dir')], '', $filename);
$options = str_replace([Config::get('rrd_dir') . '/', Config::get('rrd_dir')], '', $options);
return "$command $filename $options --daemon " . Config::get('rrdcached');
}
return "$command $filename $options";
}
/**
* Checks if the rrd file exists on the server
* This will perform a remote check if using rrdcached and rrdtool >= 1.5
@@ -47,9 +225,46 @@ function rrdtool_graph($graph_file, $options)
*/
function rrdtool_check_rrd_exists($filename)
{
return Rrd::checkRrdExists($filename);
if (Config::get('rrdcached') && version_compare(Config::get('rrdtool_version', '1.4'), '1.5', '>=')) {
$chk = rrdtool('last', $filename, '');
$filename = str_replace([Config::get('rrd_dir') . '/', Config::get('rrd_dir')], '', $filename);
return !str_contains(implode($chk), "$filename': No such file or directory");
} else {
return is_file($filename);
}
}
/**
* Updates an rrd database at $filename using $options
* Where $options is an array, each entry which is not a number is replaced with "U"
*
* @internal
* @param string $filename
* @param array $data
* @return array|string
*/
function rrdtool_update($filename, $data)
{
$values = array();
// Do some sanitation on the data if passed as an array.
if (is_array($data)) {
$values[] = 'N';
foreach ($data as $v) {
if (!is_numeric($v)) {
$v = 'U';
}
$values[] = $v;
}
$data = implode(':', $values);
return rrdtool('update', $filename, $data);
} else {
return 'Bad options passed to rrdtool_update';
}
} // rrdtool_update
/**
* Escapes strings for RRDtool
@@ -97,9 +312,11 @@ function rrdtool_escape($string, $length = null)
*/
function rrd_name($host, $extra, $extension = ".rrd")
{
return Rrd::name($host, $extra, $extension);
$filename = safename(is_array($extra) ? implode("-", $extra) : $extra);
return implode("/", array(get_rrd_dir($host), $filename.$extension));
} // rrd_name
/**
* Generates a path based on the hostname (or IP)
*
@@ -112,6 +329,107 @@ function get_rrd_dir($host)
return implode("/", [Config::get('rrd_dir'), $host]);
} // rrd_dir
/**
* Generates a filename for a proxmox cluster rrd
*
* @param $pmxcluster
* @param $vmid
* @param $vmport
* @return string full path to the rrd.
*/
function proxmox_rrd_name($pmxcluster, $vmid, $vmport)
{
$pmxcdir = join('/', [Config::get('rrd_dir'), 'proxmox', safename($pmxcluster)]);
// this is not needed for remote rrdcached
if (!is_dir($pmxcdir)) {
mkdir($pmxcdir, 0775, true);
}
return join('/', array($pmxcdir, safename($vmid.'_netif_'.$vmport.'.rrd')));
}
/**
* 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
*/
function rrdtool_tune($type, $filename, $max)
{
$fields = array();
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'
);
}
if (count($fields) > 0) {
$options = "--maximum " . implode(":$max --maximum ", $fields) . ":$max";
rrdtool('tune', $filename, $options);
}
return true;
} // rrdtool_tune
/**
* rrdtool backend implementation of data_update
*
* Tags:
* rrd_def RrdDefinition
* 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
*/
function rrdtool_data_update($device, $measurement, $tags, $fields)
{
$rrd_name = $tags['rrd_name'] ?: $measurement;
$step = $tags['rrd_step'] ?: Config::get('rrd.step');
$oldname = $tags['rrd_oldname'];
if (!empty($oldname)) {
rrd_file_rename($device, $oldname, $rrd_name);
}
if (isset($tags['rrd_proxmox_name'])) {
$pmxvars = $tags['rrd_proxmox_name'];
$rrd = proxmox_rrd_name($pmxvars['pmxcluster'], $pmxvars['vmid'], $pmxvars['vmport']);
} else {
$rrd = rrd_name($device['hostname'], $rrd_name);
}
if (isset($tags['rrd_def']) && !rrdtool_check_rrd_exists($rrd)) {
$newdef = "--step $step " . $tags['rrd_def'] . Config::get('rrd_rra');
rrdtool('create', $rrd, $newdef);
}
rrdtool_update($rrd, $fields);
} // rrdtool_data_update
/**
* rename an rrdfile, can only be done on the LibreNMS server hosting the rrd files
*
@@ -122,5 +440,18 @@ function get_rrd_dir($host)
*/
function rrd_file_rename($device, $oldname, $newname)
{
return Rrd::renameFile($device, $oldname, $newname);
$oldrrd = rrd_name($device['hostname'], $oldname);
$newrrd = rrd_name($device['hostname'], $newname);
if (is_file($oldrrd) && !is_file($newrrd)) {
if (rename($oldrrd, $newrrd)) {
log_event("Renamed $oldrrd to $newrrd", $device, "poller", 1);
return true;
} else {
log_event("Failed to rename $oldrrd to $newrrd", $device, "poller", 5);
return false;
}
} else {
// we don't need to rename the file
return true;
}
} // rrd_file_rename