From 5c803277db573c46adfc064ed94a6b52edffcee0 Mon Sep 17 00:00:00 2001 From: laf Date: Thu, 22 Oct 2015 21:10:33 +0000 Subject: [PATCH 1/2] De-dupe checks for hostname when adding hosts --- includes/functions.php | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 4682c994f..2f8a17067 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -243,7 +243,7 @@ function addHost($host, $snmpver, $port = '161', $transport = 'udp', $quiet = '0 list($hostshort) = explode(".", $host); // Test Database Exists - if (dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = ?", array($host)) == '0') { + if (dup_host_check($host) === false) { if ($config['addhost_alwayscheckip'] === TRUE) { $ip = gethostbyname($host); } else { @@ -583,13 +583,17 @@ function createHost($host, $community = NULL, $snmpver, $port = 161, $transport if ($device['os']) { - $device_id = dbInsert($device, 'devices'); - - if ($device_id) { - return($device_id); + if (dup_host_check($host) === false) { + $device_id = dbInsert($device, 'devices'); + if ($device_id) { + return($device_id); + } + else { + return false; + } } else { - return FALSE; + return false; } } else { @@ -1256,3 +1260,21 @@ function snmpTransportToAddressFamily($transport) { return AF_INET; } } + +/** + * Checks if the $hostname provided exists in the DB already + * + * @param string $hostname The hostname to check for + * + * @return bool true if hostname already exists + * false if hostname doesn't exist +**/ +function dup_host_check($hostname) { + $count = dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = ?", array($hostname)); + if ($count > 0) { + return true; + } + else { + return false; + } +} From 7e9dcc716b1d23ca28133286758221a44812eea5 Mon Sep 17 00:00:00 2001 From: laf Date: Sun, 25 Oct 2015 21:23:06 +0000 Subject: [PATCH 2/2] Updated function name for blahdeblah --- includes/functions.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 2f8a17067..11ff1531f 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -243,7 +243,7 @@ function addHost($host, $snmpver, $port = '161', $transport = 'udp', $quiet = '0 list($hostshort) = explode(".", $host); // Test Database Exists - if (dup_host_check($host) === false) { + if (host_exists($host) === false) { if ($config['addhost_alwayscheckip'] === TRUE) { $ip = gethostbyname($host); } else { @@ -583,7 +583,7 @@ function createHost($host, $community = NULL, $snmpver, $port = 161, $transport if ($device['os']) { - if (dup_host_check($host) === false) { + if (host_exists($host) === false) { $device_id = dbInsert($device, 'devices'); if ($device_id) { return($device_id); @@ -1269,7 +1269,7 @@ function snmpTransportToAddressFamily($transport) { * @return bool true if hostname already exists * false if hostname doesn't exist **/ -function dup_host_check($hostname) { +function host_exists($hostname) { $count = dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = ?", array($hostname)); if ($count > 0) { return true;