Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[procdockerstatsd] add missing unit conversion #7151

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/sonic-host-services/scripts/procdockerstatsd
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,29 @@ class ProcDockerStats(daemon_base.DaemonBase):
UNITS_B = 'B'
UNITS_KB = 'KB'
UNITS_MB = 'MB'
UNITS_GB = 'GB'
UNITS_TB = 'TB'
UNITS_MiB = 'MiB'
UNITS_GiB = 'GiB'
UNITS_TiB = 'TiB'

res = re.match(r'(\d+\.?\d*)([a-zA-Z]+)', value)
value = float(res.groups()[0])
units = res.groups()[1]
if units.lower() == UNITS_KB.lower():
value *= 1000
elif units.lower() == UNITS_MB.lower():
value *= (1000 * 1000)
value *= (1000 ** 2)
elif units.lower() == UNITS_GB.lower():
value *= (1000 ** 3)
elif units.lower() == UNITS_TB.lower():
value *= (1000 ** 4)
elif units.lower() == UNITS_MiB.lower():
value *= (1024 * 1024)
value *= (1024 ** 2)
elif units.lower() == UNITS_GiB.lower():
value *= (1024 * 1024 * 1024)
value *= (1024 ** 3)
elif units.lower() == UNITS_TiB.lower():
value *= (1024 ** 4)

return int(round(value))

Expand Down
6 changes: 5 additions & 1 deletion src/sonic-host-services/tests/procdockerstatsd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ def test_convert_to_bytes(self):
('500MiB', 524288000),
('66.41MiB', 69635932),
('333.6MiB', 349804954),
('7GB', 7000000000),
('800GB', 800000000000),
pra-moh marked this conversation as resolved.
Show resolved Hide resolved
('1GiB', 1073741824),
('500GiB', 536870912000),
('7.751GiB', 8322572878)
('7.751GiB', 8322572878),
('8.1TiB', 8906000000000000),
('12TB', 12000000000000)
pra-moh marked this conversation as resolved.
Show resolved Hide resolved
]

pdstatsd = procdockerstatsd.ProcDockerStats(procdockerstatsd.SYSLOG_IDENTIFIER)
Expand Down