function discover_new_device($hostname, $source = 'xdp', $protocol = NULL, $device = NULL, $port = NULL)
{
    global $config, $debug;
    # FIXME remodel function a bit like the one above? refactor so they share some parts?
    if ($config['autodiscovery'][$source]) {
        echo "Discovering new host {$hostname}\n";
        if (!empty($config['mydomain']) && isDomainResolves($hostname . "." . $config['mydomain'])) {
            if ($debug) {
                echo "appending " . $config['mydomain'] . "!\n";
            }
            $dst_host = $hostname . "." . $config['mydomain'];
        } else {
            $dst_host = $hostname;
        }
        $ip = gethostbyname($dst_host);
        if ($debug) {
            echo "resolving {$dst_host} to {$ip}\n";
        }
        if (match_network($config['autodiscovery']['ip_nets'], $ip)) {
            if ($debug) {
                echo "found {$ip} inside configured nets, adding!\n";
            }
            $remote_device_id = add_device($dst_host);
            if ($remote_device_id) {
                $remote_device = device_by_id_cache($remote_device_id, 1);
                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);
                }
                array_push($GLOBALS['devices'], $remote_device);
                return $remote_device_id;
            }
        }
    } else {
        if ($debug) {
            echo "{$source} autodiscovery disabled";
        }
        return FALSE;
    }
}
function add_device($hostname, $snmp_version = array(), $snmp_port = 161, $snmp_transport = 'udp', $options = array())
{
    global $config;
    // If $options['break'] set as TRUE, break recursive function execute
    if (isset($options['break']) && $options['break']) {
        return FALSE;
    }
    $return = FALSE;
    // By default return FALSE
    // Reset snmp timeout and retries options for speedup device adding
    unset($config['snmp']['timeout'], $config['snmp']['retries']);
    $hostname = trim($hostname);
    list($hostshort) = explode(".", $hostname);
    // Test if host exists in database
    if (dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = ?", array($hostname)) == '0') {
        $snmp_transport = strtolower($snmp_transport);
        $try_a = !($snmp_transport == 'udp6' || $snmp_transport == 'tcp6');
        // Use IPv6 only if transport 'udp6' or 'tcp6'
        // Test DNS lookup.
        $ip = gethostbyname6($hostname, $try_a);
        if ($ip) {
            $ip_version = get_ip_version($ip);
            // Test reachability
            if (isPingable($hostname, $try_a)) {
                // Test directory exists in /rrd/
                if (!$config['rrd_override'] && file_exists($config['rrd_dir'] . '/' . $hostname)) {
                    print_error("Directory <observium>/rrd/{$hostname} already exists.");
                    return FALSE;
                }
                // Detect snmp transport
                if (stripos($snmp_transport, 'tcp') !== FALSE) {
                    $snmp_transport = $ip_version == 4 ? 'tcp' : 'tcp6';
                } else {
                    $snmp_transport = $ip_version == 4 ? 'udp' : 'udp6';
                }
                // Detect snmp port
                if (!is_numeric($snmp_port) || $snmp_port < 1 || $snmp_port > 65535) {
                    $snmp_port = 161;
                } else {
                    $snmp_port = (int) $snmp_port;
                }
                // Detect snmp version
                if (empty($snmp_version)) {
                    // Here set default snmp version order
                    $i = 1;
                    $snmp_version_order = array();
                    foreach (array('v2c', 'v3', 'v1') as $tmp_version) {
                        if ($config['snmp']['version'] == $tmp_version) {
                            $snmp_version_order[0] = $tmp_version;
                        } else {
                            $snmp_version_order[$i] = $tmp_version;
                        }
                        $i++;
                    }
                    ksort($snmp_version_order);
                    foreach ($snmp_version_order as $tmp_version) {
                        $ret = add_device($hostname, $tmp_version, $snmp_port, $snmp_transport, $options);
                        if ($ret === FALSE) {
                            // Set $options['break'] for break recursive
                            $options['break'] = TRUE;
                        } else {
                            if (is_numeric($ret) && $ret != 0) {
                                return $ret;
                            }
                        }
                    }
                } else {
                    if ($snmp_version === "v3") {
                        // Try each set of parameters from config
                        foreach ($config['snmp']['v3'] as $snmp_v3) {
                            $device = build_initial_device_array($hostname, NULL, $snmp_version, $snmp_port, $snmp_transport, $snmp_v3);
                            print_message("Trying v3 parameters " . $device['snmp_authname'] . "/" . $device['snmp_authlevel'] . " ... ");
                            if (isSNMPable($device)) {
                                if (!check_device_duplicated($device)) {
                                    if (isset($options['test']) && $options['test']) {
                                        print_message('%WDevice "' . $hostname . '" has successfully been tested and available by ' . strtoupper($snmp_transport) . ' transport with SNMP ' . $snmp_version . ' credentials.%n', 'color');
                                        $device_id = -1;
                                    } else {
                                        $device_id = createHost($hostname, NULL, $snmp_version, $snmp_port, $snmp_transport, $snmp_v3);
                                    }
                                    return $device_id;
                                }
                            } else {
                                print_warning("No reply on credentials " . $device['snmp_authname'] . "/" . $device['snmp_authlevel'] . " using {$snmp_version}.");
                            }
                        }
                    } else {
                        if ($snmp_version === "v2c" || $snmp_version === "v1") {
                            // Try each community from config
                            foreach ($config['snmp']['community'] as $snmp_community) {
                                $device = build_initial_device_array($hostname, $snmp_community, $snmp_version, $snmp_port, $snmp_transport);
                                print_message("Trying {$snmp_version} community {$snmp_community} ...");
                                if (isSNMPable($device)) {
                                    if (!check_device_duplicated($device)) {
                                        if (isset($options['test']) && $options['test']) {
                                            print_message('%W设备 "' . $hostname . '" 已成功地测试和可用于 ' . strtoupper($snmp_transport) . ' transport with SNMP ' . $snmp_version . ' credentials.%n', 'color');
                                            $device_id = -1;
                                        } else {
                                            $device_id = createHost($hostname, $snmp_community, $snmp_version, $snmp_port, $snmp_transport);
                                        }
                                        return $device_id;
                                    }
                                } else {
                                    print_warning("No reply on community {$snmp_community} using {$snmp_version}.");
                                    $return = 0;
                                    // Return zero for continue trying next auth
                                }
                            }
                        } else {
                            print_error("Unsupported SNMP Version \"{$snmp_version}\".");
                            $return = 0;
                            // Return zero for continue trying next auth
                        }
                    }
                }
                if (!$device_id) {
                    // Failed SNMP
                    print_error("无法到达主机 {$hostname} 使用给定的SNMP参数 {$snmp_version}.");
                    $return = 0;
                    // Return zero for continue trying next auth
                }
            } else {
                // failed Reachability
                print_error("无法 ping {$hostname}.");
            }
        } else {
            // Failed DNS lookup
            print_error("无法解析 {$hostname}.");
        }
    } else {
        // found in database
        print_error("已经发现设备 {$hostname}.");
    }
    return $return;
}
示例#3
0
            }
            $snmpver = $_POST['snmpver'];
            print_message("Adding host {$hostname} communit" . (count($config['snmp']['community']) == 1 ? "y" : "ies") . " " . implode(', ', $config['snmp']['community']) . " port {$port}");
        } elseif ($_POST['snmpver'] === "v3") {
            $v3 = array('authlevel' => $_POST['authlevel'], 'authname' => $_POST['authname'], 'authpass' => $_POST['authpass'], 'authalgo' => $_POST['authalgo'], 'cryptopass' => $_POST['cryptopass'], 'cryptoalgo' => $_POST['cryptoalgo']);
            array_push($config['snmp']['v3'], $v3);
            $snmpver = "v3";
            print_message("Adding SNMPv3 host {$hostname} port {$port}");
        } else {
            print_error("Unsupported SNMP Version. There was a dropdown menu, how did you reach this error ?");
            // We have a hacker!
        }
        if ($_POST['ignorerrd'] == 'confirm') {
            $config['rrd_override'] = TRUE;
        }
        $result = add_device($hostname, $snmpver, $port);
        if ($result) {
            print_success("Device added (id = {$result})");
        }
    } else {
        print_error("You don't have the necessary privileges to add hosts.");
    }
}
$pagetitle[] = "Add host";
?>

<form name="form1" method="post" action="" class="form-horizontal">

  <p>Devices will be checked for Ping and SNMP reachability before being probed. Only devices with recognised OSes will be added.</p>

  <fieldset>
示例#4
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']);
    $host = trim($host);
    list($hostshort) = explode(".", $host);
    // Test if host exists in database
    if (dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = ?", array($host)) == '0') {
        $transport = strtolower($transport);
        $try_a = !($transport == 'udp6' || $transport == 'tcp6');
        // Use IPv6 only if transport 'udp6' or 'tcp6'
        // Test DNS lookup.
        $ip = gethostbyname6($host, $try_a);
        if ($ip) {
            $ip_version = get_ip_version($ip);
            // Test reachability
            if (isPingable($host)) {
                // Test directory exists in /rrd/
                if (!$config['rrd_override'] && file_exists($config['rrd_dir'] . '/' . $host)) {
                    print_error("Directory <observium>/rrd/{$host} already exists.");
                    return FALSE;
                }
                // 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;
                }
                // Detect snmp version
                if (empty($snmpver)) {
                    // 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) {
                        $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("No reply on credentials " . $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);
                        print_message("Trying {$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("No reply on community {$community} using {$snmpver}.");
                        }
                    }
                } else {
                    print_error("Unsupported SNMP Version \"{$snmpver}\".");
                }
                if (!$device_id) {
                    // Failed SNMP
                    print_error("Could not reach {$host} with given SNMP parameters using {$snmpver}.");
                }
            } else {
                // failed Reachability
                print_error("Could not ping {$host}.");
            }
        } else {
            // Failed DNS lookup
            print_error("Could not resolve {$host}.");
        }
    } else {
        // found in database
        print_error("Already got device {$host}.");
    }
    return FALSE;
}
示例#5
0
function parse_user($str, $game_id, $client_id, $options)
{
    global $fhs;
    global $fhe;
    global $events;
    global $parms;
    global $devices;
    debugger("Memory Usage: parse_user: "******"\n");
    $json = json_decode($str);
    if ($json != FALSE) {
        foreach ($json as $key => $value) {
            switch ($key) {
                case 'u':
                    $user = $value;
                    break;
                case 'v':
                    $version = $value;
                    break;
                case 'dv':
                    $device = $value;
                    if (!array_key_exists($device, $devices)) {
                        $devices = add_device($device);
                    }
                    break;
                case 't':
                    $epoch = round($value / 1000);
                    $datetime = new DateTime("@{$epoch}", new DateTimeZone('EST'));
                    break;
                case 'l':
                    $event = parse_events($epoch, $value);
                    break;
            }
        }
        //SESSION
        $record = "{$game_id},{$client_id},'" . $user . "','" . $version . "'," . $devices[$device] . ",'" . $datetime->format('Y-m-d H:i:s') . "'\n";
        fwrite($fhs, $record);
        //EVENTS
        if (!isset($options['u'])) {
            foreach ($event as $key => $value) {
                if (array_key_exists(strtolower($value['event']), $events)) {
                    $record = "{$game_id},{$client_id},'" . $user . "','" . $version . "'," . $devices[$device] . ",'" . $value['time'] . "'," . $events[strtolower($value['event'])] . ",";
                } else {
                    $events = add_event(strtolower($value['event']));
                    $record = "{$game_id},{$client_id},'" . $user . "','" . $version . "'," . $devices[$device] . ",'" . $value['time'] . "'," . $events[strtolower($value['event'])] . ",";
                }
                if (count($value['parameters']) > 0) {
                    foreach ($value['parameters'] as $parameter_key => $parameter_value) {
                        if ($parameter_value == '') {
                            $parameter_value = 'NULL';
                        } else {
                            $parameter_value = "'" . $parameter_value . "'";
                        }
                        if (array_key_exists(strtolower($parameter_key), $parms)) {
                            fwrite($fhe, $record . $parms[strtolower($parameter_key)] . "," . $parameter_value . "\n");
                        } else {
                            $parms = add_parm(strtolower($parameter_key));
                            fwrite($fhe, $record . $parms[strtolower($parameter_key)] . "," . $parameter_value . "\n");
                        }
                    }
                } else {
                    fwrite($fhe, $record . "NULL,NULL\n");
                }
            }
        }
        unset($datetime);
    }
    unset($json);
}
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;
}
示例#7
0
                // parse all remaining args
                if (is_numeric($arg)) {
                    $port = $arg;
                } elseif (preg_match('/(' . implode("|", $config['snmp']['transports']) . ')/i', $arg)) {
                    $transport = $arg;
                } elseif (preg_match('/^(v1|v2c)$/i', $arg)) {
                    $snmpver = $arg;
                }
            }
            $config['snmp']['community'] = $community ? array($community) : $snmp_config_community;
        }
        print_message("Try to add {$host}:");
        if ($snmpver) {
            $device_id = add_device($host, $snmpver, $port, $transport);
        } else {
            $device_id = add_device($host, NULL, $port, $transport);
        }
        if ($device_id) {
            $device = device_by_id_cache($device_id);
            print_success("Added device " . $device['hostname'] . " (" . $device_id . ").");
            $added++;
        }
    }
}
$count = count($add_array);
$failed = $count - $added;
if ($added) {
    print_message("\nDevices added: {$added}.");
    if ($failed) {
        print_message("Devices skipped: {$failed}.");
    }
示例#8
0
                    $snmp_port = $arg;
                } elseif (preg_match('/(' . implode("|", $config['snmp']['transports']) . ')/i', $arg)) {
                    $snmp_transport = $arg;
                } elseif (preg_match('/^(v1|v2c)$/i', $arg)) {
                    $snmp_version = $arg;
                }
            }
            $config['snmp']['community'] = $snmp_community ? array($snmp_community) : $snmp_config_community;
        }
        print_message("Try to add {$hostname}:");
        if (in_array($snmp_version, array('v1', 'v2c', 'v3'))) {
            // If snmp version passed in arguments, then use the exact version
            $device_id = add_device($hostname, $snmp_version, $snmp_port, $snmp_transport, $snmp_options);
        } else {
            // If snmp version unknown ckeck all possible snmp versions and auth options
            $device_id = add_device($hostname, NULL, $snmp_port, $snmp_transport, $snmp_options);
        }
        if ($device_id) {
            if (!isset($options['t'])) {
                $device = device_by_id_cache($device_id);
                print_success("Added device " . $device['hostname'] . " (" . $device_id . ").");
            }
            // Else this is device testing, success message already written by add_device()
            $added++;
        }
    }
}
$count = count($add_array);
$failed = $count - $added;
if ($added) {
    print_message("\nDevices success: {$added}.");
示例#9
0
function add_device($hostname, $snmp_version = array(), $snmp_port = 161, $snmp_transport = 'udp', $options = array(), $flags = OBS_DNS_ALL)
{
    global $config;
    // If $options['break'] set as TRUE, break recursive function execute
    if (isset($options['break']) && $options['break']) {
        return FALSE;
    }
    $return = FALSE;
    // By default return FALSE
    // Reset snmp timeout and retries options for speedup device adding
    unset($config['snmp']['timeout'], $config['snmp']['retries']);
    $snmp_transport = strtolower($snmp_transport);
    $hostname = strtolower(trim($hostname));
    // Try detect if hostname is IP
    switch (get_ip_version($hostname)) {
        case 6:
            $hostname = Net_IPv6::compress($hostname, TRUE);
            // Always use compressed IPv6 name
        // Always use compressed IPv6 name
        case 4:
            if ($config['require_hostname']) {
                print_error("Hostname should be a valid resolvable FQDN name. Or set config option \$config['require_hostname'] as FALSE.");
                return $return;
            }
            $ip = $hostname;
            break;
        default:
            if ($snmp_transport == 'udp6' || $snmp_transport == 'tcp6') {
                $flags = $flags ^ OBS_DNS_A;
                // exclude A
            }
            // Test DNS lookup.
            $ip = gethostbyname6($hostname, $flags);
    }
    // Test if host exists in database
    if (dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = ?", array($hostname)) == '0') {
        if ($ip) {
            $ip_version = get_ip_version($ip);
            // Test reachability
            $options['ping_skip'] = isset($options['ping_skip']) && $options['ping_skip'];
            if ($options['ping_skip']) {
                $flags = $flags | OBS_PING_SKIP;
            }
            if (isPingable($hostname, $flags)) {
                // Test directory exists in /rrd/
                if (!$config['rrd_override'] && file_exists($config['rrd_dir'] . '/' . $hostname)) {
                    print_error("Directory <observium>/rrd/{$hostname} already exists.");
                    return FALSE;
                }
                // Detect snmp transport
                if (stripos($snmp_transport, 'tcp') !== FALSE) {
                    $snmp_transport = $ip_version == 4 ? 'tcp' : 'tcp6';
                } else {
                    $snmp_transport = $ip_version == 4 ? 'udp' : 'udp6';
                }
                // Detect snmp port
                if (!is_numeric($snmp_port) || $snmp_port < 1 || $snmp_port > 65535) {
                    $snmp_port = 161;
                } else {
                    $snmp_port = (int) $snmp_port;
                }
                // Detect snmp version
                if (empty($snmp_version)) {
                    // Here set default snmp version order
                    $i = 1;
                    $snmp_version_order = array();
                    foreach (array('v2c', 'v3', 'v1') as $tmp_version) {
                        if ($config['snmp']['version'] == $tmp_version) {
                            $snmp_version_order[0] = $tmp_version;
                        } else {
                            $snmp_version_order[$i] = $tmp_version;
                        }
                        $i++;
                    }
                    ksort($snmp_version_order);
                    foreach ($snmp_version_order as $tmp_version) {
                        $ret = add_device($hostname, $tmp_version, $snmp_port, $snmp_transport, $options);
                        if ($ret === FALSE) {
                            // Set $options['break'] for break recursive
                            $options['break'] = TRUE;
                        } else {
                            if (is_numeric($ret) && $ret != 0) {
                                return $ret;
                            }
                        }
                    }
                } else {
                    if ($snmp_version === "v3") {
                        // Try each set of parameters from config
                        foreach ($config['snmp']['v3'] as $snmp_v3) {
                            $device = build_initial_device_array($hostname, NULL, $snmp_version, $snmp_port, $snmp_transport, $snmp_v3);
                            print_message("Trying v3 parameters " . $device['snmp_authname'] . "/" . $device['snmp_authlevel'] . " ... ");
                            if (isSNMPable($device)) {
                                if (!check_device_duplicated($device)) {
                                    if (isset($options['test']) && $options['test']) {
                                        print_message('%WDevice "' . $hostname . '" has successfully been tested and available by ' . strtoupper($snmp_transport) . ' transport with SNMP ' . $snmp_version . ' credentials.%n', 'color');
                                        $device_id = -1;
                                    } else {
                                        $device_id = createHost($hostname, NULL, $snmp_version, $snmp_port, $snmp_transport, $snmp_v3);
                                        if ($options['ping_skip']) {
                                            set_entity_attrib('device', $device_id, 'ping_skip', 1);
                                            // Force pingable check
                                            if (isPingable($hostname, $flags ^ OBS_PING_SKIP)) {
                                                print_warning("You passed option for skip device is pingable checks, but device available by ismp echo. Check device preferences.");
                                            }
                                        }
                                    }
                                    return $device_id;
                                }
                            } else {
                                print_warning("No reply on credentials " . $device['snmp_authname'] . "/" . $device['snmp_authlevel'] . " using {$snmp_version}.");
                            }
                        }
                    } else {
                        if ($snmp_version === "v2c" || $snmp_version === "v1") {
                            // Try each community from config
                            foreach ($config['snmp']['community'] as $snmp_community) {
                                $device = build_initial_device_array($hostname, $snmp_community, $snmp_version, $snmp_port, $snmp_transport);
                                print_message("Trying {$snmp_version} community {$snmp_community} ...");
                                if (isSNMPable($device)) {
                                    if (!check_device_duplicated($device)) {
                                        if (isset($options['test']) && $options['test']) {
                                            print_message('%WDevice "' . $hostname . '" has successfully been tested and available by ' . strtoupper($snmp_transport) . ' transport with SNMP ' . $snmp_version . ' credentials.%n', 'color');
                                            $device_id = -1;
                                        } else {
                                            $device_id = createHost($hostname, $snmp_community, $snmp_version, $snmp_port, $snmp_transport);
                                            if ($options['ping_skip']) {
                                                set_entity_attrib('device', $device_id, 'ping_skip', 1);
                                                // Force pingable check
                                                if (isPingable($hostname, $flags ^ OBS_PING_SKIP)) {
                                                    print_warning("You passed option for skip device is pingable checks, but device available by ismp echo. Check device preferences.");
                                                }
                                            }
                                        }
                                        return $device_id;
                                    }
                                } else {
                                    print_warning("No reply on community {$snmp_community} using {$snmp_version}.");
                                    $return = 0;
                                    // Return zero for continue trying next auth
                                }
                            }
                        } else {
                            print_error("Unsupported SNMP Version \"{$snmp_version}\".");
                            $return = 0;
                            // Return zero for continue trying next auth
                        }
                    }
                }
                if (!$device_id) {
                    // Failed SNMP
                    print_error("Could not reach {$hostname} with given SNMP parameters using {$snmp_version}.");
                    $return = 0;
                    // Return zero for continue trying next auth
                }
            } else {
                // failed Reachability
                print_error("Could not ping {$hostname}.");
            }
        } else {
            // Failed DNS lookup
            print_error("Could not resolve {$hostname}.");
        }
    } else {
        // found in database
        print_error("Already got device {$hostname}.");
    }
    return $return;
}
示例#10
0
function dbase_parse($cmd, $message)
{
    global $pisql, $dbhost, $dbuser, $dbpass, $dbname;
    global $log;
    // Used by daemon
    global $apperr, $appmsg;
    // For ajax usage
    if (!$pisql || !$pisql->ping()) {
        $log->lwrite("dbase_parse:: Ping failed, making new pisql connection to server", 1);
        $pisql = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
    }
    // If there is an error
    if ($pisql->connect_errno) {
        $log->lwrite("dbase_parse:: Failed to connect to MySQL: (" . $pisql->connect_errno . ") " . $pisql->connect_error, 1);
        return -1;
    }
    // For logging only
    if (is_array($message)) {
        $str = "";
        foreach ($message as $key => $val) {
            $str .= " {" . $key . ":" . $val . "},";
        }
        $log->lwrite("dbase_parse:: " . $cmd . ": " . $str, 1);
    } else {
        $log->lwrite("dbase_parse:: message: " . $cmd . ": " . $message, 1);
    }
    //
    // Depending on $cmd execute database function
    switch ($cmd) {
        // Database
        case "load_database":
            $ret = load_database();
            break;
            // Device
        // Device
        case "load_devices":
            $ret = load_devices();
            break;
        case "add_device":
            $ret = add_device($message);
            break;
        case "delete_device":
            $ret = delete_device($message);
            break;
        case "store_device":
            $ret = store_device($message);
            break;
            // Room
        // Room
        case "add_room":
            $ret = add_room($message);
            break;
        case "delete_room":
            $ret = delete_room($message);
            break;
            // Scene
        // Scene
        case "read_scene":
            $ret = load_scene($message);
            break;
        case "load_scenes":
            $ret = load_scenes();
            break;
        case "add_scene":
            $ret = add_scene($message);
            break;
        case "delete_scene":
            $ret = delete_scene($message);
            break;
        case "upd_scene":
            $ret = upd_scene($message);
            break;
        case "store_scene":
            $ret = store_scene($message);
            break;
            // Timer
        // Timer
        case "add_timer":
            $ret = add_timer($message);
            break;
        case "delete_timer":
            $ret = delete_timer($message);
            break;
        case "store_timer":
            $ret = store_timer($message);
            break;
            // Handset
        // Handset
        case "add_handset":
            $ret = add_handset($message);
            break;
        case "delete_handset":
            $ret = delete_handset($message);
            break;
        case "store_handset":
            $ret = store_handset($message);
            break;
            // Weather
        // Weather
        case "add_weather":
            $ret = add_weather($message);
            break;
        case "delete_weather":
            $ret = delete_weather($message);
            break;
            // Setting
        // Setting
        case "store_setting":
            $ret = store_setting($message);
            break;
        default:
    }
    if ($ret >= 0) {
        // Prepare structure to send back to the calling ajax client (in stdout)
        $send = array('tcnt' => $ret, 'appmsg' => $appmsg, 'status' => 'OK', 'apperr' => $apperr);
        $output = json_encode($send);
    } else {
        //	Functions need to fill apperr themselves!
        $send = array('tcnt' => $ret, 'appmsg' => $appmsg, 'status' => 'ERR', 'apperr' => $apperr);
        $output = json_encode($send);
    }
    return $output;
}
#!/usr/bin/php
<?php 
include "includes/defaults.inc.php";
include "config.php";
include "includes/functions.php";
$search = $argv[1] . "\$";
$data = trim(`cat ips-scanned.txt | grep alive | cut -d" " -f 1 | egrep {$search}`);
foreach (explode("\n", $data) as $ip) {
    $snmp = shell_exec("snmpget -t 0.2 -v2c -c " . $config['community'] . " {$ip} sysName.0");
    if (strstr($snmp, "STRING")) {
        $hostname = trim(str_replace("SNMPv2-MIB::sysName.0 = STRING: ", "", $snmp));
        if (mysql_result(mysql_query("SELECT COUNT(device_id) FROM devices WHERE hostname = '{$hostname}'"), 0) == '0') {
            if (gethostbyname($hostname) == gethostbyname($hostname . "." . $config['mydomain'])) {
                $hostname = $hostname . "." . $config['mydomain'];
            }
            add_device($hostname, $snmp_community, 'v2c');
            echo "添加 {$hostname} \n";
        }
    }
}
示例#12
0
                array_unshift($config['snmp']['v3'], $snmp_v3);
                $snmp_version = "v3";
                print_message("Adding SNMPv3 host {$hostname} port {$snmp_port}");
            } else {
                print_error("Unsupported SNMP Version. There was a dropdown menu, how did you reach this error?");
                // We have a hacker!
            }
        }
        if ($vars['ignorerrd'] == 'confirm' || $vars['ignorerrd'] == '1' || $vars['ignorerrd'] == 'on') {
            $config['rrd_override'] = TRUE;
        }
        $snmp_options = array();
        if ($vars['ping_skip'] == '1' || $vars['ping_skip'] == 'on') {
            $snmp_options['ping_skip'] = TRUE;
        }
        $result = add_device($hostname, $snmp_version, $snmp_port, strip_tags($vars['snmp_transport']), $snmp_options);
        if ($result) {
            print_success("Device added (id = {$result})");
        }
    } else {
        print_error("You don't have the necessary privileges to add hosts.");
    }
} else {
    // Defaults
    switch ($vars['snmp_version']) {
        case 'v1':
        case 'v2c':
        case 'v3':
            $snmp_version = $vars['snmp_version'];
            break;
        default:
示例#13
0
function dbase_parse($cmd, $message)
{
    //
    switch ($cmd) {
        case "add_device":
            $ret = add_device($message);
            break;
        case "delete_device":
            $ret = delete_device($message);
            break;
        case "add_room":
            $ret = add_room($message);
            break;
        case "delete_room":
            $ret = delete_room($message);
            break;
        case "add_scene":
            break;
        case "delete_scene":
            break;
        case "upd_scene":
            break;
        case "add_timer":
            break;
        case "delete_timer":
            break;
        case "store_timer":
            break;
        case "add_handset":
            break;
        case "delete_handset":
            break;
        case "store_handset":
            break;
        case "add_weather":
            break;
        case "delete_weather":
            break;
        case "store_setting":
            break;
        default:
    }
    if ($ret >= 0) {
        // Prepare structure to send back to the calling ajax client (in stdout)
        $send = array('tcnt' => $ret, 'appmsg' => $appmsg, 'status' => 'OK', 'apperr' => $apperr);
        $output = json_encode($send);
    } else {
        //	Functions need to fill apperr themselves!
        $send = array('tcnt' => $ret, 'appmsg' => $appmsg, 'status' => 'ERR', 'apperr' => $apperr);
        $output = json_encode($send);
    }
    return $output;
}