From 7b55f3a2bd2ce11d6ecb4290e0decda2768c3d21 Mon Sep 17 00:00:00 2001 From: Marius Hein Date: Tue, 11 Feb 2014 13:40:29 +0100 Subject: [PATCH] Implement register and run php scripts for modules refs #5597 --- .../Icinga/Application/Modules/Manager.php | 9 +++ library/Icinga/Application/Modules/Module.php | 59 ++++++++++++++----- modules/monitoring/register.php | 52 +++------------- modules/monitoring/run.php | 10 ++++ 4 files changed, 71 insertions(+), 59 deletions(-) create mode 100644 modules/monitoring/run.php diff --git a/library/Icinga/Application/Modules/Manager.php b/library/Icinga/Application/Modules/Manager.php index 80a4deaed..356e3458e 100644 --- a/library/Icinga/Application/Modules/Manager.php +++ b/library/Icinga/Application/Modules/Manager.php @@ -244,18 +244,22 @@ class Manager ) ); } + clearstatcache(true); $target = $this->installedBaseDirs[$name]; $link = $this->enableDir . '/' . $name; + if (!is_writable($this->enableDir)) { throw new SystemPermissionException( 'Can not enable module "' . $name . '". ' . 'Insufficient system permissions for enabling modules.' ); } + if (file_exists($link) && is_link($link)) { return $this; } + if (!@symlink($target, $link)) { $error = error_get_last(); if (strstr($error["message"], "File exists") === false) { @@ -266,7 +270,12 @@ class Manager ); } } + $this->enabledDirs[$name] = $link; + + $this->loadModule($name); + $this->getModule($name)->launchRegisterScript(); + return $this; } diff --git a/library/Icinga/Application/Modules/Module.php b/library/Icinga/Application/Modules/Module.php index d1c435d52..c71439d68 100644 --- a/library/Icinga/Application/Modules/Module.php +++ b/library/Icinga/Application/Modules/Module.php @@ -101,6 +101,13 @@ class Module */ private $runScript; + /** + * Module initialization script + * + * @var string + */ + private $registerScript; + /** * Module configuration script * @@ -145,17 +152,18 @@ class Module */ public function __construct(ApplicationBootstrap $app, $name, $basedir) { - $this->app = $app; - $this->name = $name; - $this->basedir = $basedir; - $this->cssdir = $basedir. '/public/css'; - $this->libdir = $basedir. '/library'; - $this->configdir = $basedir. '/config'; - $this->localedir = $basedir. '/application/locale'; - $this->formdir = $basedir. '/application/forms'; - $this->controllerdir = $basedir. '/application/controllers'; - $this->runScript = $basedir. '/register.php'; - $this->configScript = $basedir. '/configuration.php'; + $this->app = $app; + $this->name = $name; + $this->basedir = $basedir; + $this->cssdir = $basedir . '/public/css'; + $this->libdir = $basedir . '/library'; + $this->configdir = $basedir . '/config'; + $this->localedir = $basedir . '/application/locale'; + $this->formdir = $basedir . '/application/forms'; + $this->controllerdir = $basedir . '/application/controllers'; + $this->runScript = $basedir . '/run.php'; + $this->registerScript = $basedir . '/register.php'; + $this->configScript = $basedir . '/configuration.php'; } /** @@ -503,6 +511,16 @@ class Module return $this; } + /** + * Run module registration script + * + * @return self + */ + public function launchRegisterScript() + { + return $this->includeScript($this->registerScript); + } + /** * Run module bootstrap script * @@ -510,13 +528,26 @@ class Module */ protected function launchRunScript() { - if (file_exists($this->runScript) - && is_readable($this->runScript)) { - include($this->runScript); + return $this->includeScript($this->runScript); + } + + /** + * Include a php script if it is readable + * + * @param string $file File to include + * + * @return self + */ + protected function includeScript($file) + { + if (is_readable($file) === true) { + include($file); } + return $this; } + /** * Run module config script */ diff --git a/modules/monitoring/register.php b/modules/monitoring/register.php index 29022fe9a..91e062d8b 100644 --- a/modules/monitoring/register.php +++ b/modules/monitoring/register.php @@ -1,48 +1,10 @@ isCli()) { - return; -} - -$request = Icinga::app()->getFrontController()->getRequest(); - -if (AuthManager::getInstance()->isAuthenticated()) { - $hostSummary = StatusSummaryView::fromRequest( - $request, - array( - 'hosts_up', - 'hosts_unreachable_handled', - 'hosts_unreachable_unhandled', - 'hosts_down_handled', - 'hosts_down_unhandled', - 'hosts_pending' - ) - )->getQuery()->fetchRow(); - - $serviceSummary = StatusSummaryView::fromRequest( - $request, - array( - 'services_ok', - 'services_critical_handled', - 'services_critical_unhandled', - 'services_warning_handled', - 'services_warning_unhandled', - 'services_unknown_handled', - 'services_unknown_unhandled', - 'services_pending' - ) - )->getQuery()->fetchRow(); - - Topbar::addPartial( - 'topbar.phtml', - 'monitoring', - array('hostSummary' => $hostSummary, 'serviceSummary' => $serviceSummary) - ); -} - -?> +Logger::debug('Monitor/register'); \ No newline at end of file diff --git a/modules/monitoring/run.php b/modules/monitoring/run.php new file mode 100644 index 000000000..575954fe2 --- /dev/null +++ b/modules/monitoring/run.php @@ -0,0 +1,10 @@ +