diff options
author | Jose M. Guisado <jguisado@soleta.eu> | 2023-10-05 16:01:32 +0200 |
---|---|---|
committer | Jose M. Guisado <jguisado@soleta.eu> | 2023-10-05 16:19:36 +0200 |
commit | 39c13287c53bd81d151116b6af434498cf3da0be (patch) | |
tree | ee2900ba1b73c525e625ffcdfc75b2cab2b429e8 | |
parent | 49038f125aaadc3967a8eec251a4ea69ec5a8869 (diff) |
live: hw_inventory: fix empty memory bank bug
When a client's hardware presents an empty memory bank and invalid call
to _bytes_to_human is performed because None is passed as a parameter.
size = _bytes_to_human(obj.get('size', None))
Fix this by checking if 'size' is present in the JSON output from lshw.
If size is present then map the bytes to a human readable string using
_bytes_to_human, if no size is present then use 'Empty slot' to indicate
that the memory bank is not being used.
-rw-r--r-- | src/utils/hw_inventory.py | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/src/utils/hw_inventory.py b/src/utils/hw_inventory.py index 2f38ae2..e7cb463 100644 --- a/src/utils/hw_inventory.py +++ b/src/utils/hw_inventory.py @@ -64,7 +64,7 @@ class HardwareInventory(): def _bytes_to_human(size): suffixes = ['B', 'MiB', 'GiB', 'TiB'] if type(size) is not int: - raise TypeError('Invalid type') + raise TypeError(f'Invalid type in _bytes_to_human, got: {size} {type(size)}') for exponent, suffix in enumerate(suffixes, start=1): conv = size / (1024**exponent) if conv < 1024: @@ -106,11 +106,14 @@ def _process_core_cpu(inventory, obj): def _process_core_mem_bank(inventory, obj): slot = obj.get('slot', 'Unknown slot') - size = _bytes_to_human(obj.get('size', None)) - if size: - mem = ' '.join([obj.get('vendor', 'Unknown vendor'), obj.get('product', 'Unknown product'), size, f'({slot})']) - mem_elem = HardwareElement(HardwareType.MEMORY, mem) - inventory.add_element(mem_elem) + if 'size' in obj: + size = obj['size'] + human_size = _bytes_to_human(size) + mem = ' '.join([obj.get('vendor', 'Unknown vendor'), obj.get('product', 'Unknown product'), human_size, f'({slot})']) + else: + mem = ' '.join([obj.get('vendor', 'Unknown vendor'), obj.get('product', 'Unknown product'), 'Empty slot', f'({slot})']) + mem_elem = HardwareElement(HardwareType.MEMORY, mem) + inventory.add_element(mem_elem) def _process_core_mem(inventory, obj): |