summaryrefslogtreecommitdiffstats
path: root/modules-available/statistics/api.inc.php
blob: c395220a2e838b00eb9f63c4ea47fdc9592bf72a (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php

if (empty($_POST['type'])) die('Missing options.');
$type = mb_strtolower($_POST['type']);

$ip = $_SERVER['REMOTE_ADDR'];
if (substr($ip, 0, 7) === '::ffff:') $ip = substr($ip, 7);

/*
 * Section 1/2
 * Power/hw/usage stats
 */

if ($type{0} === '~') {
	// UUID is mandatory
	$uuid = Request::post('uuid', '', 'string');
	if (strlen($uuid) !== 36) die("Invalid UUID.\n");
	$macaddr = Request::post('macaddr', '', 'string');
	if (!empty($macaddr) && substr($uuid, 0, 16) === '000000000000001-') {
		// Override uuid if the mac is known and unique
		$res = Database::simpleQuery('SELECT machineuuid FROM machine WHERE macaddr = :macaddr AND machineuuid <> :uuid', compact('macaddr', 'uuid'));
		$override = false;
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			if ($override !== false) {
				$override = false;
				break;
			}
			$override = $row['machineuuid'];
		}
		if ($override !== false) {
			$uuid = $override;
		}
	}
	// External mode of operation?
	$mode = Request::post('mode', false, 'string');
	$NOW = time();
	$old = Database::queryFirst('SELECT clientip, logintime, lastseen, lastboot FROM machine WHERE machineuuid = :uuid', array('uuid' => $uuid));
	if ($old !== false) {
		settype($old['logintime'], 'integer');
		settype($old['lastseen'], 'integer');
		settype($old['lastboot'], 'integer');
	}
	// Handle event type
	if ($mode === false && $type === '~poweron') {
		// Poweron & hw stats
		$uptime = Request::post('uptime', '', 'integer');
		if (strlen($macaddr) > 17) die("Invalid MAC.\n");
		if ($uptime < 0 || $uptime > 4000000) die("Implausible uptime.\n");
		$realcores = Request::post('realcores', 0, 'integer');
		if ($realcores < 0 || $realcores > 512) $realcores = 0;
		$mbram = Request::post('mbram', 0, 'integer');
		if ($mbram < 0 || $mbram > 2048000) $mbram = 0;
		$kvmstate = Request::post('kvmstate', 'UNKNOWN', 'string');
		$valid = array('UNKNOWN', 'UNSUPPORTED', 'DISABLED', 'ENABLED');
		if (!in_array($kvmstate, $valid)) $kvmstate = 'UNKNOWN';
		$cpumodel = Request::post('cpumodel', '', 'string');
		$systemmodel = Request::post('systemmodel', '', 'string');
		$id44mb = Request::post('id44mb', 0, 'integer');
		if ($id44mb < 0 || $id44mb > 10240000) $id44mb = 0;
		$badsectors = Request::post('badsectors', 0, 'integer');
		if ($badsectors < 0 || $badsectors > 100000) $badsectors = 0;
		$hostname = gethostbyaddr($ip);
		if (!is_string($hostname) || $hostname === $ip) {
			$hostname = '';
		}
		$data = Request::post('data', '', 'string');
		if ($uptime < 120) {
			// See if we have a lingering session, create statistic entry if so
			if ($old !== false && $old['logintime'] !== 0) {
				$sessionLength = $old['lastseen'] - $old['logintime'];
				if ($sessionLength > 30 && $sessionLength < 86400*2) {
					Database::exec('INSERT INTO statistic (dateline, typeid, machineuuid, clientip, username, data)'
						. " VALUES (:start, '~session-length', :uuid, :clientip, '', :length)", array(
						'start'     => $old['logintime'],
						'uuid'     => $uuid,
						'clientip'  => $ip,
						'length'    => $sessionLength
					));
				}
			}
			// Write poweroff period length to statistic table
			if ($old !== false && $old['lastseen'] !== 0) {
				$lastSeen = $old['lastseen'];
				$offtime = ($NOW - $uptime) - $lastSeen;
				if ($offtime > 300 && $offtime < 86400 * 90) {
					Database::exec('INSERT INTO statistic (dateline, typeid, machineuuid, clientip, username, data)'
						. " VALUES (:shutdown, '~offline-length', :uuid, :clientip, '', :length)", array(
						'shutdown' => $lastSeen,
						'uuid'     => $uuid,
						'clientip' => $ip,
						'length'   => $offtime
					));
				}
			}
		}
		// Create/update machine entry
		Database::exec('INSERT INTO machine '
			. '(machineuuid, macaddr, clientip, firstseen, lastseen, logintime, position, lastboot, realcores, mbram,'
			. ' kvmstate, cpumodel, systemmodel, id44mb, badsectors, data, hostname) VALUES '
			. "(:uuid, :macaddr, :clientip, :firstseen, :lastseen, 0, '', :lastboot, :realcores, :mbram,"
			. ' :kvmstate, :cpumodel, :systemmodel, :id44mb, :badsectors, :data, :hostname)'
			. ' ON DUPLICATE KEY UPDATE'
			. ' macaddr = VALUES(macaddr),'
			. ' clientip = VALUES(clientip),'
			. ' lastseen = VALUES(lastseen),'
			. ($uptime < 180 ? ' logintime = 0, currentuser = NULL, currentsession = NULL,' : '')
			. ' lastboot = VALUES(lastboot),'
			. ' realcores = VALUES(realcores),'
			. ' mbram = VALUES(mbram),'
			. ' kvmstate = VALUES(kvmstate),'
			. ' cpumodel = VALUES(cpumodel),'
			. ' systemmodel = VALUES(systemmodel),'
			. ' id44mb = VALUES(id44mb),'
			. ' badsectors = VALUES(badsectors),'
			. ' data = VALUES(data),'
			. " hostname = If(VALUES(hostname) = '', hostname, VALUES(hostname))", array(
			'uuid'       => $uuid,
			'macaddr'    => $macaddr,
			'clientip'   => $ip,
			'firstseen'  => $NOW,
			'lastseen'   => $NOW,
			'lastboot'   => $NOW - $uptime,
			'realcores'  => $realcores,
			'mbram'      => $mbram,
			'kvmstate'   => $kvmstate,
			'cpumodel'   => $cpumodel,
			'systemmodel'=> $systemmodel,
			'id44mb'     => $id44mb,
			'badsectors' => $badsectors,
			'data'       => $data,
			'hostname'   => $hostname,
		));

		if (($old === false || $old['clientip'] !== $ip) && Module::isAvailable('locations')) {
			// New, or ip changed (dynamic pool?), update subnetlicationid
			Location::updateMapIpToLocation($uuid, $ip);
		}

		// Write statistics data

	} else if ($type === '~runstate') {
		// Usage (occupied/free)
		$sessionLength = 0;
		if ($old === false) die("Unknown machine.\n");
		if ($old['clientip'] !== $ip) {
			EventLog::warning("[runstate] IP address of client $uuid seems to have changed ({$old['clientip']} -> $ip)");
			die("Address changed.\n");
		}
		$used = Request::post('used', 0, 'integer');
		if ($old['lastboot'] === 0 && $NOW - $old['lastseen'] > 300) {
			$strUpdateBoottime = ' lastboot = UNIX_TIMESTAMP(), ';
		} else {
			$strUpdateBoottime = '';
		}
		// 1) Log last session length if we didn't see the machine for a while
		if ($NOW - $old['lastseen'] > 610 && $old['lastseen'] !== 0) {
			// Old session timed out - might be caused by hard reboot
			if ($old['logintime'] !== 0) {
				if ($old['lastseen'] > $old['logintime']) {
					$sessionLength = $old['lastseen'] - $old['logintime'];
				}
				$old['logintime'] = 0;
			}
			$old['lastboot'] = 0;
		}
		// Figure out what's happening - state changes
		if ($used === 0 && $old['logintime'] !== 0) {
			// Is not in use, was in use before
			$sessionLength = $NOW - $old['logintime'];
			Database::exec('UPDATE machine SET lastseen = UNIX_TIMESTAMP(),'
				. $strUpdateBoottime
				. ' logintime = 0, currentuser = NULL WHERE machineuuid = :uuid', array('uuid' => $uuid));
		} elseif ($used === 1 && $old['logintime'] === 0) {
			// Machine is in use, was free before
			if ($sessionLength !== 0 || $old['logintime'] === 0) {
				// This event is a start of a new session, rather than an update
				Database::exec('UPDATE machine SET lastseen = UNIX_TIMESTAMP(),'
					. $strUpdateBoottime
					. ' logintime = UNIX_TIMESTAMP(), currentuser = :user WHERE machineuuid = :uuid', array(
					'uuid' => $uuid,
					'user' => Request::post('user', null, 'string'),
				));
			}
		} else {
			// Nothing changed, simple lastseen update
			Database::exec('UPDATE machine SET '
				. $strUpdateBoottime
				. ' lastseen = UNIX_TIMESTAMP() WHERE machineuuid = :uuid', array('uuid' => $uuid));
		}
		// 9) Log last session length if applicable
		if ($mode === false && $sessionLength > 0 && $sessionLength < 86400*2 && $old['logintime'] !== 0) {
			Database::exec('INSERT INTO statistic (dateline, typeid, machineuuid, clientip, username, data)'
				. " VALUES (:start, '~session-length', :uuid, :clientip, '', :length)", array(
				'start'     =>  $old['logintime'],
				'uuid'     => $uuid,
				'clientip'  => $ip,
				'length'    => $sessionLength
			));
		}
	} elseif ($type === '~poweroff') {
		if ($old === false) die("Unknown machine.\n");
		if ($old['clientip'] !== $ip) {
			EventLog::warning("[poweroff] IP address of client $uuid seems to have changed ({$old['clientip']} -> $ip)");
			die("Address changed.\n");
		}
		if ($mode === false && $old['logintime'] !== 0) {
			$sessionLength = $old['lastseen'] - $old['logintime'];
			if ($sessionLength > 0 && $sessionLength < 86400*2) {
				Database::exec('INSERT INTO statistic (dateline, typeid, machineuuid, clientip, username, data)'
					. " VALUES (:start, '~session-length', :uuid, :clientip, '', :length)", array(
					'start'     => $old['logintime'],
					'uuid'     => $uuid,
					'clientip'  => $ip,
					'length'    => $sessionLength
				));
			}
		}
		Database::exec('UPDATE machine SET logintime = 0, lastseen = UNIX_TIMESTAMP(), lastboot = 0 WHERE machineuuid = :uuid', array('uuid' => $uuid));
	} elseif ($mode === false && $type === '~screens') {
		$screens = Request::post('screen', false, 'array');
		if (is_array($screens)) {
			// `devicetype`, `devicename`, `subid`, `machineuuid`
			// Make sure all screens are in the general hardware table
			$hwids = array();
			foreach ($screens as $port => $screen) {
				if (!array_key_exists('name', $screen))
					continue;
				if (array_key_exists($screen['name'], $hwids)) {
					$hwid = $hwids[$screen['name']];
				} else {
					$hwid = (int)Database::insertIgnore('statistic_hw', 'hwid',
						array('hwtype' => DeviceType::SCREEN, 'hwname' => $screen['name']));
					$hwids[$screen['name']] = $hwid;
				}
				// Now add new entries
				$machinehwid = Database::insertIgnore('machine_x_hw', 'machinehwid', array(
					'hwid' => $hwid,
					'machineuuid' => $uuid,
					'devpath' => $port,
				), array('disconnecttime' => 0));
				$validProps = array();
				if (count($screen) > 1) {
					// Screen has additional properties (resolution, size, etc.)
					unset($screen['name']);
					foreach ($screen as $key => $value) {
						if (!preg_match('/^[a-zA-Z0-9][\x21-\x7e]{0,15}$/', $key)) {
							echo "No matsch '$key'\n";
							continue; // Ignore evil key names
						}
						$validProps[] = $key;
						Database::exec("INSERT INTO machine_x_hw_prop (machinehwid, prop, value)"
							. " VALUES (:id, :key, :value) ON DUPLICATE KEY UPDATE value = VALUES(value)", array(
							'id' => $machinehwid,
							'key' => $key,
							'value' => $value,
						));
					}
				}
				// Purge properties that might have existed in the past
				if (empty($validProps)) {
					Database::exec("DELETE FROM machine_x_hw_prop WHERE machinehwid = :machinehwid AND prop NOT LIKE '@%'",
						array('machinehwid' => $machinehwid));
				} else {
					$qs = '?' . str_repeat(',?', count($validProps) - 1);
					array_unshift($validProps, $machinehwid);
					Database::exec("DELETE FROM machine_x_hw_prop"
						. " WHERE machinehwid = ? AND prop NOT LIKE '@%' AND prop NOT IN ($qs)",
						$validProps);
				}
			}
			// Remove/disable stale entries
			if (empty($hwids)) {
				// No screens connected at all, purge all screen entries for this machine
				Database::exec("UPDATE machine_x_hw x, statistic_hw h"
					. " SET x.disconnecttime = UNIX_TIMESTAMP()"
					. " WHERE x.machineuuid = :uuid AND x.hwid = h.hwid AND h.hwtype = :type AND x.disconnecttime = 0",
					array('uuid' => $uuid, 'type' => DeviceType::SCREEN));
			} else {
				// Some screens connected, make sure old entries get removed
				$params = array_values($hwids);
				array_unshift($params, $uuid);
				array_unshift($params, DeviceType::SCREEN);
				$qs = '?' . str_repeat(',?', count($hwids) - 1);
				Database::exec("UPDATE machine_x_hw x, statistic_hw h"
					. " SET x.disconnecttime = UNIX_TIMESTAMP()"
					. " WHERE h.hwid = x.hwid AND x.disconnecttime = 0 AND h.hwtype = ? AND x.machineuuid = ? AND x.hwid NOT IN ($qs)", $params);

			}
		}
	} else {
		die("INVALID ACTION '$type'\n");
	}
	die("OK. (RESULT=0)\n");
}

/*
 * Section 2/2
 * Session information
 */

function writeStatisticLog($type, $username, $data)
{
	global $ip;
	// Spam from IP
	$row = Database::queryFirst('SELECT Count(*) AS cnt FROM statistic WHERE clientip = :client AND dateline + 300 > UNIX_TIMESTAMP()', array(':client' => $ip));
	if ($row !== false && $row['cnt'] > 8) {
		return;
	}

	Database::exec('INSERT INTO statistic (dateline, typeid, clientip, username, data) VALUES (UNIX_TIMESTAMP(), :type, :client, :username, :data)', array(
		'type' => $type,
		'client' => $ip,
		'username' => $username,
		'data' => $data,
	));
}

// For backwards compat, we require the . prefix
if ($type{0} === '.') {
	if ($type === '.vmchooser-session') {
		$user = Request::post('user', 'unknown', 'string');
		$loguser = Request::post('loguser', 0, 'int') !== 0;
		$sessionName = Request::post('name', 'unknown', 'string');
		$sessionUuid = Request::post('uuid', '', 'string');
		$session = strlen($sessionUuid) === 36 ? $sessionUuid : $sessionName;
		Database::exec("UPDATE machine SET currentuser = :user, currentsession = :session WHERE clientip = :ip",
			compact('user', 'session', 'ip'));
		writeStatisticLog('.vmchooser-session-name', ($loguser ? $user : 'anonymous'), $sessionName);
	} else {
		if (!isset($_POST['description'])) die('Missing options..');
		$description = $_POST['description'];
		// and username embedded in message
		if (preg_match('#^\[([^\]]+)\]\s*(.*)$#m', $description, $out)) {
			writeStatisticLog($type, $out[1], $out[2]);
		}
	}
}

echo "OK.\n";