Fix oxidized web requests unclosed connections or responding fast enough (#14370)

* Fix oxidized web requests not closing connections or responding fast enough

* Update showconfig.inc.php

* Add method to get the text content of an oxidized page

* Use Oxidized getContent method instead of file_get_content

* Too much brackets with copy paste

* Fix carriage return errors because of copy paste

* Fix copy paste error again

* Fix indent

* PHPStan is waiting for a return even outside of the if loop

* Single quotes

* Variabilize timeout in baseapi

* Set Oxidized Api class timeout var to 90 because oxidized is slow and to be sure not to break half the installs here

* fix typo

* Variabilize timeout

* Variabilize timeout

* Variabilize timeout

* Variabilize timeout

* Variabilize timeout

* Spacing

* Remove timeout type because of this error

Unexpected 'int' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in app/ApiClients/BaseApi.php on line 34

* Lint needs type finally

* Use contruct instead of setting variable

* Type hinting instead for php7.3

* Type hinting fix

* Set property value instead of calling parent constructor

* Typo

* Remove unneededconstructors

* Remove unneeded constructors

* Remove unneeded constructors

* Remove unneeded constructors

* Remove unneeded constructors

* Remove unneeded constructors

* Typing not casting

* Typing not casting

* Cannot type variables outside of a class

Co-authored-by: PipoCanaja <38363551+PipoCanaja@users.noreply.github.com>
This commit is contained in:
Félix Bouynot
2022-09-24 21:24:11 +02:00
committed by GitHub
parent 64dcfcb8cc
commit 333ba7c2cd
3 changed files with 23 additions and 11 deletions

View File

@@ -31,6 +31,8 @@ use LibreNMS\Util\Proxy;
class BaseApi
{
protected $base_uri;
/** @var int */
protected $timeout = 3;
private $client;
protected function getClient(): \Illuminate\Http\Client\PendingRequest
@@ -39,7 +41,7 @@ class BaseApi
$this->client = Http::withOptions([
'proxy' => Proxy::forGuzzle($this->base_uri),
])->baseUrl($this->base_uri)
->timeout(3);
->timeout($this->timeout);
}
return $this->client;

View File

@@ -36,6 +36,7 @@ class Oxidized extends BaseApi
public function __construct()
{
$this->timeout = 90;
$this->base_uri = Config::get('oxidized.url');
$this->enabled = Config::get('oxidized.enabled') === true && $this->base_uri;
}
@@ -66,4 +67,14 @@ class Oxidized extends BaseApi
return false;
}
/* Get content of the page */
public function getContent(string $uri): string
{
if ($this->enabled) {
return $this->getClient()->get($uri);
} else {
return '';
}
}
}

View File

@@ -130,8 +130,7 @@ if (Auth::user()->hasGlobalAdmin()) {
// Try with hostname as set in librenms first
$oxidized_hostname = $device['hostname'];
// fetch info about the node and then a list of versions for that node
$node_info = json_decode(file_get_contents(Config::get('oxidized.url') . '/node/show/' . $oxidized_hostname . '?format=json'), true);
$node_info = json_decode((new \App\ApiClients\Oxidized())->getContent('/node/show/' . $oxidized_hostname . '?format=json'), true);
if (! empty($node_info['last']['start'])) {
$node_info['last']['start'] = date(Config::get('dateformat.long'), strtotime($node_info['last']['start']));
}
@@ -150,12 +149,12 @@ if (Auth::user()->hasGlobalAdmin()) {
// Try Oxidized again with new hostname, if it has changed
if ($oxidized_hostname != $device['hostname']) {
$node_info = json_decode(file_get_contents(Config::get('oxidized.url') . '/node/show/' . $oxidized_hostname . '?format=json'), true);
$node_info = json_decode((new \App\ApiClients\Oxidized())->getContent('/node/show/' . $oxidized_hostname . '?format=json'), true);
}
}
if (Config::get('oxidized.features.versioning') === true) { // fetch a list of versions
$config_versions = json_decode(file_get_contents(Config::get('oxidized.url') . '/node/version?node_full=' . (isset($node_info['full_name']) ? $node_info['full_name'] : $oxidized_hostname) . '&format=json'), true);
$config_versions = json_decode((new \App\ApiClients\Oxidized())->getContent('/node/version?node_full=' . (isset($node_info['full_name']) ? $node_info['full_name'] : $oxidized_hostname) . '&format=json'), true);
}
$config_total = 1;
@@ -193,19 +192,19 @@ if (Auth::user()->hasGlobalAdmin()) {
}
if (isset($previous_config)) {
$url = Config::get('oxidized.url') . '/node/version/diffs?node=' . $oxidized_hostname;
$uri = '/node/version/diffs?node=' . $oxidized_hostname;
if (! empty($node_info['group'])) {
$url .= '&group=' . $node_info['group'];
$uri .= '&group=' . $node_info['group'];
}
$url .= '&oid=' . urlencode($current_config['oid']) . '&date=' . urlencode($current_config['date']) . '&num=' . urlencode($current_config['version']) . '&oid2=' . $previous_config['oid'] . '&format=text';
$uri .= '&oid=' . urlencode($current_config['oid']) . '&date=' . urlencode($current_config['date']) . '&num=' . urlencode($current_config['version']) . '&oid2=' . $previous_config['oid'] . '&format=text';
$text = file_get_contents($url); // fetch diff
$text = (new \App\ApiClients\Oxidized())->getContent($uri); // fetch diff
} else {
// fetch current_version
$text = file_get_contents(Config::get('oxidized.url') . '/node/version/view?node=' . $oxidized_hostname . (! empty($node_info['group']) ? '&group=' . $node_info['group'] : '') . '&oid=' . urlencode($current_config['oid']) . '&date=' . urlencode($current_config['date']) . '&num=' . urlencode($current_config['version']) . '&format=text');
$text = (new \App\ApiClients\Oxidized())->getContent('/node/version/view?node=' . $oxidized_hostname . (! empty($node_info['group']) ? '&group=' . $node_info['group'] : '') . '&oid=' . urlencode($current_config['oid']) . '&date=' . urlencode($current_config['date']) . '&num=' . urlencode($current_config['version']) . '&format=text');
}
} else { // just fetch the only version
$text = file_get_contents(Config::get('oxidized.url') . '/node/fetch/' . (! empty($node_info['group']) ? $node_info['group'] . '/' : '') . $oxidized_hostname);
$text = (new \App\ApiClients\Oxidized())->getContent('/node/fetch/' . (! empty($node_info['group']) ? $node_info['group'] . '/' : '') . $oxidized_hostname);
}
if (is_array($node_info) || $config_total > 1) {