diff --git a/LibreNMS/Modules/Nac.php b/LibreNMS/Modules/Nac.php index 45a5373c63..cd431e9664 100644 --- a/LibreNMS/Modules/Nac.php +++ b/LibreNMS/Modules/Nac.php @@ -54,9 +54,7 @@ class Nac implements Module { if ($os instanceof NacPolling) { // discovery output (but don't install it twice (testing can can do this) - if (! PortsNac::getEventDispatcher()->hasListeners('eloquent.created: App\Models\PortsNac')) { - PortsNac::observe(new ModuleModelObserver()); - } + ModuleModelObserver::observe(PortsNac::class); $nac_entries = $os->pollNac()->keyBy('mac_address'); $existing_entries = $os->getDevice()->portsNac->keyBy('mac_address'); diff --git a/LibreNMS/Util/ModuleModelObserver.php b/LibreNMS/Util/ModuleModelObserver.php index 2e65e272ef..9cfa72ae04 100644 --- a/LibreNMS/Util/ModuleModelObserver.php +++ b/LibreNMS/Util/ModuleModelObserver.php @@ -25,21 +25,22 @@ namespace LibreNMS\Util; use Illuminate\Database\Eloquent\Model as Eloquent; -use Illuminate\Support\Str; class ModuleModelObserver { /** * Install observers to output +, -, U for models being created, deleted, and updated * - * @param string $model The model name including namespace + * @param string|\Illuminate\Database\Eloquent\Model $model The model name including namespace */ public static function observe($model) { - $model = Str::start($model, '\\'); - // discovery output (but don't install it twice (testing can can do this) - if (! $model::getEventDispatcher()->hasListeners('eloquent.created: ' . ltrim('\\', $model))) { + static $observed_models = []; // keep track of observed models so we don't duplicate output + $class = ltrim($model, '\\'); + + if (! in_array($class, $observed_models)) { $model::observe(new ModuleModelObserver()); + $observed_models[] = $class; } }