diff options
author | Simon Rettberg | 2021-09-30 17:44:30 +0200 |
---|---|---|
committer | Simon Rettberg | 2021-09-30 17:44:30 +0200 |
commit | c77d4b13c71a8e23becb5f935b8e880ac8497520 (patch) | |
tree | 017045f2b6cd7a2300a632f28343fb12b7b948cc /modules-available/statistics/inc | |
parent | [statistics] Hints (diff) | |
download | slx-admin-c77d4b13c71a8e23becb5f935b8e880ac8497520.tar.gz slx-admin-c77d4b13c71a8e23becb5f935b8e880ac8497520.tar.xz slx-admin-c77d4b13c71a8e23becb5f935b8e880ac8497520.zip |
[statistics] Adapt hw-data parsing to new json format for display
Diffstat (limited to 'modules-available/statistics/inc')
-rw-r--r-- | modules-available/statistics/inc/hardwareparser.inc.php | 378 | ||||
-rw-r--r-- | modules-available/statistics/inc/hardwareparserlegacy.inc.php | 345 |
2 files changed, 361 insertions, 362 deletions
diff --git a/modules-available/statistics/inc/hardwareparser.inc.php b/modules-available/statistics/inc/hardwareparser.inc.php index 15534749..f2ebe335 100644 --- a/modules-available/statistics/inc/hardwareparser.inc.php +++ b/modules-available/statistics/inc/hardwareparser.inc.php @@ -6,119 +6,6 @@ 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]; /** @@ -151,248 +38,12 @@ class HardwareParser 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 = PciId::getPciId('CLASS', $class); - if ($res === false) { - $pci[$entry['class']]['lookupClass'] = 'do-lookup'; - $pci[$entry['class']]['class'] = $class; - } else { - $pci[$entry['class']]['class'] = $res; - } - } - $new = array( - 'ven' => $entry['ven'], - 'dev' => $entry['ven'] . ':' . $entry['dev'], - ); - $res = PciId::getPciId('VENDOR', $new['ven']); - if ($res === false) { - $new['lookupVen'] = 'do-lookup'; - } else { - $new['ven'] = $res; - } - $res = PciId::getPciId('DEVICE', $new['dev']); - if ($res === false) { - $new['lookupDev'] = 'do-lookup'; - } else { - $new['dev'] = $res . ' (' . $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 + /** + * Decode JEDEC ID to according manufacturer + * @param string $string + * @return string + */ + public 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)) { @@ -464,7 +115,7 @@ class HardwareParser * @param int $type dmi type * @return array [ <props>, <props>, ... ] */ - private static function getDmiHandles(array $data, int $type): array + public static function getDmiHandles(array $data, int $type): array { if (empty($data['dmidecode'])) return []; @@ -532,9 +183,8 @@ class HardwareParser * * 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 + public static function prepareDmiProperties(array $data): array { $ret = []; foreach ($data as $key => $vals) { @@ -545,7 +195,7 @@ class HardwareParser 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 === 'manufacturer' || $val === 'systemmodel' || $val === 'fillbyoem' || $val === 'none') { continue; } $val = trim($vals['values'][0] ?? ''); @@ -609,9 +259,8 @@ class HardwareParser return $cache[$id]; } - public static function parseMachine(string $uuid, string $data) + public static function parseMachine(string $uuid, array $data) { - $data = json_decode($data, true); $version = $data['version'] ?? 0; if ($version != 2) { error_log("Received unsupported hw json v$version"); @@ -675,9 +324,14 @@ class HardwareParser 'Maximum Voltage'], ['Locator', 'Bank Locator', 'Serial Number', 'Asset Tag', 'Configured Memory Speed', 'Configured Voltage'] ); - // Fake RAM slots used/total into this + // Fake RAM slots used/total etc. into this $localMainboardExtra['Memory Slot Occupied'] = $ramModCount; $localMainboardExtra['Memory Installed Capacity'] = self::convertSize($capa, 'G', true); + foreach (['sockets', 'cores', 'threads'] as $key) { + if (!isset($data['cpu'][$key])) + continue; + $localMainboardExtra['cpu-' . $key] = $data['cpu'][$key]; + } self::updateHwTypeFromDmi($uuid, $data, 2, HardwareInfo::MAINBOARD, ['Manufacturer', 'Product Name'], [], ['Manufacturer', 'Product Name', 'Type', 'Version'], diff --git a/modules-available/statistics/inc/hardwareparserlegacy.inc.php b/modules-available/statistics/inc/hardwareparserlegacy.inc.php new file mode 100644 index 00000000..1bee23f9 --- /dev/null +++ b/modules-available/statistics/inc/hardwareparserlegacy.inc.php @@ -0,0 +1,345 @@ +<?php + +class HardwareParserLegacy +{ + + 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); + $pci = array(); + foreach ($out as $entry) { + if (!isset($pci[$entry['class']])) { + $class = 'c.' . $entry['class']; + $res = PciId::getPciId('CLASS', $class); + if ($res === false) { + $pci[$entry['class']]['lookupClass'] = 'do-lookup'; + $pci[$entry['class']]['class'] = $class; + } else { + $pci[$entry['class']]['class'] = $res; + } + } + $new = array( + 'ven' => $entry['ven'], + 'dev' => $entry['ven'] . ':' . $entry['dev'], + ); + $res = PciId::getPciId('VENDOR', $new['ven']); + if ($res === false) { + $new['lookupVen'] = 'do-lookup'; + } else { + $new['ven'] = $res; + } + $res = PciId::getPciId('DEVICE', $new['dev']); + if ($res === false) { + $new['lookupDev'] = 'do-lookup'; + } else { + $new['dev'] = $res . ' (' . $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'; + } + } + } + + 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)) { + $row['extram'] = true; + 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'); + } + } + if (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') { + $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 |