Files
librenms-librenms/LibreNMS/Cache/Device.php
Tony Murray d5a52ca4eb Fix various issues with loading os definitions (#11640)
* Ping only device doesn't display
if os was set to something, ping os wasn't loaded and we try to get overview graphs from it.

* Fix snmp_disable device page load error
When other os is set.

* Revamp os setting loading
the only safe way to access is Config::getOsSetting()

* Remove getOsSetting fallback behavior
Most instances don't use it and it can have unexpected results Config::getOsSetting('blah', 'group') == 'librenms'

* refactor and remove unneeded load_os/loadOs calls now since getOsSetting automatically loads it.

* restore unix overview graphs, they are different
small cleanups

* fix
2020-05-19 14:35:32 -05:00

118 lines
2.8 KiB
PHP

<?php
/**
* Device.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2019 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Cache;
class Device
{
private $devices = [];
private $primary;
/**
* Gets the current primary device.
*
* @return \App\Models\Device
*/
public function getPrimary() : \App\Models\Device
{
return $this->get($this->primary);
}
/**
* Set the primary device.
* This will be fetched by getPrimary()
*
* @param int $device_id
*/
public function setPrimary($device_id)
{
$this->primary = $device_id;
}
/**
* Get a device by device_id
*
* @param int $device_id
* @return \App\Models\Device
*/
public function get($device_id) : \App\Models\Device
{
if (!array_key_exists($device_id, $this->devices)) {
return $this->load($device_id);
}
return $this->devices[$device_id] ?: new \App\Models\Device;
}
/**
* Get a device by hostname
*
* @param string $hostname
* @return \App\Models\Device
*/
public function getByHostname($hostname) : \App\Models\Device
{
$device_id = collect($this->devices)->pluck('device_id', 'hostname')->get($hostname);
if (!$device_id) {
return $this->load($hostname, 'hostname');
}
return $this->devices[$device_id] ?: new \App\Models\Device;
}
/**
* Ignore cache and load the device fresh from the database
*
* @param int $device_id
* @return \App\Models\Device
*/
public function refresh($device_id) : \App\Models\Device
{
unset($this->devices[$device_id]);
return $this->get($device_id);
}
/**
* Flush the cache
*/
public function flush()
{
$this->devices = [];
}
private function load($value, $field = 'device_id')
{
$device = \App\Models\Device::query()->where($field, $value)->first();
if (!$device) {
return new \App\Models\Device;
}
$this->devices[$device->device_id] = $device;
return $device;
}
}