mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
* core WIP * try to finish up * trim space too and a couple of cleanups * update test data * put escapes back * another net-snmp difference * correct copy paste error * WIP * Use new code YAY * a tiny bit more * Kind of working * Handle manual modules correctly * convert core to modern module * Only save metrics if modules is not overridden * correct module exists check * database error handling * debug handling * restore bad changes * Introduce Actions RunAlertRulesAction UpdateDeviceGroupsAction * tweaks to output * Fix some issues in outside code * Style fixes * fixes to module status checks * typehints! * Use logger only and DI * OS module not named correctly * Work on quiet output a bit more * generically don't change output when disabling debug if the driver is already stack * Fix missing $device variable for legacy os polling Fix missing dbFacile functions when no legacy modules polled in RunAlertRulesAction * restore legacy os module shim * use the new poller code for tests * PollingDevice event * Fix some issues and enable/disable error reporting around legacy modules * typehints * fully update baseline * Use Process for version commands so we don't leak debug output. * don't detect rrdtool version in ci every time * style fixes * Warning fixes * more fixes * re-update baseline * remove diff noise * fix up alerts * Catch exceptions in device ip lookup * Revert accidental snmp.inc.php poller target change (should have been ?: not ??)
107 lines
3.5 KiB
PHP
107 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Observers;
|
|
|
|
use App\Models\Device;
|
|
use Log;
|
|
|
|
class DeviceObserver
|
|
{
|
|
/**
|
|
* Handle the device "created" event.
|
|
*
|
|
* @param \App\Models\Device $device
|
|
* @return void
|
|
*/
|
|
public function created(Device $device)
|
|
{
|
|
Log::event("Device $device->hostname has been created", $device, 'system', 3);
|
|
}
|
|
|
|
/**
|
|
* 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();
|
|
}
|
|
|
|
// log up/down status changes
|
|
if ($device->isDirty(['status', 'status_reason'])) {
|
|
$type = $device->status ? 'up' : 'down';
|
|
$reason = $device->status ? $device->getOriginal('status_reason') : $device->status_reason;
|
|
Log::event('Device status changed to ' . ucfirst($type) . " from $reason check.", $device, $type);
|
|
}
|
|
|
|
// key attribute changes
|
|
foreach (['os', 'sysName', 'version', 'hardware', 'features', 'serial', 'icon', 'type', 'ip'] 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;
|
|
}
|
|
}
|