]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cephadm: include memory_usage in 'ls' output
authorSage Weil <sage@newdream.net>
Fri, 5 Feb 2021 21:45:26 +0000 (15:45 -0600)
committerSebastian Wagner <sebastian.wagner@suse.com>
Tue, 9 Mar 2021 14:29:32 +0000 (15:29 +0100)
Report of container memory usage

Signed-off-by: Sage Weil <sage@newdream.net>
(cherry picked from commit 7d78d21f7c0afc4d21cdbd28125960f67b2b30c8)

src/cephadm/cephadm

index 6e99c0df448e5ec4e6172475839446132a987bb0..7fc0c49cc6fb7ff3340e89ed1ed78ceaac996961 100755 (executable)
@@ -4431,6 +4431,27 @@ def command_ls(ctx):
     print(json.dumps(ls, indent=4))
 
 
+def with_units_to_int(v: str) -> int:
+    if v.endswith('iB'):
+        v = v[:-2]
+    elif v.endswith('B'):
+        v = v[:-1]
+    mult = 1
+    if v[-1].upper() == 'K':
+        mult = 1024
+        v = v[:-1]
+    elif v[-1].upper() == 'M':
+        mult = 1024 * 1024
+        v = v[:-1]
+    elif v[-1].upper() == 'G':
+        mult = 1024 * 1024 * 1024
+        v = v[:-1]
+    elif v[-1].upper() == 'T':
+        mult = 1024 * 1024 * 1024 * 1024
+        v = v[:-1]
+    return int(float(v) * mult)
+
+
 def list_daemons(ctx, detail=True, legacy_dir=None):
     # type: (CephadmContext, bool, Optional[str]) -> List[Dict[str, str]]
     host_version: Optional[str] = None
@@ -4447,6 +4468,22 @@ def list_daemons(ctx, detail=True, legacy_dir=None):
     # keep track of image digests
     seen_digests = {}   # type: Dict[str, List[str]]
 
+    # keep track of memory usage we've seen
+    seen_memusage = {}  # type: Dict[str, int]
+    out, err, code = call(
+        ctx,
+        [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)
+
     # /var/lib/ceph
     if os.path.exists(data_dir):
         for i in os.listdir(data_dir):
@@ -4604,6 +4641,8 @@ def list_daemons(ctx, detail=True, legacy_dir=None):
                         val['container_image_name'] = image_name
                         val['container_image_id'] = image_id
                         val['container_image_digests'] = image_digests
+                        if container_id:
+                            val['memory_usage'] = seen_memusage.get(container_id[0:seen_memusage_cid_len])
                         val['version'] = version
                         val['started'] = start_stamp
                         val['created'] = get_file_timestamp(