mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
corrected display of minigraph when using sysName as hostname (#8842)
* corrected display of minigraph when using sysName as hostname * Check to see if its an IP or hostname. Make sure all 3 scenarios work. * removed test rrd symlink * removed test rrd symlink * reverted old change * Improve and add tests.
This commit is contained in:
committed by
Tony Murray
parent
01fc66436e
commit
528b91a056
+13
-11
@@ -1188,28 +1188,30 @@ function inet6_ntop($ip)
|
||||
|
||||
/**
|
||||
* If hostname is an ip, use return sysName
|
||||
* @param array $device
|
||||
* @param array $device (uses hostname and sysName fields)
|
||||
* @param string $hostname
|
||||
* @return string
|
||||
**/
|
||||
function format_hostname($device, $hostname = '')
|
||||
*/
|
||||
function format_hostname($device, $hostname = null)
|
||||
{
|
||||
global $config;
|
||||
if (empty($hostname)) {
|
||||
$hostname = $device['hostname'];
|
||||
}
|
||||
if ($config['force_ip_to_sysname'] === true && !empty($device['sysName'])) {
|
||||
if (filter_var($hostname, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) == true || filter_var($hostname, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) == true) {
|
||||
$hostname = $device['sysName'];
|
||||
|
||||
if (Config::get('force_hostname_to_sysname') && !empty($device['sysName'])) {
|
||||
if (is_valid_hostname($hostname) && !IP::isValid($hostname)) {
|
||||
return $device['sysName'];
|
||||
}
|
||||
}
|
||||
if ($config['force_hostname_to_sysname'] === true && !empty($device['sysName'])) {
|
||||
if (filter_var($hostname, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) == false && filter_var($hostname, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) == false) {
|
||||
$hostname = $device['sysName'];
|
||||
|
||||
if (Config::get('force_ip_to_sysname') && !empty($device['sysName'])) {
|
||||
if (IP::isValid($hostname)) {
|
||||
return $device['sysName'];
|
||||
}
|
||||
}
|
||||
|
||||
return $hostname;
|
||||
}//end format_hostname
|
||||
}
|
||||
|
||||
/**
|
||||
* Return valid port association modes
|
||||
|
||||
@@ -25,6 +25,8 @@
|
||||
|
||||
namespace LibreNMS\Tests;
|
||||
|
||||
use LibreNMS\Config;
|
||||
|
||||
class CommonFunctionsTest extends TestCase
|
||||
{
|
||||
public function testStrContains()
|
||||
@@ -168,4 +170,56 @@ class CommonFunctionsTest extends TestCase
|
||||
dbRollbackTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
public function testFormatHostname()
|
||||
{
|
||||
$device_dns = [
|
||||
'hostname' => 'test.librenms.org',
|
||||
'sysName' => 'Testing DNS'
|
||||
];
|
||||
$device_ip = [
|
||||
'hostname' => '192.168.1.2',
|
||||
'sysName' => 'Testing IP'
|
||||
];
|
||||
|
||||
// both false
|
||||
Config::set('force_ip_to_sysname', false);
|
||||
Config::set('force_hostname_to_sysname', false);
|
||||
$this->assertEquals('test.librenms.org', format_hostname($device_dns));
|
||||
$this->assertEquals('Not DNS', format_hostname($device_dns, 'Not DNS'));
|
||||
$this->assertEquals('192.168.5.5', format_hostname($device_dns, '192.168.5.5'));
|
||||
$this->assertEquals('192.168.1.2', format_hostname($device_ip));
|
||||
$this->assertEquals('hostname.like', format_hostname($device_ip, 'hostname.like'));
|
||||
$this->assertEquals('10.10.10.10', format_hostname($device_ip, '10.10.10.10'));
|
||||
|
||||
// ip to sysname
|
||||
Config::set('force_ip_to_sysname', true);
|
||||
Config::set('force_hostname_to_sysname', false);
|
||||
$this->assertEquals('test.librenms.org', format_hostname($device_dns));
|
||||
$this->assertEquals('Not DNS', format_hostname($device_dns, 'Not DNS'));
|
||||
$this->assertEquals('Testing DNS', format_hostname($device_dns, '192.168.5.5'));
|
||||
$this->assertEquals('Testing IP', format_hostname($device_ip));
|
||||
$this->assertEquals('hostname.like', format_hostname($device_ip, 'hostname.like'));
|
||||
$this->assertEquals('Testing IP', format_hostname($device_ip, '10.10.10.10'));
|
||||
|
||||
// dns to sysname
|
||||
Config::set('force_ip_to_sysname', false);
|
||||
Config::set('force_hostname_to_sysname', true);
|
||||
$this->assertEquals('Testing DNS', format_hostname($device_dns));
|
||||
$this->assertEquals('Not DNS', format_hostname($device_dns, 'Not DNS'));
|
||||
$this->assertEquals('192.168.5.5', format_hostname($device_dns, '192.168.5.5'));
|
||||
$this->assertEquals('192.168.1.2', format_hostname($device_ip));
|
||||
$this->assertEquals('Testing IP', format_hostname($device_ip, 'hostname.like'));
|
||||
$this->assertEquals('10.10.10.10', format_hostname($device_ip, '10.10.10.10'));
|
||||
|
||||
// both true
|
||||
Config::set('force_ip_to_sysname', true);
|
||||
Config::set('force_hostname_to_sysname', true);
|
||||
$this->assertEquals('Testing DNS', format_hostname($device_dns));
|
||||
$this->assertEquals('Not DNS', format_hostname($device_dns, 'Not DNS'));
|
||||
$this->assertEquals('Testing DNS', format_hostname($device_dns, '192.168.5.5'));
|
||||
$this->assertEquals('Testing IP', format_hostname($device_ip));
|
||||
$this->assertEquals('Testing IP', format_hostname($device_ip, 'hostname.like'));
|
||||
$this->assertEquals('Testing IP', format_hostname($device_ip, '10.10.10.10'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user