summaryrefslogtreecommitdiffstats
path: root/modules-available/statistics/inc/hardwareparser.inc.php
blob: 428f7d557a3a93fc47017721e78172b63bdcdf6b (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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
<?php

class HardwareParser
{

	const SIZE_LOOKUP = ['T' => 1099511627776, 'G' => 1073741824, 'M' => 1048576, 'K' => 1024, '' => 1];
	const SI_LOOKUP = ['T' => 1000000000000, 'G' => 1000000000, 'M' => 1000000, 'K' => 1000, '' => 1];

	/**
	 * Convert/format size unit. Input string can be a size like
	 * 8 GB or 1024 MB and will be converted according to passed parameters.
	 * @param string $string Input string
	 * @param string $scale 'a' for auto, T/G/M/K/'' for according units
	 * @param bool $appendUnit append unit string, e.g. 'GiB'
	 * @return false|string|int Formatted result
	 */
	public static function convertSize(string $string, string $scale = 'a', bool $appendUnit = true)
	{
		if (!preg_match('/(\d+)\s*([TGMK]?)/i', $string, $out)) {
			//error_log("Not size: $string");
			return false;
		}
		$val = (int)$out[1] * self::SIZE_LOOKUP[strtoupper($out[2])];
		if (!array_key_exists($scale, self::SIZE_LOOKUP)) {
			foreach (self::SIZE_LOOKUP as $k => $v) {
				if ($k === '' || $val / 8 >= $v || abs($val - $v) < 50) {
					$scale = $k;
					break;
				}
			}
		}
		$val = (int)round($val / self::SIZE_LOOKUP[$scale]);
		if ($appendUnit) {
			$val .= ' ' . ($scale === '' ? 'Byte' : $scale . 'iB'); // NBSP!!
		}
		return $val;
	}

	/**
	 * Decode JEDEC ID to according manufacturer
	 */
	public static function decodeJedec(string $string): string
	{
		// JEDEC ID:7F 7F 9E 00 00 00 00 00
		// or the ID as 8 hex digits with no spacing and prefix
		$id = null;
		if (preg_match('/JEDEC(?:\s*ID)?\s*:?\s*([0-9a-f\s]{8,23})\s*$/i', $string, $out)
				|| preg_match('/^([0-9a-f]{14}00)$/i', $string, $out)) {
			preg_match_all('/[0-9a-f]{2}/i', $out[1], $out);
			$bank = 0;
			foreach ($out[0] as $id) {
				$bank++;
				$id = hexdec($id) & 0x7f; // Let's just ignore the parity bit, and any potential error
				if ($id !== 0x7f)
					break;
			}
			if ($id !== null) {
				$id = self::lookupJedec($bank, $id);
			}
		} elseif (preg_match('/Unknown.{0,16}[\[(](?:0x)?([0-9a-fA-F]{2,4})[\])]/', $string, $out)) {
			// First byte (big endian) is id-in-bank, low byte is bank
			$id = self::decodeBankAndId($out, false);
		} elseif (preg_match('/JEDEC(?:\s*ID)?\s*:?\s*([0-9a-f]{2}\s?[0-9a-f]{2})/i', $string, $out)
				|| (preg_match('/^([0-9A-F]{4})([0-9A-F]{4})([0-9A-F]{4})$/', $string, $out) && $out[2] === '0000')) {
			// First byte is bank, second byte is id-in-bank
			$id = self::decodeBankAndId($out, true);
		} elseif (preg_match('/^([0-9a-f]{4})$/i', $string, $out)) {
			// This one was seen with both endianesses
			$id = self::decodeBankAndId($out, true);
			if ($id === null) {
				$id = self::decodeBankAndId($out, false);
			}
		}

		if ($id !== null)
			return $id;
		return $string;
	}

	private static function decodeBankAndId(array $out, bool $bankFirst): ?string
	{
		// 16bit encoding from DDR3+: lower byte is number of 0x7f bytes, upper byte is id within bank
		$id = hexdec(str_replace(' ', '', $out[1]));
		// Our bank counting starts at one. Also ignore parity bit.
		$bank = ($id & 0x7f);
		// Shift down id, get rid of parity bit
		$id = ($id >> 8) & 0x7f;
		if ($bankFirst) {
			// Observed second case, on OptiPlex 5050, is 80AD000080AD, but here endianness is reversed
			$tmp = $id;
			$id = $bank;
			$bank = $tmp;
		}
		$bank++;
		return self::lookupJedec($bank, $id);
	}

	private static function lookupJedec(int $bank, int $id): ?string
	{
		static $data = false;
		if ($data === false) {
			$data = json_decode(file_get_contents(dirname(__FILE__) . '/jedec.json'), true);
		}
		if (array_key_exists('bank' . $bank, $data) && array_key_exists('id' . $id, $data['bank' . $bank]))
			return $data['bank' . $bank]['id' . $id];
		return null;
	}

	/**
	 * Turn several numeric measurements like Size, Speed, Voltage into a unitless
	 * base representation, meant for comparison. For example, Voltages are converted
	 * to Millivolts, Anything measured in [KMGT]Bytes (per second) to bytes, GHz to
	 * Hz, and so on.
	 * @return ?int value, or null if not numeric
	 */
	private static function toNumeric(string $key, string $val): ?int
	{
		$key = strtolower($key);
		// Normalize voltage to mV
		if ((strpos($key, 'volt') !== false || strpos($key, 'current') !== false)
			&& preg_match('/^([0-9]+(?:\.[0-9]+)?)\s+(m?)V/', $val, $out)) {
			return (int)($out[1] * ($out[2] === 'm' ? 1 : 1000));
		}
		if (preg_match('/speed|width|size|capacity/', $key)
			&& preg_match('#^([0-9]+(?:\.[0-9]+)?)\s*([TGMK]?)i?([BT](?:it|yte|))s?(?:/s)?#i',
				$val, $out)) {
			// Matched (T/G/M) Bits, Bytes, etc...
			// For bits, use SI
			if ($out[3] !== 'B' && strtolower($out[3]) !== 'byte')
				return (int)($out[1] * self::SI_LOOKUP[strtoupper($out[2])]);
			// For bytes, use 1024
			return (int)($out[1] * self::SIZE_LOOKUP[strtoupper($out[2])]);
		}
		// Speed in Hz
		if (preg_match('#^([0-9]+(?:\.[0-9]+)?)\s*([TGMK]?)Hz#i',
			$val, $out)) {
			return (int)($out[1] * self::SI_LOOKUP[strtoupper($out[2])]);
		}
		// Count, size (unitless)
		if (is_numeric($val) && preg_match('/^-?[0-9]+$/', $val)
			&& preg_match('/used|occupied|count|number|speed|width|size|capacity|temperature|_start|_value|_thresh|_worst|_time|_rate/', $key)) {
			return (int)$val;
		}
		// Date
		if (preg_match('#^(?:[0-9]{2}/[0-9]{2}/[0-9]{4}|[0-9]{4}-[0-9]{2}-[0-9]{2})$#', $val)) {
			return (int)strtotime($val);
		}
		return null;
	}

	/**
	 * Takes hwinfo json, then looks up and returns all sections from the
	 * dmidecode subtree that represent the given dmi table entry type,
	 * e.g. 17 for memory. It will then return an array of 'props' subtrees.
	 *
	 * @param array $data hwinfo tree
	 * @param int $type dmi type
	 * @return array [ <props>, <props>, ... ]
	 */
	public static function getDmiHandles(array $data, int $type): array
	{
		if (empty($data['dmidecode']))
			return [];
		$ret = [];
		foreach ($data['dmidecode'] as $section) {
			if ($section['handle']['type'] == $type) {
				$ret[] = $section['props'];
			}
		}
		return $ret;
	}

	/**
	 * Takes key-value-array, returns a concatenated string of all the values with the keys given in $fields.
	 * The items are separated by spaces, and returned in the order they were given in $fields. Missing keys
	 * are silently omitted.
    */
	private static function idFromArray(array $array, string ...$fields): string
	{
		$out = '';
		foreach ($fields as $field) {
			if (!isset($array[$field]))
				continue;
			if (empty($out)) {
				$out = $array[$field];
			} else {
				$out .= ' ' . $array[$field];
			}
		}
		return $out;
	}

	/**
	 * Establish a mapping between a client and some hardware device.
	 * Optionally writes hardware properties specific to a hardware instance of a client
	 *
	 * @param string $uuid client
	 * @param int $hwid hw global hw id
	 * @param string $pathId unique identifier for the local instance of this hw, e.q. PCI slot, /dev path, something that handles the case that there are multiple instances of the same hardware in one machine
	 * @param array $props KV-pairs of properties to write for this instance; can be empty
	 * @return int ID of mapping in DB
	 */
	private static function writeLocalHardwareData(string $uuid, int $hwid, string $pathId, array $props): int
	{
		// Add mapping between hw entity and machine
		$pathId = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $pathId);
		$mappingId = Database::insertIgnore('machine_x_hw', 'machinehwid',
			['hwid' => $hwid, 'machineuuid' => $uuid, 'devpath' => $pathId],
			['disconnecttime' => 0]);
		// And all the properties specific to this entity instance (e.g. serial number)
		if (!empty($props)) {
			$vals = [];
			foreach ($props as $k => $v) {
				$vals[] = [$mappingId, $k, $v, self::toNumeric($k, $v)];
			}
			Database::exec("INSERT INTO machine_x_hw_prop (machinehwid, prop, `value`, `numeric`)
					VALUES :vals
					ON DUPLICATE KEY UPDATE `value` = VALUES(`value`), `numeric` = VALUES(`numeric`)", ['vals' => $vals]);
		}
		return $mappingId;
	}

	/**
	 * Takes an array of type [ key1 => [ 'values' => [ <val1.1>, <val1.2>, ... ] ], key2 => ... ]
	 * and turns it into [ key1 => <val1.1>, key2 => <val2.1>, ... ]
	 *
	 * Along the way:
	 * 1) any fields with bogus values, or values analogous to empty will get removed
	 */
	public static function prepareDmiProperties(array $data): array
	{
		$ret = [];
		foreach ($data as $key => $vals) {
			$val = trim($vals['values'][0] ?? 'NULL');
			if ($val === '[Empty]' || $val === 'NULL')
				continue;
			$val = preg_replace('/[^a-z0-9]/', '', strtolower($val));
			if ($val === '' || $val === 'notspecified' || $val === 'tobefilledbyoem' || $val === 'unknown'
				|| $val === 'chassismanufacture' || $val === 'chassismanufacturer' || $val === 'chassisversion'
				|| $val === 'chassisserialnumber' || $val === 'defaultstring' || $val === 'productname'
				|| $val === 'manufacturer' || $val === 'systemmodel' || $val === 'fillbyoem' || $val === 'none') {
				continue;
			}
			$val = trim($vals['values'][0] ?? '');
			if ($key === 'Manufacturer') {
				$val = self::fixManufacturer($val);
			}
			$ret[$key] = $val;
		}
		return $ret;
	}

	/**
	 * Mark all devices of a given type disconnected from the given machine, with an optional
	 * exclude list of machine-client-mapping IDs
	 *
	 * @param string $uuid client
	 * @param string $dbType type, eg HDD
	 * @param array $excludedHwIds mappingIDs to exclude, ie. devices that are still connected
	 */
	private static function markDisconnected(string $uuid, string $dbType, array $excludedHwIds)
	{
		//error_log("Marking disconnected for $dbType except " . implode(', ', $excludedHwIds));
		if (empty($excludedHwIds)) {
			Database::exec("UPDATE machine_x_hw mxh, statistic_hw h
				SET mxh.disconnecttime = UNIX_TIMESTAMP()
				WHERE h.hwtype = :type AND h.hwid = mxh.hwid AND mxh.machineuuid = :uuid
				AND mxh.disconnecttime = 0",
				['type' => $dbType, 'uuid' => $uuid]);
		} else {
			Database::exec("UPDATE machine_x_hw mxh, statistic_hw h
				SET mxh.disconnecttime = UNIX_TIMESTAMP()
				WHERE h.hwtype = :type AND h.hwid = mxh.hwid AND mxh.machineuuid = :uuid
				AND mxh.disconnecttime = 0 AND mxh.machinehwid NOT IN (:hwids)",
				['type' => $dbType, 'uuid' => $uuid, 'hwids' => $excludedHwIds]);
		}
	}

	/**
	 * Insert some hardware into database. $global is supposed to contain key-value-pairs of properties
	 * this hardware has that is the same for every instance of this hardware, like model number, speed
	 * or size. Individual properties, like a serial number, are considered local properties, and go
	 * into a different table, that would contain a row for each client that has this hardware.
	 * @param string $dbType Hardware typ (HDD, RAM, ...)
	 * @param array $global associative array of properties this hardware has
	 * @return int id of this hardware; primary key of row in statistic_hw_prop
	 */
	private static function writeGlobalHardwareData(string $dbType, array $global, array $globalExtra = []): int
	{
		static $cache = [];
		// Since the global properties are supposed to be unique for a specific piece of hardware, use them all
		// to generate a combined ID for this hardware entity, as opposed to $localProps, which should differ
		// between instances of the same hardware entity, e.g. one specific HDD model has different serial numbers.
		$id = md5(implode(' ', $global));
		// But don't include our "fake" fields in this as we might add more there later, which would
		// change the ID then.
		$global += $globalExtra;
		if (!isset($cache[$id])) {
			// Cache lookup, make sure we insert this only once for every run, as this is supposed to be general
			// information about the hardware, e.g. model number, max. resolution, capacity, ...
			$hwid = Database::insertIgnore('statistic_hw', 'hwid', ['hwtype' => $dbType, 'hwname' => $id]);
			$vals = [];
			foreach ($global as $k => $v) {
				$vals[] = [$hwid, $k, $v, self::toNumeric($k, $v)];
			}
			if (!empty($vals)) {
				Database::exec("INSERT INTO statistic_hw_prop (hwid, prop, `value`, `numeric`)
						VALUES :vals
						ON DUPLICATE KEY UPDATE `value` = VALUES(`value`), `numeric` = VALUES(`numeric`)",
					['vals' => $vals]);
			}
			$cache[$id] = $hwid;
		}
		return $cache[$id];
	}

	/**
	 * Process hardware info for given client.
	 * @param string $uuid System-UUID of client
	 * @param array $data Hardware info, deserialized assoc array.
	 * @return ?array id44mb and id45mb as calculated from given HDD data
	 */
	public static function parseMachine(string $uuid, array $data): ?array
	{
		$version = $data['version'] ?? 0;
		if ($version != 2) {
			error_log("Received unsupported hw json v$version");
			return null;
		}
		// determine misc stuff first
		$globalCpuExtra = [];
		$globalMainboardExtra = [];
		$localMainboardExtra = [];
		// physical memory array
		$memArrays = self::getDmiHandles($data, 16);
		// We mostly have a seprate hardware type for all the dmi types, but not for memory arrays.
		// These get added to the mainboard hw-type as it's practically a property of the mainboard.
		// While we can have multiple physical memory arrays, we only ever have one mainboard per
		// client. Add up the data from all arrays.
		$globalMainboardExtra['Memory Slot Count'] = 0;
		$globalMainboardExtra['Memory Maximum Capacity'] = 0;
		foreach ($memArrays as $mem) {
			$mem = self::prepareDmiProperties($mem);
			// Not all memory arrays are for RAM....
			if (($mem['Use'] ?? 0) !== 'System Memory')
				continue;
			if (isset($mem['Number Of Devices'])) {
				$globalMainboardExtra['Memory Slot Count'] += $mem['Number Of Devices'];
			}
			if (isset($mem['Maximum Capacity'])) {
				// Temporary unit is MB
				$globalMainboardExtra['Memory Maximum Capacity']
					+= self::convertSize($mem['Maximum Capacity'], 'M', false);
			}
		}
		// Now finally convert to GB
		$globalMainboardExtra['Memory Maximum Capacity']
			= self::convertSize($globalMainboardExtra['Memory Maximum Capacity'] . ' MB', 'G');
		// BIOS section - need to combine this with mainboard or system model, as it doesn't have a meaningful
		// identifier on its own. So again like above, we add this to the mainboard data.
		$bios = self::prepareDmiProperties(self::getDmiHandles($data, 0)[0] ?? []);
		foreach (['Version', 'Release Date', 'Firmware Revision'] as $k) {
			if (isset($bios[$k])) {
				// Prefix with "BIOS" to clarify, since it's added to the mainboard meta-data
				$localMainboardExtra['BIOS ' . $k] = $bios[$k];
			}
		}
		if (isset($bios['BIOS Revision'])) { // This one already has the BIOS prefix
			$localMainboardExtra['BIOS Revision'] = $bios['BIOS Revision'];
		}
		// Vendor and ROM size of BIOS *should* always be the same for a specific mainboard
		foreach (['Vendor', 'ROM Size'] as $k) {
			if (isset($bios[$k])) {
				$globalMainboardExtra['BIOS ' . $k] = $bios[$k];
			}
		}
		// "Normal" dmi entries - these map directly to one of our hardware types
		// RAM modules
		$capa = 0;
		$ramModCount = self::updateHwTypeFromDmi($uuid, $data, 17, HardwareInfo::RAM_MODULE,
			// Filter callback - we can modify the entry, or return false to ignore it
			function (array $flat) use (&$capa): bool {
				$size = self::convertSize(($flat['Size'] ?? 0), '', false);
				// Let's assume we're never running on old HW with <=128MB modules, so this
				// might be a hint that this is some other kind of memory. The proper way would be
				// to check if the related physical memory array (16) has "Use" = "System Memory"
				if ($size > 129 * 1024 * 1024) {
					$capa += $size;
					return true;
				}
				return false;
			},
			['Locator'],
			['Data Width',
				'Size',
				'Form Factor',
				'Type',
				'Type Detail',
				'Speed',
				'Manufacturer',
				'Part Number',
				'Minimum Voltage',
				'Maximum Voltage'],
			['Locator', 'Bank Locator', 'Serial Number', 'Asset Tag', 'Configured Memory Speed', 'Configured Voltage']
		);
		// Put RAM slots used/total etc. into mainboard data
		$localMainboardExtra['Memory Slot Occupied'] = $ramModCount;
		$localMainboardExtra['Memory Installed Capacity'] = self::convertSize($capa, 'G', true);
		// Also add generic socket, core and thread count to mainboard data. This doesn't seem to make too much sense
		// at first since it's not a property of the mainboard. But we can get away with it since we make it a local
		// property, i.e. specific to a client. This is just aggregated, so it's not super well suited for the CPU
		// hardware type, referenced below. In fact, on some systems the dmi/smbios tables don't contain all that much
		// information about the CPU at all, so we have at least this.
		foreach (['sockets', 'cores', 'threads'] as $key) {
			if (!isset($data['cpu'][$key]))
				continue;
			$localMainboardExtra['cpu-' . $key] = $data['cpu'][$key];
		}
		if ($data['cpu']['vmx-legacy'] ?? false) {
			$globalCpuExtra['vmx-legacy'] = 1;
		}
		// Do the same hack with the primary NIC's speed and duplex. Even if it's not an onboard NIC, we only have one
		// primary boot interface
		$bootNic = $data['net']['boot0'] ?? $data['net']['eth0'] ?? null;
		if ($bootNic !== null) {
			$localMainboardExtra['nic-speed'] = $bootNic['speed'] ?? 0;
			$localMainboardExtra['nic-duplex'] = $bootNic['duplex'] ?? 'unknown';
		}
		// Finally handle mainbard data, with all of our gathered extra fields
		self::updateHwTypeFromDmi($uuid, $data, 2, HardwareInfo::MAINBOARD, [],
			[],
			['Manufacturer', 'Product Name', 'Type', 'Version'], // Global props, don't change
			['Serial Number', 'Asset Tag', 'Location In Chassis'],
			$globalMainboardExtra, $localMainboardExtra
		);
		// System information, mostly set by OEMs, might be empty/bogus on custom systems
		self::updateHwTypeFromDmi($uuid, $data, 1, HardwareInfo::DMI_SYSTEM, ['Manufacturer', 'Product Name'],
			[],
			['Manufacturer', 'Product Name', 'Version', 'Wake-up Type'], // Global props, don't change
			['Serial Number', 'UUID', 'SKU Number']
		);
		// Might contain more or less accurate information. Mostly works on servers and OEM systems
		self::updateHwTypeFromDmi($uuid, $data, 39, HardwareInfo::POWER_SUPPLY, ['Manufacturer'],
			['Location',
				'Power Unit Group',
				'Name'], // Location might be empty/"Unknown", but Name can be something like "PSU 2"
			['Manufacturer', 'Product Name', 'Model Part Number', 'Revision', 'Max Power Capacity'], // Global props, don't change
			['Serial Number', 'Asset Tag', 'Status', 'Plugged', 'Hot Replaceable']
		);
		// On some more recent systems this contains quite some useful information
		self::updateHwTypeFromDmi($uuid, $data, 4, HardwareInfo::CPU, ['Version'],
			['Socket Designation'],
			['Type', 'Family', 'Manufacturer', 'Signature', 'Version', 'Core Count', 'Thread Count'], // Global props, don't change
			['Voltage', 'Current Speed', 'Upgrade', 'Core Enabled'],
			$globalCpuExtra);
		// Information about system slots
		self::updateHwTypeFromDmi($uuid, $data, 9, HardwareInfo::SYSTEM_SLOT,
			function (array &$entry): bool {
				// Use a callback filter to extract PCIe slot metadata into unique fields
				if (!isset($entry['Type']))
					return false;
				// Split up PCIe info – gen, electrical width and physical width are mashed into one field
				if (preg_match('/^x(?<b>\d+) PCI Express( (?<g>\d+)( x(?<s>\d+))?)?$/', $entry['Type'], $out)) {
					$entry['Type'] = 'PCI Express';
					$entry['PCIe Bus Width'] = $out['b'];
					if (!empty($out['g'])) {
						$entry['PCIe Gen'] = $out['g'];
					}
					if (!empty($out['s'])) {
						$entry['PCIe Slot Width'] = $out['s'];
					}
				}
				return true;
			},
			['Designation', 'ID', 'Bus Address'],
			['Type', 'PCIe Bus Width', 'PCIe Gen', 'PCIe Slot Width'], // Global props, don't change
			['Current Usage', 'Designation']
		);
		// dmidecode end
		// ---- lspci ------------------------------------
		$pciHwIds = [];
		foreach (($data['lspci'] ?? []) as $dev) {
			// $props is global props, don't change or the ID will change
			$props = self::propsFromArray($dev, 'vendor', 'device', 'rev', 'class');
			if (!isset($props['vendor']) || !isset($props['device']))
				continue;
			$hwid = self::writeGlobalHardwareData(HardwareInfo::PCI_DEVICE, $props);
			$mappingId = self::writeLocalHardwareData($uuid, $hwid, $dev['slot'] ?? 'unknown',
				self::propsFromArray($dev, 'slot', 'subsystem', 'subsystem_vendor', 'iommu_group'));
			$pciHwIds[] = $mappingId;
		}
		self::markDisconnected($uuid, HardwareInfo::PCI_DEVICE, $pciHwIds);
		// ---- Disks ------------------------------------
		$excludedHddHwIds = [];
		// Sum of all ID44/45 partitions in bytes
		$id44 = $id45 = 0;
		foreach (($data['drives'] ?? []) as $dev) {
			if (($dev['type'] ?? 'drive') !== 'drive')
				continue; // TODO: Handle CD/DVD drives? Still relevant?
			if (empty($dev['readlink'])) // This is the canonical entry name directly under /dev/, e.g. /dev/sda
				continue;
			// Use smartctl as the source of truth, lsblk as fallback if data is missing
			if (!isset($dev['smartctl']) || !is_array($dev['smartctl'])) {
				$smart = [];
			} else {
				$smart =& $dev['smartctl'];
			}
			if (!isset($dev['lsblk']['blockdevices'][0]) || !is_array($dev['lsblk']['blockdevices'][0])) {
				$lsblk = [];
			} else {
				$lsblk =& $dev['lsblk']['blockdevices'][0];
			}
			if (!isset($smart['rotation_rate']) && isset($lsblk['rota']) && !$lsblk['rota']) {
				// smartctl didn't report on it, lsblk says it's non-rotational
				$smart['rotation_rate'] = 0;
			}
			$size = $lsblk['size'] ?? $smart['user_capacity']['bytes'] ?? -1;
			// Don't change the global props, it would change the HW ID
			$hwid = self::writeGlobalHardwareData(HardwareInfo::HDD, [
					// Try to use the model name as the unique identifier
					'model' => $smart['model_name'] ?? $lsblk['model'] ?? 'unknown',
					// Append device size as some kind of fallback, in case model is unknown
					'size' => $size,
					'physical_block_size' => $smart['physical_block_size'] ?? $lsblk['phy-sec'] ?? 0,
					'logical_block_size' => $smart['logical_block_size'] ?? $lsblk['log-sec'] ?? 0,
				] + self::propsFromArray($smart, 'rotation_rate', 'sata_version//string',
					'interface_speed//max//string', 'model_family'));
			// Mangle smart attribute table
			// TODO: Handle used endurance indicator for (SATA) SSDs
			$table = [];
			foreach (($smart['ata_smart_attributes']['table'] ?? []) as $attr) {
				if (!isset($attr['id']))
					continue;
				$id = 'attr_' . $attr['id'] . '_';
				foreach (['value', 'worst', 'thresh', 'when_failed'] as $item) {
					if (isset($attr[$item])) {
						$table[$id . $item] = $attr[$item];
					}
				}
				if (isset($attr['raw']['value'])) {
					if ($attr['id'] === 194) {
						if (!isset($smart['temperature'])) {
							$smart['temperature'] = [];
						}
						if (!isset($smart['temperature']['current'])) {
							$smart['temperature']['current'] = $attr['raw']['value'] & 0xffff;
						}
						$smart['temperature']['min'] = ($attr['raw']['value'] >> 16) & 0xffff;
						$smart['temperature']['max'] = ($attr['raw']['value'] >> 32) & 0xffff;
					}
					$table[$id . 'raw'] = $attr['raw']['value'];
				}
			}
			if (isset($smart['nvme_smart_health_information_log'])
					&& is_array($smart['nvme_smart_health_information_log'])) {
				$table += array_filter($smart['nvme_smart_health_information_log'], function ($v, $k) {
					return !is_array($v) && $k !== 'temperature' && $k !== 'temperature_sensors';
				}, ARRAY_FILTER_USE_BOTH);
			}
			// Partitions
			$used = 0;
			if (isset($dev['sfdisk']['partitiontable'])) {
				$table['partition_table'] = $dev['sfdisk']['partitiontable']['label'] ?? 'none';
				switch ($dev['sfdisk']['partitiontable']['unit'] ?? 'sectors') {
				case 'sectors':
					$fac = 512;
					break;
				case 'bytes':
					$fac = 1;
					break;
				default:
					$fac = 0;
				}
				$i = 0;
				foreach (($dev['sfdisk']['partitiontable']['partitions'] ?? []) as $part) {
					if (!isset($part['size']))
						continue;
					if ($table['partition_table'] === 'dos') {
						$type = hexdec($part['type'] ?? '0');
						if ($type === 0x0 || $type === 0x5 || $type === 0xf || $type === 0x15 || $type === 0x1f
								|| $type === 0x85 || $type === 0xc5 || $type == 0xcf) {
							// Extended partition, ignore
							continue;
						}
					}
					$used += $part['size'] * $fac;
					if (isset($part['node']) && preg_match('/-part(\d+)$/', $part['node'], $out)) {
						$id = 'part_' . $out[1] . '_';
					} else {
						$id = 'part_' . ($i + 1) . '_';
					}
					foreach (['start', 'size', 'type', 'uuid', 'name'] as $item) {
						if (!isset($part[$item]))
							continue;
						if ($item === 'size' || $item === 'start') {
							// Turn size and start into byte offsets
							$table[$id . $item] = $part[$item] * $fac;
						} else {
							$table[$id . $item] = $part[$item];
						}
					}
					$type = $table[$id . 'type'] ?? 0;
					$name = $table[$id . 'name'] ?? '';
					if ($type == '44' || strtolower($type) === '87f86132-ff94-4987-b250-444444444444'
						|| $name === 'OpenSLX-ID44') {
						$table[$id . 'slxtype'] = '44';
						$id44 += $part['size'] * $fac;
					} elseif ($type == '45' || strtolower($type) === '87f86132-ff94-4987-b250-454545454545'
						|| $name === 'OpenSLX-ID45') {
						$table[$id . 'slxtype'] = '45';
						$id45 += $part['size'] * $fac;
					}
					//
					++$i;
				}
			}
			$table['unused'] = $size - $used;
			$table['dev'] = $dev['readlink'];
			$table += self::propsFromArray($smart + $lsblk,
				'serial_number', 'firmware_version',
				'interface_speed//current//string',
				'smart_status//passed', 'temperature//current', 'temperature//min', 'temperature//max',
				'power_on_time//hours');
			$mappingId = self::writeLocalHardwareData($uuid, $hwid, $dev['readlink'],
				$table);
			// Delete old partition and smart attribute entries
			Database::exec("DELETE FROM machine_x_hw_prop WHERE machinehwid = :id AND prop NOT IN (:keep)
					AND prop NOT LIKE '@%'", [
				'id' => $mappingId,
				'keep' => array_keys($table),
			]);
			$excludedHddHwIds[] = $mappingId;
			unset($smart, $lsblk);
		} // End loop over disks
		self::markDisconnected($uuid, HardwareInfo::HDD, $excludedHddHwIds);
		//
		// Mark parse date
		$params = [
			'uuid' => $uuid,
			'id44mb' => round($id44 / (1024 * 1024)),
			'id45mb' => round($id45 / (1024 * 1024)),
		];
		Database::exec("UPDATE machine SET dataparsetime = UNIX_TIMESTAMP(), id44mb = :id44mb, id45mb = :id45mb
				WHERE machineuuid = :uuid", $params);
		return $params;
	}

	/**
	 * Unify different variants of manufacturer names
	 */
	private static function fixManufacturer(string $in): string
	{
		$in = self::decodeJedec($in);
		switch (strtolower($in)) {
		case 'advanced micro devices, inc.':
		case 'advanced micro devices':
		case 'authenticamd':
			return 'AMD';
		case 'apple inc.':
			return 'Apple';
		case 'asustek computer inc.':
			return 'ASUS';
		case 'crucial';
		case 'crucial technology':
			return 'Crucial';
		case 'dell inc.':
			return 'Dell';
		case 'fujitsu':
		case 'fujitsu client computing limited':
			return 'Fujitsu';
		case 'hewlett packard':
		case 'hewlett-packard':
			return 'HP';
		case 'genuineintel':
		case 'intel corporation':
		case 'intel(r) corp.':
		case 'intel(r) corporation':
			return 'Intel';
		case 'micron technology':
			return 'Micron';
		case 'ramaxel technology':
			return 'Ramaxel';
		case 'samsung sdi':
			return 'Samsung';
		case 'hynix semiconduc':
		case 'hynix/hyundai':
		case 'hyundai electronics hynix semiconductor inc':
		case 'hynix semiconductor inc sk hynix':
			return 'SK Hynix';
		}
		return $in;
	}

	/**
	 * Takes key-value-array, returns a new array with only the keys listed in $fields.
	 * Checks if the given key is not an array. If it's an array, it will be ignored.
	 * Supports nested arrays. Nested keys are separated by '//', so to query
	 * $array['x']['y'], add 'x//y' to $fields. The value will be added to the return
	 * value as key 'x//y'.
	 */
	private static function propsFromArray(array $array, string ...$fields): array
	{
		$ret = [];
		foreach ($fields as $field) {
			if (strpos($field, '//') === false) {
				if (isset($array[$field]) && !is_array($array[$field])) {
					$ret[$field] = $array[$field];
				}
			} else {
				$parts = explode('//', $field);
				$elem = $array;
				foreach ($parts as $part) {
					if (isset($elem[$part])) {
						$elem = $elem[$part];
					} else {
						$elem = false;
						break;
					}
				}
				if ($elem !== false && !is_array($elem)) {
					$ret[preg_replace('~//(value|string)$~', '', $field)] = $elem;
				}
			}
		}
		return $ret;
	}

	/**
	 * Extract data from dmi/smbios and write to DB.
	 * This is a pretty involved function that does several things, among them is splitting
	 * up the data by global and local properties, create new hardware entry if new, and
	 * making sure other hardware of same type gets marked as disconnected from given client.
	 * @param string $uuid client uuid
	 * @param array $data dmidecode part of hardware info
	 * @param int $type dmi type to extract from $data
	 * @param string $dbType hardware type to write this to DB as
	 * @param array|callable $requiredPropsOrCallback either a list of properties that are
	 * 		mandatory for this hwtype, or a callback function that returns true/false for
	 * 		valid/invalid dmi entries
	 * @param array $pathFields fields from entry that define the path or location of the
	 * 		hardware in the client
	 * @param array $globalProps properties of entry that are considered the same for all
	 * 		instances of that hardware, e.g. model name
	 * @param array $localProps properties of entry that are considered different for each
	 * 		instance of this hardware per client, e.g. serial number, power-on hours
	 * @param array $globalExtra additional key-value-pairs to write to DB as being global
	 * @param array $localExtra additional key-value-pairs to write to DB as being local
	 * @return int number of table entries written to DB, i.e. passed $requiredPropsOrCallback
	 */
	private static function updateHwTypeFromDmi(
		string $uuid, array $data, int $type, string $dbType,
				 $requiredPropsOrCallback, array $pathFields, array $globalProps, array $localProps,
		array  $globalExtra = [], array $localExtra = []
	): int
	{
		$sections = self::getDmiHandles($data, $type);
		if (empty($sections) && !empty($globalExtra) || !empty($localExtra)) {
			// Section not found, but as we want to store additional artificial columms,
			// just create one empty fake section so the loop will be executed
			$sections = [[]];
		}
		$thisMachineHwIds = [];
		foreach ($sections as $section) {
			$flat = self::prepareDmiProperties($section);
			if (is_array($requiredPropsOrCallback)) {
				foreach ($requiredPropsOrCallback as $prop) {
					if (!isset($flat[$prop]))
						continue 2;
				}
			}
			if (is_callable($requiredPropsOrCallback)) {
				if (!$requiredPropsOrCallback($flat))
					continue;
			}
			// Global
			$props = self::propsFromArray($flat, ...$globalProps);
			$hwid = self::writeGlobalHardwareData($dbType, $props, $globalExtra);
			// Local
			$pathId = md5(self::idFromArray($flat, ...$pathFields));
			$props = self::propsFromArray($flat, ...$localProps);
			$mappingId = self::writeLocalHardwareData($uuid, $hwid, $pathId, $props + $localExtra);
			$thisMachineHwIds[] = $mappingId;
		}
		// Any hw <-> client mappings not in that list get marked as disconnected
		self::markDisconnected($uuid, $dbType, $thisMachineHwIds);
		return count($thisMachineHwIds);
	}

}