function discover_device($device, $options = NULL) { global $config, $valid, $exec_status, $discovered_devices; // Initialise variables $valid = array(); // Reset $valid array $modules = array(); $cache_discovery = array(); // Specific discovery cache for exchange snmpwalk data between modules (memory/storage/sensors/etc) $attribs = get_entity_attribs('device', $device['device_id']); $device_start = utime(); // Start counting device poll time // Check if device discovery already running $pid_info = check_process_run($device); if ($pid_info) { // Process ID exist in DB print_message("%rAnother " . $pid_info['process_name'] . " process (PID: " . $pid_info['PID'] . ", UID: " . $pid_info['UID'] . ", STARTED: " . $pid_info['STARTED'] . ") already running for device " . $device['hostname'] . " (" . $device['device_id'] . ").%n", 'color'); return FALSE; } add_process_info($device); // Store process info print_cli_heading($device['hostname'] . " [" . $device['device_id'] . "]", 1); $detect_os = TRUE; // Set TRUE or FALSE for module 'os' (exclude double os detection) if ($device['os'] == 'generic' || isset($options['h']) && $options['h'] == 'new') { $detect_os = FALSE; $old_os = $device['os']; $device['os'] = get_device_os($device); if ($device['os'] != $old_os) { print_cli_data("Device OS changed", $old_os . " -> " . $device['os'], 1); log_event('OS changed: ' . $old_os . ' -> ' . $device['os'], $device, 'device', $device['device_id'], 'warning'); dbUpdate(array('os' => $device['os']), 'devices', '`device_id` = ?', array($device['device_id'])); } } print_cli_data("OS Type", $device['os'], 1); if ($config['os'][$device['os']]['group']) { $device['os_group'] = $config['os'][$device['os']]['group']; print_cli_data("OS Group", $device['os_group'], 1); } print_cli_data("SNMP Version", $device['snmp_version'], 1); print_cli_data("Last discovery", $device['last_discovered'], 1); print_cli_data("Last duration", $device['last_discovered_timetaken'] . " seconds", 1); echo PHP_EOL; // Either only run the modules specified on the commandline, or run all modules in config. if ($options['m']) { foreach (explode(",", $options['m']) as $module) { $modules[$module] = TRUE; } } else { if ($device['force_discovery'] && $options['h'] == 'new' && isset($attribs['force_discovery_modules'])) { // Forced discovery specific modules foreach (json_decode($attribs['force_discovery_modules'], TRUE) as $module) { $modules[$module] = TRUE; } log_event('Forced discovery module(s): ' . implode(', ', array_keys($modules)), $device, 'device', $device['device_id'], 'debug'); } else { $modules = $config['discovery_modules']; } } // Use os specific modules order //print_vars($modules); if (isset($config['os'][$device['os']]['discovery_order'])) { //print_vars($config['os'][$device['os']]['discovery_order']); foreach ($config['os'][$device['os']]['discovery_order'] as $module => $module_order) { if (array_key_exists($module, $modules)) { $module_status = $modules[$module]; switch ($module_order) { case 'last': // add to end of modules list unset($modules[$module]); $modules[$module] = $module_status; break; case 'first': // add to begin of modules list, but not before os/system $new_modules = array(); if ($modules['os']) { $new_modules['os'] = $modules['os']; unset($modules['os']); } if ($modules['system']) { $new_modules['system'] = $modules['system']; unset($modules['system']); } $new_modules[$module] = $module_status; unset($modules[$module]); $modules = $new_modules + $modules; break; default: // add into specific place (after module name in $module_order) // yes, this is hard and magically if (array_key_exists($module_order, $modules)) { unset($modules[$module]); $new_modules = array(); foreach ($modules as $new_module => $new_status) { array_shift($modules); $new_modules[$new_module] = $new_status; if ($new_module == $module_order) { $new_modules[$module] = $module_status; break; } } $modules = array_merge($new_modules, (array) $modules); } } } } //print_vars($modules); } foreach ($modules as $module => $module_status) { if (discovery_module_excluded($device, $module) === FALSE) { if ($attribs['discover_' . $module] || $module_status && !isset($attribs['discover_' . $module])) { $m_start = utime(); $GLOBALS['module_stats'][$module] = array(); print_cli_heading("Module Start: %R" . $module . ""); include "includes/discovery/{$module}.inc.php"; $m_end = utime(); $GLOBALS['module_stats'][$module]['time'] = round($m_end - $m_start, 4); print_module_stats($device, $module); echo PHP_EOL; //print_cli_heading("Module End: %R".$module.""); } elseif (isset($attribs['discover_' . $module]) && $attribs['discover_' . $module] == "0") { print_debug("Module [ {$module} ] disabled on host."); } else { print_debug("Module [ {$module} ] disabled globally."); } } } // Set type to a predefined type for the OS if it's not already set if ($device['type'] == "unknown" || $device['type'] == "") { if ($config['os'][$device['os']]['type']) { $device['type'] = $config['os'][$device['os']]['type']; } } $device_end = utime(); $device_run = $device_end - $device_start; $device_time = substr($device_run, 0, 5); dbUpdate(array('last_discovered' => array('NOW()'), 'type' => $device['type'], 'last_discovered_timetaken' => $device_time, 'force_discovery' => 0), 'devices', '`device_id` = ?', array($device['device_id'])); if (isset($attribs['force_discovery_modules'])) { del_entity_attrib('device', $device['device_id'], 'force_discovery_modules'); } // Put performance into devices_perftimes table // Not worth putting discovery data into rrd. it's not done every 5 mins :) dbInsert(array('device_id' => $device['device_id'], 'operation' => 'discover', 'start' => $device_start, 'duration' => $device_run), 'devices_perftimes'); print_cli_heading($device['hostname'] . " [" . $device['device_id'] . "] completed discovery modules at " . date("Y-m-d H:i:s"), 1); print_cli_data("Discovery time", $device_time . " seconds", 1); echo PHP_EOL; $discovered_devices++; // Clean del_process_info($device); // Remove process info unset($cache_discovery); }
function discover_device($device, $options = NULL) { global $config, $valid, $exec_status, $discovered_devices; // Initialise variables $valid = array(); // Reset $valid array $cache_discovery = array(); // Specific discovery cache for exchange snmpwalk data betwen modules (memory/storage/sensors/etc) $attribs = get_dev_attribs($device['device_id']); $device_start = utime(); // Start counting device poll time echo $device['hostname'] . " " . $device['device_id'] . " " . $device['os'] . " "; $detect_os = TRUE; // Set TRUE or FALSE for module 'os' (exclude double os detection) if ($device['os'] == 'generic' || isset($options['h']) && $options['h'] == 'new') { $detect_os = FALSE; $old_os = $device['os']; $device['os'] = get_device_os($device); if ($device['os'] != $old_os) { print_warning("设备OS更改: {$old_os} -> " . $device['os'] . "!"); log_event('OS更改: ' . $old_os . ' -> ' . $device['os'], $device, 'device', $device['device_id'], 'warning'); dbUpdate(array('os' => $device['os']), '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'] . ")"; } echo PHP_EOL; // If we've specified a module, use that, else walk the modules array if ($options['m']) { foreach (explode(",", $options['m']) as $module) { if (is_file("includes/discovery/" . $module . ".inc.php")) { $m_start = utime(); $GLOBALS['module_stats'][$module] = array(); include "includes/discovery/" . $module . ".inc.php"; $m_end = utime(); $GLOBALS['module_stats'][$module]['time'] = round($m_end - $m_start, 4); print_module_stats($device, $module); } } } else { foreach ($config['discovery_modules'] as $module => $module_status) { if (in_array($device['os_group'], $config['os']['discovery_blacklist'])) { // Module is blacklisted for this OS. print_debug("模块 [ {$module} ] 被列入黑名单在 " . $device['os_group']); } elseif (in_array($device['os'], $config['os']['discovery_blacklist'])) { // Module is blacklisted for this OS. print_debug("模块 [ {$module} ] 被列入黑名单在 " . $device['os']); } else { if ($attribs['discover_' . $module] || $module_status && !isset($attribs['discover_' . $module])) { $m_start = utime(); $GLOBALS['module_stats'][$module] = array(); include "includes/discovery/{$module}.inc.php"; $m_end = utime(); $GLOBALS['module_stats'][$module]['time'] = round($m_end - $m_start, 4); print_module_stats($device, $module); } elseif (isset($attribs['discover_' . $module]) && $attribs['discover_' . $module] == "0") { print_debug("模块 [ {$module} ] 在主机禁用."); } else { print_debug("模块 [ {$module} ] 全局禁用."); } } } } // Set type to a predefined type for the OS if it's not already set if ($device['type'] == "unknown" || $device['type'] == "") { if ($config['os'][$device['os']]['type']) { $device['type'] = $config['os'][$device['os']]['type']; } } $device_end = utime(); $device_run = $device_end - $device_start; $device_time = substr($device_run, 0, 5); dbUpdate(array('last_discovered' => array('NOW()'), 'type' => $device['type'], 'last_discovered_timetaken' => $device_time, 'force_discovery' => 0), 'devices', '`device_id` = ?', array($device['device_id'])); // put performance into devices_perftimes dbInsert(array('device_id' => $device['device_id'], 'operation' => 'discover', 'start' => $device_start, 'duration' => $device_run), 'devices_perftimes'); print_message("设备 [ " . $device['hostname'] . " ] 发现 {$device_time} 秒"); // not worth putting discovery data into rrd. it's not done every 5 mins :) echo PHP_EOL; $discovered_devices++; // Clean unset($cache_discovery); }