From d183661be552acf7f30fce2e038173c5bdfa8421 Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Mon, 19 Jul 2021 12:13:11 +0200 Subject: [PATCH] cephadm: `stats` might return `--` for containers Signed-off-by: Sebastian Wagner --- src/cephadm/cephadm | 27 +++++++++++++++++++-------- src/cephadm/tests/test_cephadm.py | 5 +++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/cephadm/cephadm b/src/cephadm/cephadm index cd42a32637d..58eac3677be 100755 --- a/src/cephadm/cephadm +++ b/src/cephadm/cephadm @@ -4955,14 +4955,7 @@ def list_daemons(ctx, detail=True, legacy_dir=None): [container_path, 'stats', '--format', '{{.ID}},{{.MemUsage}}', '--no-stream'], verbosity=CallVerbosity.DEBUG ) - seen_memusage_cid_len = 0 - if not code: - for line in out.splitlines(): - (cid, usage) = line.split(',') - (used, limit) = usage.split(' / ') - seen_memusage[cid] = with_units_to_int(used) - if not seen_memusage_cid_len: - seen_memusage_cid_len = len(cid) + seen_memusage_cid_len, seen_memusage = _parse_mem_usage(code, out) # /var/lib/ceph if os.path.exists(data_dir): @@ -5145,6 +5138,24 @@ def list_daemons(ctx, detail=True, legacy_dir=None): return ls +def _parse_mem_usage(code: int, out: str) -> Tuple[int, Dict[str, int]]: + # keep track of memory usage we've seen + seen_memusage = {} # type: Dict[str, int] + seen_memusage_cid_len = 0 + if not code: + for line in out.splitlines(): + (cid, usage) = line.split(',') + (used, limit) = usage.split(' / ') + try: + seen_memusage[cid] = with_units_to_int(used) + if not seen_memusage_cid_len: + seen_memusage_cid_len = len(cid) + except ValueError: + logger.info('unable to parse memory usage line\n>{}'.format(line)) + pass + return seen_memusage_cid_len, seen_memusage + + def get_daemon_description(ctx, fsid, name, detail=False, legacy_dir=None): # type: (CephadmContext, str, str, bool, Optional[str]) -> Dict[str, str] diff --git a/src/cephadm/tests/test_cephadm.py b/src/cephadm/tests/test_cephadm.py index 5eeac11fb93..4fa0d63d374 100644 --- a/src/cephadm/tests/test_cephadm.py +++ b/src/cephadm/tests/test_cephadm.py @@ -159,6 +159,11 @@ class TestCephAdm(object): args = cd._parse_args(['--image', 'foo', 'version']) assert args.image == 'foo' + def test_parse_mem_usage(self): + cd.logger = mock.Mock() + len, summary = cd._parse_mem_usage(0, 'c6290e3f1489,-- / --') + assert summary == {} + def test_CustomValidation(self): assert cd._parse_args(['deploy', '--name', 'mon.a', '--fsid', 'fsid']) -- 2.39.5