Save guessed limits (#16396)

* Save guessed limits
Previous code was guessing, then not saving the guess

* Move to creating, which revealed that limits were swapped

* Apply fixes from StyleCI

---------

Co-authored-by: Tony Murray <murrant@users.noreply.github.com>
This commit is contained in:
Tony Murray 2024-09-13 17:50:37 -05:00 committed by GitHub
parent a0587154c4
commit a6b69c9c4d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 7 deletions

View File

@ -99,7 +99,7 @@ class Sensor extends DeviceRelatedModel implements Keyable
public function guessLimits(): void
{
$this->sensor_limit = match ($this->sensor_class) {
$this->sensor_limit_low = match ($this->sensor_class) {
'temperature' => $this->sensor_current - 10,
'voltage' => $this->sensor_current * 0.85,
'humidity' => 30,
@ -110,7 +110,7 @@ class Sensor extends DeviceRelatedModel implements Keyable
default => null,
};
$this->sensor_limit_low = match ($this->sensor_class) {
$this->sensor_limit = match ($this->sensor_class) {
'temperature' => $this->sensor_current + 20,
'voltage' => $this->sensor_current * 1.15,
'humidity' => 70,

View File

@ -5,6 +5,7 @@ namespace App\Observers;
use App\Models\Eventlog;
use App\Models\Sensor;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Log;
use LibreNMS\Enum\Severity;
class SensorObserver
@ -20,6 +21,8 @@ class SensorObserver
{
// fix inverted limits
if ($sensor->sensor_limit !== null && $sensor->sensor_limit_low !== null && $sensor->sensor_limit_low > $sensor->sensor_limit) {
Log::error('Fixing swapped sensor limits');
// Fix high/low thresholds (i.e. on negative numbers)
[$sensor->sensor_limit, $sensor->sensor_limit_low] = [$sensor->sensor_limit_low, $sensor->sensor_limit];
}
@ -29,6 +32,14 @@ class SensorObserver
}
}
public function creating(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();
}
}
/**
* Handle the service "created" event.
*
@ -37,11 +48,6 @@ class SensorObserver
*/
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) {