]>
git.proxmox.com Git - ceph.git/blob - ceph/src/mon/health_check.h
51f0e1095d8e5179d4481f94c81f4844d189935a
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
9 #include "include/health.h"
10 #include "common/Formatter.h"
12 struct health_check_t
{
13 health_status_t severity
;
15 std::list
<std::string
> detail
;
17 DENC(health_check_t
, v
, p
) {
25 friend bool operator==(const health_check_t
& l
,
26 const health_check_t
& r
) {
27 return l
.severity
== r
.severity
&&
28 l
.summary
== r
.summary
&&
31 friend bool operator!=(const health_check_t
& l
,
32 const health_check_t
& r
) {
36 void dump(Formatter
*f
) const {
37 f
->dump_stream("severity") << severity
;
38 f
->dump_string("summary", summary
);
39 f
->open_array_section("detail");
40 for (auto& p
: detail
) {
41 f
->dump_string("item", p
);
46 static void generate_test_instances(list
<health_check_t
*>& ls
) {
47 ls
.push_back(new health_check_t
);
48 ls
.push_back(new health_check_t
);
49 ls
.back()->severity
= HEALTH_ERR
;
50 ls
.back()->summary
= "summarization";
51 ls
.back()->detail
= {"one", "two", "three"};
54 WRITE_CLASS_DENC(health_check_t
)
57 struct health_check_map_t
{
58 map
<std::string
,health_check_t
> checks
;
60 DENC(health_check_map_t
, v
, p
) {
66 void dump(Formatter
*f
) const {
67 for (auto& p
: checks
) {
68 f
->dump_object(p
.first
.c_str(), p
.second
);
72 static void generate_test_instances(list
<health_check_map_t
*>& ls
) {
73 ls
.push_back(new health_check_map_t
);
74 ls
.push_back(new health_check_map_t
);
76 auto& d
= ls
.back()->add("FOO", HEALTH_WARN
, "foo");
77 d
.detail
.push_back("a");
78 d
.detail
.push_back("b");
81 auto& d
= ls
.back()->add("BAR", HEALTH_ERR
, "bar!");
82 d
.detail
.push_back("c");
83 d
.detail
.push_back("d");
91 return checks
.empty();
93 void swap(health_check_map_t
& other
) {
94 checks
.swap(other
.checks
);
97 health_check_t
& add(const std::string
& code
,
98 health_status_t severity
,
99 const std::string
& summary
) {
100 assert(checks
.count(code
) == 0);
101 health_check_t
& r
= checks
[code
];
102 r
.severity
= severity
;
106 health_check_t
& get_or_add(const std::string
& code
,
107 health_status_t severity
,
108 const std::string
& summary
) {
109 health_check_t
& r
= checks
[code
];
110 r
.severity
= severity
;
115 void merge(const health_check_map_t
& o
) {
116 for (auto& p
: o
.checks
) {
117 auto q
= checks
.find(p
.first
);
118 if (q
== checks
.end()) {
120 checks
[p
.first
] = p
.second
;
122 // merge details, and hope the summary matches!
123 q
->second
.detail
.insert(
124 q
->second
.detail
.end(),
125 p
.second
.detail
.begin(),
126 p
.second
.detail
.end());
131 health_status_t
dump_summary(Formatter
*f
, std::string
*plain
,
132 const char *sep
, bool detail
) const {
133 health_status_t r
= HEALTH_OK
;
134 for (auto& p
: checks
) {
135 if (r
> p
.second
.severity
) {
136 r
= p
.second
.severity
;
139 f
->open_object_section(p
.first
.c_str());
140 f
->dump_stream("severity") << p
.second
.severity
;
141 f
->dump_string("message", p
.second
.summary
);
143 f
->open_array_section("detail");
144 for (auto& d
: p
.second
.detail
) {
145 f
->dump_string("item", d
);
151 if (!plain
->empty()) {
154 *plain
+= p
.second
.summary
;
160 void dump_summary_compat(Formatter
*f
) const {
161 for (auto& p
: checks
) {
162 f
->open_object_section("item");
163 f
->dump_stream("severity") << p
.second
.severity
;
164 f
->dump_string("summary", p
.second
.summary
);
169 void dump_detail(Formatter
*f
, std::string
*plain
, bool compat
) const {
170 for (auto& p
: checks
) {
173 // this is sloppy, but the best we can do: just dump all of the
174 // individual checks' details together
175 for (auto& d
: p
.second
.detail
) {
176 f
->dump_string("item", d
);
181 *plain
+= p
.first
+ " " + p
.second
.summary
+ "\n";
183 for (auto& d
: p
.second
.detail
) {
194 friend bool operator==(const health_check_map_t
& l
,
195 const health_check_map_t
& r
) {
196 return l
.checks
== r
.checks
;
198 friend bool operator!=(const health_check_map_t
& l
,
199 const health_check_map_t
& r
) {
203 WRITE_CLASS_DENC(health_check_map_t
)