Skip to content

Commit

Permalink
validate.php proper exit code (librenms#16274)
Browse files Browse the repository at this point in the history
some fixes around database & poller checks
  • Loading branch information
murrant authored Aug 9, 2024
1 parent b62a4ba commit f296464
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 13 deletions.
7 changes: 5 additions & 2 deletions LibreNMS/Validations/Poller/CheckActivePoller.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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
Expand All @@ -40,7 +41,9 @@ public function validate(): ValidationResult
$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) {
Expand All @@ -55,6 +58,6 @@ public function validate(): ValidationResult
*/
public function enabled(): bool
{
return Device::exists();
return Eloquent::isConnected() && Device::exists();
}
}
3 changes: 2 additions & 1 deletion LibreNMS/Validations/Poller/CheckDispatcherService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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
Expand All @@ -49,7 +50,7 @@ public function validate(): ValidationResult
*/
public function enabled(): bool
{
return Device::exists();
return Eloquent::isConnected() && Device::exists();
}

private function checkDispatchService(): ValidationResult
Expand Down
3 changes: 2 additions & 1 deletion LibreNMS/Validations/Poller/CheckPythonWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use App\Models\Poller;
use App\Models\PollerCluster;
use LibreNMS\Config;
use LibreNMS\DB\Eloquent;
use LibreNMS\ValidationResult;

class CheckPythonWrapper implements \LibreNMS\Interfaces\Validation
Expand Down Expand Up @@ -70,7 +71,7 @@ public function validate(): ValidationResult
*/
public function enabled(): bool
{
return true;
return Eloquent::isConnected();
}

private function checkPythonWrapper(): ValidationResult
Expand Down
14 changes: 14 additions & 0 deletions LibreNMS/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ public function getGroupStatus(string $validation_group): int
}, 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.
*
Expand Down
2 changes: 1 addition & 1 deletion includes/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function module_selected($module, $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...
Expand Down
2 changes: 1 addition & 1 deletion lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@
],
'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',
],
Expand Down
2 changes: 1 addition & 1 deletion lang/pt-BR/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@
],
'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',
],
Expand Down
2 changes: 1 addition & 1 deletion lang/zh-CN/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@
],
'poller' => [
'CheckActivePoller' => [
'fail' => '未检测到活动的轮询方法',
'fail' => '轮询器未运行。 过去 :interval 秒内没有运行任何轮询器。',
'both_fail' => '调度程序服务和Python包装器最近都处于活动状态,这可能导致双重轮询',
'ok' => '找到活动的轮询器',
],
Expand Down
17 changes: 12 additions & 5 deletions validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
Example: ./validate.php -g mail.
";
exit;
exit(1);
}

if (function_exists('posix_getuid') && posix_getuid() === 0) {
Expand All @@ -62,7 +62,7 @@
// 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';
Expand Down Expand Up @@ -114,17 +114,17 @@
}

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()) {
Expand All @@ -144,9 +144,16 @@
$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();
Expand Down

0 comments on commit f296464

Please sign in to comment.