#include "common/pipe.h"
#include "common/safe_io.h"
#include "common/version.h"
+#include "common/Formatter.h"
#include <errno.h>
#include <fcntl.h>
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;
}
};
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;
}
};
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;
};