diff --git a/lib/php/Converter.php b/lib/php/Converter.php index e70b570..6e91274 100644 --- a/lib/php/Converter.php +++ b/lib/php/Converter.php @@ -420,8 +420,6 @@ private function convertTo01(array $data): array 'drives/free', 'drives/total', 'hardware/etime', - 'hardware/memory', - 'hardware/swap', 'storages/disksize', 'physical_volumes/free', 'physical_volumes/pe_size', @@ -433,16 +431,12 @@ private function convertTo01(array $data): array 'volume_groups/size', 'logical_volumes/seg_count', 'logical_volumes/size', - 'memories/capacity', 'memories/numslots', 'processes/pid', 'processes/virtualmemory', 'networks/mtu', 'softwares/filesize', - 'virtualmachines/memory', 'virtualmachines/vcpu', - 'videos/memory', - 'batteries/real_capacity', 'network_ports/ifinerrors', 'network_ports/ifinoctets', 'network_ports/ifinbytes', @@ -458,8 +452,6 @@ private function convertTo01(array $data): array 'network_ports/iftype', 'network_components/fru', 'network_components/index', - 'network_device/ram', - 'network_device/memory', 'network_device/credentials', 'pagecounters/total', 'pagecounters/black', @@ -845,7 +837,6 @@ private function convertTo01(array $data): array } } - //Fix batteries capacities & voltages if (isset($data['content']['batteries'])) { foreach ($data['content']['batteries'] as &$battery) { @@ -890,7 +881,6 @@ private function convertTo01(array $data): array } } - //type on ports is required if (isset($data['content']['ports'])) { foreach ($data['content']['ports'] as &$port) { @@ -917,6 +907,45 @@ private function convertTo01(array $data): array } } + //fix memories that can have a unit + if (isset($data['content']['hardware']['memory'])) { + $data['content']['hardware']['memory'] = $this->convertMemory($data['content']['hardware']['memory']); + } + if (isset($data['content']['hardware']['swap'])) { + $data['content']['hardware']['swap'] = $this->convertMemory($data['content']['hardware']['swap']); + } + if (isset($data['content']['memories'])) { + foreach ($data['content']['memories'] as &$memory) { + if (isset($memory['capacity'])) { + $memory['capacity'] = $this->convertMemory($memory['capacity']); + } + } + } + if (isset($data['content']['videos'])) { + foreach ($data['content']['videos'] as &$video) { + if (isset($video['memory'])) { + $video['memory'] = $this->convertMemory($video['memory']); + } + } + } + if (isset($data['content']['virtualmachines'])) { + foreach ($data['content']['virtualmachines'] as &$vm) { + if (isset($vm['memory'])) { + $vm['memory'] = $this->convertMemory($vm['memory']); + } + } + } + if (isset($data['content']['network_device'])) { + foreach ($data['content']['network_device'] as &$netdev) { + if (isset($netdev['memory'])) { + $netdev['memory'] = $this->convertMemory($netdev['memory']); + } + if (isset($netdev['ram'])) { + $netdev['ram'] = $this->convertMemory($netdev['ram']); + } + } + } + //no longer existing $drops = [ 'registry', @@ -1317,6 +1346,54 @@ public function convertBatteryVoltage(string $voltage) return false; } + /** + * Convert memory capacity + * + * @param string $capacity Inventoried capacity + * + * @return ?integer + */ + public function convertMemory(string $capacity) + { + if (is_int($casted_capa = $this->getCastedValue($capacity, 'integer'))) { + return $casted_capa; + } + + $mem_pattern = "/^([0-9]+([\.|,][0-9])?) ?(.?B)$/i"; + + $matches = []; + if (preg_match($mem_pattern, $capacity, $matches)) { + //we got a memory with a unit. first, convert to bytes + $real_value = $this->getCastedValue($matches[1], 'integer'); + switch (strtolower($matches[3])) { + case 'pb': + $real_value *= 1024; + //no break, continue to next + case 'tb': + $real_value *= 1024; + //no break, continue to next + case 'gb': + $real_value *= 1024; + //no break, continue to next + case 'mb': + $real_value *= 1024; + //no break, continue to next + case 'kb': + $real_value *= 1024; + //no break, continue to next + case 'b': + break; + default: + return null; + } + + //then return as Mb. + return $real_value / 1024 / 1024; + } + + return null; + } + /** * Handle network inventory XML format * diff --git a/tests/Glpi/Inventory/tests/units/Converter.php b/tests/Glpi/Inventory/tests/units/Converter.php index 1f78eb1..218bc2a 100644 --- a/tests/Glpi/Inventory/tests/units/Converter.php +++ b/tests/Glpi/Inventory/tests/units/Converter.php @@ -887,4 +887,42 @@ public function testAssetTagFromInventory() ); $this->assertCount(1, $json->content->network_ports); } + + /** + * Memory capacities convert provider + * + * @return array + */ + public static function memoryCapasToConvertProvider() + { + return [ + ['2048', 2048], + ['2048Mb', 2048], + ['2048MB', 2048], + ['2048 Mb', 2048], + ['2097152Kb', 2048], + ['2147483648b', 2048], + ['2.0Gb', 2048], + ['5Tb', 5242880], + ['3Pb', 3221225472] + ]; + } + + /** + * Test memory capacity conversion + * + * @dataProvider memoryCapasToConvertProvider + * + * @param string $orig Original data + * @param string $expected Expected result + * + * @return void + */ + public function testConvertMemoryCapacity($orig, $expected) + { + $instance = new \Glpi\Inventory\Converter(); + $this->assertSame($expected, $instance->convertMemory($orig)); + } + + } diff --git a/tests/data/1.xml b/tests/data/1.xml index c95bfe6..ecd4683 100644 --- a/tests/data/1.xml +++ b/tests/data/1.xml @@ -1713,7 +1713,7 @@ libvirt - 4194 + 4194MB win8.1 off kvm