From: Dan Mick Date: Thu, 11 Jul 2013 00:15:35 +0000 (-0700) Subject: mon: Monitor: support multiple formatters on some status functions X-Git-Tag: v0.67-rc1~99 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=300f42b876295dbbaa3ecd906045e14e11958045;p=ceph.git mon: Monitor: support multiple formatters on some status functions Commands such as 'mon_status', 'quorum_status', 'sync_status' and 'sync_force' didn't support other formatter besides json. Regardless of '--format=foo' being specified, they would always output in json. This commit changes that behavior, allowing a format to be passed. These functions do not output in plain-text however. Plain-text will default to 'json' -- the reason: the information they provide are better outputted in a structured fashion, and I was too lazy to come up with a plain-text version that could be at least as good. Signed-off-by: Joao Eduardo Luis Reviewed-by: Sage Weil --- diff --git a/src/ceph_mon.cc b/src/ceph_mon.cc index b62443aa0354..7705bdbfb32d 100644 --- a/src/ceph_mon.cc +++ b/src/ceph_mon.cc @@ -525,7 +525,7 @@ int main(int argc, const char **argv) if (force_sync) { derr << "flagging a forced sync ..." << dendl; - mon->sync_force(cerr); + mon->sync_force(NULL, cerr); } err = mon->preinit(); diff --git a/src/mon/Monitor.cc b/src/mon/Monitor.cc index 9f3f185299e9..aafd2883e0a3 100644 --- a/src/mon/Monitor.cc +++ b/src/mon/Monitor.cc @@ -235,10 +235,16 @@ public: void Monitor::do_admin_command(string command, string args, ostream& ss) { Mutex::Locker l(lock); + + map cmdmap; + string format; + cmd_getval(g_ceph_context, cmdmap, "format", format, string("plain")); + boost::scoped_ptr f(new_formatter(format)); + if (command == "mon_status") - _mon_status(ss); + _mon_status(f.get(), ss); else if (command == "quorum_status") - _quorum_status(ss); + _quorum_status(f.get(), ss); else if (command == "sync_force") { if (args != "--yes-i-really-mean-it") { ss << "are you SURE? this will mean the monitor store will be erased " @@ -246,7 +252,7 @@ void Monitor::do_admin_command(string command, string args, ostream& ss) "'--yes-i-really-mean-it' if you really do."; return; } - sync_force(ss); + sync_force(f.get(), ss); } else if (command.find("add_bootstrap_peer_hint") == 0) _add_bootstrap_peer_hint(command, args, ss); else @@ -1529,97 +1535,125 @@ bool Monitor::_allowed_command(MonSession *s, map& cmd) return retval; } -void Monitor::sync_force(ostream& ss) +void Monitor::sync_force(Formatter *f, ostream& ss) { + bool free_formatter = false; + + if (!f) { + // louzy/lazy hack: default to json if no formatter has been defined + f = new JSONFormatter(); + free_formatter = true; + } + MonitorDBStore::Transaction tx; tx.put("mon_sync", "force_sync", 1); store->apply_transaction(tx); - JSONFormatter jf(true); - jf.open_object_section("sync_force"); - jf.dump_int("ret", 0); - jf.dump_stream("msg") << "forcing store sync the next time the monitor starts"; - jf.close_section(); - jf.flush(ss); + f->open_object_section("sync_force"); + f->dump_int("ret", 0); + f->dump_stream("msg") << "forcing store sync the next time the monitor starts"; + f->close_section(); // sync_force + f->flush(ss); + if (free_formatter) + delete f; } -void Monitor::_quorum_status(ostream& ss) +void Monitor::_quorum_status(Formatter *f, ostream& ss) { - JSONFormatter jf(true); - jf.open_object_section("quorum_status"); - jf.dump_int("election_epoch", get_epoch()); - - jf.open_array_section("quorum"); + bool free_formatter = false; + + if (!f) { + // louzy/lazy hack: default to json if no formatter has been defined + f = new JSONFormatter(); + free_formatter = true; + } + f->open_object_section("quorum_status"); + f->dump_int("election_epoch", get_epoch()); + + f->open_array_section("quorum"); for (set::iterator p = quorum.begin(); p != quorum.end(); ++p) - jf.dump_int("mon", *p); - jf.close_section(); + f->dump_int("mon", *p); + f->close_section(); // quorum - jf.open_object_section("monmap"); - monmap->dump(&jf); - jf.close_section(); + f->open_object_section("monmap"); + monmap->dump(f); + f->close_section(); // monmap - jf.close_section(); - jf.flush(ss); + f->close_section(); // quorum_status + f->flush(ss); + if (free_formatter) + delete f; } -void Monitor::_mon_status(ostream& ss) +void Monitor::_mon_status(Formatter *f, ostream& ss) { - JSONFormatter jf(true); - jf.open_object_section("mon_status"); - jf.dump_string("name", name); - jf.dump_int("rank", rank); - jf.dump_string("state", get_state_name()); - jf.dump_int("election_epoch", get_epoch()); + bool free_formatter = false; - jf.open_array_section("quorum"); - for (set::iterator p = quorum.begin(); p != quorum.end(); ++p) - jf.dump_int("mon", *p); - jf.close_section(); + if (!f) { + // louzy/lazy hack: default to json if no formatter has been defined + f = new JSONFormatter(); + free_formatter = true; + } + + f->open_object_section("mon_status"); + f->dump_string("name", name); + f->dump_int("rank", rank); + f->dump_string("state", get_state_name()); + f->dump_int("election_epoch", get_epoch()); + + f->open_array_section("quorum"); + for (set::iterator p = quorum.begin(); p != quorum.end(); ++p) { + f->dump_int("mon", *p); + } + + f->close_section(); // quorum - jf.open_array_section("outside_quorum"); + f->open_array_section("outside_quorum"); for (set::iterator p = outside_quorum.begin(); p != outside_quorum.end(); ++p) - jf.dump_string("mon", *p); - jf.close_section(); + f->dump_string("mon", *p); + f->close_section(); // outside_quorum - jf.open_array_section("extra_probe_peers"); + f->open_array_section("extra_probe_peers"); for (set::iterator p = extra_probe_peers.begin(); p != extra_probe_peers.end(); ++p) - jf.dump_stream("peer") << *p; - jf.close_section(); + f->dump_stream("peer") << *p; + f->close_section(); // extra_probe_peers - jf.open_array_section("sync_provider"); + f->open_array_section("sync_provider"); for (map::const_iterator p = sync_providers.begin(); p != sync_providers.end(); ++p) { - jf.dump_unsigned("cookie", p->second.cookie); - jf.dump_stream("entity") << p->second.entity; - jf.dump_stream("timeout") << p->second.timeout; - jf.dump_unsigned("last_committed", p->second.last_committed); - jf.dump_stream("last_key") << p->second.last_key; + f->dump_unsigned("cookie", p->second.cookie); + f->dump_stream("entity") << p->second.entity; + f->dump_stream("timeout") << p->second.timeout; + f->dump_unsigned("last_committed", p->second.last_committed); + f->dump_stream("last_key") << p->second.last_key; } - jf.close_section(); + f->close_section(); if (is_synchronizing()) { - jf.open_object_section("sync"); - jf.dump_stream("sync_provider") << sync_provider; - jf.dump_unsigned("sync_cookie", sync_cookie); - jf.dump_unsigned("sync_start_version", sync_start_version); - jf.close_section(); + f->open_object_section("sync"); + f->dump_stream("sync_provider") << sync_provider; + f->dump_unsigned("sync_cookie", sync_cookie); + f->dump_unsigned("sync_start_version", sync_start_version); + f->close_section(); } if (g_conf->mon_sync_provider_kill_at > 0) - jf.dump_int("provider_kill_at", g_conf->mon_sync_provider_kill_at); + f->dump_int("provider_kill_at", g_conf->mon_sync_provider_kill_at); if (g_conf->mon_sync_requester_kill_at > 0) - jf.dump_int("requester_kill_at", g_conf->mon_sync_requester_kill_at); + f->dump_int("requester_kill_at", g_conf->mon_sync_requester_kill_at); - jf.open_object_section("monmap"); - monmap->dump(&jf); - jf.close_section(); + f->open_object_section("monmap"); + monmap->dump(f); + f->close_section(); - jf.close_section(); - - jf.flush(ss); + f->close_section(); // mon_status + + f->flush(ss); + if (free_formatter) + delete f; } void Monitor::get_health(string& status, bufferlist *detailbl, Formatter *f) @@ -2088,7 +2122,7 @@ void Monitor::handle_command(MMonCommand *m) waitfor_quorum.push_back(new C_RetryMessage(this, m)); return; } - _quorum_status(ds); + _quorum_status(f.get(), ds); rdata.append(ds); rs = ""; r = 0; @@ -2098,7 +2132,7 @@ void Monitor::handle_command(MMonCommand *m) rs = "access denied"; goto out; } - _mon_status(ds); + _mon_status(f.get(), ds); rdata.append(ds); rs = ""; r = 0; @@ -2114,7 +2148,7 @@ void Monitor::handle_command(MMonCommand *m) "--i-know-what-i-am-doing' if you really do."; goto out; } - sync_force(ds); + sync_force(f.get(), ds); rs = ds.str(); r = 0; } else if (prefix == "heap") { diff --git a/src/mon/Monitor.h b/src/mon/Monitor.h index 863f4e5de38c..4c27024dd203 100644 --- a/src/mon/Monitor.h +++ b/src/mon/Monitor.h @@ -332,7 +332,7 @@ public: /** * force a sync on next mon restart */ - void sync_force(ostream& ss); + void sync_force(Formatter *f, ostream& ss); private: /** @@ -570,8 +570,8 @@ public: void handle_subscribe(MMonSubscribe *m); void handle_mon_get_map(MMonGetMap *m); bool _allowed_command(MonSession *s, map& cmd); - void _mon_status(ostream& ss); - void _quorum_status(ostream& ss); + void _mon_status(Formatter *f, ostream& ss); + void _quorum_status(Formatter *f, ostream& ss); void _add_bootstrap_peer_hint(string cmd, string args, ostream& ss); void handle_command(class MMonCommand *m); void handle_route(MRoute *m);