diff options
Diffstat (limited to 'modules-available/statistics/inc/hardwareparserlegacy.inc.php')
-rw-r--r-- | modules-available/statistics/inc/hardwareparserlegacy.inc.php | 186 |
1 files changed, 81 insertions, 105 deletions
diff --git a/modules-available/statistics/inc/hardwareparserlegacy.inc.php b/modules-available/statistics/inc/hardwareparserlegacy.inc.php index 1bee23f9..4d8a8502 100644 --- a/modules-available/statistics/inc/hardwareparserlegacy.inc.php +++ b/modules-available/statistics/inc/hardwareparserlegacy.inc.php @@ -5,66 +5,71 @@ class HardwareParserLegacy public static function parseHdd(&$row, $data) { - $hdds = array(); + $hdds = []; // Could have more than one disk - linear scan $lines = preg_split("/[\r\n]+/", $data); $i = 0; - $mbrToMbFactor = $sectorToMbFactor = 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 (substr($out[1], 0, 8) === '/dev/dm-') // Ignore device mapper + if (preg_match('#^/dev/(dm-|x?loop|d?nbd)#', $out[1])) // Ignore device mapper etc. continue; // disk total size and name - $mbrToMbFactor = 0; // This is != 0 for mbr - $sectorToMbFactor = 0; // This is != for gpt - $hdd = array( + $mbrToByteFactor = 0; // This is != 0 for mbr + $sectorToByteFactor = 0; // This is != for gpt + $hdd = [ 'devid' => 'devid-' . ++$i, 'dev' => $out[1], 'sectors' => 0, - 'size' => round($out[2] / (1024 * 1024 * 1024)), + 'size' => $out[2], 'used' => 0, - 'partitions' => array(), - 'json' => array(), - ); + '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 (substr($out[1], 0, 8) === '/dev/dm-') // Ignore device mapper + if (preg_match('#^/dev/(dm-|x?loop|d?nbd)#', $out[1])) // Ignore device mapper etc. continue; // disk total size and name - $mbrToMbFactor = 0; // This is != 0 for mbr - $sectorToMbFactor = 0; // This is != for gpt - $hdd = array( + $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' => array(), - 'json' => array(), - ); + '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 - $mbrToMbFactor = $out[1] / (1024 * 1024); // Convert so that multiplying by unit yields MiB + $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 --- - $sectorToMbFactor = $out[1] / (1024 * 1024); + $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) && $mbrToMbFactor !== 0 && preg_match(',^/dev/(\S+)\s+.*\s(\d+)[+\-]?\s+(\d+)[+\-]?\s+\d+[+\-]?\s+([0-9a-f]+)\s+(.*)$,i', $line, $out)) { + } 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]); @@ -72,81 +77,58 @@ class HardwareParserLegacy 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( + $start = $out[2] * $mbrToByteFactor; + $partsize = ($out[3] - $out[2]) * $mbrToByteFactor; + $hdd['partitions'][] = [ '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, - ); + 'index' => $out[1], + 'start' => $start, + 'size' => $partsize, + 'name' => $out[5], + 'slxtype' => $type, + ]; $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)) { + } 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 - $type = $out[5]; - if ($type === 'OpenSLX-ID44') { - $color = '#5c1'; - } elseif ($type === 'OpenSLX-ID45') { - $color = '#0d7'; - } elseif ($type === 'Linux swap') { - $color = '#48f'; - } else { - $color = '#e55'; + $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]; - $partsize = round(($out[3] - $out[2]) * $sectorToMbFactor); - $hdd['partitions'][] = array( + $start = $out[2] * $sectorToByteFactor; + $partsize = ($out[3] - $out[2]) * $sectorToByteFactor; + $hdd['partitions'][] = [ 'id' => $id, - 'name' => $out[1], - 'size' => round($partsize / 1024, $partsize < 1024 ? 1 : 0), - 'type' => $type, - ); - $hdd['json'][] = array( - 'label' => $id, - 'value' => $partsize, - 'color' => $color, - ); + 'index' => $out[1], + 'start' => $start, + 'size' => $partsize, + 'name' => $out[5], + 'slxtype' => $slxtype, + ]; $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); + $hdd['size'] = round($hdd['sectors'] * $sectorToByteFactor); } - $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; @@ -213,32 +195,28 @@ class HardwareParserLegacy } 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 ($key === 'ModelNumber' || $key === 'DeviceModel') { + $dev['model'] = $out[2]; + } elseif ($key === 'ModelFamily') { + $dev['model_family'] = $out[2]; + } elseif ($key === 'SerialNumber') { + $dev['serial_number'] = $out[2]; } - if ($val > 720) { - $hdd['PowerOnTime'] .= floor($val / 720) . 'M, '; - $val %= 720; + } 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']; } - if ($val > 24) { - $hdd['PowerOnTime'] .= floor($val / 24) . 'd, '; - $val %= 24; - } - $hdd['PowerOnTime'] .= $val . 'h'; } } } @@ -309,14 +287,12 @@ class HardwareParserLegacy } } 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') { + } 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]; @@ -325,7 +301,7 @@ class HardwareParserLegacy } 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] !== 'Unknown' && $out[2] !== '' && $out[2] !== 'Not Specified' && $out[2] !== 'None') { $ramslot[$out[1]] = $out[2]; } } elseif ($section === 'BIOS Information') { |