From: Joao Eduardo Luis Date: Wed, 2 Jan 2013 16:07:33 +0000 (+0000) Subject: mon: Monitor: unify 'ceph health' and 'ceph status'; add json output X-Git-Tag: v0.57~223^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=58e03ecb89ba4ea4294e24925557fa6453bedf98;p=ceph.git mon: Monitor: unify 'ceph health' and 'ceph status'; add json output Signed-off-by: Joao Eduardo Luis --- diff --git a/src/mon/Monitor.cc b/src/mon/Monitor.cc index bc2004edf9f4..3b3b7fd43663 100644 --- a/src/mon/Monitor.cc +++ b/src/mon/Monitor.cc @@ -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 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::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; diff --git a/src/mon/Monitor.h b/src/mon/Monitor.h index e4eeb4a75c90..dc4e9849a974 100644 --- a/src/mon/Monitor.h +++ b/src/mon/Monitor.h @@ -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);