コード例 #1
0
function discover_new_device_ip($host)
{
    global $config;
    if (match_network($config['autodiscovery']['ip_nets'], $host)) {
        if (isPingable($host)) {
            echo "Pingable ";
            foreach ($config['snmp']['community'] as $community) {
                $device = deviceArray($host, $community, "v2c", "161", "udp", NULL);
                print_message("Trying community {$community} ...");
                if (isSNMPable($device)) {
                    echo "SNMPable ";
                    $snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
                    if (dbFetchCell("SELECT COUNT(device_id) FROM devices WHERE sysName = ?", array($snmphost)) == '0') {
                        $device_id = createHost($snmphost, $community, "v2c", "161", "udp");
                        $device = device_by_id_cache($device_id, 1);
                        array_push($GLOBALS['devices'], $device);
                        return $device_id;
                    } else {
                        echo "Already have host with sysName {$snmphost}\n";
                    }
                }
            }
        }
    }
}
コード例 #2
0
/**
 * Observium
 *
 *   This file is part of Observium.
 *
 * @package    observium
 * @subpackage discovery
 * @subpackage functions
 * @author     Adam Armstrong <*****@*****.**>
 * @copyright  (C) 2006-2014 Adam Armstrong
 *
 */
function discover_new_device_ip($host, $source = 'xdp', $protocol = NULL, $device = NULL, $port = NULL)
{
    global $config;
    print_debug("Discovering possible new device on {$host}");
    if ($config['autodiscovery'][$source]) {
        if (match_network($config['autodiscovery']['ip_nets'], $host)) {
            $db = dbFetchRow("SELECT * FROM ipv4_addresses AS A, ports AS P, devices AS D WHERE A.ipv4_address = ? AND P.port_id = A.port_id AND D.device_id = P.device_id", array($host));
            if (is_array($db)) {
                print_debug("Already have {$host} on " . $db['hostname']);
            } else {
                if (isPingable($host)) {
                    echo "Pingable ";
                    foreach ($config['snmp']['community'] as $community) {
                        $newdevice = deviceArray($host, $community, "v2c", "161", "udp", NULL);
                        print_message("Trying community {$community} ...");
                        if (isSNMPable($newdevice)) {
                            echo "SNMPable ";
                            $snmphost = snmp_get($newdevice, "sysName.0", "-Oqv", "SNMPv2-MIB");
                            if (dbFetchCell("SELECT COUNT(device_id) FROM devices WHERE sysName = ?", array($snmphost)) == '0') {
                                $device_id = createHost($snmphost, $community, "v2c", "161", "udp");
                                $newdevice = device_by_id_cache($device_id, 1);
                                array_push($GLOBALS['devices'], $newdevice);
                                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);
                                }
                                return $device_id;
                            } else {
                                echo "Already have host with sysName {$snmphost}\n";
                            }
                        }
                    }
                } else {
                    print_debug("IP not pingable.");
                }
            }
        } else {
            print_debug("Host does not match configured nets");
        }
    } else {
        print_debug("Source {$source} disabled for autodiscovery!");
    }
}
コード例 #3
0
ファイル: functions.php プロジェクト: syzdek/librenms
function addHost($host, $snmpver, $port = '161', $transport = 'udp', $quiet = '0', $poller_group = '0', $force_add = '0')
{
    global $config;
    list($hostshort) = explode(".", $host);
    // Test Database Exists
    if (dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = ?", array($host)) == '0') {
        if ($config['addhost_alwayscheckip'] === TRUE) {
            $ip = gethostbyname($host);
        } else {
            $ip = $host;
        }
        if (ip_exists($ip) === false) {
            // Test reachability
            if ($force_add == 1 || isPingable($host)) {
                if (empty($snmpver)) {
                    // Try SNMPv2c
                    $snmpver = 'v2c';
                    $ret = addHost($host, $snmpver, $port, $transport, $quiet, $poller_group, $force_add);
                    if (!$ret) {
                        //Try SNMPv3
                        $snmpver = 'v3';
                        $ret = addHost($host, $snmpver, $port, $transport, $quiet, $poller_group, $force_add);
                        if (!$ret) {
                            // Try SNMPv1
                            $snmpver = 'v1';
                            return addHost($host, $snmpver, $port, $transport, $quiet, $poller_group, $force_add);
                        } else {
                            return $ret;
                        }
                    } else {
                        return $ret;
                    }
                }
                if ($snmpver === "v3") {
                    // Try each set of parameters from config
                    foreach ($config['snmp']['v3'] as $v3) {
                        $device = deviceArray($host, NULL, $snmpver, $port, $transport, $v3);
                        if ($quiet == '0') {
                            print_message("Trying v3 parameters " . $v3['authname'] . "/" . $v3['authlevel'] . " ... ");
                        }
                        if ($force_add == 1 || isSNMPable($device)) {
                            $snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
                            if (empty($snmphost) or $snmphost == $host || ($hostshort = $host)) {
                                $device_id = createHost($host, NULL, $snmpver, $port, $transport, $v3, $poller_group);
                                return $device_id;
                            } else {
                                if ($quiet == '0') {
                                    print_error("Given hostname does not match SNMP-read hostname ({$snmphost})!");
                                }
                            }
                        } else {
                            if ($quiet == '0') {
                                print_error("No reply on credentials " . $v3['authname'] . "/" . $v3['authlevel'] . " using {$snmpver}");
                            }
                        }
                    }
                } elseif ($snmpver === "v2c" or $snmpver === "v1") {
                    // try each community from config
                    foreach ($config['snmp']['community'] as $community) {
                        $device = deviceArray($host, $community, $snmpver, $port, $transport, NULL);
                        if ($quiet == '0') {
                            print_message("Trying community {$community} ...");
                        }
                        if ($force_add == 1 || isSNMPable($device)) {
                            $snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
                            if (empty($snmphost) || $snmphost && ($snmphost == $host || ($hostshort = $host))) {
                                $device_id = createHost($host, $community, $snmpver, $port, $transport, array(), $poller_group);
                                return $device_id;
                            } else {
                                if ($quiet == '0') {
                                    print_error("Given hostname does not match SNMP-read hostname ({$snmphost})!");
                                }
                            }
                        } else {
                            if ($quiet == '0') {
                                print_error("No reply on community {$community} using {$snmpver}");
                            }
                        }
                    }
                } else {
                    if ($quiet == '0') {
                        print_error("Unsupported SNMP Version \"{$snmpver}\".");
                    }
                }
                if (!$device_id) {
                    // Failed SNMP
                    if ($quiet == '0') {
                        print_error("Could not reach {$host} with given SNMP community using {$snmpver}");
                    }
                }
            } else {
                // failed Reachability
                if ($quiet == '0') {
                    print_error("Could not ping {$host}");
                }
            }
        } else {
            if ($quiet == 0) {
                print_error("Already have host with this IP {$host}");
            }
        }
    } else {
        // found in database
        if ($quiet == '0') {
            print_error("Already got host {$host}");
        }
    }
    return 0;
}
コード例 #4
0
ファイル: snmp.inc.php プロジェクト: pblasquez/librenms
 $max_repeaters = mres($_POST['max_repeaters']);
 $v3 = array('authlevel' => mres($_POST['authlevel']), 'authname' => mres($_POST['authname']), 'authpass' => mres($_POST['authpass']), 'authalgo' => mres($_POST['authalgo']), 'cryptopass' => mres($_POST['cryptopass']), 'cryptoalgo' => mres($_POST['cryptoalgo']));
 // FIXME needs better feedback
 $update = array('community' => $community, 'snmpver' => $snmpver, 'port' => $port, 'transport' => $transport, 'poller_group' => $poller_group, 'port_association_mode' => $port_assoc_mode);
 if ($_POST['timeout']) {
     $update['timeout'] = $timeout;
 } else {
     $update['timeout'] = array('NULL');
 }
 if ($_POST['retries']) {
     $update['retries'] = $retries;
 } else {
     $update['retries'] = array('NULL');
 }
 $update = array_merge($update, $v3);
 $device_tmp = deviceArray($device['hostname'], $community, $snmpver, $port, $transport, $v3, $port_assoc_mode);
 if (isSNMPable($device_tmp)) {
     $rows_updated = dbUpdate($update, 'devices', '`device_id` = ?', array($device['device_id']));
     $max_repeaters_set = false;
     if (is_numeric($max_repeaters) && $max_repeaters != 0) {
         set_dev_attrib($device, 'snmp_max_repeaters', $max_repeaters);
         $max_repeaters_set = true;
     } else {
         del_dev_attrib($device, 'snmp_max_repeaters');
         $max_repeaters_set = true;
     }
     if ($rows_updated > 0) {
         $update_message = $rows_updated . ' Device record updated.';
         $updated = 1;
     } else {
         if ($rows_updated = '-1') {
コード例 #5
0
ファイル: functions.inc.php プロジェクト: skive/observium
function detect_device_snmpauth($hostname, $port = 161, $transport = 'udp', $detect_ip_version = FALSE)
{
    global $config;
    // Additional checks for IP version
    if ($detect_ip_version) {
        $ip_version = get_ip_version($hostname);
        if (!$ip_version) {
            $ip = gethostbyname6($hostname);
            $ip_version = get_ip_version($ip);
        }
        // Detect snmp transport
        if (stripos($transport, 'tcp') !== FALSE) {
            $transport = $ip_version == 4 ? 'tcp' : 'tcp6';
        } else {
            $transport = $ip_version == 4 ? 'udp' : 'udp6';
        }
    }
    // Detect snmp port
    if (!is_numeric($port) || $port < 1 || $port > 65535) {
        $port = 161;
    } else {
        $port = (int) $port;
    }
    // Here set default snmp version order
    $i = 1;
    $snmpver_order = array();
    foreach (array('v2c', 'v3', 'v1') as $snmpver) {
        if ($config['snmp']['version'] == $snmpver) {
            $snmpver_order[0] = $snmpver;
        } else {
            $snmpver_order[$i] = $snmpver;
        }
        $i++;
    }
    ksort($snmpver_order);
    foreach ($snmpver_order as $snmpver) {
        if ($snmpver === 'v3') {
            // Try each set of parameters from config
            foreach ($config['snmp']['v3'] as $v3) {
                $device = deviceArray($hostname, NULL, $snmpver, $port, $transport, $v3);
                print_message("Trying v3 parameters " . $v3['authname'] . "/" . $v3['authlevel'] . " ... ");
                if (isSNMPable($device)) {
                    return $device;
                } else {
                    print_warning("No reply on credentials " . $v3['authname'] . "/" . $v3['authlevel'] . " using {$snmpver}.");
                }
            }
        } else {
            // if ($snmpver === "v2c" || $snmpver === "v1")
            // Try each community from config
            foreach ($config['snmp']['community'] as $community) {
                $device = deviceArray($hostname, $community, $snmpver, $port, $transport);
                print_message("Trying {$snmpver} community {$community} ...");
                if (isSNMPable($device)) {
                    return $device;
                } else {
                    print_warning("No reply on community {$community} using {$snmpver}.");
                }
            }
        }
    }
    return FALSE;
}
コード例 #6
0
function addHost($host, $snmpver, $port = '161', $transport = 'udp', $quiet = '0')
{
    global $config;
    list($hostshort) = explode(".", $host);
    // Test Database Exists
    if (dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = ?", array($host)) == '0') {
        // Test if IP or Hostname
        if (!inet_pton($host)) {
            // Test reachability
            if (isPingable($host)) {
                $added = 0;
                if (empty($snmpver)) {
                    // Try SNMPv2c
                    $snmpver = 'v2c';
                    $ret = addHost($host, $snmpver);
                    if (!$ret) {
                        //Try SNMPv3
                        $snmpver = 'v3';
                        $ret = addHost($host, $snmpver);
                        if (!$ret) {
                            // Try SNMPv1
                            $snmpver = 'v1';
                            return addHost($host, $snmpver);
                        } else {
                            return $ret;
                        }
                    } else {
                        return $ret;
                    }
                }
                if ($snmpver === "v3") {
                    // Try each set of parameters from config
                    foreach ($config['snmp']['v3'] as $v3) {
                        $device = deviceArray($host, NULL, $snmpver, $port, $transport, $v3);
                        if ($quiet == '0') {
                            print_message("Trying v3 parameters " . $v3['authname'] . "/" . $v3['authlevel'] . " ... ");
                        }
                        if (isSNMPable($device)) {
                            $snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
                            if (empty($snmphost) or $snmphost == $host || ($hostshort = $host)) {
                                $device_id = createHost($host, NULL, $snmpver, $port, $transport, $v3);
                                return $device_id;
                            } else {
                                if ($quiet == '0') {
                                    print_error("Given hostname does not match SNMP-read hostname ({$snmphost})!");
                                }
                            }
                        } else {
                            if ($quiet == '0') {
                                print_error("No reply on credentials " . $v3['authname'] . "/" . $v3['authlevel'] . " using {$snmpver}");
                            }
                        }
                    }
                } elseif ($snmpver === "v2c" or $snmpver === "v1") {
                    // try each community from config
                    foreach ($config['snmp']['community'] as $community) {
                        $device = deviceArray($host, $community, $snmpver, $port, $transport, NULL);
                        if ($quiet == '0') {
                            print_message("Trying community {$community} ...");
                        }
                        if (isSNMPable($device)) {
                            $snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
                            if ($snmphost == "" || $snmphost && ($snmphost == $host || ($hostshort = $host))) {
                                $device_id = createHost($host, $community, $snmpver, $port, $transport);
                                return $device_id;
                            } else {
                                if ($quiet == '0') {
                                    print_error("Given hostname does not match SNMP-read hostname ({$snmphost})!");
                                }
                            }
                        } else {
                            if ($quiet == '0') {
                                print_error("No reply on community {$community} using {$snmpver}");
                            }
                        }
                    }
                } else {
                    if ($quiet == '0') {
                        print_error("Unsupported SNMP Version \"{$snmpver}\".");
                    }
                }
                if (!$device_id) {
                    // Failed SNMP
                    if ($quiet == '0') {
                        print_error("Could not reach {$host} with given SNMP community using {$snmpver}");
                    }
                }
            } else {
                // failed Reachability
                if ($quiet == '0') {
                    print_error("Could not ping {$host}");
                }
            }
        } else {
            // Failed DNS lookup
            if ($quiet == '0') {
                print_error("{$host} looks like an IP address, please use FQDN");
            }
        }
    } else {
        // found in database
        if ($quiet == '0') {
            print_error("Already got host {$host}");
        }
    }
    return 0;
}
コード例 #7
0
function add_device($host, $snmpver = array(), $port = '161', $transport = 'udp', $error = FALSE)
{
    global $config;
    // If $error set as TRUE break recursive function execute
    if ($error) {
        return FALSE;
    }
    // Reset snmp timeout and retries options for speedup device adding
    unset($config['snmp']['timeout'], $config['snmp']['retries']);
    list($hostshort) = explode(".", $host);
    // Test if host exists in database
    if (dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = ?", array($host)) == '0') {
        // Test DNS lookup.
        if (gethostbyname6($host, TRUE)) {
            // Test reachability
            if (isPingable($host)) {
                // Test directory exists in /rrd/
                if (!$config['rrd_override'] && file_exists($config['rrd_dir'] . '/' . $host)) {
                    print_error("目录 rrd/{$host} 已经存在.");
                    return FALSE;
                }
                if (empty($snmpver)) {
                    foreach (array('v2c', 'v3', 'v1') as $snmpver) {
                        // Try SNMP v2c, v3 and v1
                        $ret = add_device($host, $snmpver, $port, $transport, $error);
                        if ($ret === FALSE) {
                            $error = TRUE;
                        } elseif (is_numeric($ret) && $ret > 0) {
                            return $ret;
                        }
                    }
                }
                if ($snmpver === "v3") {
                    // Try each set of parameters from config
                    foreach ($config['snmp']['v3'] as $v3) {
                        $device = deviceArray($host, NULL, $snmpver, $port, $transport, $v3);
                        print_message("Trying v3 parameters " . $v3['authname'] . "/" . $v3['authlevel'] . " ... ");
                        if (isSNMPable($device)) {
                            if (!check_device_duplicated($device)) {
                                $device_id = createHost($host, NULL, $snmpver, $port, $transport, $v3);
                                return $device_id;
                            }
                        } else {
                            print_warning("证书没有回应 " . $v3['authname'] . "/" . $v3['authlevel'] . " using {$snmpver}.");
                        }
                    }
                } elseif ($snmpver === "v2c" || $snmpver === "v1") {
                    // Try each community from config
                    foreach ($config['snmp']['community'] as $community) {
                        $device = deviceArray($host, $community, $snmpver, $port, $transport, NULL);
                        print_message("尝试 {$snmpver} community {$community} ...");
                        if (isSNMPable($device)) {
                            if (!check_device_duplicated($device)) {
                                $device_id = createHost($host, $community, $snmpver, $port, $transport);
                                return $device_id;
                            }
                        } else {
                            print_warning("Community 没有应答 {$community} 使用 {$snmpver}.");
                        }
                    }
                } else {
                    print_error("不支持的协议版本 \"{$snmpver}\".");
                }
                if (!$device_id) {
                    // Failed SNMP
                    print_error("不可到达的 {$host} 与给定 SNMP community 使用 {$snmpver}.");
                }
            } else {
                // failed Reachability
                print_error("无法 ping {$host}.");
            }
        } else {
            // Failed DNS lookup
            print_error("无法解析 {$host}.");
        }
    } else {
        // found in database
        print_error("已有设备 {$host}.");
    }
    return FALSE;
}
コード例 #8
0
ファイル: functions.php プロジェクト: ekoyle/librenms
/**
 * Add a device to LibreNMS
 *
 * @param string $host dns name or ip address
 * @param string $snmp_version If this is empty, try v2c,v3,v1.  Otherwise, use this specific version.
 * @param string $port the port to connect to for snmp
 * @param string $transport udp or tcp
 * @param string $poller_group the poller group this device will belong to
 * @param boolean $force_add add even if the device isn't reachable
 * @param string $port_assoc_mode snmp field to use to determine unique ports
 *
 * @return int returns the device_id of the added device
 *
 * @throws HostExistsException This hostname already exists
 * @throws HostIpExistsException We already have a host with this IP
 * @throws HostUnreachableException We could not reach this device is some way
 * @throws HostUnreachablePingException We could not ping the device
 * @throws InvalidPortAssocModeException The given port association mode was invalid
 * @throws SnmpVersionUnsupportedException The given snmp version was invalid
 */
function addHost($host, $snmp_version = '', $port = '161', $transport = 'udp', $poller_group = '0', $force_add = false, $port_assoc_mode = 'ifIndex')
{
    global $config;
    // Test Database Exists
    if (host_exists($host) === true) {
        throw new HostExistsException("Already have host {$host}");
    }
    // Valid port assoc mode
    if (!is_valid_port_assoc_mode($port_assoc_mode)) {
        throw new InvalidPortAssocModeException("Invalid port association_mode '{$port_assoc_mode}'. Valid modes are: " . join(', ', get_port_assoc_modes()));
    }
    // check if we have the host by IP
    if ($config['addhost_alwayscheckip'] === true) {
        $ip = gethostbyname($host);
    } else {
        $ip = $host;
    }
    if (ip_exists($ip)) {
        throw new HostIpExistsException("Already have host with this IP {$host}");
    }
    // Test reachability
    if (!$force_add) {
        $address_family = snmpTransportToAddressFamily($transport);
        $ping_result = isPingable($host, $address_family);
        if (!$ping_result['result']) {
            throw new HostUnreachablePingException("Could not ping {$host}");
        }
    }
    // if $snmpver isn't set, try each version of snmp
    if (empty($snmp_version)) {
        $snmpvers = array('v2c', 'v3', 'v1');
    } else {
        $snmpvers = array($snmp_version);
    }
    $host_unreachable_exception = new HostUnreachableException("Could not connect, please check the snmp details and snmp reachability");
    // try different snmp variables to add the device
    foreach ($snmpvers as $snmpver) {
        if ($snmpver === "v3") {
            // Try each set of parameters from config
            foreach ($config['snmp']['v3'] as $v3) {
                $device = deviceArray($host, null, $snmpver, $port, $transport, $v3, $port_assoc_mode);
                if ($force_add === true || isSNMPable($device)) {
                    if ($force_add !== true) {
                        $snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
                    }
                    $result = createHost($host, null, $snmpver, $port, $transport, $v3, $poller_group, $port_assoc_mode, $snmphost, $force_add);
                    if ($result !== false) {
                        return $result;
                    }
                } else {
                    $host_unreachable_exception->addReason("SNMP {$snmpver}: No reply with credentials " . $v3['authname'] . "/" . $v3['authlevel']);
                }
            }
        } elseif ($snmpver === "v2c" || $snmpver === "v1") {
            // try each community from config
            foreach ($config['snmp']['community'] as $community) {
                $device = deviceArray($host, $community, $snmpver, $port, $transport, null, $port_assoc_mode);
                if ($force_add === true || isSNMPable($device)) {
                    if ($force_add !== true) {
                        $snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
                    }
                    $result = createHost($host, $community, $snmpver, $port, $transport, array(), $poller_group, $port_assoc_mode, $snmphost, $force_add);
                    if ($result !== false) {
                        return $result;
                    }
                } else {
                    $host_unreachable_exception->addReason("SNMP {$snmpver}: No reply with community {$community}");
                }
            }
        } else {
            throw new SnmpVersionUnsupportedException("Unsupported SNMP Version \"{$snmpver}\", must be v1, v2c, or v3");
        }
    }
    throw $host_unreachable_exception;
}