From c691f2f8763a9db081a0b61294c5eedf4e9c2cee Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Fri, 12 Jul 2019 16:34:37 -0500 Subject: [PATCH] mgr/crash: make 'crash ls' a nice table with a NEW column Signed-off-by: Sage Weil --- qa/workunits/rados/test_crash.sh | 2 +- src/pybind/mgr/crash/module.py | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/qa/workunits/rados/test_crash.sh b/qa/workunits/rados/test_crash.sh index 6e7aaaaba6348..04597c1953753 100755 --- a/qa/workunits/rados/test_crash.sh +++ b/qa/workunits/rados/test_crash.sh @@ -29,5 +29,5 @@ sudo systemctl restart ceph-crash sleep 30 # must be 3 crashdumps registered and moved to crash/posted -[ $(ceph crash ls | wc -l) = 3 ] || exit 1 +[ $(ceph crash ls | wc -l) = 4 ] || exit 1 # 4 here bc of the table header [ $(sudo find /var/lib/ceph/crash/posted/ -name meta | wc -l) = 3 ] || exit 1 diff --git a/src/pybind/mgr/crash/module.py b/src/pybind/mgr/crash/module.py index df2771bf156ac..df298fbc50626 100644 --- a/src/pybind/mgr/crash/module.py +++ b/src/pybind/mgr/crash/module.py @@ -4,6 +4,7 @@ import errno import json import six from collections import defaultdict +from prettytable import PrettyTable DATEFMT = '%Y-%m-%dT%H:%M:%S.%f' @@ -83,12 +84,23 @@ class Module(MgrModule): return 0, '', '' def do_ls(self, cmd, inbuf): - keys = [] + r = [] for k, meta in self.timestamp_filter(lambda ts: True): - entity_name = meta.get('entity_name', 'unknown') - keys.append("%s %s" % (k.replace('crash/', ''), entity_name)) - keys.sort() - return 0, '\n'.join(keys), '' + r.append(meta) + if cmd.get('format') == 'json' or cmd.get('format') == 'json-pretty': + return 0, json.dumps(r, indent=4), '' + else: + table = PrettyTable(['ID', 'ENTITY', 'NEW'], + border=False) + table.left_padding_width = 0 + table.right_padding_width = 1 + table.align['ID'] = 'l' + table.align['ENTITY'] = 'l' + for c in r: + table.add_row([c.get('crash_id'), + c.get('entity_name','unknown'), + '' if 'archived' in c else '*']) + return 0, table.get_string(), '' def do_rm(self, cmd, inbuf): crashid = cmd['id'] -- 2.39.5