diff options
Diffstat (limited to 'modules-available/statistics/inc/hardwareparser.inc.php')
-rw-r--r-- | modules-available/statistics/inc/hardwareparser.inc.php | 946 |
1 files changed, 946 insertions, 0 deletions
diff --git a/modules-available/statistics/inc/hardwareparser.inc.php b/modules-available/statistics/inc/hardwareparser.inc.php new file mode 100644 index 00000000..f3fe9289 --- /dev/null +++ b/modules-available/statistics/inc/hardwareparser.inc.php @@ -0,0 +1,946 @@ +<?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]; + + public static function parseCpu(&$row, $data) + { + if (0 >= preg_match_all('/^(.+):\s+(\d+)$/im', $data, $out, PREG_SET_ORDER)) { + return; + } + foreach ($out as $entry) { + $row[str_replace(' ', '', $entry[1])] = $entry[2]; + } + } + + public static function parseDmiDecode(&$row, $data) + { + $lines = preg_split("/[\r\n]+/", $data); + $section = false; + $ramOk = false; + $ramForm = $ramType = $ramSpeed = $ramClockSpeed = false; + $ramslot = []; + $row['ramslotcount'] = $row['maxram'] = 0; + foreach ($lines as $line) { + if (empty($line)) { + continue; + } + if ($line[0] !== "\t" && $line[0] !== ' ') { + if (isset($ramslot['size'])) { + $row['ramslot'][] = $ramslot; + $ramslot = []; + } + $section = $line; + $ramOk = false; + if (($ramForm || $ramType) && ($ramSpeed || $ramClockSpeed)) { + if (isset($row['ramtype']) && !$ramClockSpeed) { + continue; + } + $row['ramtype'] = $ramType . ' ' . $ramForm; + if ($ramClockSpeed) { + $row['ramtype'] .= ', ' . $ramClockSpeed; + } elseif ($ramSpeed) { + $row['ramtype'] .= ', ' . $ramSpeed; + } + $ramForm = false; + $ramType = false; + $ramClockSpeed = false; + } + continue; + } + if ($section === 'Base Board Information') { + if (preg_match('/^\s*Product Name: +(\S.+?) *$/i', $line, $out)) { + $row['mobomodel'] = $out[1]; + } + if (preg_match('/^\s*Manufacturer: +(\S.+?) *$/i', $line, $out)) { + $row['mobomanufacturer'] = $out[1]; + } + } elseif ($section === 'System Information') { + if (preg_match('/^\s*Product Name: +(\S.+?) *$/i', $line, $out)) { + $row['pcmodel'] = $out[1]; + } + if (preg_match('/^\s*Manufacturer: +(\S.+?) *$/i', $line, $out)) { + $row['pcmanufacturer'] = $out[1]; + } + } elseif ($section === 'Physical Memory Array') { + if (!$ramOk && preg_match('/Use: System Memory/i', $line)) { + $ramOk = true; + } + if ($ramOk && preg_match('/^\s*Number Of Devices:\s+(\d+)\s*$/i', $line, $out)) { + $row['ramslotcount'] += $out[1]; + } + if ($ramOk && preg_match('/^\s*Maximum Capacity:\s+(\d.+)/i', $line, $out)) { + $row['maxram'] += self::convertSize($out[1], 'G', false); + } + } elseif ($section === 'Memory Device') { + if (preg_match('/^\s*Size:\s*(.*?)\s*$/i', $line, $out)) { + $row['extram'] = true; + if (preg_match('/(\d+)\s*(\w)i?B/i', $out[1])) { + if (self::convertSize($out[1], 'M', false) < 35) + continue; // TODO: Parsing this line by line is painful. Check for other indicators, like Locator + $ramslot['size'] = self::convertSize($out[1], 'G'); + } elseif (!isset($row['ramslot']) || (count($row['ramslot']) < 8 && (!isset($row['ramslotcount']) || $row['ramslotcount'] <= 8))) { + $ramslot['size'] = '_____'; + } + } + if (preg_match('/^\s*Manufacturer:\s*(.*?)\s*$/i', $line, $out) && $out[1] !== 'Unknown') { + $ramslot['manuf'] = self::decodeJedec($out[1]); + } + if (preg_match('/^\s*Form Factor:\s*(.*?)\s*$/i', $line, $out) && $out[1] !== 'Unknown') { + $ramForm = $out[1]; + } + if (preg_match('/^\s*Type:\s*(.*?)\s*$/i', $line, $out) && $out[1] !== 'Unknown') { + $ramType = $out[1]; + } + if (preg_match('/^\s*Speed:\s*(\d.*?)\s*$/i', $line, $out)) { + $ramSpeed = $out[1]; + } + if (preg_match('/^\s*Configured (?:Clock|Memory) Speed:\s*(\d.*?)\s*$/i', $line, $out)) { + $ramClockSpeed = $out[1]; + } + } elseif ($section === 'BIOS Information') { + if (preg_match(',^\s*Release Date:\s*(\d{2}/\d{2}/\d{4})\s*$,i', $line, $out)) { + $row['biosdate'] = date('d.m.Y', strtotime($out[1])); + } elseif (preg_match('/^\s*BIOS Revision:\s*(.*?)\s*$/i', $line, $out)) { + $row['biosrevision'] = $out[1]; + } elseif (preg_match('/^\s*Version:\s*(.*?)\s*$/i', $line, $out)) { + $row['biosversion'] = $out[1]; + } + } + } + if (empty($row['ramslotcount']) || (isset($row['ramslot']) && $row['ramslotcount'] < count($row['ramslot']))) { + $row['ramslotcount'] = isset($row['ramslot']) ? count($row['ramslot']) : 0; + } + if ($row['maxram'] > 0) { + $row['maxram'] .= ' GiB'; + } + } + + const LOOKUP = ['T' => 1099511627776, 'G' => 1073741824, 'M' => 1048576, 'K' => 1024, '' => 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::LOOKUP[strtoupper($out[2])]; + if (!array_key_exists($scale, self::LOOKUP)) { + foreach (self::LOOKUP as $k => $v) { + if ($k === '' || $val / 8 >= $v || abs($val - $v) < 50) { + $scale = $k; + break; + } + } + } + $val = (int)round($val / self::LOOKUP[$scale]); + if ($appendUnit) { + $val .= ' ' . ($scale === '' ? 'Byte' : $scale . 'iB'); // NBSP!! + } + return $val; + } + + public static function parseHdd(&$row, $data) + { + $hdds = array(); + // Could have more than one disk - linear scan + $lines = preg_split("/[\r\n]+/", $data); + $i = 0; + $mbrToMbFactor = $sectorToMbFactor = 0; + foreach ($lines as $line) { + if (preg_match('/^Disk (\S+):.* (\d+) bytes/i', $line, $out)) { + // --- Beginning of MBR disk --- + unset($hdd); + if ($out[2] < 10000) // sometimes vmware reports lots of 512byte disks + continue; + if (substr($out[1], 0, 8) === '/dev/dm-') // Ignore device mapper + continue; + // disk total size and name + $mbrToMbFactor = 0; // This is != 0 for mbr + $sectorToMbFactor = 0; // This is != for gpt + $hdd = array( + 'devid' => 'devid-' . ++$i, + 'dev' => $out[1], + 'sectors' => 0, + 'size' => round($out[2] / (1024 * 1024 * 1024)), + 'used' => 0, + 'partitions' => array(), + 'json' => array(), + ); + $hdds[] = &$hdd; + } elseif (preg_match('/^Disk (\S+):\s+(\d+)\s+sectors,/i', $line, $out)) { + // --- Beginning of GPT disk --- + unset($hdd); + if ($out[2] < 1000) // sometimes vmware reports lots of 512byte disks + continue; + if (substr($out[1], 0, 8) === '/dev/dm-') // Ignore device mapper + continue; + // disk total size and name + $mbrToMbFactor = 0; // This is != 0 for mbr + $sectorToMbFactor = 0; // This is != for gpt + $hdd = array( + 'devid' => 'devid-' . ++$i, + 'dev' => $out[1], + 'sectors' => $out[2], + 'size' => 0, + 'used' => 0, + 'partitions' => array(), + 'json' => array(), + ); + $hdds[] = &$hdd; + } elseif (preg_match('/^Units =.*= (\d+) bytes/i', $line, $out)) { + // --- MBR: Line that tells us how to interpret units for the partition lines --- + // Unit for start and end + $mbrToMbFactor = $out[1] / (1024 * 1024); // Convert so that multiplying by unit yields MiB + } elseif (preg_match('/^Logical sector size:\s*(\d+)/i', $line, $out)) { + // --- GPT: Line that tells us the logical sector size used everywhere --- + $sectorToMbFactor = $out[1] / (1024 * 1024); + } elseif (isset($hdd) && preg_match('/^First usable sector.* is (\d+)$/i', $line, $out)) { + // --- Some fdisk versions are messed up and report 2^32 as the sector count in the first line, + // but the correct value in the "last usable sector is xxxx" line below --- + if ($out[1] > $hdd['sectors']) { + $hdd['sectors'] = $out[1]; + } + } elseif (isset($hdd) && $mbrToMbFactor !== 0 && preg_match(',^/dev/(\S+)\s+.*\s(\d+)[+\-]?\s+(\d+)[+\-]?\s+\d+[+\-]?\s+([0-9a-f]+)\s+(.*)$,i', $line, $out)) { + // --- MBR: Partition entry --- + // Some partition + $type = strtolower($out[4]); + if ($type === '5' || $type === 'f' || $type === '85') { + continue; + } elseif ($type === '44') { + $out[5] = 'OpenSLX-ID44'; + $color = '#5c1'; + } elseif ($type === '45') { + $out[5] = 'OpenSLX-ID45'; + $color = '#0d7'; + } elseif ($type === '82') { + $color = '#48f'; + } else { + $color = '#e55'; + } + + $partsize = round(($out[3] - $out[2]) * $mbrToMbFactor); + $hdd['partitions'][] = array( + 'id' => $out[1], + 'name' => $out[1], + 'size' => round($partsize / 1024, $partsize < 1024 ? 1 : 0), + 'type' => $out[5], + ); + $hdd['json'][] = array( + 'label' => $out[1], + 'value' => $partsize, + 'color' => $color, + ); + $hdd['used'] += $partsize; + } elseif (isset($hdd) && $sectorToMbFactor !== 0 && preg_match(',^\s*(\d+)\s+(\d+)[+\-]?\s+(\d+)[+\-]?\s+\S+\s+([0-9a-f]+)\s+(.*)$,i', $line, $out)) { + // --- GPT: Partition entry --- + // Some partition + $type = $out[5]; + if ($type === 'OpenSLX-ID44') { + $color = '#5c1'; + } elseif ($type === 'OpenSLX-ID45') { + $color = '#0d7'; + } elseif ($type === 'Linux swap') { + $color = '#48f'; + } else { + $color = '#e55'; + } + $id = $hdd['devid'] . '-' . $out[1]; + $partsize = round(($out[3] - $out[2]) * $sectorToMbFactor); + $hdd['partitions'][] = array( + 'id' => $id, + 'name' => $out[1], + 'size' => round($partsize / 1024, $partsize < 1024 ? 1 : 0), + 'type' => $type, + ); + $hdd['json'][] = array( + 'label' => $id, + 'value' => $partsize, + 'color' => $color, + ); + $hdd['used'] += $partsize; + } + } + unset($hdd); + $i = 0; + foreach ($hdds as &$hdd) { + $hdd['used'] = round($hdd['used'] / 1024); + if ($hdd['size'] === 0 && $hdd['sectors'] !== 0) { + $hdd['size'] = round(($hdd['sectors'] * $sectorToMbFactor) / 1024); + } + $free = $hdd['size'] - $hdd['used']; + if ($hdd['size'] > 0 && ($free > 5 || ($free / $hdd['size']) > 0.1)) { + $hdd['partitions'][] = array( + 'id' => 'free-id-' . $i, + 'name' => Dictionary::translate('unused'), + 'size' => $free, + 'type' => '-', + ); + $hdd['json'][] = array( + 'label' => 'free-id-' . $i, + 'value' => $free * 1024, + 'color' => '#aaa', + ); + ++$i; + } + $hdd['json'] = json_encode($hdd['json']); + } + unset($hdd); + $row['hdds'] = &$hdds; + } + + public static function parsePci(&$pci1, &$pci2, $data) + { + preg_match_all('/[a-f0-9:.]{7}\s+"(Class\s*)?(?<class>[a-f0-9]{4})"\s+"(?<ven>[a-f0-9]{4})"\s+"(?<dev>[a-f0-9]{4})"/is', $data, $out, PREG_SET_ORDER); + $NOW = time(); + $pci = array(); + foreach ($out as $entry) { + if (!isset($pci[$entry['class']])) { + $class = 'c.' . $entry['class']; + $res = Page_Statistics::getPciId('CLASS', $class); + if ($res === false || $res['dateline'] < $NOW) { + $pci[$entry['class']]['lookupClass'] = 'do-lookup'; + $pci[$entry['class']]['class'] = $class; + } else { + $pci[$entry['class']]['class'] = $res['value']; + } + } + $new = array( + 'ven' => $entry['ven'], + 'dev' => $entry['ven'] . ':' . $entry['dev'], + ); + $res = Page_Statistics::getPciId('VENDOR', $new['ven']); + if ($res === false || $res['dateline'] < $NOW) { + $new['lookupVen'] = 'do-lookup'; + } else { + $new['ven'] = $res['value']; + } + $res = Page_Statistics::getPciId('DEVICE', $new['dev']); + if ($res === false || $res['dateline'] < $NOW) { + $new['lookupDev'] = 'do-lookup'; + } else { + $new['dev'] = $res['value'] . ' (' . $new['dev'] . ')'; + } + $pci[$entry['class']]['entries'][] = $new; + } + ksort($pci); + foreach ($pci as $class => $entry) { + if ($class === '0300' || $class === '0200' || $class === '0403') { + $pci1[] = $entry; + } else { + $pci2[] = $entry; + } + } + } + + public static function parseSmartctl(&$hdds, $data) + { + $lines = preg_split("/[\r\n]+/", $data); + foreach ($lines as $line) { + if (preg_match('/^NEXTHDD=(.+)$/', $line, $out)) { + unset($dev); + foreach ($hdds as &$hdd) { + if ($hdd['dev'] === $out[1]) { + $dev = &$hdd; + } + } + continue; + } + if (!isset($dev)) { + continue; + } + if (preg_match('/^([A-Z][^:]+):\s*(.*)$/', $line, $out)) { + $key = preg_replace('/\s|-|_/', '', $out[1]); + if ($key === 'ModelNumber') { + $key = 'DeviceModel'; + } + $dev['s_' . $key] = $out[2]; + } elseif (preg_match('/^\s*\d+\s+(\S+)\s+\S+\s+\d+\s+\d+\s+\S+\s+\S+\s+(\d+)(\s|$)/', $line, $out)) { + $dev['s_' . preg_replace('/\s|-|_/', '', $out[1])] = $out[2]; + } + } + // Format strings + foreach ($hdds as &$hdd) { + if (isset($hdd['s_PowerOnHours'])) { + $hdd['PowerOnTime'] = ''; + $val = (int)str_replace('.', '', $hdd['s_PowerOnHours']); + if ($val > 8760) { + $hdd['PowerOnTime'] .= floor($val / 8760) . 'Y, '; + $val %= 8760; + } + if ($val > 720) { + $hdd['PowerOnTime'] .= floor($val / 720) . 'M, '; + $val %= 720; + } + if ($val > 24) { + $hdd['PowerOnTime'] .= floor($val / 24) . 'd, '; + $val %= 24; + } + $hdd['PowerOnTime'] .= $val . 'h'; + } + } + } + + private static function decodeJedec(string $string): string + { + // JEDEC ID:7F 7F 9E 00 00 00 00 00 + if (preg_match('/JEDEC(?:\s*ID)?\s*:\s*([0-9a-f\s]+)/i', $string, $out)) { + preg_match_all('/[0-9a-f]{2}/i', $out[1], $out); + $bank = 0; + $id = 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 !== 0) { + 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 $string; + } + + /** + * @param string $key + * @param string $value + * @return ?int value, or null if not numeric + */ + private static function toNumeric(string $key, string $val) + { + $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|size/', $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>, ... ] + */ + private 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 + */ + private static function writeLocalHardwareData(string $uuid, int $hwid, string $pathId, array $props): int + { + // Add mapping between hw entity and machine + $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 + * 2) Any values ending in Bytes, bits or speed will be normalized + */ + private static function prepareDmiProperties(array $data): array + { + $ret = []; + foreach ($data as $key => $vals) { + $val = trim($vals['values'][0]); + 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') { + 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 from " . 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]); + } + } + + private static function writeGlobalHardwareData(string $dbType, array $global): 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)); + 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. resultion, 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]; + } + + public static function parseMachine(string $uuid, string $data) + { + $data = json_decode($data, true); + $version = $data['version'] ?? 0; + if ($version != 2) { + error_log("Received unsupported hw json v$version"); + return; + } + // determine misc stuff first + $globalMainboardExtra = []; + $localMainboardExtra = []; + // physical memory array + $memArrays = self::getDmiHandles($data, 16); + $globalMainboardExtra['Memory Slot Count'] = 0; + $globalMainboardExtra['Memory Maximum Capacity'] = 0; + foreach ($memArrays as $mem) { + $mem = self::prepareDmiProperties($mem); + if (isset($mem['Number Of Devices']) && ($mem['Use'] ?? 0) === 'System Memory') { + $globalMainboardExtra['Memory Slot Count'] += $mem['Number Of Devices']; + } + if (isset($mem['Maximum Capacity'])) { + $globalMainboardExtra['Memory Maximum Capacity'] += HardwareParser::convertSize($mem['Maximum Capacity'], 'M', false); + } + } + $globalMainboardExtra['Memory Maximum Capacity'] = HardwareParser::convertSize($globalMainboardExtra['Memory Maximum Capacity'] . ' MB'); + // BIOS section - need to cross-match this with mainboard or system model, as it doesn't have a meaningful + // identifier on its own + $bios = self::prepareDmiProperties(self::getDmiHandles($data, 0)[0]); + foreach (['Version', 'Release Date', 'Firmware Revision'] as $k) { + if (isset($bios[$k])) { + $localMainboardExtra['BIOS ' . $k] = $bios[$k]; + } + } + if (isset($bios['BIOS Revision'])) { + $localMainboardExtra['BIOS Revision'] = $bios['BIOS Revision']; + } + foreach (['Vendor', 'ROM Size'] as $k) { + if (isset($bios[$k])) { + $globalMainboardExtra['BIOS ' . $k] = $bios[$k]; + } + } + // Using the general helper function + $ramModCount = self::updateHwTypeFromDmi($uuid, $data, 17, HardwareInfo::RAM_MODULE, function (array $flat): bool { + return HardwareParser::convertSize(($flat['Size'] ?? 0), '', false) > 65 * 1024 * 1024; + }, + ['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'] + ); + // Fake RAM slots used/total into this + $localMainboardExtra['Memory Slot Occupied'] = $ramModCount; + self::updateHwTypeFromDmi($uuid, $data, 2, HardwareInfo::MAINBOARD, ['Manufacturer', 'Product Name'], + [], + ['Manufacturer', 'Product Name', 'Type', 'Version'], + ['Serial Number', 'Asset Tag', 'Location In Chassis'], + $globalMainboardExtra, $localMainboardExtra + ); + self::updateHwTypeFromDmi($uuid, $data, 1, HardwareInfo::DMI_SYSTEM, ['Manufacturer', 'Product Name'], + [], + ['Manufacturer', 'Product Name', 'Version', 'Wake-up Type'], + ['Serial Number', 'UUID', 'SKU Number'] + ); + 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'], + ['Serial Number', 'Asset Tag', 'Status', 'Plugged', 'Hot Replaceable'] + ); + self::updateHwTypeFromDmi($uuid, $data, 4, HardwareInfo::CPU, ['Version'], + ['Socket Designation'], + ['Type', 'Family', 'Manufacturer', 'Signature', 'Version', 'Core Count', 'Thread Count'], + ['Voltage', 'Current Speed', 'Upgrade', 'Core Enabled']); + self::updateHwTypeFromDmi($uuid, $data, 9, HardwareInfo::SYSTEM_SLOT, function (array &$entry): bool { + if (!isset($entry['Type'])) + return false; + // Split up PCIe info + 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'], + ['Current Usage', 'Designation'] + ); + // ---- lspci ------------------------------------ + $pciHwIds = []; + foreach (($data['lspci'] ?? []) as $dev) { + $hwid = self::writeGlobalHardwareData(HardwareInfo::PCI_DEVICE, + self::propsFromArray($dev, 'vendor', 'device', 'rev', 'class')); + $mappingId = self::writeLocalHardwareData($uuid, $hwid, $dev['slot'] ?? 'unknown', + self::propsFromArray($dev, 'slot', 'subsystem', 'subsystem_vendor')); + $pciHwIds[] = $mappingId; + } + self::markDisconnected($uuid, HardwareInfo::PCI_DEVICE, $pciHwIds); + // ---- Disks ------------------------------------0Y3R3K + $hddHwIds = []; + foreach (($data['drives'] ?? []) as $dev) { + if (empty($dev['readlink'])) + continue; + if (!isset($dev['smartctl'])) { + $smart = []; + } else { + $smart =& $dev['smartctl']; + } + if (!isset($dev['lsblk']['blockdevices'][0])) { + $lsblk = []; + } else { + $lsblk =& $dev['lsblk']['blockdevices'][0]; + } + if (!isset($smart['rotation_rate']) && isset($lsblk['rota']) && !$lsblk['rota']) { + $smart['rotation_rate'] = 0; + } + $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' => $lsblk['size'] ?? $smart['user_capacity']['bytes'] ?? 'unknown', + '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')); + // 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 - find special ones + 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) { + $id = 'part_' . $i . '_'; + 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'; + } elseif ($type == '45' || strtolower($type) === '87f86132-ff94-4987-b250-454545454545' + || $name === 'OpenSLX-ID45') { + $table[$id . 'slxtype'] = '45'; + } + // + ++$i; + } + } + $mappingId = self::writeLocalHardwareData($uuid, $hwid, $dev['readlink'], + self::propsFromArray($smart + ($lsblk ?? []), + 'serial_number', 'firmware_version', + 'interface_speed//current//string', + 'smart_status//passed', 'temperature//current', 'temperature//min', 'temperature//max') + $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), + ]); + $hddHwIds[] = $mappingId; + unset($smart, $lsblk); + } // End loop over disks + self::markDisconnected($uuid, HardwareInfo::HDD, $hddHwIds); + // + // Mark parse date + Database::exec("UPDATE machine SET dataparsetime = UNIX_TIMESTAMP() WHERE machineuuid = :uuid", + ['uuid' => $uuid]); + } + + /** + * Unify different variants of manufacturer names + */ + private static function fixManufacturer(string $in): string + { + $in = HardwareParser::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 'dell inc.': + return 'Dell'; + case 'fujitsu': + case 'fujitsu client computing limited': + return 'Fujitsu'; + case 'hewlett packard': + case 'hewlett-packard': + return 'HP'; + case 'hynix semiconduc': + case 'hynix/hyundai': + case 'hyundai electronics hynix semiconductor inc': + case 'hynix semiconductor inc sk hynix': + return 'SK Hynix'; + case 'genuineintel': + case 'intel corporation': + case 'intel(r) corp.': + case 'intel(r) corporation': + return 'Intel'; + case 'samsung sdi': + return 'Samsung'; + } + return $in; + } + + /** + * Takes key-value-array, returns a new array with only the keys listed in $fields. + */ + 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; + } + + 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); + $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); + } + +} |