]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mon: Monitor: unify 'ceph health' and 'ceph status'; add json output
authorJoao Eduardo Luis <joao.luis@inktank.com>
Wed, 2 Jan 2013 16:07:33 +0000 (16:07 +0000)
committerJoao Eduardo Luis <joao.luis@inktank.com>
Fri, 11 Jan 2013 00:44:21 +0000 (00:44 +0000)
Signed-off-by: Joao Eduardo Luis <joao.luis@inktank.com>
src/mon/Monitor.cc
src/mon/Monitor.h

index bc2004edf9f4e6c865e121201ef4dfef53b2eab4..3b3b7fd43663af826b484169953a70da7847c752 100644 (file)
@@ -1423,6 +1423,34 @@ void Monitor::get_health(string& status, bufferlist *detailbl, Formatter *f)
     f->close_section();
 }
 
+void Monitor::get_status(stringstream &ss, Formatter *f)
+{
+  if (f)
+    f->open_object_section("status");
+
+  // reply with the status for all the components
+  string health;
+  get_health(health, NULL, f);
+
+  if (f) {
+    f->dump_stream("monmap") << *monmap;
+    f->dump_stream("election_epoch") << get_epoch();
+    f->dump_stream("quorum") << get_quorum();
+    f->dump_stream("quorum_names") << get_quorum_names();
+    f->dump_stream("osdmap") << osdmon()->osdmap;
+    f->dump_stream("pgmap") << pgmon()->pg_map;
+    f->dump_stream("mdsmap") << mdsmon()->mdsmap;
+    f->close_section();
+  } else {
+    ss << "   health " << health << "\n";
+    ss << "   monmap " << *monmap << ", election epoch " << get_epoch()
+      << ", quorum " << get_quorum() << " " << get_quorum_names() << "\n";
+    ss << "   osdmap " << osdmon()->osdmap << "\n";
+    ss << "    pgmap " << pgmon()->pg_map << "\n";
+    ss << "   mdsmap " << mdsmon()->mdsmap << "\n";
+  }
+}
+
 void Monitor::handle_command(MMonCommand *m)
 {
   if (m->fsid != monmap->fsid) {
@@ -1528,22 +1556,63 @@ void Monitor::handle_command(MMonCommand *m)
       rs = "must supply options to be parsed in a single string";
       r = -EINVAL;
     }
-  } else if (m->cmd[0] == "status") {
+  } else if ((m->cmd[0] == "status") || (m->cmd[0] == "health")) {
     if (!access_r) {
       r = -EACCES;
       rs = "access denied";
       goto out;
     }
-    // reply with the status for all the components
-    string health;
-    get_health(health, NULL, NULL);
+
+    vector<const char *> args;
+    for (unsigned int i = 0; i < m->cmd.size(); ++i)
+      args.push_back(m->cmd[i].c_str());
+
+    string format = "plain";
+    JSONFormatter *jf = NULL;
+    for (vector<const char*>::iterator i = args.begin(); i != args.end();) {
+      string val;
+      if (ceph_argparse_witharg(args, i, &val,
+            "-f", "--format", (char*)NULL)) {
+        format = val;
+      } else {
+        ++i;
+      }
+    }
+
+    if (format != "plain") {
+      if (format == "json") {
+        jf = new JSONFormatter(true);
+      } else {
+        r = -EINVAL;
+        stringstream err_ss;
+        err_ss << "unrecognized format '" << format
+          << "' (available: plain, json)";
+        rs = err_ss.str();
+        goto out;
+      }
+    }
+
     stringstream ss;
-    ss << "   health " << health << "\n";
-    ss << "   monmap " << *monmap << ", election epoch " << get_epoch() << ", quorum " << get_quorum()
-       << " " << get_quorum_names() << "\n";
-    ss << "   osdmap " << osdmon()->osdmap << "\n";
-    ss << "    pgmap " << pgmon()->pg_map << "\n";
-    ss << "   mdsmap " << mdsmon()->mdsmap << "\n";
+    if (string(args[0]) == "status") {
+      get_status(ss, jf);
+
+      if (jf) {
+        jf->flush(ss);
+        ss << '\n';
+      }
+    } else if (string(args[0]) == "health") {
+      string health_str;
+      get_health(health_str, (args.size() > 1) ? &rdata : NULL, jf);
+      if (jf) {
+        jf->flush(ss);
+        ss << '\n';
+      } else {
+        ss << health_str;
+      }
+    } else {
+      assert(0 == "We should never get here!");
+      return;
+    }
     rs = ss.str();
     r = 0;
   } else if (m->cmd[0] == "report") {
@@ -1615,14 +1684,6 @@ void Monitor::handle_command(MMonCommand *m)
     _mon_status(ss);
     rs = ss.str();
     r = 0;
-  } else if (m->cmd[0] == "health") {
-    if (!access_r) {
-      r = -EACCES;
-      rs = "access denied";
-      goto out;
-    }
-    get_health(rs, (m->cmd.size() > 1) ? &rdata : NULL, NULL);
-    r = 0;
   } else if (m->cmd[0] == "heap") {
     if (!access_all) {
       r = -EACCES;
index e4eeb4a75c903ec311f68dc702e67877458c5bdd..dc4e9849a974fc9e43f1ca82aa2c55ae38e6878d 100644 (file)
@@ -373,6 +373,7 @@ public:
    * @param detailbl optional bufferlist* to fill with a detailed report
    */
   void get_health(string& status, bufferlist *detailbl, Formatter *f);
+  void get_status(stringstream &ss, Formatter *f);
 
   void reply_command(MMonCommand *m, int rc, const string &rs, version_t version);
   void reply_command(MMonCommand *m, int rc, const string &rs, bufferlist& rdata, version_t version);