From c8bdd10e6a0be655040e71018494ba4de8009951 Mon Sep 17 00:00:00 2001 From: SourceDoctor Date: Tue, 18 Feb 2020 13:26:41 +0100 Subject: [PATCH] Reduce DB polling while getting SNMP data (#11162) --- app/Models/Device.php | 25 ++++++++++++++++--------- includes/snmp.inc.php | 2 +- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/app/Models/Device.php b/app/Models/Device.php index 685833abe2..6240f730b8 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -106,19 +106,26 @@ class Device extends BaseModel /** * Returns IP/Hostname where polling will be targeted to * - * @param string $hostname hostname which will be triggered + * @param string $device hostname which will be triggered + * array $device associative array with device data * @return string IP/Hostname to which Device polling is targeted */ - public static function pollerTarget($hostname) + public static function pollerTarget($device) { - $ret = static::where('hostname', $hostname)->first(['hostname', 'overwrite_ip']); - if (empty($ret)) { - return $hostname; + if (! is_array($device)) { + $ret = static::where('hostname', $device)->first(['hostname', 'overwrite_ip']); + if (empty($ret)) { + return $device; + } + $overwrite_ip = $ret->overwrite_ip; + $hostname = $ret->hostname; + } elseif (array_key_exists('overwrite_ip', $device)) { + $overwrite_ip = $device['overwrite_ip']; + $hostname = $device['hostname']; + } else { + return $device['hostname']; } - $_overwrite_ip = $ret->overwrite_ip; - $_hostname = $ret->hostname; - - return $_overwrite_ip ?: $_hostname; + return $overwrite_ip ?: $hostname; } public static function findByIp($ip) diff --git a/includes/snmp.inc.php b/includes/snmp.inc.php index 348728d953..5533226037 100644 --- a/includes/snmp.inc.php +++ b/includes/snmp.inc.php @@ -172,7 +172,7 @@ function gen_snmp_cmd($cmd, $device, $oids, $options = null, $mib = null, $mibdi array_push($cmd, '-r', $retries); } - $pollertarget = Device::pollerTarget($device['hostname']); + $pollertarget = Device::pollerTarget($device); $cmd[] = $device['transport'].':'.$pollertarget.':'.$device['port']; $cmd = array_merge($cmd, (array)$oids);