]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mon/LogMonitor: 'log last [num]' command
authorSage Weil <sage@redhat.com>
Mon, 5 Jun 2017 19:37:15 +0000 (15:37 -0400)
committerSage Weil <sage@redhat.com>
Mon, 5 Jun 2017 19:37:15 +0000 (15:37 -0400)
Default to 20 lines of log if not specified.

Signed-off-by: Sage Weil <sage@redhat.com>
src/mon/LogMonitor.cc
src/mon/MonCommands.h

index 5555a71bb1f0122b063adb5c917bbf4dd53cb042..cc5fd6bcb4fd22c67e56c6ee9786f6d69ea01293 100644 (file)
@@ -373,17 +373,64 @@ bool LogMonitor::should_propose(double& delay)
 bool LogMonitor::preprocess_command(MonOpRequestRef op)
 {
   op->mark_logmon_event("preprocess_command");
-  int r = -1;
+  MMonCommand *m = static_cast<MMonCommand*>(op->get_req());
+  int r = -EINVAL;
   bufferlist rdata;
   stringstream ss;
 
-  if (r != -1) {
-    string rs;
-    getline(ss, rs);
-    mon->reply_command(op, r, rs, rdata, get_last_committed());
+  map<string, cmd_vartype> cmdmap;
+  if (!cmdmap_from_json(m->cmd, &cmdmap, ss)) {
+    string rs = ss.str();
+    mon->reply_command(op, -EINVAL, rs, get_last_committed());
     return true;
-  } else
+  }
+  MonSession *session = m->get_session();
+  if (!session) {
+    mon->reply_command(op, -EACCES, "access denied", get_last_committed());
+    return true;
+  }
+
+  string prefix;
+  cmd_getval(g_ceph_context, cmdmap, "prefix", prefix);
+
+  string format;
+  cmd_getval(g_ceph_context, cmdmap, "format", format, string("plain"));
+  boost::scoped_ptr<Formatter> f(Formatter::create(format));
+
+  if (prefix == "log last") {
+    int64_t num = 20;
+    cmd_getval(g_ceph_context, cmdmap, "num", num);
+    if (f) {
+      f->open_array_section("tail");
+    }
+    auto p = summary.tail.end();
+    while (num > 0 && p != summary.tail.begin()) {
+      num--;
+      --p;
+    }
+    ostringstream ss;
+    for ( ; p != summary.tail.end(); ++p) {
+      if (f) {
+       f->dump_object("entry", *p);
+      } else {
+       ss << *p << "\n";
+      }
+    }
+    if (f) {
+      f->close_section();
+      f->flush(rdata);
+    } else {
+      rdata.append(ss.str());
+    }
+    r = 0;
+  } else {
     return false;
+  }
+
+  string rs;
+  getline(ss, rs);
+  mon->reply_command(op, r, rs, rdata, get_last_committed());
+  return true;
 }
 
 
index 31edf3e1657399c5bd654748ca318f53309300b7..205101abf181fa612236cfadc8d587c0f722fe55 100644 (file)
@@ -197,6 +197,9 @@ COMMAND_WITH_FLAG("scrub", "scrub the monitor stores", \
 COMMAND("fsid", "show cluster FSID/UUID", "mon", "r", "cli,rest")
 COMMAND("log name=logtext,type=CephString,n=N", \
        "log supplied text to the monitor log", "mon", "rw", "cli,rest")
+COMMAND("log last name=num,type=CephInt,range=1,req=false", \
+       "print last few lines of the cluster log", \
+       "mon", "rw", "cli,rest")
 COMMAND_WITH_FLAG("injectargs " \
             "name=injected_args,type=CephString,n=N",                  \
             "inject config arguments into monitor", "mon", "rw", "cli,rest",