Skip to content

Commit

Permalink
RRDtool dual process (remote creation/check) (#4104)
Browse files Browse the repository at this point in the history
* Prepare for dual rrdtool processes
Disabled at this time
Split out rrdtool version checks into rrdtool_create_command()
Tests for rrdtool_create_command()
Fixes a few small issues.

* Enable dual process and remote rrd check/creation

* remove full path for remote commands

* Doc updates
minor fix to rrdtool_tune()

* Set up bootstrap for phpunit
Fix issues with FileExistsException

* Attempt to fix phpunit

* Fix classloader and bootstrap to use full paths only (not depending on $config)

* Fix phpunit tests, config.php does not exist.
  • Loading branch information
murrant authored and laf committed Aug 22, 2016
1 parent 27c1c72 commit e80a385
Show file tree
Hide file tree
Showing 11 changed files with 399 additions and 125 deletions.
76 changes: 68 additions & 8 deletions LibreNMS/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,22 @@
class ClassLoader
{
/**
* @var array stores dynamically added class > file mappings
* @var array stores dynamically added class > file mappings ($classMap[fullclass] = $file)
*/
private $classMap;

/**
* @var array stores dynamically added namespace > directory mappings ($dirMap[namespace][$dir] =1)
*/
private $dirMap;

/**
* ClassLoader constructor.
*/
public function __construct()
{
$this->classMap = array();
$this->dirMap = array();
}

/**
Expand All @@ -52,9 +58,10 @@ public function __construct()
*/
public static function psrLoad($name)
{
global $config, $vdebug;
global $vdebug;

$file = str_replace(array('\\', '_'), DIRECTORY_SEPARATOR, $name) . '.php';
$fullFile = $config['install_dir'] ? $config['install_dir'] . '/' . $file : $file;
$fullFile = realpath(__DIR__ . '/..') . DIRECTORY_SEPARATOR . $file;

if($vdebug) {
echo __CLASS__ . " [[ $name > $fullFile ]]\n";
Expand All @@ -73,7 +80,8 @@ public static function psrLoad($name)
public function customLoad($name)
{
global $vdebug;
if (array_key_exists($name, $this->classMap)) {

if (isset($this->classMap[$name])) {
$file = $this->classMap[$name];

if($vdebug) {
Expand All @@ -82,31 +90,72 @@ public function customLoad($name)

if (is_readable($file)) {
include $file;
return;
}
}

list($namespace, $class) = $this->splitNamespace($name);
if (isset($this->dirMap[$namespace])) {
foreach (array_keys($this->dirMap[$namespace]) as $dir) {
$file = $dir . DIRECTORY_SEPARATOR . $class . '.php';

if($vdebug) {
echo __CLASS__ . " (( $name > $file ))\n";
}

if (is_readable($file)) {
include $file;
return;
}
}
}
}

/**
* Add or set a custom class > file mapping
* Register a custom class > file mapping
*
* @param string $class The full class name
* @param string $file The path to the file containing the class, full path is preferred
*/
public function mapClass($class, $file)
public function registerClass($class, $file)
{
$this->classMap[$class] = $file;
}

/**
* Remove a class from the list of class > file mappings
* Unregister a class from the list of class > file mappings
*
* @param string $class The full class name
*/
public function unMapClass($class)
public function unregisterClass($class)
{
unset($this->classMap[$class]);
}

/**
* Register a directory to search for classes in.
* If a namespace is specified, it will search for
* classes with that exact namespace in those directories.
*
* @param string $dir directory containing classes with filename = class.php
* @param string $namespace the namespace of the classes
*/
public function registerDir($dir, $namespace = '')
{
$this->dirMap[$namespace][$dir] = 1;
}

/**
* Unregister a directory
*
* @param string $dir the directory to remove
* @param string $namespace the namespace of the classes
*/
public function unregisterDir($dir, $namespace = '')
{
unset($this->dirMap[$namespace][$dir]);
}

/**
* Register this autoloader
* Custom mappings will take precedence over PSR-0
Expand All @@ -125,4 +174,15 @@ public function unregister()
spl_autoload_unregister(array($this, 'customLoad'));
spl_autoload_unregister(__NAMESPACE__.'\ClassLoader::psrLoad');
}

/**
* Split a class into namspace/classname
* @param string $class the full class name to split
* @return array of the split class [namespace, classname]
*/
private function splitNamespace($class) {
$parts = explode('\\', $class);
$last = array_pop($parts);
return array(implode('\\', $parts), $last);
}
}
4 changes: 2 additions & 2 deletions check-services.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@
$influxdb = false;
}

rrdtool_pipe_open($rrd_process, $rrd_pipes);
rrdtool_initialize();

foreach (dbFetchRows('SELECT * FROM `devices` AS D, `services` AS S WHERE S.device_id = D.device_id ORDER by D.device_id DESC') as $service) {
// Run the polling function
poll_service($service);

} //end foreach
rrdtool_pipe_close($rrd_process, $rrd_pipes);
rrdtool_terminate();
4 changes: 2 additions & 2 deletions html/graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
// initialize the class loader and add custom mappings
require_once $config['install_dir'] . '/LibreNMS/ClassLoader.php';
$classLoader = new LibreNMS\ClassLoader();
$classLoader->mapClass('Console_Color2', $config['install_dir'] . '/includes/console_colour.php');
$classLoader->mapClass('PasswordHash', $config['install_dir'] . '/html/lib/PasswordHash.php');
$classLoader->registerClass('Console_Color2', $config['install_dir'] . '/includes/console_colour.php');
$classLoader->registerClass('PasswordHash', $config['install_dir'] . '/html/lib/PasswordHash.php');
$classLoader->register();

require_once '../includes/common.php';
Expand Down
10 changes: 5 additions & 5 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
// initialize the class loader and add custom mappings
require_once $config['install_dir'] . '/LibreNMS/ClassLoader.php';
$classLoader = new LibreNMS\ClassLoader();
$classLoader->mapClass('Console_Color2', $config['install_dir'] . '/includes/console_colour.php');
$classLoader->mapClass('Console_Table', $config['install_dir'] . '/includes/console_table.php');
$classLoader->mapClass('PHPMailer', $config['install_dir'] . "/includes/phpmailer/class.phpmailer.php");
$classLoader->mapClass('SMTP', $config['install_dir'] . "/includes/phpmailer/class.smtp.php");
$classLoader->mapClass('PasswordHash', $config['install_dir'] . '/html/lib/PasswordHash.php');
$classLoader->registerClass('Console_Color2', $config['install_dir'] . '/includes/console_colour.php');
$classLoader->registerClass('Console_Table', $config['install_dir'] . '/includes/console_table.php');
$classLoader->registerClass('PHPMailer', $config['install_dir'] . "/includes/phpmailer/class.phpmailer.php");
$classLoader->registerClass('SMTP', $config['install_dir'] . "/includes/phpmailer/class.smtp.php");
$classLoader->registerClass('PasswordHash', $config['install_dir'] . '/html/lib/PasswordHash.php');
$classLoader->register();

// Include from PEAR
Expand Down
Loading

0 comments on commit e80a385

Please sign in to comment.