From bd07256bfd7b4f72eebaf07d9b8cbf99612fbe4b Mon Sep 17 00:00:00 2001 From: pra-moh <49077256+pra-moh@users.noreply.github.com> Date: Tue, 16 Mar 2021 05:54:19 -0700 Subject: [PATCH] [201911][procdockerstatsd] Fix unit conversion for docker stats (#7063) Bug exists in 201911 branch where unit conversion for docker stats is incorrect. Both MiB/GiB to byes conversion is incorrect Example: admin@str-s6000-acs-10:/usr/bin$ docker stats --no-stream -a CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS e958c81d27a8 mgmt-framework 0.00% 0B / 0B 0.00% 0B / 0B 0B / 0B 0 9b6b7b4361d5 telemetry 3.13% 86.31MiB / 7.785GiB 1.08% 0B / 0B 0B / 106kB 30 e7fee0b617fe snmp 70.28% 57.03MiB / 7.785GiB 0.72% 0B / 0B 0B / 102kB 9 admin@str-s6000-acs-10:/usr/bin$ redis-cli -n 6 hgetall "DOCKER_STATS|e7fee0b617fe" "MEM%" "0.72" "MEM_LIMIT_BYTES" "8359080099840" "NAME" "snmp" "NET_OUT_BYTES" "0" "MEM_BYTES" "5980028928" "BLOCK_OUT_BYTES" "102000" "NET_IN_BYTES" "0" "BLOCK_IN_BYTES" "0" "PIDS" "9" "CPU%" "5.96" --- .../procdockerstatsd/procdockerstatsd | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/files/image_config/procdockerstatsd/procdockerstatsd b/files/image_config/procdockerstatsd/procdockerstatsd index b7dd1705db..2216c0744f 100755 --- a/files/image_config/procdockerstatsd/procdockerstatsd +++ b/files/image_config/procdockerstatsd/procdockerstatsd @@ -66,28 +66,26 @@ class ProcDockerStats(daemon_base.DaemonBase): return process_data_list def convert_to_bytes(self, value): - unit_value = re.search('[a-zA-Z]+', value) - value_to_convert = float(filter(str.isdigit, value)) - unit = unit_value.group(0) UNITS_B = 'B' UNITS_KB = 'KB' UNITS_MB = 'MB' UNITS_MiB = 'MiB' UNITS_GiB = 'GiB' - if unit.lower() == UNITS_B.lower(): - return int(round(value_to_convert)) - elif unit.lower() == UNITS_KB.lower(): - value_converted = value_to_convert * 1000 - return int(round(value_converted)) + + res = re.match(r'(\d+\.?\d*)([a-zA-Z]+)', value) + value = float(res.groups()[0]) + units = res.groups()[1] + + if unit.lower() == UNITS_KB.lower(): + value *= 1000 elif unit.lower() == UNITS_MB.lower(): - value_converted = value_to_convert * 1000 * 1000 - return int(round(value_converted)) + value *= (1000 * 1000) elif unit.lower() == UNITS_MiB.lower(): - value_converted = value_to_convert * 1024 * 1024 - return int(round(value_converted)) + value *= (1024 * 1024) elif unit.lower() == UNITS_GiB.lower(): - value_converted = value_to_convert * 1024 * 1024 * 1024 - return int(round(value_converted)) + value *= (1024 * 1024 * 1024) + + return int(round(value)) def create_docker_dict(self, dict_list): dockerdict = {}