diff options
author | Simon Rettberg | 2021-09-30 17:44:30 +0200 |
---|---|---|
committer | Simon Rettberg | 2022-03-09 15:06:54 +0100 |
commit | d736e75ade7a4472aefb72af9036f86016adcb42 (patch) | |
tree | 39e38d5dd687bc6d25cb1de45ce4b78423aa7984 /modules-available/statistics/inc/hardwareparserlegacy.inc.php | |
parent | [passthrough] New module for managing hardware passthrough for QEMU (diff) | |
download | slx-admin-d736e75ade7a4472aefb72af9036f86016adcb42.tar.gz slx-admin-d736e75ade7a4472aefb72af9036f86016adcb42.tar.xz slx-admin-d736e75ade7a4472aefb72af9036f86016adcb42.zip |
[statistics] Adapt hw-data parsing to new json format for display
Diffstat (limited to 'modules-available/statistics/inc/hardwareparserlegacy.inc.php')
-rw-r--r-- | modules-available/statistics/inc/hardwareparserlegacy.inc.php | 284 |
1 files changed, 284 insertions, 0 deletions
diff --git a/modules-available/statistics/inc/hardwareparserlegacy.inc.php b/modules-available/statistics/inc/hardwareparserlegacy.inc.php new file mode 100644 index 00000000..277f6d4a --- /dev/null +++ b/modules-available/statistics/inc/hardwareparserlegacy.inc.php @@ -0,0 +1,284 @@ +<?php + +class HardwareParserLegacy +{ + + public static function parseHdd(&$row, $data) + { + $hdds = []; + // Could have more than one disk - linear scan + $lines = preg_split("/[\r\n]+/", $data); + $i = 0; + $mbrToByteFactor = $sectorToByteFactor = 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 (preg_match('#^/dev/(dm-|x?loop|d?nbd)#', $out[1])) // Ignore device mapper etc. + continue; + // disk total size and name + $mbrToByteFactor = 0; // This is != 0 for mbr + $sectorToByteFactor = 0; // This is != for gpt + $hdd = [ + 'devid' => 'devid-' . ++$i, + 'dev' => $out[1], + 'sectors' => 0, + 'size' => $out[2], + 'used' => 0, + 'partitions' => [], + ]; + $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 (preg_match('#^/dev/(dm-|x?loop|d?nbd)#', $out[1])) // Ignore device mapper etc. + continue; + // disk total size and name + $mbrToByteFactor = 0; // This is != 0 for mbr + $sectorToByteFactor = 0; // This is != for gpt + $hdd = [ + 'devid' => 'devid-' . ++$i, + 'dev' => $out[1], + 'sectors' => $out[2], + 'size' => 0, + 'used' => 0, + 'partitions' => [], + ]; + $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 + $mbrToByteFactor = $out[1]; // 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 --- + $sectorToByteFactor = $out[1]; + } 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) && $mbrToByteFactor !== 0 && preg_match(', + ^/dev/(\S+) # device + \s+.*\s(\d+)[+\-]? # start + \s+(\d+)[+\-]? # end + \s+\d+[+\-]? # size + \s+([0-9a-f]+) # typeid + \s+(.*)$ # type name + ,ix', $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'; + } elseif ($type === '45') { + $out[5] = 'OpenSLX-ID45'; + } + + $start = $out[2] * $mbrToByteFactor; + $partsize = ($out[3] - $out[2]) * $mbrToByteFactor; + $hdd['partitions'][] = [ + 'id' => $out[1], + 'index' => $out[1], + 'start' => $start, + 'size' => $partsize, + 'name' => $out[5], + 'slxtype' => $type, + ]; + $hdd['used'] += $partsize; + } elseif (isset($hdd) && $sectorToByteFactor !== 0 && preg_match(', + ^\s*(\d+) # index + \s+(\d+)[+\-]? # start + \s+(\d+)[+\-]? # end + \s+\S+ # human readable size + \s+([0-9a-f]{2})[0-9a-f]* # pseudo-type-id + \s+(.*)$ # PartLabel + ,ix', $line, $out)) { + // --- GPT: Partition entry --- + // Some partition + $slxtype = $out[4]; + if ($out[5] === 'OpenSLX-ID44') { + $slxtype = '44'; + } elseif ($out[5] === 'OpenSLX-ID45') { + $slxtype = '45'; + } elseif ($out[5] === 'Linux swap') { + $slxtype = '82'; + } + $id = $hdd['devid'] . '-' . $out[1]; + $start = $out[2] * $sectorToByteFactor; + $partsize = ($out[3] - $out[2]) * $sectorToByteFactor; + $hdd['partitions'][] = [ + 'id' => $id, + 'index' => $out[1], + 'start' => $start, + 'size' => $partsize, + 'name' => $out[5], + 'slxtype' => $slxtype, + ]; + $hdd['used'] += $partsize; + } + } + unset($hdd); + foreach ($hdds as &$hdd) { + if ($hdd['size'] === 0 && $hdd['sectors'] !== 0) { + $hdd['size'] = round($hdd['sectors'] * $sectorToByteFactor); + } + } + unset($hdd); + $row['hdds'] = &$hdds; + } + + public static function parsePci(string $data): array + { + preg_match_all('/[a-f0-9:.]{7}\s+"(Class\s*)?(?<class>[a-f0-9]{4})"\s+"(?<vendor>[a-f0-9]{4})"\s+"(?<device>[a-f0-9]{4})"/is', $data, $out, PREG_SET_ORDER); + return $out; + } + + 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['model'] = $out[2]; + } elseif ($key === 'ModelFamily') { + $dev['model_family'] = $out[2]; + } elseif ($key === 'SerialNumber') { + $dev['serial_number'] = $out[2]; + } + } elseif (preg_match('/ + ^\s*(?<id>\d+)\s+\S+ # flags + \s+\S+\s+(?<v>\d+) + \s+(?<w>\d+) + \s+(?<t>\S+)\s+\S+ # fail + \s+(?<raw>\d+)(\s|$)/x', $line, $out)) { + $dev['attr_' . $out['id']] = [ + 'value' => $out['v'], + 'worst' => $out['w'], + 'thresh' => $out['t'], + 'raw' => $out['raw'], + ]; + if ($out['id'] == 194) { + $dev['temperature'] = $out['raw']; + } + } + } + } + + public static function parseCpu(&$row, $data) + { + if (0 >= preg_match_all('/^(.+):\s+(\d+)$/im', $data, $out, PREG_SET_ORDER)) { + return; + } + $tmp = []; + foreach ($out as $entry) { + $tmp[str_replace(' ', '', $entry[1])] = $entry[2]; + } + $row['cpu']['sockets'] = $tmp['Sockets']; + $row['cpu']['cores'] = $tmp['Realcores']; + $row['cpu']['threads'] = $tmp['Virtualcores']; + } + + public static function parseDmiDecode(&$row, $data) + { + $lines = preg_split("/[\r\n]+/", $data); + $section = false; + $ramOk = false; + $ramForm = $ramType = false; + $ramslot = []; + $row['ram'] = $row['system'] = $row['mainboard'] = $row['bios'] = []; + $row['Memory Slot Count'] = $row['Memory Maximum Capacity'] = 0; + foreach ($lines as $line) { + if (empty($line)) { + continue; + } + if ($line[0] !== "\t" && $line[0] !== ' ') { + if (isset($ramslot['Size'])) { + $row['ram'][] = $ramslot; + } + $ramslot = []; + $section = $line; + $ramOk = false; + if ($ramForm || $ramType) { + if (isset($row['ramtype'])) { + continue; + } + $row['ramtype'] = $ramType . '-' . $ramForm; + $ramForm = false; + $ramType = false; + } + continue; + } + if ($section === 'Base Board Information') { + if (preg_match('/^\s*([^:]+):\s*(.*?)\s*$/i', $line, $out) + && $out[2] !== 'Unknown' && $out[2] !== '' && $out[2] !== 'Not Specified') { + $row['mainboard'][$out[1]] = $out[2]; + } + } elseif ($section === 'System Information') { + if (preg_match('/^\s*([^:]+):\s*(.*?)\s*$/i', $line, $out) + && $out[2] !== 'Unknown' && $out[2] !== '' && $out[2] !== 'Not Specified') { + $row['system'][$out[1]] = $out[2]; + } + } 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['Memory Slot Count'] += $out[1]; + } + if ($ramOk && preg_match('/^\s*Maximum Capacity:\s+(\d.+)/i', $line, $out)) { + $row['Memory Maximum Capacity'] += HardwareParser::convertSize($out[1], 'G', false); + } + } elseif ($section === 'Memory Device') { + if (preg_match('/^\s*Size:\s*(.*?)\s*$/i', $line, $out)) { + if (preg_match('/(\d+)\s*(\w)i?B/i', $out[1])) { + if (HardwareParser::convertSize($out[1], 'M', false) < 35) + continue; // TODO: Parsing this line by line is painful. Check for other indicators, like Locator + $ramslot['Size'] = HardwareParser::convertSize($out[1], 'G'); + } + } elseif (preg_match('/^\s*Manufacturer:\s*(.*?)\s*$/i', $line, $out) && $out[1] !== 'Unknown') { + $ramslot['Manufacturer'] = HardwareParser::decodeJedec($out[1]); + } elseif (preg_match('/^\s*Form Factor:\s*(.*?)\s*$/i', $line, $out) && $out[1] !== 'Unknown') { + $ramForm = $out[1]; + } elseif (preg_match('/^\s*Type:\s*(.*?)\s*$/i', $line, $out) && $out[1] !== 'Unknown') { + $ramType = $out[1]; + } elseif (preg_match('/^\s*Configured Memory Speed:\s*(.*?)\s*$/i', $line, $out) && $out[1] !== 'Unknown') { + $ramslot['Configured Clock Speed'] = $out[1]; + } elseif (preg_match('/^\s*([^:]+):\s*(.*?)\s*$/i', $line, $out) + && $out[2] !== 'Unknown' && $out[2] !== '' && $out[2] !== 'Not Specified' && $out[2] !== 'None') { + $ramslot[$out[1]] = $out[2]; + } + } elseif ($section === 'BIOS Information') { + if (preg_match('/^\s*([^:]+):\s*(.*?)\s*$/i', $line, $out) + && $out[2] !== 'Unknown' && $out[2] !== '' && $out[2] !== 'Not Specified') { + $row['bios'][$out[1]] = $out[2]; + } + } + } + if (empty($row['Memory Slot Count']) || (isset($row['ramslot']) && $row['Memory Slot Count'] < count($row['ramslot']))) { + $row['Memory Slot Count'] = isset($row['ramslot']) ? count($row['ramslot']) : 0; + } + if ($row['Memory Maximum Capacity'] > 0) { + $row['Memory Maximum Capacity'] .= ' GiB'; + } + } +}
\ No newline at end of file |