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 }
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"; } } }
echo " Instances start at 0. 0-3 for -n 4\n\n"; echo "\n"; echo "Debugging and testing options:\n"; echo "-d Enable debugging output\n"; echo "-m Specify single module to be run\n"; echo "\n"; echo "Invalid arguments!\n"; exit; } include "includes/sql-schema/update.php"; $discovered_devices = 0; if ($config['distributed_poller'] === TRUE) { $where .= " AND poller_group IN(" . $config['distributed_poller_group'] . ")"; } foreach (dbFetch("SELECT * FROM `devices` WHERE status = 1 AND disabled = 0 {$where} ORDER BY device_id DESC") as $device) { discover_device($device, $options); } $end = utime(); $run = $end - $start; $proctime = substr($run, 0, 5); if ($discovered_devices) { dbInsert(array('type' => 'discover', 'doing' => $doing, 'start' => $start, 'duration' => $proctime, 'devices' => $discovered_devices), 'perf_times'); } $string = $argv[0] . " {$doing} " . date($config['dateformat']['compact']) . " - {$discovered_devices} devices discovered in {$proctime} secs"; if ($debug) { echo "{$string}\n"; } if ($options['h'] != "new" && $config['version_check']) { include "includes/versioncheck.inc.php"; } if (!isset($options['q'])) {
function createHost($hostname, $snmp_community = NULL, $snmp_version, $snmp_port = 161, $snmp_transport = 'udp', $snmp_v3 = array()) { $hostname = trim(strtolower($hostname)); $device = array('hostname' => $hostname, 'sysName' => $hostname, 'status' => '1', 'snmp_community' => $snmp_community, 'snmp_port' => $snmp_port, 'snmp_transport' => $snmp_transport, 'snmp_version' => $snmp_version); // Add snmp v3 auth params foreach (array('authlevel', 'authname', 'authpass', 'authalgo', 'cryptopass', 'cryptoalgo') as $v3_key) { if (isset($snmp_v3['snmp_' . $v3_key])) { // Or $snmp_v3['snmp_authlevel'] $device['snmp_' . $v3_key] = $snmp_v3['snmp_' . $v3_key]; } else { if (isset($snmp_v3[$v3_key])) { // Or $snmp_v3['authlevel'] $device['snmp_' . $v3_key] = $snmp_v3[$v3_key]; } } } // This is compatibility code after refactor in r6306, for keep devices up before DB updated if (get_db_version() < 189) { // FIXME. Remove this in r7000 $device['snmpver'] = $device['snmp_version']; unset($device['snmp_version']); foreach (array('transport', 'port', 'timeout', 'retries', 'community', 'authlevel', 'authname', 'authpass', 'authalgo', 'cryptopass', 'cryptoalgo') as $old_key) { if (isset($device['snmp_' . $old_key])) { // Convert to old device snmp keys $device[$old_key] = $device['snmp_' . $old_key]; unset($device['snmp_' . $old_key]); } } } $device['os'] = get_device_os($device); $device['snmpEngineID'] = snmp_cache_snmpEngineID($device); $device['sysName'] = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB", mib_dirs()); $device['location'] = snmp_get($device, "sysLocation.0", "-Oqv", "SNMPv2-MIB", mib_dirs()); $device['sysContact'] = snmp_get($device, "sysContact.0", "-Oqv", "SNMPv2-MIB", mib_dirs()); if ($device['os']) { $device_id = dbInsert($device, 'devices'); if ($device_id) { log_event("设备添加: {$hostname}", $device_id, 'device', $device_id, 5); // severity 5, for logging user/console info if (is_cli()) { print_success("正在使用自动发现功能 " . $device['hostname'] . " (id = " . $device_id . ")"); $device['device_id'] = $device_id; // Discover things we need when linking this to other hosts. discover_device($device, $options = array('m' => 'ports')); discover_device($device, $options = array('m' => 'ipv4-addresses')); discover_device($device, $options = array('m' => 'ipv6-addresses')); log_event("snmpEngineID -> " . $device['snmpEngineID'], $device, 'device', $device['device_id']); // Reset `last_discovered` for full rediscover device by cron dbUpdate(array('last_discovered' => 'NULL'), 'devices', '`device_id` = ?', array($device_id)); array_push($GLOBALS['devices'], $device_id); } return $device_id; } else { return FALSE; } } else { return FALSE; } }
function createHost($host, $community = NULL, $snmpver, $port = 161, $transport = 'udp', $v3 = array()) { $host = trim(strtolower($host)); $device = array('hostname' => $host, 'sysName' => $host, 'community' => $community, 'port' => $port, 'transport' => $transport, 'status' => '1', 'snmpver' => $snmpver); $device = array_merge($device, $v3); $device['os'] = get_device_os($device); $device['snmpEngineID'] = snmp_cache_snmpEngineID($device); $device['sysName'] = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB", mib_dirs()); $device['location'] = snmp_get($device, "sysLocation.0", "-Oqv", "SNMPv2-MIB", mib_dirs()); $device['sysContact'] = snmp_get($device, "sysContact.0", "-Oqv", "SNMPv2-MIB", mib_dirs()); if ($device['os']) { $device_id = dbInsert($device, 'devices'); if ($device_id) { log_event("Device added: {$host}", $device_id, 'device'); if (is_cli()) { print_success("Now discovering " . $device['hostname'] . " (id = " . $device_id . ")"); $device['device_id'] = $device_id; // Discover things we need when linking this to other hosts. discover_device($device, $options = array('m' => 'ports')); discover_device($device, $options = array('m' => 'ipv4-addresses')); discover_device($device, $options = array('m' => 'ipv6-addresses')); log_event("snmpEngineID -> " . $device['snmpEngineID'], $device, 'system'); // Reset `last_discovered` for full rediscover device by cron dbUpdate(array('last_discovered' => 'NULL'), 'devices', '`device_id` = ?', array($device_id)); array_push($GLOBALS['devices'], $device_id); } return $device_id; } else { return FALSE; } } else { return FALSE; } }
function createHost($hostname, $snmp_community = NULL, $snmp_version, $snmp_port = 161, $snmp_transport = 'udp', $snmp_v3 = array()) { $hostname = trim(strtolower($hostname)); $device = array('hostname' => $hostname, 'sysName' => $hostname, 'status' => '1', 'snmp_community' => $snmp_community, 'snmp_port' => $snmp_port, 'snmp_transport' => $snmp_transport, 'snmp_version' => $snmp_version); // Add snmp v3 auth params foreach (array('authlevel', 'authname', 'authpass', 'authalgo', 'cryptopass', 'cryptoalgo') as $v3_key) { if (isset($snmp_v3['snmp_' . $v3_key])) { // Or $snmp_v3['snmp_authlevel'] $device['snmp_' . $v3_key] = $snmp_v3['snmp_' . $v3_key]; } else { if (isset($snmp_v3[$v3_key])) { // Or $snmp_v3['authlevel'] $device['snmp_' . $v3_key] = $snmp_v3[$v3_key]; } } } $device['os'] = get_device_os($device); $device['snmpEngineID'] = snmp_cache_snmpEngineID($device); $device['sysName'] = snmp_get($device, 'sysName.0', '-Oqv', 'SNMPv2-MIB'); $device['location'] = snmp_get($device, 'sysLocation.0', '-Oqv', 'SNMPv2-MIB'); $device['sysContact'] = snmp_get($device, 'sysContact.0', '-Oqv', 'SNMPv2-MIB'); if ($device['os']) { $device_id = dbInsert($device, 'devices'); if ($device_id) { log_event("Device added: {$hostname}", $device_id, 'device', $device_id, 5); // severity 5, for logging user/console info if (is_cli()) { print_success("Now discovering " . $device['hostname'] . " (id = " . $device_id . ")"); $device['device_id'] = $device_id; // Discover things we need when linking this to other hosts. discover_device($device, $options = array('m' => 'ports')); discover_device($device, $options = array('m' => 'ipv4-addresses')); discover_device($device, $options = array('m' => 'ipv6-addresses')); log_event("snmpEngineID -> " . $device['snmpEngineID'], $device, 'device', $device['device_id']); // Reset `last_discovered` for full rediscover device by cron dbUpdate(array('last_discovered' => 'NULL'), 'devices', '`device_id` = ?', array($device_id)); array_push($GLOBALS['devices'], $device_id); } return $device_id; } else { return FALSE; } } else { return FALSE; } }
function createHost($host, $community = NULL, $snmpver, $port = 161, $transport = 'udp', $v3 = array()) { $host = trim(strtolower($host)); $device = array('hostname' => $host, 'sysName' => $host, 'community' => $community, 'port' => $port, 'transport' => $transport, 'status' => '1', 'snmpver' => $snmpver); $device = array_merge($device, $v3); $device['os'] = getHostOS($device); $device['sysName'] = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB"); $device['location'] = snmp_get($device, "sysLocation.0", "-Oqv", "SNMPv2-MIB"); $device['sysContact'] = snmp_get($device, "sysContact.0", "-Oqv", "SNMPv2-MIB"); if ($device['os']) { $device_id = dbInsert($device, 'devices'); if ($device_id) { echo "Discovering " . $device['hostname'] . " (" . $device_id . ")"; $device['device_id'] = $device_id; // Discover things we need when linking this to other hosts. discover_device($device, $options = array('m' => 'ports')); discover_device($device, $options = array('m' => 'ipv4-addresses')); discover_device($device, $options = array('m' => 'ipv6-addresses')); array_push($GLOBAL['devices'], $device_id); return $device_id; } else { return FALSE; } } else { return FALSE; } }