From 062681a72f3bb48b8bf0abb41439e94bdcfdcfa9 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Tue, 30 Jul 2019 11:23:57 -0500 Subject: [PATCH] mon: refactor/simplify health [detail] Get rid of single caller helpers. Instead, assimilate all the checks together at once, and have two separate blocks, one for formatted, and one for plaintext output. Much easier to follow! Signed-off-by: Sage Weil --- src/mon/Monitor.cc | 63 +++++++++++++++++++++++++++++++----------- src/mon/health_check.h | 46 ------------------------------ 2 files changed, 47 insertions(+), 62 deletions(-) diff --git a/src/mon/Monitor.cc b/src/mon/Monitor.cc index 713dbe04075..a4f06b76312 100644 --- a/src/mon/Monitor.cc +++ b/src/mon/Monitor.cc @@ -2784,39 +2784,70 @@ health_status_t Monitor::get_health_status( const char *sep1, const char *sep2) { + health_check_map_t all; + for (auto& svc : paxos_service) { + all.merge(svc->get_health_checks()); + } health_status_t r = HEALTH_OK; + for (auto& p : all.checks) { + if (r > p.second.severity) { + r = p.second.severity; + } + } if (f) { f->open_object_section("health"); + f->dump_stream("status") << r; f->open_object_section("checks"); - } - - string summary; - string *psummary = f ? nullptr : &summary; - for (auto& svc : paxos_service) { - r = std::min(r, svc->get_health_checks().dump_summary( - f, psummary, sep2, want_detail)); - } + for (auto& p : all.checks) { + f->open_object_section(p.first.c_str()); + f->dump_stream("severity") << p.second.severity; - if (f) { + f->open_object_section("summary"); + f->dump_string("message", p.second.summary); + f->close_section(); + if (want_detail) { + f->open_array_section("detail"); + for (auto& d : p.second.detail) { + f->open_object_section("detail_item"); + f->dump_string("message", d); + f->close_section(); + } + f->close_section(); + } + f->close_section(); + } + f->close_section(); + f->open_array_section("mutes"); + for (auto& p : healthmon()->mutes) { + f->dump_object("mute", p.second); + } f->close_section(); - f->dump_stream("status") << r; f->close_section(); } else { // one-liner: HEALTH_FOO[ thing1[; thing2 ...]] + string summary; + for (auto& p : all.checks) { + if (!summary.empty()) { + summary += sep2; + } + summary += p.second.summary; + } *plain = stringify(r); if (summary.size()) { *plain += sep1; *plain += summary; } *plain += "\n"; - } - - if (want_detail && !f) { - for (auto& svc : paxos_service) { - svc->get_health_checks().dump_detail(plain); + // detail + for (auto& p : all.checks) { + *plain += p.first + ": " + p.second.summary + "\n"; + for (auto& d : p.second.detail) { + *plain += " "; + *plain += d; + *plain += "\n"; + } } } - return r; } diff --git a/src/mon/health_check.h b/src/mon/health_check.h index 112ab8b53d4..54dc93b92a3 100644 --- a/src/mon/health_check.h +++ b/src/mon/health_check.h @@ -134,52 +134,6 @@ struct health_check_map_t { } } - health_status_t dump_summary(ceph::Formatter *f, std::string *plain, - const char *sep, bool detail) const { - health_status_t r = HEALTH_OK; - for (auto& p : checks) { - if (r > p.second.severity) { - r = p.second.severity; - } - if (f) { - f->open_object_section(p.first.c_str()); - f->dump_stream("severity") << p.second.severity; - - f->open_object_section("summary"); - f->dump_string("message", p.second.summary); - f->close_section(); - - if (detail) { - f->open_array_section("detail"); - for (auto& d : p.second.detail) { - f->open_object_section("detail_item"); - f->dump_string("message", d); - f->close_section(); - } - f->close_section(); - } - f->close_section(); - } else { - if (!plain->empty()) { - *plain += sep; - } - *plain += p.second.summary; - } - } - return r; - } - - void dump_detail(std::string *plain) const { - for (auto& p : checks) { - *plain += p.first + ": " + p.second.summary + "\n"; - for (auto& d : p.second.detail) { - *plain += " "; - *plain += d; - *plain += "\n"; - } - } - } - friend bool operator==(const health_check_map_t& l, const health_check_map_t& r) { return l.checks == r.checks; -- 2.39.5