]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
admin_socket: json output, always
authorSage Weil <sage@inktank.com>
Tue, 24 Jul 2012 21:53:06 +0000 (14:53 -0700)
committerSage Weil <sage@inktank.com>
Wed, 25 Jul 2012 00:23:07 +0000 (17:23 -0700)
If the perfcounters stuff were refactored to use the Formatter, we could
put the JSONFormatter in the admin_socket code and make this a bit less
annoying.  Later.

Signed-off-by: Sage Weil <sage@inktank.com>
src/common/admin_socket.cc
src/common/ceph_context.cc
src/common/config.cc

index 283eec7b3086394a81cbadf4766d98c1c39ccc23..93830045f87f759777f9d600e5a2a60cd3d76caf 100644 (file)
@@ -21,6 +21,7 @@
 #include "common/pipe.h"
 #include "common/safe_io.h"
 #include "common/version.h"
+#include "common/Formatter.h"
 
 #include <errno.h>
 #include <fcntl.h>
@@ -395,12 +396,20 @@ int AdminSocket::unregister_command(std::string command)
 class VersionHook : public AdminSocketHook {
 public:
   virtual bool call(std::string command, std::string args, bufferlist& out) {
-    if (command == "version")
-      out.append(ceph_version_to_str());
-    else if (command == "git_version")
-      out.append(git_version_to_str());
-    else if (command == "0")
+    if (command == "0") {
       out.append(CEPH_ADMIN_SOCK_VERSION);
+    } else {
+      JSONFormatter jf;
+      jf.open_object_section("version");
+      if (command == "version")
+       jf.dump_string("version", ceph_version_to_str());
+      else if (command == "git_version")
+       jf.dump_string("git_version", git_version_to_str());
+      ostringstream ss;
+      jf.close_section();
+      jf.flush(ss);
+      out.append(ss.str());
+    }
     return true;
   }
 };
@@ -410,25 +419,17 @@ class HelpHook : public AdminSocketHook {
 public:
   HelpHook(AdminSocket *as) : m_as(as) {}
   bool call(string command, string args, bufferlist& out) {
-    unsigned max = 0;
-    for (map<string,string>::iterator p = m_as->m_help.begin();
-        p != m_as->m_help.end();
-        ++p) {
-      if (p->first.length() > max)
-       max = p->first.length();
-    }
-    max += 1;
-    char spaces[max];
-    for (unsigned i=0; i<max; ++i)
-      spaces[i] = ' ';
+    JSONFormatter jf(true);
+    jf.open_object_section("help");
     for (map<string,string>::iterator p = m_as->m_help.begin();
         p != m_as->m_help.end();
         ++p) {
-      out.append(p->first);
-      out.append(spaces, max - p->first.length());
-      out.append(p->second);
-      out.append("\n");
+      jf.dump_string(p->first.c_str(), p->second);
     }
+    jf.close_section();
+    ostringstream ss;
+    jf.flush(ss);
+    out.append(ss.str());
     return true;
   }
 };
index 838f9d6c935782977a0d0d0fe52291695d8ed3d7..97005ac77cfe58d2ef0776d6970bcf796130f1fa 100644 (file)
@@ -172,43 +172,47 @@ void CephContext::do_command(std::string command, std::string args, bufferlist *
           command == "perf schema") {
     _perf_counters_collection->write_json_to_buf(*out, true);
   }
-  else if (command == "config show") {
+  else {
     JSONFormatter jf(true);
-    _conf->show_config(&jf);
-    ostringstream ss;
-    jf.flush(ss);
-    out->append(ss.str());
-  }
-  else if (command == "config set") {
-    std::string var = args;
-    size_t pos = var.find(' ');
-    if (pos == string::npos) {
-      out->append("set_config syntax is 'set_config <var> <value>'");
-    } else {
-      std::string val = var.substr(pos+1);
-      var.resize(pos);
-      std::vector<const char*> args;
-      int r = _conf->set_val(var.c_str(), val.c_str());
-      ostringstream ss;
-      if (r < 0) {
-       ss << "error setting '" << var << "' to '" << val << "': " << cpp_strerror(r);
+    jf.open_object_section(command.c_str());
+    if (command == "config show") {
+      _conf->show_config(&jf);
+    }
+    else if (command == "config set") {
+      std::string var = args;
+      size_t pos = var.find(' ');
+      if (pos == string::npos) {
+       jf.dump_string("error", "set_config syntax is 'set_config <var> <value>'");
       } else {
-       _conf->apply_changes(&ss);
+       std::string val = var.substr(pos+1);
+       var.resize(pos);
+       std::vector<const char*> args;
+       int r = _conf->set_val(var.c_str(), val.c_str());
+       if (r < 0) {
+         jf.dump_stream("error") << "error setting '" << var << "' to '" << val << "': " << cpp_strerror(r);
+       } else {
+         ostringstream ss;
+         _conf->apply_changes(&ss);
+         jf.dump_string("success", ss.str());
+       }
       }
-      out->append(ss.str());
     }
-  }
-  else if (command == "log flush") {
-    _log->flush();
-  }
-  else if (command == "log dump") {
-    _log->dump_recent();
-  }
-  else if (command == "log reopen") {
-    _log->reopen_log_file();
-  }
-  else {
-    assert(0 == "registered under wrong command?");    
+    else if (command == "log flush") {
+      _log->flush();
+    }
+    else if (command == "log dump") {
+      _log->dump_recent();
+    }
+    else if (command == "log reopen") {
+      _log->reopen_log_file();
+    }
+    else {
+      assert(0 == "registered under wrong command?");    
+    }
+    ostringstream ss;
+    jf.close_section();
+    jf.flush(ss);
+    out->append(ss.str());
   }
   lgeneric_dout(this, 1) << "do_command '" << command << "' '" << args << "' result is " << out->length() << " bytes" << dendl;
 };
index 21ff86063d04c13c619306b4d264730449dff098..c66bc5102e7e6c16343e7f13e24f981e2283f414 100644 (file)
@@ -300,7 +300,6 @@ void md_config_t::_show_config(std::ostream *out, Formatter *f)
     *out << "cluster = " << cluster << std::endl;
   }
   if (f) {
-    f->open_object_section("config");
     f->dump_string("name", stringify(name));
     f->dump_string("cluster", cluster);
   }
@@ -325,8 +324,6 @@ void md_config_t::_show_config(std::ostream *out, Formatter *f)
     if (f)
       f->dump_string(opt->name, buf);
   }
-  if (f)
-    f->close_section();
 }
 
 int md_config_t::parse_argv(std::vector<const char*>& args)