mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Sensors remove reliance on global variable (#16344)
* Sensors remove reliance on global variable * Apply fixes from StyleCI * Clear the instance instead of reset. Remove $valid['sensors'] from docs --------- Co-authored-by: Tony Murray <murrant@users.noreply.github.com>
This commit is contained in:
79
app/Discovery/Sensor.php
Normal file
79
app/Discovery/Sensor.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/**
|
||||
* Sensor.php
|
||||
*
|
||||
* Collects discovered sensors and allows the deletion of non-discovered sensors
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @link https://www.librenms.org
|
||||
*
|
||||
* @copyright 2024 Tony Murray
|
||||
* @author Tony Murray <murraytony@gmail.com>
|
||||
*/
|
||||
|
||||
namespace App\Discovery;
|
||||
|
||||
use App\Models\Device;
|
||||
use Illuminate\Support\Collection;
|
||||
use LibreNMS\DB\SyncsModels;
|
||||
|
||||
class Sensor
|
||||
{
|
||||
use SyncsModels;
|
||||
|
||||
private Collection $models;
|
||||
/** @var bool[] */
|
||||
private array $discovered = [];
|
||||
private string $relationship = 'sensors';
|
||||
private Device $device;
|
||||
|
||||
public function __construct(Device $device)
|
||||
{
|
||||
$this->device = $device;
|
||||
$this->models = new Collection;
|
||||
}
|
||||
|
||||
public function discover(\App\Models\Sensor $sensor): static
|
||||
{
|
||||
$this->models->push($sensor);
|
||||
$this->discovered[$sensor->syncGroup()] = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isDiscovered(string $type): bool
|
||||
{
|
||||
return $this->discovered[$type] ?? false;
|
||||
}
|
||||
|
||||
public function sync(...$params): Collection
|
||||
{
|
||||
$type = implode('-', $params);
|
||||
|
||||
if (! $this->isDiscovered($type)) {
|
||||
$synced = $this->syncModelsByGroup($this->device, 'sensors', $this->getModels(), $params);
|
||||
$this->discovered[$type] = true;
|
||||
|
||||
return $synced;
|
||||
}
|
||||
|
||||
return new Collection;
|
||||
}
|
||||
|
||||
public function getModels(): Collection
|
||||
{
|
||||
return $this->models;
|
||||
}
|
||||
}
|
@@ -5,13 +5,35 @@ namespace App\Models;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
use LibreNMS\Interfaces\Models\Keyable;
|
||||
|
||||
class Sensor extends DeviceRelatedModel
|
||||
class Sensor extends DeviceRelatedModel implements Keyable
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public $timestamps = false;
|
||||
protected $primaryKey = 'sensor_id';
|
||||
protected $fillable = [
|
||||
'poller_type',
|
||||
'sensor_class',
|
||||
'device_id',
|
||||
'sensor_oid',
|
||||
'sensor_index',
|
||||
'sensor_type',
|
||||
'sensor_descr',
|
||||
'sensor_divisor',
|
||||
'sensor_multiplier',
|
||||
'sensor_limit',
|
||||
'sensor_limit_warn',
|
||||
'sensor_limit_low',
|
||||
'sensor_limit_low_warn',
|
||||
'sensor_current',
|
||||
'entPhysicalIndex',
|
||||
'entPhysicalIndex_measured',
|
||||
'user_func',
|
||||
'group',
|
||||
'rrd_type',
|
||||
];
|
||||
protected static $icons = [
|
||||
'airflow' => 'angle-double-right',
|
||||
'ber' => 'sort-amount-desc',
|
||||
@@ -75,6 +97,32 @@ class Sensor extends DeviceRelatedModel
|
||||
return self::$icons;
|
||||
}
|
||||
|
||||
public function guessLimits(): void
|
||||
{
|
||||
$this->sensor_limit = match ($this->sensor_class) {
|
||||
'temperature' => $this->sensor_current - 10,
|
||||
'voltage' => $this->sensor_current * 0.85,
|
||||
'humidity' => 30,
|
||||
'fanspeed' => $this->sensor_current * 0.80,
|
||||
'power_factor' => -1,
|
||||
'signal' => -80,
|
||||
'airflow', 'snr', 'frequency', 'pressure', 'cooling' => $this->sensor_current * 0.95,
|
||||
default => null,
|
||||
};
|
||||
|
||||
$this->sensor_limit_low = match ($this->sensor_class) {
|
||||
'temperature' => $this->sensor_current + 20,
|
||||
'voltage' => $this->sensor_current * 1.15,
|
||||
'humidity' => 70,
|
||||
'fanspeed' => $this->sensor_current * 1.80,
|
||||
'power_factor' => 1,
|
||||
'signal' => -30,
|
||||
'load' => 80,
|
||||
'airflow', 'snr', 'frequency', 'pressure', 'cooling' => $this->sensor_current * 1.05,
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
// ---- Define Relationships ----
|
||||
public function events(): MorphMany
|
||||
{
|
||||
@@ -85,4 +133,14 @@ class Sensor extends DeviceRelatedModel
|
||||
{
|
||||
return $this->belongsToMany(StateTranslation::class, 'sensors_to_state_indexes', 'sensor_id', 'state_index_id');
|
||||
}
|
||||
|
||||
public function getCompositeKey(): string
|
||||
{
|
||||
return "$this->poller_type-$this->sensor_class-$this->device_id-$this->sensor_type-$this->sensor_index";
|
||||
}
|
||||
|
||||
public function syncGroup(): string
|
||||
{
|
||||
return "$this->sensor_class-$this->poller_type";
|
||||
}
|
||||
}
|
||||
|
121
app/Observers/SensorObserver.php
Normal file
121
app/Observers/SensorObserver.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\Eventlog;
|
||||
use App\Models\Sensor;
|
||||
use Illuminate\Foundation\Application;
|
||||
use LibreNMS\Enum\Severity;
|
||||
|
||||
class SensorObserver
|
||||
{
|
||||
private bool $runningInConsole;
|
||||
|
||||
public function __construct(Application $app)
|
||||
{
|
||||
$this->runningInConsole = $app->runningInConsole();
|
||||
}
|
||||
|
||||
public function saving(Sensor $sensor): void
|
||||
{
|
||||
// fix inverted limits
|
||||
if ($sensor->sensor_limit !== null && $sensor->sensor_limit_low !== null && $sensor->sensor_limit_low > $sensor->sensor_limit) {
|
||||
// Fix high/low thresholds (i.e. on negative numbers)
|
||||
[$sensor->sensor_limit, $sensor->sensor_limit_low] = [$sensor->sensor_limit_low, $sensor->sensor_limit];
|
||||
}
|
||||
|
||||
if ($this->runningInConsole && ! $sensor->isDirty()) {
|
||||
echo '.';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the service "created" event.
|
||||
*
|
||||
* @param Sensor $sensor
|
||||
* @return void
|
||||
*/
|
||||
public function created(Sensor $sensor): void
|
||||
{
|
||||
$guess_limits = \LibreNMS\Config::get('sensors.guess_limits', true);
|
||||
if ($guess_limits && $sensor->sensor_current !== null && $sensor->sensor_limit === null && $sensor->sensor_limit_low === null) {
|
||||
$sensor->guessLimits();
|
||||
}
|
||||
|
||||
EventLog::log('Sensor Added: ' . $sensor->sensor_class . ' ' . $sensor->sensor_type . ' ' . $sensor->sensor_index . ' ' . $sensor->sensor_descr, $sensor->device_id, 'sensor', Severity::Notice, $sensor->sensor_id);
|
||||
|
||||
if ($this->runningInConsole) {
|
||||
echo '+';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Stp "updating" event.
|
||||
*
|
||||
* @param \App\Models\Sensor $sensor
|
||||
* @return void
|
||||
*/
|
||||
public function updating(Sensor $sensor)
|
||||
{
|
||||
// prevent update of limits
|
||||
if ($sensor->sensor_custom !== 'Yes') {
|
||||
if ($sensor->getOriginal('sensor_limit') !== null) {
|
||||
$sensor->sensor_limit = $sensor->getOriginal('sensor_limit');
|
||||
}
|
||||
if ($sensor->getOriginal('sensor_limit_low') !== null) {
|
||||
$sensor->sensor_limit_low = $sensor->getOriginal('sensor_limit_low');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function updated(Sensor $sensor): void
|
||||
{
|
||||
// log limit changes
|
||||
if ($sensor->sensor_custom == 'No') {
|
||||
if ($sensor->isDirty('sensor_limit')) {
|
||||
EventLog::log('Sensor High Limit Updated: ' . $sensor->sensor_class . ' ' . $sensor->sensor_type . ' ' . $sensor->sensor_index . ' ' . $sensor->sensor_descr . ' (' . $sensor->sensor_limit . ')', $sensor->device_id, 'sensor', Severity::Notice, $sensor->sensor_id);
|
||||
|
||||
if ($this->runningInConsole) {
|
||||
echo 'H';
|
||||
}
|
||||
}
|
||||
|
||||
if ($sensor->isDirty('sensor_limit_low')) {
|
||||
EventLog::log('Sensor Low Limit Updated: ' . $sensor->sensor_class . ' ' . $sensor->sensor_type . ' ' . $sensor->sensor_index . ' ' . $sensor->sensor_descr . ' (' . $sensor->sensor_limit_low . ')', $sensor->device_id, 'sensor', Severity::Notice, $sensor->sensor_id);
|
||||
|
||||
if ($this->runningInConsole) {
|
||||
echo 'L';
|
||||
}
|
||||
}
|
||||
|
||||
if ($sensor->isDirty('sensor_limit_warn')) {
|
||||
EventLog::log('Sensor Warn High Limit Updated: ' . $sensor->sensor_class . ' ' . $sensor->sensor_type . ' ' . $sensor->sensor_index . ' ' . $sensor->sensor_descr . ' (' . $sensor->sensor_limit_warn . ')', $sensor->device_id, 'sensor', Severity::Notice, $sensor->sensor_id);
|
||||
|
||||
if ($this->runningInConsole) {
|
||||
echo 'WH';
|
||||
}
|
||||
}
|
||||
|
||||
if ($sensor->isDirty('sensor_limit_low_warn')) {
|
||||
EventLog::log('Sensor Warn Low Limit Updated: ' . $sensor->sensor_class . ' ' . $sensor->sensor_type . ' ' . $sensor->sensor_index . ' ' . $sensor->sensor_descr . ' (' . $sensor->sensor_limit_low_warn . ')', $sensor->device_id, 'sensor', Severity::Notice, $sensor->sensor_id);
|
||||
|
||||
if ($this->runningInConsole) {
|
||||
echo 'WL';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->runningInConsole) {
|
||||
echo 'U';
|
||||
}
|
||||
|
||||
EventLog::log('Sensor Updated: ' . $sensor->sensor_class . ' ' . $sensor->sensor_type . ' ' . $sensor->sensor_index . ' ' . $sensor->sensor_descr, $sensor->device_id, 'sensor', Severity::Notice, $sensor->sensor_id);
|
||||
}
|
||||
|
||||
public function deleted(): void
|
||||
{
|
||||
if ($this->runningInConsole) {
|
||||
echo '-';
|
||||
}
|
||||
}
|
||||
}
|
@@ -3,6 +3,7 @@
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Models\Sensor;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
use Illuminate\Database\Eloquent\Relations\Relation;
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
@@ -25,22 +26,26 @@ class AppServiceProvider extends ServiceProvider
|
||||
$this->registerFacades();
|
||||
$this->registerGeocoder();
|
||||
|
||||
$this->app->singleton('permissions', function ($app) {
|
||||
$this->app->singleton('permissions', function () {
|
||||
return new PermissionsCache();
|
||||
});
|
||||
$this->app->singleton('device-cache', function ($app) {
|
||||
$this->app->singleton('device-cache', function () {
|
||||
return new \LibreNMS\Cache\Device();
|
||||
});
|
||||
$this->app->singleton('git', function ($app) {
|
||||
$this->app->singleton('git', function () {
|
||||
return new \LibreNMS\Util\Git();
|
||||
});
|
||||
|
||||
$this->app->bind(\App\Models\Device::class, function () {
|
||||
$this->app->bind(\App\Models\Device::class, function (Application $app) {
|
||||
/** @var \LibreNMS\Cache\Device $cache */
|
||||
$cache = $this->app->make('device-cache');
|
||||
$cache = $app->make('device-cache');
|
||||
|
||||
return $cache->hasPrimary() ? $cache->getPrimary() : new \App\Models\Device;
|
||||
});
|
||||
|
||||
$this->app->singleton('sensor-discovery', function (Application $app) {
|
||||
return new \App\Discovery\Sensor($app->make('device-cache')->getPrimary());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -137,6 +142,7 @@ class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
\App\Models\Device::observe(\App\Observers\DeviceObserver::class);
|
||||
\App\Models\Package::observe(\App\Observers\PackageObserver::class);
|
||||
\App\Models\Sensor::observe(\App\Observers\SensorObserver::class);
|
||||
\App\Models\Service::observe(\App\Observers\ServiceObserver::class);
|
||||
\App\Models\Stp::observe(\App\Observers\StpObserver::class);
|
||||
\App\Models\User::observe(\App\Observers\UserObserver::class);
|
||||
|
Reference in New Issue
Block a user