librenms-librenms/app/Observers/DeviceObserver.php
Tony Murray b573c9ef73 Implement OS specific information discovery (#11446)
* Implement OS specific information discovery
That way it doesn't have to be fetched during polling
Also improve discovery process, os is only detected once, in the core module.
EXA is the test os, a couple improvements there.

* Use local variable, then unset it so we don't pollute.

* fix style issues

* test and other fixes

* attribute update fixes

* Update exa data, need new source data

* null missing "os" values

* fix ftos odd character

* fix ftos odd character

* only null for new style or we will reset to null every discovery

* Move device observer to own class

* Handle location, relocate event logging

* update exa e7-2 data

* update ird test data, apparently unicode is now working.

* update Linux ntc, now uses correct icon

* Only load all os on the web, also, we can't load existing the the database isn't connected.

* only for devices that have a location

* revert ftos test data apparently

* revert ird
2020-05-14 11:27:59 -05:00

88 lines
2.8 KiB
PHP

<?php
namespace App\Observers;
use App\Models\Device;
class DeviceObserver
{
/**
* Handle the device "updated" event.
*
* @param \App\Models\Device $device
* @return void
*/
public function updated(Device $device)
{
// handle device dependency updates
if ($device->isDirty('max_depth')) {
$device->children->each->updateMaxDepth();
}
// key attribute changes
foreach (['os', 'sysName', 'version', 'hardware', 'features', 'serial', 'icon'] as $attribute) {
if ($device->isDirty($attribute)) {
\Log::event(self::attributeChangedMessage($attribute, $device->$attribute, $device->getOriginal($attribute)), $device, 'system', 3);
}
}
if ($device->isDirty('location_id')) {
\Log::event(self::attributeChangedMessage('location', (string)$device->location, null), $device, 'system', 3);
}
}
/**
* Handle the device "deleting" event.
*
* @param \App\Models\Device $device
* @return void
*/
public function deleting(Device $device)
{
// delete related data
$device->ports()->delete();
$device->syslogs()->delete();
$device->eventlogs()->delete();
$device->applications()->delete();
// handle device dependency updates
$device->children->each->updateMaxDepth($device->device_id);
}
/**
* Handle the device "Pivot Attached" event.
*
* @param \App\Models\Device $device
* @param string $relationName parents or children
* @param array $pivotIds list of pivot ids
* @param array $pivotIdsAttributes additional pivot attributes
* @return void
*/
public function pivotAttached(Device $device, $relationName, $pivotIds, $pivotIdsAttributes)
{
if ($relationName == 'parents') {
// a parent attached to this device
// update the parent's max depth incase it used to be standalone
Device::whereIn('device_id', $pivotIds)->get()->each->validateStandalone();
// make sure this device's max depth is updated
$device->updateMaxDepth();
} elseif ($relationName == 'children') {
// a child device attached to this device
// if this device used to be standalone, we need to udpate max depth
$device->validateStandalone();
// make sure the child's max depth is updated
Device::whereIn('device_id', $pivotIds)->get()->each->updateMaxDepth();
}
}
public static function attributeChangedMessage($attribute, $value, $previous)
{
return trans("device.attributes.$attribute") . ': '
. (($previous && $previous != $value) ? "$previous -> " : '')
. $value;
}
}