Skip to content

Commit

Permalink
Convert memory with unit to proper int
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed Feb 6, 2024
1 parent cdd76c4 commit db40b22
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 11 deletions.
97 changes: 87 additions & 10 deletions lib/php/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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',
Expand Down Expand Up @@ -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
*
Expand Down
38 changes: 38 additions & 0 deletions tests/Glpi/Inventory/tests/units/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}


}
2 changes: 1 addition & 1 deletion tests/data/1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1713,7 +1713,7 @@
<VMTYPE>libvirt</VMTYPE>
</VIRTUALMACHINES>
<VIRTUALMACHINES>
<MEMORY>4194</MEMORY>
<MEMORY>4194MB</MEMORY>
<NAME>win8.1</NAME>
<STATUS>off</STATUS>
<SUBSYSTEM>kvm</SUBSYSTEM>
Expand Down

0 comments on commit db40b22

Please sign in to comment.