summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--modules-available/dnbd3/inc/dnbd3.inc.php13
-rw-r--r--modules-available/dnbd3/inc/dnbd3rpc.inc.php65
-rw-r--r--modules-available/dnbd3/inc/dnbd3util.inc.php8
-rw-r--r--modules-available/dnbd3/page.inc.php24
4 files changed, 54 insertions, 56 deletions
diff --git a/modules-available/dnbd3/inc/dnbd3.inc.php b/modules-available/dnbd3/inc/dnbd3.inc.php
index 83baffbf..0c640973 100644
--- a/modules-available/dnbd3/inc/dnbd3.inc.php
+++ b/modules-available/dnbd3/inc/dnbd3.inc.php
@@ -26,4 +26,17 @@ class Dnbd3 {
Property::set(self::PROP_NFS_FALLBACK, $bool ? 1 : 0);
}
+ public static function getActiveServers(): array
+ {
+ $res = Database::simpleQuery('SELECT s.serverid, m.clientip, s.fixedip
+ FROM dnbd3_server s
+ LEFT JOIN machine m ON (s.machineuuid = m.machineuuid)
+ WHERE s.lastseen > :cutoff', ['cutoff' => time() - 310]);
+ $lookup = [];
+ foreach ($res as $row) {
+ $lookup[$row['fixedip'] ?? $row['clientip'] ?? ''] = $row['serverid'];
+ }
+ return $lookup;
+ }
+
}
diff --git a/modules-available/dnbd3/inc/dnbd3rpc.inc.php b/modules-available/dnbd3/inc/dnbd3rpc.inc.php
index 9d7ba46c..a26ae4fd 100644
--- a/modules-available/dnbd3/inc/dnbd3rpc.inc.php
+++ b/modules-available/dnbd3/inc/dnbd3rpc.inc.php
@@ -2,11 +2,18 @@
class Dnbd3Rpc {
- const QUERY_UNREACHABLE = 1;
- const QUERY_NOT_200 = 2;
- const QUERY_NOT_JSON = 3;
+ const ERROR_UNREACHABLE = 1;
+ const ERROR_NOT_200 = 2;
+ const ERROR_NOT_JSON = 3;
- private static function translateServer($server)
+ const QUERY_STATS = 'stats';
+ const QUERY_CLIENTS = 'clients';
+ const QUERY_IMAGES = 'images';
+ const QUERY_SPACE = 'space';
+ const QUERY_CONFIG = 'config';
+ const QUERY_ALTSERVERS = 'altservers';
+
+ private static function translateServer(string $server): string
{
// Special case - local server
if ($server === '<self>') {
@@ -30,44 +37,24 @@ class Dnbd3Rpc {
* Query given DNBD3 server for status information.
*
* @param string $server server address
- * @param bool $stats include general stats
- * @param bool $clients include client list
- * @param bool $images include image list
- * @param bool $diskSpace include disk space stats
- * @param bool $config get config
- * @param bool $altservers list of alt servers with status
- * @return int|array the queried data as an array, or false on error
+ * @param array $queryOptions Options to query, self::QUERY_*
+ * @return int|array the queried data as an array, or error code (self::ERROR_*) on error
*/
- public static function query($server, $stats, $clients, $images, $diskSpace = false, $config = false, $altservers = false)
+ public static function query(string $server, array $queryOptions)
{
$server = self::translateServer($server);
- $url = 'http://' . $server . '/query?';
- if ($stats) {
- $url .= 'q=stats&q=version&';
- }
- if ($clients) {
- $url .= 'q=clients&';
- }
- if ($images) {
- $url .= 'q=images&';
- }
- if ($diskSpace) {
- $url .= 'q=space&';
- }
- if ($config) {
- $url .= 'q=config&';
- }
- if ($altservers) {
- $url .= 'q=altservers&';
+ $url = 'http://' . $server . '/query?q=version';
+ if (!empty($queryOptions)) {
+ $url .= '&q=' . implode('&q=', $queryOptions);
}
$str = Download::asString($url, 3, $code);
if ($str === false)
- return self::QUERY_UNREACHABLE;
+ return self::ERROR_UNREACHABLE;
if ($code !== 200)
- return self::QUERY_NOT_200;
+ return self::ERROR_NOT_200;
$ret = json_decode($str, true);
if (!is_array($ret))
- return self::QUERY_NOT_JSON;
+ return self::ERROR_NOT_JSON;
return $ret;
}
@@ -76,9 +63,9 @@ class Dnbd3Rpc {
$server = self::translateServer($server);
$str = Download::asString('http://' . $server . '/cachemap?id=' . $imgId, 3, $code);
if ($str === false)
- return self::QUERY_UNREACHABLE;
+ return self::ERROR_UNREACHABLE;
if ($code !== 200)
- return self::QUERY_NOT_200;
+ return self::ERROR_NOT_200;
return $str;
}
@@ -87,17 +74,21 @@ class Dnbd3Rpc {
* @param string[] $servers
* @return array
*/
- public static function getStatsMulti(array $servers, int $timeout = 2): array
+ public static function getStatsMulti(array $servers, array $queryOptions = [], int $timeout = 2): array
{
if (empty($servers))
return [];
+ $extra = '';
+ if (!empty($queryOptions)) {
+ $extra = '&q=' . implode('&q=', $queryOptions);
+ }
$active = [];
$mh = curl_multi_init();
curl_multi_setopt($mh, CURLMOPT_MAXCONNECTS, 8);
curl_multi_setopt($mh, CURLMOPT_MAX_HOST_CONNECTIONS, 2);
curl_multi_setopt($mh, CURLMOPT_MAX_TOTAL_CONNECTIONS, 8);
foreach ($servers as $server) {
- $url = 'http://' . self::translateServer($server) . '/query?q=stats';
+ $url = 'http://' . self::translateServer($server) . '/query?q=version' . $extra;
$res = curl_init($url);
if ($res === false) {
error_log("curl_init($url) failed with $res");
diff --git a/modules-available/dnbd3/inc/dnbd3util.inc.php b/modules-available/dnbd3/inc/dnbd3util.inc.php
index d9bfaeee..9aaa5432 100644
--- a/modules-available/dnbd3/inc/dnbd3util.inc.php
+++ b/modules-available/dnbd3/inc/dnbd3util.inc.php
@@ -64,12 +64,12 @@ class Dnbd3Util {
// Now query them all
$NOW = time();
foreach ($servers as $server) {
- $data = Dnbd3Rpc::query($server['addr'], true, false, false, true);
- if ($data === Dnbd3Rpc::QUERY_UNREACHABLE) {
+ $data = Dnbd3Rpc::query($server['addr'], [Dnbd3Rpc::QUERY_STATS, Dnbd3Rpc::QUERY_SPACE]);
+ if ($data === Dnbd3Rpc::ERROR_UNREACHABLE) {
$error = 'No (HTTP) reply from ' . $server['addr'];
- } elseif ($data === Dnbd3Rpc::QUERY_NOT_200) {
+ } elseif ($data === Dnbd3Rpc::ERROR_NOT_200) {
$error = 'No HTTP 200 OK from ' . $server['addr'];
- } elseif ($data === Dnbd3Rpc::QUERY_NOT_JSON) {
+ } elseif ($data === Dnbd3Rpc::ERROR_NOT_JSON) {
$error = 'Reply to status query is not JSON';
} elseif (!is_array($data) || !isset($data['runId'])) {
if (is_array($data) && isset($data['errorMsg'])) {
diff --git a/modules-available/dnbd3/page.inc.php b/modules-available/dnbd3/page.inc.php
index a373d0ed..7173fc10 100644
--- a/modules-available/dnbd3/page.inc.php
+++ b/modules-available/dnbd3/page.inc.php
@@ -263,7 +263,8 @@ class Page_Dnbd3 extends Page
Module::isAvailable('js_stupidtable');
$server = $this->getServerById();
Render::addTemplate('page-proxy-header', $server);
- $stats = Dnbd3Rpc::query($server['ip'], true, true, true, true, true, true);
+ $stats = Dnbd3Rpc::query($server['ip'], [Dnbd3Rpc::QUERY_STATS, Dnbd3Rpc::QUERY_CLIENTS,
+ Dnbd3Rpc::QUERY_IMAGES, Dnbd3Rpc::QUERY_SPACE, Dnbd3Rpc::QUERY_CONFIG, Dnbd3Rpc::QUERY_ALTSERVERS]);
if (!is_array($stats) || !isset($stats['runId'])) {
Message::addError('server-unreachable');
return;
@@ -515,12 +516,12 @@ class Page_Dnbd3 extends Page
if ($res !== false)
die('{"error": "Server with this IP already exists", "fatal": true}');
// Query
- $reply = Dnbd3Rpc::query($ip,true, false, false, true);
- if ($reply === Dnbd3Rpc::QUERY_UNREACHABLE)
+ $reply = Dnbd3Rpc::query($ip, [Dnbd3Rpc::QUERY_STATS, Dnbd3Rpc::QUERY_SPACE]);
+ if ($reply === Dnbd3Rpc::ERROR_UNREACHABLE)
die('{"error": "Could not reach server"}');
- if ($reply === Dnbd3Rpc::QUERY_NOT_200)
+ if ($reply === Dnbd3Rpc::ERROR_NOT_200)
die('{"error": "Server did not reply with 200 OK"}');
- if ($reply === Dnbd3Rpc::QUERY_NOT_JSON)
+ if ($reply === Dnbd3Rpc::ERROR_NOT_JSON)
die('{"error": "No JSON received from server"}');
if (!is_array($reply) || !isset($reply['uptime']) || !isset($reply['clientCount']))
die('{"error": "Reply does not suggest this is a dnbd3 server"}');
@@ -607,11 +608,11 @@ class Page_Dnbd3 extends Page
die('Invalid/no image id');
}
$data = Dnbd3Rpc::getCacheMap($server['ip'], $imgId);
- if ($data === Dnbd3Rpc::QUERY_UNREACHABLE) {
+ if ($data === Dnbd3Rpc::ERROR_UNREACHABLE) {
Header('HTTP/1.1 504 Gateway Timeout');
die('Proxy not reachable');
}
- if ($data === Dnbd3Rpc::QUERY_NOT_200) {
+ if ($data === Dnbd3Rpc::ERROR_NOT_200) {
Header('HTTP/1.1 503 Service Unavailable');
die("Proxy didn't reply with 200 OK");
}
@@ -621,14 +622,7 @@ class Page_Dnbd3 extends Page
private function ajaxStats()
{
- $res = Database::simpleQuery('SELECT s.serverid, m.clientip, s.fixedip
- FROM dnbd3_server s
- LEFT JOIN machine m ON (s.machineuuid = m.machineuuid)
- WHERE s.lastseen > :cutoff', ['cutoff' => time() - 310]);
- $lookup = [];
- foreach ($res as $row) {
- $lookup[$row['fixedip'] ?? $row['clientip'] ?? ''] = $row['serverid'];
- }
+ $lookup = Dnbd3::getActiveServers();
$result = Dnbd3Rpc::getStatsMulti(array_keys($lookup));
$return = [];
foreach ($result as $ip => $data) {