From: John Mulligan Date: Thu, 30 Jan 2025 00:25:36 +0000 (-0500) Subject: cephadm: add parsed_container_cpu_perc to container_engines X-Git-Tag: v20.0.0~245^2~2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=1834881bd1c7a2412ed0bf80442c43ccc9a7198a;p=ceph.git cephadm: add parsed_container_cpu_perc to container_engines Add a new function that combines the call and parse operations that exist in cephadm.py (currently _parse_cpu_perc and related command calls). This will be used in a future commit to replace that code and reduce the size of cephadm.py. Signed-off-by: John Mulligan --- diff --git a/src/cephadm/cephadmlib/container_engines.py b/src/cephadm/cephadmlib/container_engines.py index 77e3714c18acc..af094ecbd54c2 100644 --- a/src/cephadm/cephadmlib/container_engines.py +++ b/src/cephadm/cephadmlib/container_engines.py @@ -248,3 +248,57 @@ def parsed_container_mem_usage( ctx, container_path=container_path, verbosity=verbosity ) return _parse_mem_usage(code, out) + + +def _container_cpu_perc( + ctx: CephadmContext, + *, + container_path: str = '', + verbosity: CallVerbosity = CallVerbosity.QUIET, +) -> Tuple[str, str, int]: + container_path = container_path or ctx.container_engine.path + out, err, code = call( + ctx, + [ + container_path, + 'stats', + '--format', + '{{.ID}},{{.CPUPerc}}', + '--no-stream', + ], + verbosity=CallVerbosity.QUIET, + ) + return out, err, code + + +def _parse_cpu_perc(code: int, out: str) -> Tuple[int, Dict[str, str]]: + seen_cpuperc = {} + seen_cpuperc_cid_len = 0 + if not code: + for line in out.splitlines(): + (cid, cpuperc) = line.split(',') + try: + seen_cpuperc[cid] = cpuperc + if not seen_cpuperc_cid_len: + seen_cpuperc_cid_len = len(cid) + except ValueError: + logger.info( + 'unable to parse cpu percentage line\n>{}'.format(line) + ) + pass + return seen_cpuperc_cid_len, seen_cpuperc + + +def parsed_container_cpu_perc( + ctx: CephadmContext, + *, + container_path: str = '', + verbosity: CallVerbosity = CallVerbosity.QUIET, +) -> Tuple[int, Dict[str, str]]: + """Return cpu percentage used values parsed from the container engine's + container status. + """ + out, _, code = _container_cpu_perc( + ctx, container_path=container_path, verbosity=verbosity + ) + return _parse_cpu_perc(code, out)