function discover_new_device($hostname, $source = 'xdp')
{
    global $config, $debug;
    if ($config['autodiscovery'][$source]) {
        echo "Discovering new host {$hostname}\n";
        if (!empty($config['mydomain']) && isDomainResolves($hostname . "." . $config['mydomain'])) {
            if ($debug) {
                echo "appending " . $config['mydomain'] . "!\n";
            }
            $dst_host = $hostname . "." . $config['mydomain'];
        } else {
            $dst_host = $hostname;
        }
        $ip = gethostbyname($dst_host);
        if ($debug) {
            echo "resolving {$dst_host} to {$ip}\n";
        }
        if (match_network($config['autodiscovery']['ip_nets'], $ip)) {
            if ($debug) {
                echo "found {$ip} inside configured nets, adding!\n";
            }
            $remote_device_id = addHost($dst_host);
            if ($remote_device_id) {
                $remote_device = device_by_id_cache($remote_device_id, 1);
                array_push($GLOBALS['devices'], $remote_device);
                return $remote_device_id;
            }
        }
    } else {
        if ($debug) {
            echo "{$source} autodiscovery disabled";
        }
        return FALSE;
    }
}
Exemple #2
0
function discover_new_device($hostname, $device = '', $method = '', $interface = '')
{
    global $config;
    if (!empty($config['mydomain']) && isDomainResolves($hostname . '.' . $config['mydomain'])) {
        $dst_host = $hostname . '.' . $config['mydomain'];
    } else {
        $dst_host = $hostname;
    }
    d_echo("discovering {$dst_host}\n");
    $ip = gethostbyname($dst_host);
    if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false && filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
        // $ip isn't a valid IP so it must be a name.
        if ($ip == $dst_host) {
            d_echo("name lookup of {$dst_host} failed\n");
            log_event("{$method} discovery of " . $dst_host . " failed - Check name lookup", $device['device_id'], 'discovery');
            return false;
        }
    } elseif (filter_var($dst_host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === true || filter_var($dst_host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === true) {
        // gethostbyname returned a valid $ip, was $dst_host an IP?
        if ($config['discovery_by_ip'] === false) {
            d_echo('Discovery by IP disabled, skipping ' . $dst_host);
            log_event("{$method} discovery of " . $dst_host . " failed - Discovery by IP disabled", $device['device_id'], 'discovery');
            return false;
        }
    }
    d_echo("ip lookup result: {$ip}\n");
    $dst_host = rtrim($dst_host, '.');
    // remove trailing dot
    if (match_network($config['autodiscovery']['nets-exclude'], $ip)) {
        d_echo("{$ip} in an excluded network - skipping\n");
        return false;
    }
    if (match_network($config['nets'], $ip)) {
        try {
            $remote_device_id = addHost($dst_host, '', '161', 'udp', $config['distributed_poller_group']);
            $remote_device = device_by_id_cache($remote_device_id, 1);
            echo '+[' . $remote_device['hostname'] . '(' . $remote_device['device_id'] . ')]';
            discover_device($remote_device);
            device_by_id_cache($remote_device_id, 1);
            if ($remote_device_id && is_array($device) && !empty($method)) {
                $extra_log = '';
                $int = ifNameDescr($interface);
                if (is_array($int)) {
                    $extra_log = ' (port ' . $int['label'] . ') ';
                }
                log_event('Device ' . $remote_device['hostname'] . " ({$ip}) {$extra_log} autodiscovered through {$method} on " . $device['hostname'], $remote_device_id, 'discovery');
            } else {
                log_event("{$method} discovery of " . $remote_device['hostname'] . " ({$ip}) failed - Check ping and SNMP access", $device['device_id'], 'discovery');
            }
            return $remote_device_id;
        } catch (HostExistsException $e) {
            // already have this device
        } catch (Exception $e) {
            log_event("{$method} discovery of " . $dst_host . " ({$ip}) failed - " . $e->getMessage());
        }
    } else {
        d_echo("{$ip} not in a matched network - skipping\n");
    }
    //end if
}
Exemple #3
0
function discover_new_device($hostname, $device = '', $method = '', $interface = '')
{
    global $config, $debug;
    if (!empty($config['mydomain']) && isDomainResolves($hostname . '.' . $config['mydomain'])) {
        $dst_host = $hostname . '.' . $config['mydomain'];
    } else {
        $dst_host = $hostname;
    }
    if ($debug) {
        echo "discovering {$dst_host}\n";
    }
    $ip = gethostbyname($dst_host);
    if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false && filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
        // $ip isn't a valid IP so it must be a name.
        if ($ip == $dst_host) {
            if ($debug) {
                echo "name lookup of {$dst_host} failed\n";
            }
            return false;
        }
    }
    if ($debug) {
        echo "ip lookup result: {$ip}\n";
    }
    $dst_host = rtrim($dst_host, '.');
    // remove trailing dot
    if (match_network($config['autodiscovery']['nets-exclude'], $ip)) {
        if ($debug) {
            echo "{$ip} in an excluded network - skipping\n";
        }
        return false;
    }
    if (match_network($config['nets'], $ip)) {
        $remote_device_id = addHost($dst_host, '', '161', 'udp', '0', $config['distributed_poller_group']);
        if ($remote_device_id) {
            $remote_device = device_by_id_cache($remote_device_id, 1);
            echo '+[' . $remote_device['hostname'] . '(' . $remote_device['device_id'] . ')]';
            discover_device($remote_device);
            device_by_id_cache($remote_device_id, 1);
            if ($remote_device_id && is_array($device) && !empty($method)) {
                $extra_log = '';
                $int = ifNameDescr($interface);
                if (is_array($int)) {
                    $extra_log = ' (port ' . $int['label'] . ') ';
                }
                log_event('Device $' . $remote_device['hostname'] . " ({$ip}) {$extra_log} autodiscovered through {$method} on " . $device['hostname'], $remote_device_id, 'system');
            } else {
                log_event("{$method} discovery of " . $remote_device['hostname'] . " ({$ip}) failed - check ping and SNMP access", $device['device_id'], 'system');
            }
            return $remote_device_id;
        }
    } else {
        if ($debug) {
            echo "{$ip} not in a matched network - skipping\n";
        }
    }
    //end if
}
function discover_new_device($hostname)
{
    global $config, $debug;
    if ($config['autodiscovery']['xdp']) {
        if (isDomainResolves($hostname . "." . $config['mydomain'])) {
            $dst_host = $hostname . "." . $config['mydomain'];
        } else {
            $dst_host = $hostname;
        }
        if ($debug) {
            echo "discovering {$dst_host}\n";
        }
        $ip = gethostbyname($dst_host);
        if ($ip == $dst_host) {
            if ($debug) {
                echo "name lookup of {$dst_host} failed\n";
            }
            return FALSE;
        } else {
            if ($debug) {
                echo "ip lookup result: {$ip}\n";
            }
        }
        $dst_host = rtrim($dst_host, '.');
        // remove trailing dot
        if (match_network($config['autodiscovery']['nets-exclude'], $ip)) {
            if ($debug) {
                echo "{$ip} in an excluded network - skipping\n";
            }
            return FALSE;
        }
        if (match_network($config['nets'], $ip)) {
            $remote_device_id = addHost($dst_host);
            if ($remote_device_id) {
                $remote_device = device_by_id_cache($remote_device_id, 1);
                echo "+[" . $remote_device['hostname'] . "(" . $remote_device['device_id'] . ")]";
                discover_device($remote_device);
                $remote_device = device_by_id_cache($remote_device_id, 1);
                return $remote_device_id;
            }
        } else {
            if ($debug) {
                echo "{$ip} not in a matched network - skipping\n";
            }
        }
    } else {
        if ($debug) {
            echo "autodiscovery disabled - skipping\n";
        }
        return FALSE;
    }
}
function discover_new_device($hostname)
{
    global $config, $debug;
    if (!empty($config['mydomain']) && isDomainResolves($hostname . "." . $config['mydomain'])) {
        $dst_host = $hostname . "." . $config['mydomain'];
    } else {
        $dst_host = $hostname;
    }
    if ($debug) {
        echo "discovering {$dst_host}\n";
    }
    $ip = gethostbyname($dst_host);
    if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === FALSE && filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === FALSE) {
        // $ip isn't a valid IP so it must be a name.
        if ($ip == $dst_host) {
            if ($debug) {
                echo "name lookup of {$dst_host} failed\n";
            }
            return FALSE;
        }
    }
    if ($debug) {
        echo "ip lookup result: {$ip}\n";
    }
    $dst_host = rtrim($dst_host, '.');
    // remove trailing dot
    if (match_network($config['autodiscovery']['nets-exclude'], $ip)) {
        if ($debug) {
            echo "{$ip} in an excluded network - skipping\n";
        }
        return FALSE;
    }
    if (match_network($config['nets'], $ip)) {
        $remote_device_id = addHost($dst_host, '', '161', 'udp', '0', $config['distributed_poller_group']);
        if ($remote_device_id) {
            $remote_device = device_by_id_cache($remote_device_id, 1);
            echo "+[" . $remote_device['hostname'] . "(" . $remote_device['device_id'] . ")]";
            discover_device($remote_device);
            device_by_id_cache($remote_device_id, 1);
            return $remote_device_id;
        }
    } else {
        if ($debug) {
            echo "{$ip} not in a matched network - skipping\n";
        }
    }
}
function discover_new_device($hostname, $source = 'xdp', $protocol = NULL, $device = NULL, $port = NULL)
{
    global $config, $debug;
    # FIXME remodel function a bit like the one above? refactor so they share some parts?
    if ($config['autodiscovery'][$source]) {
        echo "Discovering new host {$hostname}\n";
        if (!empty($config['mydomain']) && isDomainResolves($hostname . "." . $config['mydomain'])) {
            if ($debug) {
                echo "appending " . $config['mydomain'] . "!\n";
            }
            $dst_host = $hostname . "." . $config['mydomain'];
        } else {
            $dst_host = $hostname;
        }
        $ip = gethostbyname($dst_host);
        if ($debug) {
            echo "resolving {$dst_host} to {$ip}\n";
        }
        if (match_network($config['autodiscovery']['ip_nets'], $ip)) {
            if ($debug) {
                echo "found {$ip} inside configured nets, adding!\n";
            }
            $remote_device_id = add_device($dst_host);
            if ($remote_device_id) {
                $remote_device = device_by_id_cache($remote_device_id, 1);
                if (!$protocol) {
                    $protocol = strtoupper($source);
                }
                if ($port) {
                    humanize_port($port);
                    log_event("Device autodiscovered through {$protocol} on " . $device['hostname'] . " (port " . $port['label'] . ")", $remote_device_id, 'interface', $port['port_id']);
                } else {
                    log_event("Device autodiscovered through {$protocol} on " . $device['hostname'], $remote_device_id);
                }
                array_push($GLOBALS['devices'], $remote_device);
                return $remote_device_id;
            }
        }
    } else {
        if ($debug) {
            echo "{$source} autodiscovery disabled";
        }
        return FALSE;
    }
}
function discover_new_device($hostname, $source = 'xdp', $protocol = NULL, $device = NULL, $snmp_port = 161)
{
    global $config;
    $source = strtolower($source);
    // Check if source is enabled for autodiscovery
    if ($config['autodiscovery'][$source]) {
        $flags = OBS_DNS_ALL;
        if (!$protocol) {
            $protocol = strtoupper($source);
        }
        print_cli_data("Try discovering host", "{$hostname} through {$protocol}", 3);
        // By first detect hostname is IP or domain name (IPv4/6 == 4/6, hostname == FALSE)
        $ip_version = get_ip_version($hostname);
        if ($ip_version) {
            // Hostname is IPv4/IPv6
            $use_ip = TRUE;
            $ip = $hostname;
        } else {
            $use_ip = FALSE;
            // Add "mydomain" configuration if this resolves, converts switch1 -> switch1.mydomain.com
            if (!empty($config['mydomain']) && isDomainResolves($hostname . '.' . $config['mydomain'], $flags)) {
                $hostname .= '.' . $config['mydomain'];
            }
            // Determine v4 vs v6
            $ip = gethostbyname6($hostname, $flags);
            if ($ip) {
                $ip_version = get_ip_version($ip);
                print_debug("Host {$hostname} resolved as {$ip}");
            } else {
                // No DNS records
                print_debug("Host {$hostname} not resolved, autodiscovery fails.");
                return FALSE;
            }
        }
        if ($ip_version == 6) {
            $flags = $flags ^ OBS_DNS_A;
            // Exclude IPv4
        }
        if (isset($config['autodiscovery']['ping_skip']) && $config['autodiscovery']['ping_skip']) {
            $flags = $flags | OBS_PING_SKIP;
            // Add skip pings flag
        }
        if (match_network($ip, $config['autodiscovery']['ip_nets'])) {
            print_debug("Host {$hostname} ({$ip}) founded inside configured nets, trying to add:");
            // By first check if pingable
            $pingable = isPingable($ip, $flags);
            if (!$pingable && (isset($config['autodiscovery']['ping_skip']) && $config['autodiscovery']['ping_skip'])) {
                $flags = $flags | OBS_PING_SKIP;
                // Add skip pings flag if allowed in config
                $pingable = TRUE;
            }
            if ($pingable) {
                // Check if device duplicated by IP
                $ip = $ip_version == 4 ? $ip : Net_IPv6::uncompress($ip, TRUE);
                $db = dbFetchRow('SELECT D.`hostname` FROM ipv' . $ip_version . '_addresses AS A
                         LEFT JOIN `ports`   AS P ON A.`port_id`   = P.`port_id`
                         LEFT JOIN `devices` AS D ON D.`device_id` = P.`device_id`
                         WHERE D.`disabled` = 0 AND A.`ipv' . $ip_version . '_address` = ?', array($ip));
                if ($db) {
                    print_debug('Already have device ' . $db['hostname'] . " with IP {$ip}");
                    return FALSE;
                }
                // Detect snmp transport, net-snmp needs udp6 for ipv6
                $snmp_transport = $ip_version == 4 ? 'udp' : 'udp6';
                $new_device = detect_device_snmpauth($ip, $snmp_port, $snmp_transport);
                if ($new_device) {
                    if ($use_ip) {
                        // Detect FQDN hostname
                        // by sysName
                        $snmphost = snmp_get($new_device, 'sysName.0', '-Oqv', 'SNMPv2-MIB');
                        if ($snmphost) {
                            $snmp_ip = gethostbyname6($snmphost, $flags);
                        }
                        if ($snmp_ip == $ip) {
                            $hostname = $snmphost;
                        } else {
                            // by PTR
                            $ptr = gethostbyaddr6($ip);
                            if ($ptr) {
                                $ptr_ip = gethostbyname6($ptr, $flags);
                            }
                            if ($ptr && $ptr_ip == $ip) {
                                $hostname = $ptr;
                            } else {
                                if ($config['autodiscovery']['require_hostname']) {
                                    print_debug("Device IP {$ip} does not seem to have FQDN.");
                                    return FALSE;
                                } else {
                                    $hostname = $ip_version == 4 ? $ip : Net_IPv6::compress($hostname, TRUE);
                                    // Always use compressed IPv6 name
                                }
                            }
                        }
                        print_debug("Device IP {$ip} linked to FQDN name: {$hostname}");
                    }
                    $new_device['hostname'] = $hostname;
                    if (!check_device_duplicated($new_device)) {
                        $snmp_v3 = array();
                        if ($new_device['snmp_version'] === 'v3') {
                            $snmp_v3['snmp_authlevel'] = $new_device['snmp_authlevel'];
                            $snmp_v3['snmp_authname'] = $new_device['snmp_authname'];
                            $snmp_v3['snmp_authpass'] = $new_device['snmp_authpass'];
                            $snmp_v3['snmp_authalgo'] = $new_device['snmp_authalgo'];
                            $snmp_v3['snmp_cryptopass'] = $new_device['snmp_cryptopass'];
                            $snmp_v3['snmp_cryptoalgo'] = $new_device['snmp_cryptoalgo'];
                        }
                        $remote_device_id = createHost($new_device['hostname'], $new_device['snmp_community'], $new_device['snmp_version'], $new_device['snmp_port'], $new_device['snmp_transport'], $snmp_v3);
                        if ($remote_device_id) {
                            if (is_flag_set(OBS_PING_SKIP, $flags)) {
                                set_entity_attrib('device', $remote_device_id, 'ping_skip', 1);
                            }
                            $remote_device = device_by_id_cache($remote_device_id, 1);
                            if ($port) {
                                humanize_port($port);
                                log_event("Device autodiscovered through {$protocol} on " . $device['hostname'] . " (port " . $port['port_label'] . ")", $remote_device_id, 'port', $port['port_id']);
                            } else {
                                log_event("Device autodiscovered through {$protocol} on " . $device['hostname'], $remote_device_id, $protocol);
                            }
                            //array_push($GLOBALS['devices'], $remote_device); // createHost() already puth this
                            return $remote_device_id;
                        }
                    }
                }
            }
        } else {
            print_debug("IP {$ip} ({$hostname}) not permitted inside \$config['autodiscovery']['ip_nets'] in config.php");
        }
        print_debug('Autodiscovery for host ' . $hostname . ' failed.');
    } else {
        print_debug('Autodiscovery for protocol ' . $protocol . ' disabled.');
    }
    return FALSE;
}
function addHost($host, $community, $snmpver, $port = 161)
{
    global $config;
    list($hostshort) = explode(".", $host);
    if (isDomainResolves($host)) {
        if (isPingable($host)) {
            if (mysql_result(mysql_query("SELECT COUNT(*) FROM `devices` WHERE `hostname` = '{$host}'"), 0) == '0') {
                $snmphost = shell_exec($config['snmpget'] . " -m SNMPv2-MIB -Oqv -{$snmpver} -c {$community} {$host}:{$port} sysName.0");
                if ($snmphost == $host || ($hostshort = $host)) {
                    createHost($host, $community, $snmpver, $port);
                } else {
                    echo "Given hostname does not match SNMP-read hostname!\n";
                }
            } else {
                echo "Already got host {$host}\n";
            }
        } else {
            echo "Could not ping {$host}\n";
        }
    } else {
        echo "Could not resolve {$host}\n";
    }
}
Exemple #9
0
 $host = strtolower($argv[1]);
 $community = $argv[2];
 $snmpver = strtolower($argv[3]);
 if (is_numeric($argv[4])) {
     $port = $argv[4];
 } else {
     $port = 161;
 }
 if (!$snmpver) {
     $snmpver = "v2c";
 }
 if (!$community) {
     $community = $config['community'];
 }
 list($hostshort) = explode(".", $host);
 if (isDomainResolves($argv[1])) {
     if (isPingable($argv[1])) {
         if (mysql_result(mysql_query("SELECT COUNT(*) FROM `devices` WHERE `hostname` = '" . mres($host) . "'"), 0) == '0') {
             $snmphost = trim(str_replace("\"", "", shell_exec($config['snmpget'] . " -m SNMPv2-MIB -Oqv -{$snmpver} -c {$community} {$host}:{$port} sysName.0")));
             if ($snmphost && ($snmphost == $host || ($hostshort = $host))) {
                 $return = createHost($host, $community, $snmpver, $port);
                 if ($return) {
                     echo $return . "\n";
                 } else {
                     echo "Adding {$host} failed\n";
                 }
             } else {
                 echo "Given hostname does not match SNMP-read hostname!\n";
             }
         } else {
             echo "Already got host {$host}\n";
Exemple #10
0
function discover_new_device($hostname, $source = 'xdp', $protocol = NULL, $device = NULL, $port = 161)
{
    global $config;
    $source = strtolower($source);
    if ($config['autodiscovery'][$source]) {
        if (!$protocol) {
            $protocol = strtoupper($source);
        }
        print_message("Discovering new host {$hostname} through {$protocol}");
        // By first detect hostname is IP or domain name (IPv4/6 == 4/6, hostname == FALSE)
        $ip_version = get_ip_version($hostname);
        if ($ip_version) {
            // Hostname is IPv4/IPv6
            $use_ip = TRUE;
        } else {
            $use_ip = FALSE;
            if (!empty($config['mydomain']) && isDomainResolves($hostname . '.' . $config['mydomain'])) {
                $hostname .= '.' . $config['mydomain'];
            }
            $ip = gethostbyname6($hostname);
            if ($ip) {
                $ip_version = get_ip_version($ip);
                print_debug("Host {$hostname} resolved as {$ip}");
            } else {
                // No DNS records
                print_debug("Host {$hostname} not resolved, autodiscovery fails.");
                return FALSE;
            }
        }
        if (match_network($ip, $config['autodiscovery']['ip_nets'])) {
            print_debug("Host {$hostname} ({$ip}) founded inside configured nets, try to adding:");
            if (isPingable($ip)) {
                // Check if device duplicated by IP
                $ip = $ip_version == 4 ? $hostname : Net_IPv6::uncompress($hostname, TRUE);
                $db = dbFetchRow('SELECT D.`hostname` FROM ipv' . $ip_version . '_addresses AS A
                         LEFT JOIN `ports`   AS P ON A.`port_id`   = P.`port_id`
                         LEFT JOIN `devices` AS D ON D.`device_id` = P.`device_id`
                         WHERE D.`disabled` = 0 AND A.`ipv' . $ip_version . '_address` = ?', array($ip));
                if ($db) {
                    print_debug('Already have device ' . $db['hostname'] . " with {$ip}");
                    return FALSE;
                }
                // Detect snmp transport
                $transport = $ip_version == 4 ? 'udp' : 'udp6';
                $new_device = detect_device_snmpauth($ip, $port, $transport);
                if ($new_device) {
                    if ($use_ip) {
                        // Detect FQDN hostname
                        // by sysName
                        $snmphost = snmp_get($new_device, "sysName.0", "-Oqv", "SNMPv2-MIB", mib_dirs());
                        if ($snmphost) {
                            $snmp_ip = gethostbyname6($snmphost);
                        }
                        if ($snmp_ip == $ip) {
                            $hostname = $snmphost;
                        } else {
                            // by PTR
                            $ptr = gethostbyaddr6($ip);
                            if ($ptr) {
                                $ptr_ip = gethostbyname6($ptr);
                            }
                            if ($ptr && $ptr_ip == $ip) {
                                $hostname = $ptr;
                            } else {
                                print_debug("Device IP {$ip} not have FQDN name");
                                return FALSE;
                            }
                        }
                        print_debug("Device IP {$ip} founded FQDN name: {$hostname}");
                    }
                    $new_device['hostname'] = $hostname;
                    if (!check_device_duplicated($new_device)) {
                        $v3 = array();
                        if ($new_device['snmpver'] === 'v3') {
                            $v3['authlevel'] = $new_device['authlevel'];
                            $v3['authname'] = $new_device['authname'];
                            $v3['authpass'] = $new_device['authpass'];
                            $v3['authalgo'] = $new_device['authalgo'];
                            $v3['cryptopass'] = $new_device['cryptopass'];
                            $v3['cryptoalgo'] = $new_device['cryptoalgo'];
                        }
                        $remote_device_id = createHost($new_device['hostname'], $new_device['community'], $new_device['snmpver'], $new_device['port'], $new_device['transport'], $v3);
                        if ($remote_device_id) {
                            $remote_device = device_by_id_cache($remote_device_id, 1);
                            if ($port) {
                                humanize_port($port);
                                log_event("Device autodiscovered through {$protocol} on " . $device['hostname'] . " (port " . $port['label'] . ")", $remote_device_id, 'port', $port['port_id']);
                            } else {
                                log_event("Device autodiscovered through {$protocol} on " . $device['hostname'], $remote_device_id, $protocol);
                            }
                            //array_push($GLOBALS['devices'], $remote_device); // createHost() already puth this
                            return $remote_device_id;
                        }
                    }
                }
            }
        } else {
            print_debug("IP {$ip} ({$hostname}) not permitted inside \$config['autodiscovery']['ip_nets'] in config.php");
        }
        print_debug('Autodiscovery for host ' . $hostname . ' fails.');
    } else {
        print_debug('Autodiscovery for protocol ' . $protocol . ' disabled.');
    }
    return FALSE;
}
function discover_new_device($hostname, $source = 'xdp', $protocol = NULL, $device = NULL, $snmp_port = 161)
{
    global $config;
    $source = strtolower($source);
    if ($config['autodiscovery'][$source]) {
        if (!$protocol) {
            $protocol = strtoupper($source);
        }
        print_message("发现新主机 {$hostname} 通过 {$protocol}");
        // By first detect hostname is IP or domain name (IPv4/6 == 4/6, hostname == FALSE)
        $ip_version = get_ip_version($hostname);
        if ($ip_version) {
            // Hostname is IPv4/IPv6
            $use_ip = TRUE;
            $ip = $hostname;
        } else {
            $use_ip = FALSE;
            if (!empty($config['mydomain']) && isDomainResolves($hostname . '.' . $config['mydomain'])) {
                $hostname .= '.' . $config['mydomain'];
            }
            $ip = gethostbyname6($hostname);
            if ($ip) {
                $ip_version = get_ip_version($ip);
                print_debug("主机 {$hostname} 解析为 {$ip}");
            } else {
                // No DNS records
                print_debug("主机 {$hostname} 无法解析, 自动发现失败.");
                return FALSE;
            }
        }
        if (match_network($ip, $config['autodiscovery']['ip_nets'])) {
            print_debug("主机 {$hostname} ({$ip}) 内部网络创建配置, 尝试增加:");
            if (isPingable($ip)) {
                // Check if device duplicated by IP
                $ip = $ip_version == 4 ? $ip : Net_IPv6::uncompress($ip, TRUE);
                $db = dbFetchRow('SELECT D.`hostname` FROM ipv' . $ip_version . '_addresses AS A
                         LEFT JOIN `ports`   AS P ON A.`port_id`   = P.`port_id`
                         LEFT JOIN `devices` AS D ON D.`device_id` = P.`device_id`
                         WHERE D.`disabled` = 0 AND A.`ipv' . $ip_version . '_address` = ?', array($ip));
                if ($db) {
                    print_debug('已经有设备 ' . $db['hostname'] . " 包含 {$ip}");
                    return FALSE;
                }
                // Detect snmp transport
                $snmp_transport = $ip_version == 4 ? 'udp' : 'udp6';
                $new_device = detect_device_snmpauth($ip, $snmp_port, $snmp_transport);
                if ($new_device) {
                    if ($use_ip) {
                        // Detect FQDN hostname
                        // by sysName
                        $snmphost = snmp_get($new_device, "sysName.0", "-Oqv", "SNMPv2-MIB", mib_dirs());
                        if ($snmphost) {
                            $snmp_ip = gethostbyname6($snmphost);
                        }
                        if ($snmp_ip == $ip) {
                            $hostname = $snmphost;
                        } else {
                            // by PTR
                            $ptr = gethostbyaddr6($ip);
                            if ($ptr) {
                                $ptr_ip = gethostbyname6($ptr);
                            }
                            if ($ptr && $ptr_ip == $ip) {
                                $hostname = $ptr;
                            } else {
                                print_debug("设备 IP {$ip} 没有 FQDN 名称");
                                return FALSE;
                            }
                        }
                        print_debug("设备 IP {$ip} 发现 FQDN 名称: {$hostname}");
                    }
                    $new_device['hostname'] = $hostname;
                    if (!check_device_duplicated($new_device)) {
                        $snmp_v3 = array();
                        if ($new_device['snmp_version'] === 'v3') {
                            $snmp_v3['snmp_authlevel'] = $new_device['snmp_authlevel'];
                            $snmp_v3['snmp_authname'] = $new_device['snmp_authname'];
                            $snmp_v3['snmp_authpass'] = $new_device['snmp_authpass'];
                            $snmp_v3['snmp_authalgo'] = $new_device['snmp_authalgo'];
                            $snmp_v3['snmp_cryptopass'] = $new_device['snmp_cryptopass'];
                            $snmp_v3['snmp_cryptoalgo'] = $new_device['snmp_cryptoalgo'];
                        }
                        $remote_device_id = createHost($new_device['hostname'], $new_device['snmp_community'], $new_device['snmp_version'], $new_device['snmp_port'], $new_device['snmp_transport'], $snmp_v3);
                        if ($remote_device_id) {
                            $remote_device = device_by_id_cache($remote_device_id, 1);
                            if ($port) {
                                humanize_port($port);
                                log_event("设备自动发现通过 {$protocol} 在 " . $device['hostname'] . " (port " . $port['label'] . ")", $remote_device_id, 'port', $port['port_id']);
                            } else {
                                log_event("设备自动发现通过 {$protocol} 在 " . $device['hostname'], $remote_device_id, $protocol);
                            }
                            //array_push($GLOBALS['devices'], $remote_device); // createHost() already puth this
                            return $remote_device_id;
                        }
                    }
                }
            }
        } else {
            print_debug("IP {$ip} ({$hostname}) 不允许内部 \$config['autodiscovery']['ip_nets'] 位于 config.php");
        }
        print_debug('自动发现主机 ' . $hostname . ' 错误.');
    } else {
        print_debug('自动发现协议 ' . $protocol . ' 禁用.');
    }
    return FALSE;
}