Distributed Poller improved validation (#12269)

This commit is contained in:
Tony Murray
2020-11-23 00:33:56 -06:00
committed by GitHub
parent 7550ef1d8c
commit 7ed34c889e
6 changed files with 164 additions and 20 deletions

View File

@@ -0,0 +1,32 @@
<?php
/*
* InvalidNameException.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2020 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Exceptions;
class InvalidNameException extends \Exception
{
}

View File

@@ -29,6 +29,7 @@ class ValidationResult
const FAILURE = 0; const FAILURE = 0;
const WARNING = 1; const WARNING = 1;
const SUCCESS = 2; const SUCCESS = 2;
const INFO = 3;
private $message; private $message;
private $status; private $status;
@@ -71,6 +72,16 @@ class ValidationResult
return new self($message, self::WARNING, $fix); return new self($message, self::WARNING, $fix);
} }
/**
* Create a new informational Validation result
* @param string $message The message to describe this result
* @return ValidationResult
*/
public static function info($message)
{
return new self($message, self::INFO);
}
/** /**
* Create a new failure Validation result * Create a new failure Validation result
* @param string $message The message to describe this result * @param string $message The message to describe this result
@@ -173,15 +184,14 @@ class ValidationResult
*/ */
public static function getStatusText($status) public static function getStatusText($status)
{ {
if ($status === self::SUCCESS) { $table = [
return '%gOK%n'; self::SUCCESS => '%gOK%n',
} elseif ($status === self::WARNING) { self::WARNING => '%YWARN%n',
return '%YWARN%n'; self::FAILURE => '%RFAIL%n',
} elseif ($status === self::FAILURE) { self::INFO => '%CINFO%n',
return '%RFAIL%n'; ];
}
return 'Unknown'; return $table[$status] ?? 'Unknown';
} }
public function getListDescription() public function getListDescription()

View File

@@ -31,12 +31,18 @@
namespace LibreNMS\Validations; namespace LibreNMS\Validations;
use App\Models\PollerCluster;
use Carbon\Carbon;
use LibreNMS\Config; use LibreNMS\Config;
use LibreNMS\Validator; use LibreNMS\Validator;
class DistributedPoller extends BaseValidation class DistributedPoller extends BaseValidation
{ {
protected static $RUN_BY_DEFAULT = false; public function isDefault()
{
// run by default if distributed polling is enabled
return Config::get('distributed_poller');
}
/** /**
* Validate this module. * Validate this module.
@@ -47,11 +53,62 @@ class DistributedPoller extends BaseValidation
public function validate(Validator $validator) public function validate(Validator $validator)
{ {
if (! Config::get('distributed_poller')) { if (! Config::get('distributed_poller')) {
$validator->fail('You have not enabled distributed_poller'); $validator->fail('You have not enabled distributed_poller', 'lnms config:set distributed_poller true');
return; return;
} }
if (! Config::get('rrdcached')) {
$validator->fail('You have not configured $config[\'rrdcached\']');
} elseif (! is_dir(Config::get('rrd_dir'))) {
$validator->fail('You have not configured $config[\'rrd_dir\']');
} else {
Rrd::checkRrdcached($validator);
}
if (PollerCluster::exists()) {
if (PollerCluster::isActive()->exists()) {
$validator->info('Detected Dispatcher Service');
$this->checkDispatcherService($validator);
return;
}
$validator->warn('Dispatcher Service has been used in your cluster, but not recently. It may take up to 5 minutes to register.');
}
$validator->info('Detected Python Wrapper');
$this->checkPythonWrapper($validator);
}
private function checkDispatcherService(Validator $validator)
{
$driver = config('cache.default');
if ($driver != 'redis') {
$validator->warn("Using $driver for distributed locking, you should set CACHE_DRIVER=redis");
}
try {
$lock = \Cache::lock('dist_test_validation');
$lock->get();
$lock->release();
} catch (\Exception $e) {
$validator->fail('Locking server issue: ' . $e->getMessage());
}
$node = PollerCluster::firstWhere('node_id', config('librenms.node_id'));
if (! $node->exists) {
$validator->fail('Dispatcher service is enabled on your cluster, but not in use on this node');
return;
}
if ($node->last_report->lessThan(Carbon::now()->subSeconds($node->getSettingValue('poller_frequency')))) {
$validator->fail('Dispatcher service has not reported stats within the last poller window');
}
}
private function checkPythonWrapper(Validator $validator)
{
if (! Config::get('distributed_poller_memcached_host')) { if (! Config::get('distributed_poller_memcached_host')) {
$validator->fail('You have not configured $config[\'distributed_poller_memcached_host\']'); $validator->fail('You have not configured $config[\'distributed_poller_memcached_host\']');
} elseif (! Config::get('distributed_poller_memcached_port')) { } elseif (! Config::get('distributed_poller_memcached_port')) {
@@ -65,13 +122,5 @@ class DistributedPoller extends BaseValidation
$validator->ok('Connection to memcached is ok'); $validator->ok('Connection to memcached is ok');
} }
} }
if (! Config::get('rrdcached')) {
$validator->fail('You have not configured $config[\'rrdcached\']');
} elseif (! is_dir(Config::get('rrd_dir'))) {
$validator->fail('You have not configured $config[\'rrd_dir\']');
} else {
Rrd::checkRrdcached($validator);
}
} }
} }

View File

@@ -218,6 +218,17 @@ class Validator
$this->result(new ValidationResult($message, ValidationResult::FAILURE, $fix), $group); $this->result(new ValidationResult($message, ValidationResult::FAILURE, $fix), $group);
} }
/**
* Submit an informational validation result.
*
* @param string $message
* @param string $group manually specify the group, otherwise this will be inferred from the callers class name
*/
public function info($message, $group = null)
{
$this->result(new ValidationResult($message, ValidationResult::INFO), $group);
}
/** /**
* Get version_info() array. This will cache the result and add remote data if requested and not already existing. * Get version_info() array. This will cache the result and add remote data if requested and not already existing.
* *

View File

@@ -25,6 +25,7 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use LibreNMS\Exceptions\InvalidNameException;
class PollerCluster extends Model class PollerCluster extends Model
{ {
@@ -32,6 +33,9 @@ class PollerCluster extends Model
protected $table = 'poller_cluster'; protected $table = 'poller_cluster';
protected $primaryKey = 'id'; protected $primaryKey = 'id';
protected $fillable = ['poller_name']; protected $fillable = ['poller_name'];
protected $casts = [
'last_report' => 'datetime',
];
// ---- Accessors/Mutators ---- // ---- Accessors/Mutators ----
@@ -40,17 +44,45 @@ class PollerCluster extends Model
$this->attributes['poller_groups'] = is_array($groups) ? implode(',', $groups) : $groups; $this->attributes['poller_groups'] = is_array($groups) ? implode(',', $groups) : $groups;
} }
// ---- Scopes ----
public function scopeIsActive($query)
{
$default = (int) \LibreNMS\Config::get('service_poller_frequency');
$query->where('last_report', '>=', \DB::raw("DATE_SUB(NOW(),INTERVAL COALESCE(`poller_frequency`, $default) SECOND)"));
}
// ---- Helpers ---- // ---- Helpers ----
/**
* Get the value of a setting (falls back to the global value if not set on this node)
*
* @param string $name
* @return mixed
* @throws \LibreNMS\Exceptions\InvalidNameException
*/
public function getSettingValue(string $name)
{
$definition = $this->configDefinition(false);
foreach ($definition as $entry) {
if ($entry['name'] == $name) {
return $entry['value'];
}
}
throw new InvalidNameException("Poller group setting named \"$name\" is invalid");
}
/** /**
* Get the frontend config definition for this poller * Get the frontend config definition for this poller
* *
* @param \Illuminate\Support\Collection $groups optionally supply full list of poller groups to avoid fetching multiple times * @param \Illuminate\Support\Collection|bool|null $groups optionally supply full list of poller groups to avoid fetching multiple times
* @return array[] * @return array[]
*/ */
public function configDefinition($groups = null) public function configDefinition($groups = null)
{ {
if (empty($groups)) { if ($groups === null || $groups === true) {
$groups = PollerGroup::list(); $groups = PollerGroup::list();
} }

View File

@@ -43,4 +43,14 @@ return [
'install' => env('INSTALL', false), 'install' => env('INSTALL', false),
/*
|--------------------------------------------------------------------------
| NODE ID
|--------------------------------------------------------------------------
|
| Unique value to identify this node. Primarily used for distributed polling.
*/
'node_id' => env('NODE_ID'),
]; ];