summaryrefslogtreecommitdiffstats
path: root/modules-available/dnbd3/inc/dnbd3rpc.inc.php
blob: 9d7ba46c61787e9067441937eb5b8a14a4e49d52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php

class Dnbd3Rpc {

	const QUERY_UNREACHABLE = 1;
	const QUERY_NOT_200 = 2;
	const QUERY_NOT_JSON = 3;

	private static function translateServer($server)
	{
		// Special case - local server
		if ($server === '<self>') {
			$server = '127.0.0.1:5003';
		} elseif (($out = Dnbd3Util::matchAddress($server))) {
			if (isset($out['v4'])) {
				$server = $out['v4'];
			} else {
				$server = '[' . $out['v6'] . ']';
			}
			if (isset($out['port'])) {
				$server .= $out['port'];
			} else {
				$server .= ':5003';
			}
		}
		return $server;
	}

	/**
	 * 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
	 */
	public static function query($server, $stats, $clients, $images, $diskSpace = false, $config = false, $altservers = false)
	{
		$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&';
		}
		$str = Download::asString($url, 3, $code);
		if ($str === false)
			return self::QUERY_UNREACHABLE;
		if ($code !== 200)
			return self::QUERY_NOT_200;
		$ret = json_decode($str, true);
		if (!is_array($ret))
			return self::QUERY_NOT_JSON;
		return $ret;
	}

	public static function getCacheMap($server, $imgId)
	{
		$server = self::translateServer($server);
		$str = Download::asString('http://' . $server . '/cachemap?id=' . $imgId, 3, $code);
		if ($str === false)
			return self::QUERY_UNREACHABLE;
		if ($code !== 200)
			return self::QUERY_NOT_200;
		return $str;
	}

	/**
	 * Get statistics for multiple servers at once.
	 * @param string[] $servers
	 * @return array
	 */
	public static function getStatsMulti(array $servers, int $timeout = 2): array
	{
		if (empty($servers))
			return [];
		$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';
			$res = curl_init($url);
			if ($res === false) {
				error_log("curl_init($url) failed with $res");
				continue;
			}
			curl_setopt_array($res, [
				CURLOPT_CONNECTTIMEOUT => $timeout,
				CURLOPT_TIMEOUT => $timeout,
				CURLOPT_FOLLOWLOCATION => 0,
				CURLOPT_ACCEPT_ENCODING => '', // Use everything libcurl supports
				CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
				CURLOPT_RETURNTRANSFER => 1,
			]);
			$err = curl_multi_add_handle($mh, $res);
			if ($err !== 0) {
				error_log("curl_multi_add_handle() failed with $err");
				curl_close($res);
			} else {
				$active[(int)$res] = $server;
			}
		}
		// Wait
		$running = 1;
		$result = [];
		$startTime = microtime(true);
		for (;;) {
			$ret = curl_multi_exec($mh, $running);
			while (($info = curl_multi_info_read($mh)) !== false) {
				if ($info['msg'] === CURLMSG_DONE) {
					if (isset($active[(int)$info['handle']])) {
						$server = $active[(int)$info['handle']];
						unset($active[(int)$info['handle']]);
						if ($info['result'] === CURLE_OK) {
							$data = json_decode(curl_multi_getcontent($info['handle']), true);
							if (is_array($data)) {
								$data['ts'] = microtime(true);
								$result[$server] = $data;
							}
						}
					}
					curl_multi_remove_handle($mh, $info['handle']);
					curl_close($info['handle']);
				}
			}
			$delay = ($startTime + $timeout) - microtime(true);
			if ($ret !== CURLM_OK || !$running || $delay <= 0)
				break;
			$sret = curl_multi_select($mh, $delay);
			if ($sret < 0) {
				error_log("curl_multi_select returned $sret");
				break;
			}
		}
		curl_multi_close($mh);
		return $result;
	}

}