]>
Commit | Line | Data |
---|---|---|
224ce89b WB |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
4 | #pragma once | |
5 | ||
6 | #include <string> | |
7 | #include <map> | |
8 | ||
9 | #include "include/health.h" | |
9f95a23c | 10 | #include "include/utime.h" |
224ce89b WB |
11 | #include "common/Formatter.h" |
12 | ||
13 | struct health_check_t { | |
14 | health_status_t severity; | |
15 | std::string summary; | |
16 | std::list<std::string> detail; | |
9f95a23c | 17 | int64_t count = 0; |
224ce89b WB |
18 | |
19 | DENC(health_check_t, v, p) { | |
9f95a23c | 20 | DENC_START(2, 1, p); |
224ce89b WB |
21 | denc(v.severity, p); |
22 | denc(v.summary, p); | |
23 | denc(v.detail, p); | |
9f95a23c TL |
24 | if (struct_v >= 2) { |
25 | denc(v.count, p); | |
26 | } | |
224ce89b WB |
27 | DENC_FINISH(p); |
28 | } | |
29 | ||
30 | friend bool operator==(const health_check_t& l, | |
31 | const health_check_t& r) { | |
32 | return l.severity == r.severity && | |
33 | l.summary == r.summary && | |
9f95a23c TL |
34 | l.detail == r.detail && |
35 | l.count == r.count; | |
224ce89b WB |
36 | } |
37 | friend bool operator!=(const health_check_t& l, | |
38 | const health_check_t& r) { | |
39 | return !(l == r); | |
40 | } | |
41 | ||
9f95a23c | 42 | void dump(ceph::Formatter *f, bool want_detail=true) const { |
224ce89b | 43 | f->dump_stream("severity") << severity; |
d2e6a577 FG |
44 | |
45 | f->open_object_section("summary"); | |
46 | f->dump_string("message", summary); | |
9f95a23c | 47 | f->dump_int("count", count); |
d2e6a577 FG |
48 | f->close_section(); |
49 | ||
9f95a23c TL |
50 | if (want_detail) { |
51 | f->open_array_section("detail"); | |
52 | for (auto& p : detail) { | |
53 | f->open_object_section("detail_item"); | |
54 | f->dump_string("message", p); | |
55 | f->close_section(); | |
56 | } | |
d2e6a577 | 57 | f->close_section(); |
224ce89b | 58 | } |
224ce89b WB |
59 | } |
60 | ||
b32b8144 | 61 | static void generate_test_instances(std::list<health_check_t*>& ls) { |
224ce89b WB |
62 | ls.push_back(new health_check_t); |
63 | ls.push_back(new health_check_t); | |
64 | ls.back()->severity = HEALTH_ERR; | |
65 | ls.back()->summary = "summarization"; | |
66 | ls.back()->detail = {"one", "two", "three"}; | |
9f95a23c | 67 | ls.back()->count = 42; |
224ce89b WB |
68 | } |
69 | }; | |
70 | WRITE_CLASS_DENC(health_check_t) | |
71 | ||
72 | ||
9f95a23c TL |
73 | struct health_mute_t { |
74 | std::string code; | |
75 | utime_t ttl; | |
76 | bool sticky = false; | |
77 | string summary; | |
78 | int64_t count; | |
79 | ||
80 | DENC(health_mute_t, v, p) { | |
81 | DENC_START(1, 1, p); | |
82 | denc(v.code, p); | |
83 | denc(v.ttl, p); | |
84 | denc(v.sticky, p); | |
85 | denc(v.summary, p); | |
86 | denc(v.count, p); | |
87 | DENC_FINISH(p); | |
88 | } | |
89 | ||
90 | void dump(ceph::Formatter *f) const { | |
91 | f->dump_string("code", code); | |
92 | if (ttl != utime_t()) { | |
93 | f->dump_stream("ttl") << ttl; | |
94 | } | |
95 | f->dump_bool("sticky", sticky); | |
96 | f->dump_string("summary", summary); | |
97 | f->dump_int("count", count); | |
98 | } | |
99 | ||
100 | static void generate_test_instances(std::list<health_mute_t*>& ls) { | |
101 | ls.push_back(new health_mute_t); | |
102 | ls.push_back(new health_mute_t); | |
103 | ls.back()->code = "OSD_DOWN"; | |
104 | ls.back()->ttl = utime_t(1, 2); | |
105 | ls.back()->sticky = true; | |
106 | ls.back()->summary = "foo bar"; | |
107 | ls.back()->count = 2; | |
108 | } | |
109 | }; | |
110 | WRITE_CLASS_DENC(health_mute_t) | |
111 | ||
224ce89b | 112 | struct health_check_map_t { |
b32b8144 | 113 | std::map<std::string,health_check_t> checks; |
224ce89b WB |
114 | |
115 | DENC(health_check_map_t, v, p) { | |
116 | DENC_START(1, 1, p); | |
117 | denc(v.checks, p); | |
118 | DENC_FINISH(p); | |
119 | } | |
120 | ||
9f95a23c | 121 | void dump(ceph::Formatter *f) const { |
224ce89b WB |
122 | for (auto& p : checks) { |
123 | f->dump_object(p.first.c_str(), p.second); | |
124 | } | |
125 | } | |
126 | ||
b32b8144 | 127 | static void generate_test_instances(std::list<health_check_map_t*>& ls) { |
224ce89b WB |
128 | ls.push_back(new health_check_map_t); |
129 | ls.push_back(new health_check_map_t); | |
130 | { | |
9f95a23c | 131 | auto& d = ls.back()->add("FOO", HEALTH_WARN, "foo", 2); |
224ce89b WB |
132 | d.detail.push_back("a"); |
133 | d.detail.push_back("b"); | |
134 | } | |
135 | { | |
9f95a23c | 136 | auto& d = ls.back()->add("BAR", HEALTH_ERR, "bar!", 3); |
224ce89b WB |
137 | d.detail.push_back("c"); |
138 | d.detail.push_back("d"); | |
9f95a23c | 139 | d.detail.push_back("e"); |
224ce89b WB |
140 | } |
141 | } | |
142 | ||
143 | void clear() { | |
144 | checks.clear(); | |
145 | } | |
c07f9fc5 FG |
146 | bool empty() const { |
147 | return checks.empty(); | |
148 | } | |
224ce89b WB |
149 | void swap(health_check_map_t& other) { |
150 | checks.swap(other.checks); | |
151 | } | |
152 | ||
153 | health_check_t& add(const std::string& code, | |
154 | health_status_t severity, | |
9f95a23c TL |
155 | const std::string& summary, |
156 | int64_t count) { | |
11fdf7f2 | 157 | ceph_assert(checks.count(code) == 0); |
224ce89b WB |
158 | health_check_t& r = checks[code]; |
159 | r.severity = severity; | |
160 | r.summary = summary; | |
9f95a23c | 161 | r.count = count; |
224ce89b WB |
162 | return r; |
163 | } | |
164 | health_check_t& get_or_add(const std::string& code, | |
165 | health_status_t severity, | |
9f95a23c TL |
166 | const std::string& summary, |
167 | int64_t count) { | |
224ce89b WB |
168 | health_check_t& r = checks[code]; |
169 | r.severity = severity; | |
170 | r.summary = summary; | |
9f95a23c | 171 | r.count += count; |
224ce89b WB |
172 | return r; |
173 | } | |
174 | ||
175 | void merge(const health_check_map_t& o) { | |
176 | for (auto& p : o.checks) { | |
177 | auto q = checks.find(p.first); | |
178 | if (q == checks.end()) { | |
179 | // new check | |
180 | checks[p.first] = p.second; | |
181 | } else { | |
182 | // merge details, and hope the summary matches! | |
183 | q->second.detail.insert( | |
184 | q->second.detail.end(), | |
185 | p.second.detail.begin(), | |
186 | p.second.detail.end()); | |
9f95a23c | 187 | q->second.count += p.second.count; |
224ce89b WB |
188 | } |
189 | } | |
190 | } | |
191 | ||
192 | friend bool operator==(const health_check_map_t& l, | |
193 | const health_check_map_t& r) { | |
194 | return l.checks == r.checks; | |
195 | } | |
196 | friend bool operator!=(const health_check_map_t& l, | |
197 | const health_check_map_t& r) { | |
198 | return !(l == r); | |
199 | } | |
200 | }; | |
201 | WRITE_CLASS_DENC(health_check_map_t) |