From 49adff3895da01866a2d65376728c64b4e40f530 Mon Sep 17 00:00:00 2001 From: laf Date: Sun, 17 Jan 2016 23:59:51 +0000 Subject: [PATCH 1/3] Added the lookup of IP if device hostname is a hostname --- html/includes/dev-overview-data.inc.php | 7 +++++++ includes/common.php | 14 ++++++++++++++ includes/functions.php | 25 +++++++++++++++++++++++++ includes/polling/functions.inc.php | 8 ++++++++ sql-schema/091.sql | 1 + 5 files changed, 55 insertions(+) create mode 100644 sql-schema/091.sql diff --git a/html/includes/dev-overview-data.inc.php b/html/includes/dev-overview-data.inc.php index 6ff78d43c..2e7ed141e 100644 --- a/html/includes/dev-overview-data.inc.php +++ b/html/includes/dev-overview-data.inc.php @@ -30,6 +30,13 @@ echo ' '.$device['sysName'].' '; +if ($ip = inet6_ntop($device['ip'])) { + echo ' + Resolved IP + '.$ip.' + '; +} + if ($device['hardware']) { echo ' Hardware diff --git a/includes/common.php b/includes/common.php index f80583a83..e24e83b86 100644 --- a/includes/common.php +++ b/includes/common.php @@ -1040,3 +1040,17 @@ function version_info($remote=true) { return $output; }//end version_info() + +/** +* Convert a MySQL binary v4 (4-byte) or v6 (16-byte) IP address to a printable string. +* @param string $ip A binary string containing an IP address, as returned from MySQL's INET6_ATON function +* @return string Empty if not valid. +*/ +// Fuction is from http://uk3.php.net/manual/en/function.inet-ntop.php +function inet6_ntop($ip) { + $l = strlen($ip); + if ($l == 4 or $l == 16) { + return inet_ntop(pack('A' . $l, $ip)); + } + return ''; +} diff --git a/includes/functions.php b/includes/functions.php index c9d842b9a..63fd646b7 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1320,3 +1320,28 @@ function oxidized_reload_nodes() { curl_close($ch); } } + +/** + * Perform DNS lookup + * + * @param array $device Device array from database + * @param string $type The type of record to lookup + * + * @return string ip + * +**/ +function dnslookup($device,$type=false) { + if (empty($type)) { + // We are going to use the transport to work out the record type + if ($device['transport'] == 'udp6' || $device['transport'] == 'tcp6') { + $type = DNS_AAAA; + $return = 'ipv6'; + } + else { + $type = DNS_A; + $return = 'ip'; + } + } + $record = dns_get_record($device['hostname'],$type); + return $record[0][$return]; +}//end dnslookup diff --git a/includes/polling/functions.inc.php b/includes/polling/functions.inc.php index 266c3d4a2..e6435a390 100644 --- a/includes/polling/functions.inc.php +++ b/includes/polling/functions.inc.php @@ -138,6 +138,14 @@ function poll_device($device, $options) { $device_start = microtime(true); // Start counting device poll time echo $device['hostname'].' '.$device['device_id'].' '.$device['os'].' '; + $ip = dnslookup($device); + + if (!empty($ip) && $ip != inet6_ntop($device['ip'])) { + log_event('Device IP changed to '.$ip, $device, 'system'); + $db_ip = inet_pton($ip); + dbUpdate(array('ip' => $db_ip), 'devices', 'device_id=?', array($device['device_id'])); + } + if ($config['os'][$device['os']]['group']) { $device['os_group'] = $config['os'][$device['os']]['group']; echo '('.$device['os_group'].')'; diff --git a/sql-schema/091.sql b/sql-schema/091.sql new file mode 100644 index 000000000..5400018f1 --- /dev/null +++ b/sql-schema/091.sql @@ -0,0 +1 @@ +ALTER TABLE `devices` ADD `ip` VARBINARY( 16 ) NOT NULL AFTER `sysName` ; From cc25e87bcd8ac022358d900a6660405741864204 Mon Sep 17 00:00:00 2001 From: laf Date: Mon, 18 Jan 2016 00:04:30 +0000 Subject: [PATCH 2/3] Added check for hostname --- includes/functions.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/includes/functions.php b/includes/functions.php index 63fd646b7..c9a9a2709 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1331,6 +1331,9 @@ function oxidized_reload_nodes() { * **/ function dnslookup($device,$type=false) { + if (filter_var($device['hostname'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) == true || filter_var($device['hostname'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) == truee) { + return ''; + } if (empty($type)) { // We are going to use the transport to work out the record type if ($device['transport'] == 'udp6' || $device['transport'] == 'tcp6') { From 47ff913baa65561b2a728fdb9ba86db010838699 Mon Sep 17 00:00:00 2001 From: laf Date: Mon, 18 Jan 2016 12:54:25 +0000 Subject: [PATCH 3/3] Updated for Scrut fix --- includes/functions.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/includes/functions.php b/includes/functions.php index c9a9a2709..ebd7008b3 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1330,7 +1330,7 @@ function oxidized_reload_nodes() { * @return string ip * **/ -function dnslookup($device,$type=false) { +function dnslookup($device,$type=false,$return=false) { if (filter_var($device['hostname'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) == true || filter_var($device['hostname'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) == truee) { return ''; } @@ -1345,6 +1345,9 @@ function dnslookup($device,$type=false) { $return = 'ip'; } } + if (empty($return)) { + return ''; + } $record = dns_get_record($device['hostname'],$type); return $record[0][$return]; }//end dnslookup