コード例 #1
0
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;
    }
}
コード例 #2
0
ファイル: functions.inc.php プロジェクト: Natolumin/observium
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);
}
コード例 #3
0
ファイル: functions.inc.php プロジェクト: skive/observium
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;
    }
}
コード例 #4
0
ファイル: os.inc.php プロジェクト: skive/observium
<?php

/**
 * Observium
 *
 *   This file is part of Observium.
 *
 * @package    observium
 * @subpackage discovery
 * @copyright  (C) 2006-2014 Adam Armstrong
 *
 */
if ($detect_os) {
    $os = get_device_os($device);
    if ($os != $device['os']) {
        $type = isset($config['os'][$os]['type']) ? $config['os'][$os]['type'] : 'unknown';
        // Also change $type
        print_warning("Device OS changed: " . $device['os'] . " -> {$os}!");
        log_event('OS changed: ' . $device['os'] . ' -> ' . $os, $device, 'system');
        dbUpdate(array('os' => $os), 'devices', '`device_id` = ?', array($device['device_id']));
        $device['os'] = $os;
        $device['type'] = $type;
    }
}
// EOF
コード例 #5
0
ファイル: functions.inc.php プロジェクト: skive/observium
function discover_device($device, $options = NULL)
{
    global $config, $valid, $exec_status, $discovered_devices;
    $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'] . " ";
    $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("Device OS changed: {$old_os} -> " . $device['os'] . "!");
            log_event('OS changed: ' . $old_os . ' -> ' . $device['os'], $device, 'system');
            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")) {
                $m_start = utime();
                include "includes/discovery/{$module}.inc.php";
                $m_end = utime();
                $m_run = round($m_end - $m_start, 4);
                print_message("Module [ {$module} ] time: {$m_run}" . "s");
            }
        }
    } 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 [ {$module} ] is in the blacklist for " . $device['os_group']);
            } elseif (in_array($device['os'], $config['os']['discovery_blacklist'])) {
                // Module is blacklisted for this OS.
                print_debug("Module [ {$module} ] is in the blacklist for " . $device['os']);
            } else {
                if ($attribs['discover_' . $module] || $module_status && !isset($attribs['discover_' . $module])) {
                    $m_start = utime();
                    include "includes/discovery/{$module}.inc.php";
                    $m_end = utime();
                    $m_run = round($m_end - $m_start, 4);
                    print_message("Module [ {$module} ] time: {$m_run}" . "s");
                } 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), '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("Discovered in {$device_time} seconds");
    // not worth putting discovery data into rrd. it's not done every 5 mins :)
    echo PHP_EOL;
    $discovered_devices++;
}
コード例 #6
0
 /**
  * @dataProvider providerGetDeviceOS
  * @group snmp
  */
 public function testGetDeviceOS($result, $old_os, $sysObjectID, $sysDescr)
 {
     $device = array('device_id' => 0, 'disabled' => 0, 'ignore' => 0, 'status' => 1, 'snmp_version' => 'v2c', 'snmp_community' => 'test', 'snmp_port' => 161, 'snmp_timeout' => 1, 'snmp_retries' => 0, 'hostname' => 'example.test', 'os' => $old_os);
     $fake_data = 'sysDescr.0 = ' . $sysDescr . PHP_EOL;
     $fake_data .= 'sysObjectID.0 = ' . $sysObjectID;
     $GLOBALS['config']['snmpget'] = dirname(__FILE__) . '/data/snmpfake.sh fakedata ' . escapeshellarg($fake_data);
     //.' -d';
     $os = get_device_os($device);
     $this->assertSame($result, $os);
 }
コード例 #7
0
ファイル: functions.inc.php プロジェクト: Natolumin/observium
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;
    }
}
コード例 #8
0
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);
}