/** * Set up and test an os * If $filename is not set, it will use the snmprec file matching $expected_os * * @param string $expected_os The os we should get back from getHostOS() * @param string $filename the name of the snmprec file to use */ private function checkOS($expected_os, $filename = null) { $community = $filename ?: $expected_os; ob_start(); $os = getHostOS($this->genDevice($community)); $output = ob_get_contents(); ob_end_clean(); $this->assertEquals($expected_os, $os, "Test file: {$community}.snmprec\n{$output}"); }
function discover_device($device, $options = null) { global $config, $valid; $valid = array(); // Reset $valid array $attribs = get_dev_attribs($device['device_id']); $device_start = utime(); // Start counting device poll time echo $device['hostname'] . ' ' . $device['device_id'] . ' ' . $device['os'] . ' '; if ($device['os'] == 'generic') { // verify if OS has changed from generic $device['os'] = getHostOS($device); if ($device['os'] != 'generic') { echo "\nDevice os was updated to " . $device['os'] . '!'; 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 "\n"; // If we've specified modules, use them, else walk the modules array if ($options['m']) { foreach (explode(',', $options['m']) as $module) { if (is_file("includes/discovery/{$module}.inc.php")) { include "includes/discovery/{$module}.inc.php"; } } } else { foreach ($config['discovery_modules'] as $module => $module_status) { if ($attribs['discover_' . $module] || $module_status && !isset($attribs['discover_' . $module])) { include 'includes/discovery/' . $module . '.inc.php'; } else { if (isset($attribs['discover_' . $module]) && $attribs['discover_' . $module] == '0') { echo "Module [ {$module} ] disabled on host.\n"; } else { echo "Module [ {$module} ] disabled globally.\n"; } } } } // 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), 'devices', '`device_id` = ?', array($device['device_id'])); echo "Discovered in {$device_time} seconds\n"; global $discovered_devices; echo "\n"; $discovered_devices++; }
function createHost($host, $community = NULL, $snmpver, $port = 161, $transport = 'udp', $v3 = array(), $poller_group = '0') { global $config; $host = trim(strtolower($host)); $poller_group = getpollergroup($poller_group); $device = array('hostname' => $host, 'sysName' => $host, 'community' => $community, 'port' => $port, 'transport' => $transport, 'status' => '1', 'snmpver' => $snmpver, 'poller_group' => $poller_group); $device = array_merge($device, $v3); $device['os'] = getHostOS($device); if ($device['os']) { $device_id = dbInsert($device, 'devices'); if ($device_id) { return $device_id; } else { return FALSE; } } else { return FALSE; } }
function discover_device($device, $options = null) { global $config, $valid; $valid = array(); // Reset $valid array $attribs = get_dev_attribs($device['device_id']); $device['snmp_max_repeaters'] = $attribs['snmp_max_repeaters']; $device_start = microtime(true); // Start counting device poll time echo $device['hostname'] . ' ' . $device['device_id'] . ' ' . $device['os'] . ' '; if ($device['os'] == 'generic') { // verify if OS has changed from generic $device['os'] = getHostOS($device); if ($device['os'] != 'generic') { echo "\nDevice os was updated to " . $device['os'] . '!'; dbUpdate(array('os' => $device['os']), 'devices', '`device_id` = ?', array($device['device_id'])); } } // 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']; } } if ($config['os'][$device['os']]['group']) { $device['os_group'] = $config['os'][$device['os']]['group']; echo ' (' . $device['os_group'] . ')'; } echo "\n"; // If we've specified modules, use them, else walk the modules array $force_module = false; if ($options['m']) { $config['discovery_modules'] = array(); foreach (explode(',', $options['m']) as $module) { if (is_file("includes/discovery/{$module}.inc.php")) { $config['discovery_modules'][$module] = 1; $force_module = true; } } } foreach ($config['discovery_modules'] as $module => $module_status) { if ($force_module === true || $attribs['discover_' . $module] || $module_status && !isset($attribs['discover_' . $module])) { $module_start = microtime(true); echo "#### Load disco module {$module} ####\n"; include "includes/discovery/{$module}.inc.php"; $module_time = microtime(true) - $module_start; $module_time = substr($module_time, 0, 5); echo "\n>> Runtime for discovery module '{$module}': {$module_time} seconds\n"; echo "#### Unload disco module {$module} ####\n\n"; } elseif (isset($attribs['discover_' . $module]) && $attribs['discover_' . $module] == '0') { echo "Module [ {$module} ] disabled on host.\n\n"; } else { echo "Module [ {$module} ] disabled globally.\n\n"; } } if (is_mib_poller_enabled($device)) { $devicemib = array($device['sysObjectID'] => 'all'); register_mibs($device, $devicemib, "includes/discovery/functions.inc.php"); } $device_end = microtime(true); $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), 'devices', '`device_id` = ?', array($device['device_id'])); echo "Discovered in {$device_time} seconds\n"; global $discovered_devices; echo "\n"; $discovered_devices++; }
function createHost($host, $community = NULL, $snmpver, $port = 161, $transport = 'udp', $v3 = array(), $poller_group = '0', $port_assoc_mode = 'ifIndex') { global $config; $host = trim(strtolower($host)); $poller_group = getpollergroup($poller_group); /* Get port_assoc_mode id if neccessary * We can work with names of IDs here */ if (!is_int($port_assoc_mode)) { $port_assoc_mode = get_port_assoc_mode_id($port_assoc_mode); } $device = array('hostname' => $host, 'sysName' => $host, 'community' => $community, 'port' => $port, 'transport' => $transport, 'status' => '1', 'snmpver' => $snmpver, 'poller_group' => $poller_group, 'status_reason' => '', 'port_association_mode' => $port_assoc_mode); $device = array_merge($device, $v3); $device['os'] = getHostOS($device); if ($device['os']) { if (host_exists($host) === false) { $device_id = dbInsert($device, 'devices'); if ($device_id) { oxidized_reload_nodes(); return $device_id; } else { return false; } } else { return false; } } else { return FALSE; } }
<?php echo 'OS: '; $os = getHostOS($device); if ($os != $device['os']) { log_event('Device OS changed ' . $device['os'] . " => {$os}", $device, 'system'); $device['os'] = $os; $sql = dbUpdate(array('os' => $os), 'devices', 'device_id=?', array($device['device_id'])); echo "Changed OS! : {$os}\n"; } $icon = getImageName($device, false); if ($icon != $device['icon']) { log_event('Device Icon changed ' . $device['icon'] . " => {$icon}", $device, 'system'); $device['icon'] = $icon; $sql = dbUpdate(array('icon' => $icon), 'devices', 'device_id=?', array($device['device_id'])); echo "Changed Icon! : {$icon}\n"; }
function createHost($host, $community, $snmpver, $port = 161) { $host = trim(strtolower($host)); $device = array('hostname' => $host, 'community' => $community, 'snmpver' => $snmpver, 'port' => $port); $host_os = getHostOS($device); if ($host_os) { $sql = mysql_query("INSERT INTO `devices` (`hostname`, `sysName`, `community`, `port`, `os`, `status`,`snmpver`) VALUES ('{$host}', '{$host}', '{$community}', '{$port}', '{$host_os}', '1','{$snmpver}')"); if (mysql_affected_rows()) { $device_id = mysql_result(mysql_query("SELECT device_id FROM devices WHERE hostname = '{$host}'"), 0); mysql_query("INSERT INTO devices_attribs (attrib_type, attrib_value, device_id) VALUES ('discover','1','{$device_id}')"); return "Created host : {$host} (id:{$device_id}) (os:{$host_os})"; } 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; } }
function discover_device($device, $options = NULL) { global $config, $valid; $valid = array(); // Reset $valid array $attribs = get_dev_attribs($device['device_id']); $device_start = utime(); // Start counting device poll time echo $device['hostname'] . " " . $device['device_id'] . " " . $device['os'] . " "; if ($device['os'] == 'generic') { $device['os'] = getHostOS($device); if ($device['os'] != 'generic') { echo "\nDevice os was updated to " . $device['os'] . "!"; 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 "\n"; // 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")) { include "includes/discovery/" . $module . ".inc.php"; } } } 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. debug("Module {$module} is in the blacklist for " . $device['os_group'] . "\n"); } elseif (in_array($device['os'], $config['os']['discovery_blacklist'])) { // Module is blacklisted for this OS. debug("Module {$module} is in the blacklist for " . $device['os'] . "\n"); } else { if ($attribs['discover_' . $module] || $module_status && !isset($attribs['discover_' . $module])) { include 'includes/discovery/' . $module . '.inc.php'; } elseif (isset($attribs['discover_' . $module]) && $attribs['discover_' . $module] == "0") { echo "Module [ {$module} ] disabled on host.\n"; } else { echo "Module [ {$module} ] disabled globally.\n"; } } } } // 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), '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'); echo "Discovered in {$device_time} seconds\n"; // not worth putting discovery data into rrd. it's not done every 5 mins :) global $discovered_devices; echo "\n"; $discovered_devices++; }