]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mon: create default tabular format for the output of `mgr module ls`
authorLaura Flores <lflores@redhat.com>
Thu, 14 Oct 2021 15:59:34 +0000 (15:59 +0000)
committerLaura Flores <lflores@redhat.com>
Thu, 14 Oct 2021 23:32:18 +0000 (23:32 +0000)
Signed-off-by: Laura Flores <lflores@redhat.com>
src/mon/MgrMonitor.cc

index b2dae3ce8412aef1f803de3ad0735c7dff33f1c6..c9458aab4eeb98a5986fb47cb530a687ca1d2bd1 100644 (file)
 #include "ConfigMonitor.h"
 #include "HealthMonitor.h"
 
+#include "common/TextTable.h"
+#include "common/cmdparse.h"
+#include "include/stringify.h"
+
 #include "MgrMonitor.h"
 
 #define MGR_METADATA_PREFIX "mgr_metadata"
@@ -946,10 +950,8 @@ bool MgrMonitor::preprocess_command(MonOpRequestRef op)
     return true;
   }
 
-  string format;
-  cmd_getval(cmdmap, "format", format);
-  boost::scoped_ptr<Formatter> f(Formatter::create(format, "json-pretty",
-                                                  "json-pretty"));
+  string format = cmd_getval_or<string>(cmdmap, "format", "plain");
+  boost::scoped_ptr<Formatter> f(Formatter::create(format));
 
   string prefix;
   cmd_getval(cmdmap, "prefix", prefix);
@@ -957,6 +959,9 @@ bool MgrMonitor::preprocess_command(MonOpRequestRef op)
   int r = 0;
 
   if (prefix == "mgr stat") {
+    if (!f) {
+      f.reset(Formatter::create(format, "json-pretty", "json-pretty"));
+    }
     f->open_object_section("stat");
     f->dump_unsigned("epoch", map.get_epoch());
     f->dump_bool("available", map.get_available());
@@ -965,6 +970,9 @@ bool MgrMonitor::preprocess_command(MonOpRequestRef op)
     f->close_section();
     f->flush(rdata);
   } else if (prefix == "mgr dump") {
+    if (!f) {
+      f.reset(Formatter::create(format, "json-pretty", "json-pretty"));
+    }
     int64_t epoch = cmd_getval_or<int64_t>(cmdmap, "epoch", map.get_epoch());
     if (epoch == (int64_t)map.get_epoch()) {
       f->dump_object("mgrmap", map);
@@ -985,42 +993,75 @@ bool MgrMonitor::preprocess_command(MonOpRequestRef op)
   } else if (prefix == "mgr module ls") {
     string detail;
     cmd_getval(cmdmap, "detail", detail);
+    if (f || (detail == "detail")) {
+      if (!f) {
+        f.reset(Formatter::create(format, "json-pretty", "json-pretty"));
+      }
+      f->open_object_section("modules");
+      {
+        f->open_array_section("always_on_modules");
+        for (auto& p : map.get_always_on_modules()) {
+          f->dump_string("module", p);
+        }
+        f->close_section();
+        f->open_array_section("enabled_modules");
+        for (auto& p : map.modules) {
+          if (map.get_always_on_modules().count(p) > 0)
+            continue;
+          // We only show the name for enabled modules.  The any errors
+          // etc will show up as a health checks.
+          f->dump_string("module", p);
+        }
+        f->close_section();
+        f->open_array_section("disabled_modules");
+        for (auto& p : map.available_modules) {
+          if (map.modules.count(p.name) == 0 &&
+            map.get_always_on_modules().count(p.name) == 0) {
+            if (detail == "detail") {
+              // For disabled modules, we show the full info if the detail
+              // parameter is enabled, to give a hint about whether enabling it will work
+              p.dump(f.get());
+            } else {
+              // Otherwise, we give a shortened summary by default
+              f->dump_string("module", p.name);
+            }
+          }
+        }
+        f->close_section();
+      }
+      f->close_section();
+      f->flush(rdata);
+    } else {
+      TextTable tbl;
+      tbl.define_column("MODULE", TextTable::LEFT, TextTable::LEFT);
+      tbl.define_column("      ", TextTable::LEFT, TextTable::LEFT);
 
-    f->open_object_section("modules");
-    {
-      f->open_array_section("always_on_modules");
       for (auto& p : map.get_always_on_modules()) {
-        f->dump_string("module", p);
+        tbl << p;
+        tbl << "on (always on)";
+        tbl << TextTable::endrow;
       }
-      f->close_section();
-      f->open_array_section("enabled_modules");
       for (auto& p : map.modules) {
         if (map.get_always_on_modules().count(p) > 0)
           continue;
-        // We only show the name for enabled modules.  The any errors
-        // etc will show up as a health checks.
-        f->dump_string("module", p);
+        tbl << p;
+        tbl << "on";
+        tbl << TextTable::endrow;
       }
-      f->close_section();
-      f->open_array_section("disabled_modules");
       for (auto& p : map.available_modules) {
         if (map.modules.count(p.name) == 0 &&
             map.get_always_on_modules().count(p.name) == 0) {
-         if (detail == "detail") {
-           // For disabled modules, we show the full info if the detail
-           // parameter is enabled, to give a hint about whether enabling it will work
-           p.dump(f.get());
-         } else {
-           // Otherwise, we give a shortened summary by default
-           f->dump_string("module", p.name);
-         }
+          tbl << p.name;
+          tbl << "-";
+          tbl << TextTable::endrow;
         }
       }
-      f->close_section();
+      rdata.append(stringify(tbl));
     }
-    f->close_section();
-    f->flush(rdata);
   } else if (prefix == "mgr services") {
+    if (!f) {
+      f.reset(Formatter::create(format, "json-pretty", "json-pretty"));
+    }
     f->open_object_section("services");
     for (const auto &i : map.services) {
       f->dump_string(i.first.c_str(), i.second);
@@ -1028,6 +1069,9 @@ bool MgrMonitor::preprocess_command(MonOpRequestRef op)
     f->close_section();
     f->flush(rdata);
   } else if (prefix == "mgr metadata") {
+    if (!f) {
+      f.reset(Formatter::create(format, "json-pretty", "json-pretty"));
+    }
     string name;
     cmd_getval(cmdmap, "who", name);
     if (name.size() > 0 && !map.have_name(name)) {
@@ -1035,9 +1079,6 @@ bool MgrMonitor::preprocess_command(MonOpRequestRef op)
       r = -ENOENT;
       goto reply;
     }
-    string format;
-    cmd_getval(cmdmap, "format", format);
-    boost::scoped_ptr<Formatter> f(Formatter::create(format, "json-pretty", "json-pretty"));
     if (name.size()) {
       f->open_object_section("mgr_metadata");
       f->dump_string("name", name);
@@ -1066,10 +1107,16 @@ bool MgrMonitor::preprocess_command(MonOpRequestRef op)
     }
     f->flush(rdata);
   } else if (prefix == "mgr versions") {
+    if (!f) {
+      f.reset(Formatter::create(format, "json-pretty", "json-pretty"));
+    }
     count_metadata("ceph_version", f.get());
     f->flush(rdata);
     r = 0;
   } else if (prefix == "mgr count-metadata") {
+    if (!f) {
+      f.reset(Formatter::create(format, "json-pretty", "json-pretty"));
+    }
     string field;
     cmd_getval(cmdmap, "property", field);
     count_metadata(field, f.get());