From: Nitzan Mordechai Date: Sun, 30 Jan 2022 13:52:23 +0000 (+0200) Subject: pybind/mgr: fix "osd status" command output with format json X-Git-Tag: v18.0.0~279^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=5f6d412fd051f771c1eb27b78e1765ec56e79a74;p=ceph.git pybind/mgr: fix "osd status" command output with format json currently command "ceph -f json osd status" return table view instead of json view. the fix will fix that and will also fix the json-pretty format output. Signed-off-by: Nitzan Mordechai --- diff --git a/src/pybind/mgr/status/module.py b/src/pybind/mgr/status/module.py index 879ab29cf073..72487eb32728 100644 --- a/src/pybind/mgr/status/module.py +++ b/src/pybind/mgr/status/module.py @@ -5,7 +5,7 @@ High level status display commands from collections import defaultdict from prettytable import PrettyTable -from typing import Dict, List, Optional, Tuple, Union +from typing import Any, Dict, List, Optional, Tuple, Union import errno import fnmatch import mgr_util @@ -279,10 +279,14 @@ class Module(MgrModule): return HandleCommandResult(stdout=output) @CLIReadCommand("osd status") - def handle_osd_status(self, bucket: Optional[str] = None) -> Tuple[int, str, str]: + def handle_osd_status(self, bucket: Optional[str] = None, format: str = 'plain') -> Tuple[int, str, str]: """ Show the status of OSDs within a bucket, or all """ + json_output: Dict[str, List[Any]] = \ + dict(OSDs=[]) + output_format = format + osd_table = PrettyTable(['ID', 'HOST', 'USED', 'AVAIL', 'WR OPS', 'WR DATA', 'RD OPS', 'RD DATA', 'STATE'], border=False) @@ -349,5 +353,22 @@ class Module(MgrModule): mgr_util.format_bytes(rd_byte_rate, 5), ','.join(osd['state']), ]) + if output_format in ('json', 'json-pretty'): + json_output['OSDs'].append({ + 'id': osd_id, + 'host name': hostname, + 'kb used' : kb_used, + 'kb available':kb_avail, + 'write ops rate': wr_ops_rate, + 'write byte rate': wr_byte_rate, + 'read ops rate': rd_ops_rate, + 'read byte rate': rd_byte_rate, + 'state': osd['state'] + }) - return 0, osd_table.get_string(), "" + if output_format == "json": + return 0, json.dumps(json_output, sort_keys=True) , "" + elif output_format == "json-pretty": + return 0, json.dumps(json_output, sort_keys=True,indent=4,separators=(',', ': ')) , "" + else: + return 0, osd_table.get_string(), ""