From e320c242869d5ecf097d07e35037b50a81d1d05e Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Mon, 5 Jun 2017 15:37:15 -0400 Subject: [PATCH] mon/LogMonitor: 'log last [num]' command Default to 20 lines of log if not specified. Signed-off-by: Sage Weil --- src/mon/LogMonitor.cc | 59 ++++++++++++++++++++++++++++++++++++++----- src/mon/MonCommands.h | 3 +++ 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/mon/LogMonitor.cc b/src/mon/LogMonitor.cc index 5555a71bb1f01..cc5fd6bcb4fd2 100644 --- a/src/mon/LogMonitor.cc +++ b/src/mon/LogMonitor.cc @@ -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(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 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 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; } diff --git a/src/mon/MonCommands.h b/src/mon/MonCommands.h index 31edf3e165739..205101abf181f 100644 --- a/src/mon/MonCommands.h +++ b/src/mon/MonCommands.h @@ -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", -- 2.39.5