[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"
This commit is contained in:
pra-moh 2021-03-16 05:54:19 -07:00 committed by GitHub
parent 385e7265ce
commit bd07256bfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 = {}