From f296464daeab47a31245339dfe93a631c263addd Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Thu, 8 Aug 2024 20:00:56 -0500 Subject: [PATCH] validate.php proper exit code (#16274) some fixes around database & poller checks --- .../Validations/Poller/CheckActivePoller.php | 7 +++++-- .../Poller/CheckDispatcherService.php | 3 ++- .../Validations/Poller/CheckPythonWrapper.php | 3 ++- LibreNMS/Validator.php | 14 ++++++++++++++ includes/init.php | 2 +- lang/en/validation.php | 2 +- lang/pt-BR/validation.php | 2 +- lang/zh-CN/validation.php | 2 +- validate.php | 17 ++++++++++++----- 9 files changed, 39 insertions(+), 13 deletions(-) diff --git a/LibreNMS/Validations/Poller/CheckActivePoller.php b/LibreNMS/Validations/Poller/CheckActivePoller.php index 5d2b4e7336..6076ad2ea3 100644 --- a/LibreNMS/Validations/Poller/CheckActivePoller.php +++ b/LibreNMS/Validations/Poller/CheckActivePoller.php @@ -28,6 +28,7 @@ namespace LibreNMS\Validations\Poller; use App\Models\Device; use App\Models\Poller; use App\Models\PollerCluster; +use LibreNMS\DB\Eloquent; use LibreNMS\ValidationResult; class CheckActivePoller implements \LibreNMS\Interfaces\Validation @@ -40,7 +41,9 @@ class CheckActivePoller implements \LibreNMS\Interfaces\Validation $dispatcher_exists = PollerCluster::isActive()->exists(); $wrapper_exists = Poller::isActive()->exists(); if (! $dispatcher_exists && ! $wrapper_exists) { - return ValidationResult::fail(trans('validation.validations.poller.CheckActivePoller.fail')); + $interval = (int) \LibreNMS\Config::get('rrd.step'); + + return ValidationResult::fail(trans('validation.validations.poller.CheckActivePoller.fail', ['interval' => $interval])); } if ($dispatcher_exists && $wrapper_exists) { @@ -55,6 +58,6 @@ class CheckActivePoller implements \LibreNMS\Interfaces\Validation */ public function enabled(): bool { - return Device::exists(); + return Eloquent::isConnected() && Device::exists(); } } diff --git a/LibreNMS/Validations/Poller/CheckDispatcherService.php b/LibreNMS/Validations/Poller/CheckDispatcherService.php index 407c1904af..a3fe181076 100644 --- a/LibreNMS/Validations/Poller/CheckDispatcherService.php +++ b/LibreNMS/Validations/Poller/CheckDispatcherService.php @@ -28,6 +28,7 @@ namespace LibreNMS\Validations\Poller; use App\Models\Device; use App\Models\Poller; use App\Models\PollerCluster; +use LibreNMS\DB\Eloquent; use LibreNMS\ValidationResult; class CheckDispatcherService implements \LibreNMS\Interfaces\Validation @@ -49,7 +50,7 @@ class CheckDispatcherService implements \LibreNMS\Interfaces\Validation */ public function enabled(): bool { - return Device::exists(); + return Eloquent::isConnected() && Device::exists(); } private function checkDispatchService(): ValidationResult diff --git a/LibreNMS/Validations/Poller/CheckPythonWrapper.php b/LibreNMS/Validations/Poller/CheckPythonWrapper.php index c4fd02be70..8aab8f003d 100644 --- a/LibreNMS/Validations/Poller/CheckPythonWrapper.php +++ b/LibreNMS/Validations/Poller/CheckPythonWrapper.php @@ -28,6 +28,7 @@ namespace LibreNMS\Validations\Poller; use App\Models\Poller; use App\Models\PollerCluster; use LibreNMS\Config; +use LibreNMS\DB\Eloquent; use LibreNMS\ValidationResult; class CheckPythonWrapper implements \LibreNMS\Interfaces\Validation @@ -70,7 +71,7 @@ class CheckPythonWrapper implements \LibreNMS\Interfaces\Validation */ public function enabled(): bool { - return true; + return Eloquent::isConnected(); } private function checkPythonWrapper(): ValidationResult diff --git a/LibreNMS/Validator.php b/LibreNMS/Validator.php index 83ea7c0571..ec14b1bf70 100644 --- a/LibreNMS/Validator.php +++ b/LibreNMS/Validator.php @@ -117,6 +117,20 @@ class Validator }, ValidationResult::SUCCESS); } + /** + * Get overall status + */ + public function getStatus(): int + { + return array_reduce($this->results, function ($compound, array $results) { + foreach ($results as $result) { + $compound = min($compound, $result->getStatus()); + } + + return $compound; + }, ValidationResult::SUCCESS); + } + /** * Get the ValidationResults for a specific validation group. * diff --git a/includes/init.php b/includes/init.php index c0ec618226..f0805afddc 100644 --- a/includes/init.php +++ b/includes/init.php @@ -100,7 +100,7 @@ if (! module_selected('nodb', $init_modules)) { echo "Check the install docs for more info: https://docs.librenms.org/Installation/\n"; } - exit; + exit(1); } } \LibreNMS\DB\Eloquent::setStrictMode(false); // disable strict mode for legacy code... diff --git a/lang/en/validation.php b/lang/en/validation.php index 47a21c0e35..3bad18cab8 100644 --- a/lang/en/validation.php +++ b/lang/en/validation.php @@ -267,7 +267,7 @@ return [ ], 'poller' => [ 'CheckActivePoller' => [ - 'fail' => 'No active polling method detected', + 'fail' => 'Poller is not running. No poller has run within the last :interval seconds.', 'both_fail' => 'Both Dispatcher Service and Python Wrapper were active recently, this could cause double polling', 'ok' => 'Active pollers found', ], diff --git a/lang/pt-BR/validation.php b/lang/pt-BR/validation.php index 601d5ac55a..aa9fa3b8c8 100644 --- a/lang/pt-BR/validation.php +++ b/lang/pt-BR/validation.php @@ -268,7 +268,7 @@ return [ ], 'poller' => [ 'CheckActivePoller' => [ - 'fail' => 'Nenhum método de polling ativo detectado', + 'fail' => 'O Poller não está em execução. Nenhum poller foi executado nos últimos :interval segundos', 'both_fail' => 'Tanto o Dispatcher Service quanto o Python Wrapper estiveram ativos recentemente, isso pode causar duplo polling', 'ok' => 'Pollers ativos encontrados', ], diff --git a/lang/zh-CN/validation.php b/lang/zh-CN/validation.php index 5a3e7adb2d..474cb339ca 100644 --- a/lang/zh-CN/validation.php +++ b/lang/zh-CN/validation.php @@ -268,7 +268,7 @@ return [ ], 'poller' => [ 'CheckActivePoller' => [ - 'fail' => '未检测到活动的轮询方法', + 'fail' => '轮询器未运行。 过去 :interval 秒内没有运行任何轮询器。', 'both_fail' => '调度程序服务和Python包装器最近都处于活动状态,这可能导致双重轮询', 'ok' => '找到活动的轮询器', ], diff --git a/validate.php b/validate.php index 1a176c4b34..0b0930d2b7 100755 --- a/validate.php +++ b/validate.php @@ -51,7 +51,7 @@ if (isset($options['h'])) { Example: ./validate.php -g mail. "; - exit; + exit(1); } if (function_exists('posix_getuid') && posix_getuid() === 0) { @@ -62,7 +62,7 @@ if (function_exists('posix_getuid') && posix_getuid() === 0) { // Check autoload if (! file_exists('vendor/autoload.php')) { print_fail('Composer has not been run, dependencies are missing', './scripts/composer_wrapper.php install --no-dev'); - exit; + exit(1); } require_once 'vendor/autoload.php'; @@ -114,17 +114,17 @@ if ($validator->getGroupStatus('dependencies') == ValidationResult::FAILURE) { } if ($pre_checks_failed) { - exit; + exit(1); } -$init_modules = []; +$init_modules = ['nodb']; require 'includes/init.php'; // make sure install_dir is set correctly, or the next includes will fail if (! file_exists(Config::get('install_dir') . '/.env')) { $suggested = realpath(__DIR__); print_fail('\'install_dir\' config setting is not set correctly.', "It should probably be set to: $suggested"); - exit; + exit(1); } if (\LibreNMS\DB\Eloquent::isConnected()) { @@ -144,9 +144,16 @@ if (isset($options['g'])) { $modules = []; // all modules } +// the code below may not show the database check above, always print it +if (! in_array('database', $modules)) { + $validator->printResults('database'); +} + // run checks $validator->validate($modules, isset($options['s']) || ! empty($modules)); +exit($validator->getStatus() ? 0 : 1); + function print_header() { $output = ob_get_clean();