. * * @link https://www.librenms.org * * @copyright 2020 Tony Murray * @author Tony Murray */ namespace LibreNMS\Modules; use App\Models\Location; use LibreNMS\Interfaces\Module; use LibreNMS\Interfaces\Polling\OSPolling; use LibreNMS\Util\Url; class Os implements Module { public function discover(\LibreNMS\OS $os): void { $this->updateLocation($os); $this->sysContact($os); // null out values in case they aren't filled. $os->getDevice()->fill([ 'hardware' => null, 'version' => null, 'features' => null, 'serial' => null, 'icon' => null, ]); $os->discoverOS($os->getDevice()); $this->handleChanges($os); } public function poll(\LibreNMS\OS $os): void { $deviceModel = $os->getDevice(); /** @var \App\Models\Device $deviceModel */ if ($os instanceof OSPolling) { $os->pollOS(); } else { // legacy poller files global $graphs, $device; if (empty($device)) { $device = $os->getDeviceArray(); } $location = null; if (is_file(base_path('/includes/polling/os/' . $device['os'] . '.inc.php'))) { // OS Specific include base_path('/includes/polling/os/' . $device['os'] . '.inc.php'); } elseif (! empty($device['os_group']) && is_file(base_path('/includes/polling/os/' . $device['os_group'] . '.inc.php'))) { // OS Group Specific include base_path('/includes/polling/os/' . $device['os_group'] . '.inc.php'); } else { echo "Generic :(\n"; } // handle legacy variables, sometimes they are false $deviceModel->version = ($version ?? $deviceModel->version) ?: null; $deviceModel->hardware = ($hardware ?? $deviceModel->hardware) ?: null; $deviceModel->features = ($features ?? $deviceModel->features) ?: null; $deviceModel->serial = ($serial ?? $deviceModel->serial) ?: null; if (! empty($location)) { // legacy support, remove when no longer needed $deviceModel->setLocation($location); optional($deviceModel->location)->save(); } } $this->handleChanges($os); } public function cleanup(\LibreNMS\OS $os): void { // no cleanup needed? } private function handleChanges(\LibreNMS\OS $os): void { $device = $os->getDevice(); $device->icon = basename(Url::findOsImage($device->os, $device->features, null, 'images/os/')); echo trans('device.attributes.location') . ': ' . optional($device->location)->display() . PHP_EOL; foreach (['hardware', 'version', 'features', 'serial'] as $attribute) { echo \App\Observers\DeviceObserver::attributeChangedMessage($attribute, $device->$attribute, $device->getOriginal($attribute)) . PHP_EOL; } $device->save(); } private function updateLocation(\LibreNMS\OS $os): void { $device = $os->getDevice(); $new_location = $device->override_sysLocation ? new Location() : $os->fetchLocation(); // fetch location data from device $device->setLocation($new_location, true); // set location and lookup coordinates if needed optional($device->location)->save(); } private function sysContact(\LibreNMS\OS $os): void { $device = $os->getDevice(); $device->sysContact = snmp_get($os->getDeviceArray(), 'sysContact.0', '-Ovq', 'SNMPv2-MIB'); $device->sysContact = str_replace(['', '"', '\n', 'not set'], '', $device->sysContact); if (empty($device->sysContact)) { $device->sysContact = null; } } }