]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cephadm: add parsed_container_cpu_perc to container_engines
authorJohn Mulligan <jmulligan@redhat.com>
Thu, 30 Jan 2025 00:25:36 +0000 (19:25 -0500)
committerJohn Mulligan <jmulligan@redhat.com>
Thu, 30 Jan 2025 00:48:18 +0000 (19:48 -0500)
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 <jmulligan@redhat.com>
src/cephadm/cephadmlib/container_engines.py

index 77e3714c18acc7ff52fe52f8b6cb141c19e1a271..af094ecbd54c22cecdcf7f3d53cf4674ef58e494 100644 (file)
@@ -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)