Added the lookup of IP if device hostname is a hostname

This commit is contained in:
laf
2016-01-17 23:59:51 +00:00
parent e380cc5100
commit 49adff3895
5 changed files with 55 additions and 0 deletions

View File

@@ -30,6 +30,13 @@ echo '<tr>
<td>'.$device['sysName'].' </td> <td>'.$device['sysName'].' </td>
</tr>'; </tr>';
if ($ip = inet6_ntop($device['ip'])) {
echo '<tr>
<td>Resolved IP</td>
<td>'.$ip.'</td>
</tr>';
}
if ($device['hardware']) { if ($device['hardware']) {
echo '<tr> echo '<tr>
<td>Hardware</td> <td>Hardware</td>

View File

@@ -1040,3 +1040,17 @@ function version_info($remote=true) {
return $output; return $output;
}//end version_info() }//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 '';
}

View File

@@ -1320,3 +1320,28 @@ function oxidized_reload_nodes() {
curl_close($ch); 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

View File

@@ -138,6 +138,14 @@ function poll_device($device, $options) {
$device_start = microtime(true); $device_start = microtime(true);
// Start counting device poll time // Start counting device poll time
echo $device['hostname'].' '.$device['device_id'].' '.$device['os'].' '; 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']) { if ($config['os'][$device['os']]['group']) {
$device['os_group'] = $config['os'][$device['os']]['group']; $device['os_group'] = $config['os'][$device['os']]['group'];
echo '('.$device['os_group'].')'; echo '('.$device['os_group'].')';

1
sql-schema/091.sql Normal file
View File

@@ -0,0 +1 @@
ALTER TABLE `devices` ADD `ip` VARBINARY( 16 ) NOT NULL AFTER `sysName` ;