Updated device cache support (#10795)

* Device Cache
also some additional device related models

* attribs from cache

* replace common attribute functions

* remove legacy cache usage
tidy up some collection manipulation
remove some unused or single use functions

* cleanup some items

* always return a device, to prevent bugs

* clear device cache when testing after each test

* fix double assignment

* Clean up function to take advantage of null object
This commit is contained in:
Tony Murray
2019-11-14 21:56:06 +00:00
committed by Neil Lathwood
parent d1c73b14f3
commit dec9a498ee
20 changed files with 311 additions and 132 deletions

View File

@@ -16,6 +16,7 @@
* the source code distribution for details.
*/
use App\Models\Device;
use LibreNMS\Config;
use LibreNMS\Exceptions\InvalidIpException;
use LibreNMS\Util\Git;
@@ -239,23 +240,6 @@ function get_port_by_ifIndex($device_id, $ifIndex)
return dbFetchRow("SELECT * FROM `ports` WHERE `device_id` = ? AND `ifIndex` = ?", array($device_id, $ifIndex));
}
function get_all_devices()
{
global $cache;
$devices = array();
// FIXME needs access control checks!
// FIXME respect $type (server, network, etc) -- needs an array fill in topnav.
if (isset($cache['devices']['hostname'])) {
$devices = array_keys($cache['devices']);
} else {
$devices = dbFetchRows("SELECT * FROM `devices` ORDER BY hostname");
}
return $devices;
}
function table_from_entity_type($type)
{
// Fuck you, english pluralisation.
@@ -347,10 +331,9 @@ function ifclass($ifOperStatus, $ifAdminStatus)
return \LibreNMS\Util\Url::portLinkDisplayClass((object) ['ifOperStatus' => $ifOperStatus, 'ifAdminStatus' => $ifAdminStatus]);
}
function device_by_name($name, $refresh = 0)
function device_by_name($name)
{
// FIXME - cache name > id too.
return device_by_id_cache(getidbyname($name), $refresh);
return device_by_id_cache(getidbyname($name));
}
@@ -365,29 +348,15 @@ function accesspoint_by_id($ap_id, $refresh = '0')
function device_by_id_cache($device_id, $refresh = false)
{
global $cache;
$model = $refresh ? DeviceCache::refresh($device_id) : DeviceCache::get($device_id);
if (!$refresh && isset($cache['devices']['id'][$device_id]) && is_array($cache['devices']['id'][$device_id])) {
$device = $cache['devices']['id'][$device_id];
} else {
$device = dbFetchRow("SELECT `devices`.*, `location`, `lat`, `lng` FROM `devices` LEFT JOIN locations ON `devices`.location_id=`locations`.`id` WHERE `device_id` = ?", [$device_id]);
$device['attribs'] = get_dev_attribs($device['device_id']);
load_os($device);
$device = $model->toArray();
$device['location'] = $model->location->location;
$device['lat'] = $model->location->lat;
$device['lng'] = $model->location->lng;
$device['attribs'] = $model->getAttribs();
$device['vrf_lite_cisco'] = $model->vrfLites->keyBy('context_name')->toArray();
//order vrf_lite_cisco with context, this will help to get the vrf_name and instance_name all the time
$vrfs_lite_cisco = dbFetchRows("SELECT * FROM `vrf_lite_cisco` WHERE `device_id` = ?", array($device_id));
if (!empty($vrfs_lite_cisco)) {
$device['vrf_lite_cisco'] = array();
foreach ($vrfs_lite_cisco as $vrf) {
$device['vrf_lite_cisco'][$vrf['context_name']] = $vrf;
}
}
if (!empty($device['ip'])) {
$device['ip'] = inet6_ntop($device['ip']);
}
$cache['devices']['id'][$device_id] = $device;
}
return $device;
}
@@ -419,17 +388,9 @@ function getifhost($id)
return dbFetchCell("SELECT `device_id` from `ports` WHERE `port_id` = ?", array($id));
}
function gethostbyid($id)
function gethostbyid($device_id)
{
global $cache;
if (isset($cache['devices']['id'][$id]['hostname'])) {
$hostname = $cache['devices']['id'][$id]['hostname'];
} else {
$hostname = dbFetchCell("SELECT `hostname` FROM `devices` WHERE `device_id` = ?", array($id));
}
return $hostname;
return DeviceCache::get($device_id)->hostname;
}
function strgen($length = 16)
@@ -470,28 +431,7 @@ function getifdescrbyid($id)
function getidbyname($hostname)
{
global $cache;
if (isset($cache['devices']['hostname'][$hostname])) {
$id = $cache['devices']['hostname'][$hostname];
} else {
$id = dbFetchCell("SELECT `device_id` FROM `devices` WHERE `hostname` = ?", array($hostname));
}
return $id;
}
function gethostosbyid($id)
{
global $cache;
if (isset($cache['devices']['id'][$id]['os'])) {
$os = $cache['devices']['id'][$id]['os'];
} else {
$os = dbFetchCell("SELECT `os` FROM `devices` WHERE `device_id` = ?", array($id));
}
return $os;
return DeviceCache::getByHostname($hostname)->device_id;
}
function safename($name)
@@ -516,21 +456,12 @@ function zeropad($num, $length = 2)
function set_dev_attrib($device, $attrib_type, $attrib_value)
{
if (dbFetchCell("SELECT COUNT(*) FROM devices_attribs WHERE `device_id` = ? AND `attrib_type` = ?", array($device['device_id'],$attrib_type))) {
$return = dbUpdate(array('attrib_value' => $attrib_value), 'devices_attribs', 'device_id=? and attrib_type=?', array($device['device_id'], $attrib_type));
} else {
$return = dbInsert(array('device_id' => $device['device_id'], 'attrib_type' => $attrib_type, 'attrib_value' => $attrib_value), 'devices_attribs');
}
return $return;
return DeviceCache::get($device['device_id'])->setAttrib($attrib_type, $attrib_value);
}
function get_dev_attribs($device)
function get_dev_attribs($device_id)
{
$attribs = array();
foreach (dbFetchRows("SELECT * FROM devices_attribs WHERE `device_id` = ?", array($device)) as $entry) {
$attribs[$entry['attrib_type']] = $entry['attrib_value'];
}
return $attribs;
return DeviceCache::get($device_id)->getAttribs();
}
function get_dev_entity_state($device)
@@ -543,36 +474,14 @@ function get_dev_entity_state($device)
return $state;
}
function get_dev_attrib($device, $attrib_type, $attrib_value = '')
function get_dev_attrib($device, $attrib_type)
{
$sql = '';
$params = array($device['device_id'], $attrib_type);
if (!empty($attrib_value)) {
$sql = " AND `attrib_value`=?";
array_push($params, $attrib_value);
}
if ($row = dbFetchRow("SELECT attrib_value FROM devices_attribs WHERE `device_id` = ? AND `attrib_type` = ? $sql", $params)) {
return $row['attrib_value'];
} else {
return null;
}
}
function is_dev_attrib_enabled($device, $attrib, $default = true)
{
$val = get_dev_attrib($device, $attrib);
if ($val != null) {
// attribute is set
return ($val != 0);
} else {
// attribute not set
return $default;
}
return DeviceCache::get($device['device_id'])->getAttrib($attrib_type);
}
function del_dev_attrib($device, $attrib_type)
{
return dbDelete('devices_attribs', "`device_id` = ? AND `attrib_type` = ?", array($device['device_id'], $attrib_type));
return DeviceCache::get($device['device_id'])->forgetAttrib($attrib_type);
}
function formatRates($value, $round = '2', $sf = '3')