1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "common/ceph_json.h"
#include "common/errno.h"
#include "include/rados/librados.hpp"
#include "include/stringify.h"
#include "tools/rbd/MirrorDaemonServiceInfo.h"
#include <boost/scope_exit.hpp>
#include <iostream>
#include "json_spirit/json_spirit.h"
namespace rbd {
std::ostream& operator<<(std::ostream& os, MirrorHealth mirror_health) {
switch (mirror_health) {
case MIRROR_HEALTH_OK:
os << "OK";
break;
case MIRROR_HEALTH_UNKNOWN:
os << "UNKNOWN";
break;
case MIRROR_HEALTH_WARNING:
os << "WARNING";
break;
case MIRROR_HEALTH_ERROR:
os << "ERROR";
break;
}
return os;
}
std::string MirrorService::get_image_description() const {
std::string description = (!client_id.empty() ? client_id :
stringify(service_id));
if (!hostname.empty()) {
description += " on " + hostname;
}
return description;
}
void MirrorService::dump_image(
argument_types::Format::Formatter formatter) const {
formatter->open_object_section("daemon_service");
formatter->dump_string("service_id", service_id);
formatter->dump_string("instance_id", instance_id);
formatter->dump_string("daemon_id", client_id);
formatter->dump_string("hostname", hostname);
formatter->close_section();
}
int MirrorDaemonServiceInfo::init() {
int r = get_mirror_service_dump();
if (r < 0) {
return r;
} else if (m_mirror_services.empty()) {
return 0;
}
r = get_mirror_service_status();
if (r < 0) {
return r;
}
return 0;
}
const MirrorService* MirrorDaemonServiceInfo::get_by_service_id(
const std::string& service_id) const {
auto it = m_mirror_services.find(service_id);
if (it == m_mirror_services.end()) {
return nullptr;
}
return &it->second;
}
const MirrorService* MirrorDaemonServiceInfo::get_by_instance_id(
const std::string& instance_id) const {
auto it = m_instance_to_service_ids.find(instance_id);
if (it == m_instance_to_service_ids.end()) {
return nullptr;
}
return get_by_service_id(it->second);
}
MirrorServices MirrorDaemonServiceInfo::get_mirror_services() const {
MirrorServices mirror_services;
for (auto& it : m_mirror_services) {
mirror_services.push_back(it.second);
}
return mirror_services;
}
int MirrorDaemonServiceInfo::get_mirror_service_dump() {
librados::Rados rados(m_io_ctx);
std::string cmd = R"({"prefix": "service dump", "format": "json"})";
bufferlist in_bl;
bufferlist out_bl;
int r = rados.mon_command(cmd, in_bl, &out_bl, nullptr);
if (r < 0) {
std::cerr << "rbd: failed to query services: " << cpp_strerror(r)
<< std::endl;
return r;
}
json_spirit::mValue json_root;
if(!json_spirit::read(out_bl.to_str(), json_root)) {
std::cerr << "rbd: invalid service dump JSON received" << std::endl;
return -EBADMSG;
}
try {
auto& services = json_root.get_obj()["services"];
if (services.is_null()) {
std::cerr << "rbd: missing services in service dump JSON" << std::endl;
return -EBADMSG;
}
auto& service = services.get_obj()["rbd-mirror"];
if (service.is_null()) {
// no rbd-mirror daemons running
return 0;
}
auto& daemons = service.get_obj()["daemons"];
if (daemons.is_null()) {
return 0;
}
for (auto& daemon_pair : daemons.get_obj()) {
// rbd-mirror instances will always be integers but other objects
// are included
auto& service_id = daemon_pair.first;
if (daemon_pair.second.type() != json_spirit::obj_type) {
continue;
}
auto& daemon = daemon_pair.second.get_obj();
auto& metadata_val = daemon["metadata"];
if (metadata_val.is_null()) {
continue;
}
auto& metadata = metadata_val.get_obj();
MirrorService mirror_service{service_id};
auto& client_id = metadata["id"];
if (!client_id.is_null()) {
mirror_service.client_id = client_id.get_str();
}
auto& ceph_version = metadata["ceph_version_short"];
if (!ceph_version.is_null()) {
mirror_service.ceph_version = ceph_version.get_str();
}
auto& hostname = metadata["hostname"];
if (!hostname.is_null()) {
mirror_service.hostname = hostname.get_str();
}
m_mirror_services[service_id] = mirror_service;
}
} catch (std::runtime_error&) {
std::cerr << "rbd: unexpected service dump JSON received" << std::endl;
return -EBADMSG;
}
return 0;
}
int MirrorDaemonServiceInfo::get_mirror_service_status() {
librados::Rados rados(m_io_ctx);
std::string cmd = R"({"prefix": "service status", "format": "json"})";
bufferlist in_bl;
bufferlist out_bl;
int r = rados.mon_command(cmd, in_bl, &out_bl, nullptr);
if (r < 0) {
std::cerr << "rbd: failed to query service status: " << cpp_strerror(r)
<< std::endl;
return r;
}
json_spirit::mValue json_root;
if(!json_spirit::read(out_bl.to_str(), json_root)) {
std::cerr << "rbd: invalid service status JSON received" << std::endl;
return -EBADMSG;
}
bool found_leader = false;
bool found_pool = false;
try {
auto& service = json_root.get_obj()["rbd-mirror"];
if (service.is_null()) {
return 0;
}
for (auto& daemon_pair : service.get_obj()) {
std::string service_id = daemon_pair.first;
auto it = m_mirror_services.find(service_id);
if (it == m_mirror_services.end()) {
continue;
}
auto& mirror_service = it->second;
auto& daemon = daemon_pair.second.get_obj();
auto& status = daemon["status"];
if (status.is_null()) {
mirror_service.callouts.push_back("not reporting status");
mirror_service.health = MIRROR_HEALTH_WARNING;
continue;
}
auto& json = status.get_obj()["json"];
if (json.is_null()) {
mirror_service.callouts.push_back("not reporting status");
mirror_service.health = MIRROR_HEALTH_WARNING;
continue;
}
json_spirit::mValue json_status;
if(!json_spirit::read(json.get_str(), json_status)) {
std::cerr << "rbd: invalid service status daemon status JSON received"
<< std::endl;
return -EBADMSG;
}
auto& pool_val = json_status.get_obj()[stringify(m_io_ctx.get_id())];
if (pool_val.is_null()) {
mirror_service.callouts.push_back("not reporting status for pool");
mirror_service.health = MIRROR_HEALTH_WARNING;
continue;
}
auto& pool = pool_val.get_obj();
found_pool = true;
auto& instance_id = pool["instance_id"];
if (!instance_id.is_null()) {
mirror_service.instance_id = instance_id.get_str();
m_instance_to_service_ids[mirror_service.instance_id] = service_id;
}
auto& leader = pool["leader"];
if (!leader.is_null() && leader.get_bool()) {
mirror_service.leader = true;
found_leader = true;
}
MirrorHealth mirror_service_health = MIRROR_HEALTH_OK;
auto& callouts = pool["callouts"];
if (!callouts.is_null()) {
for (auto& callout_pair : callouts.get_obj()) {
auto& callout = callout_pair.second.get_obj();
auto& level = callout["level"];
if (level.is_null()) {
continue;
}
auto& level_str = level.get_str();
if (mirror_service_health < MIRROR_HEALTH_ERROR &&
level_str == "error") {
mirror_service_health = MIRROR_HEALTH_ERROR;
} else if (mirror_service_health < MIRROR_HEALTH_WARNING &&
level_str == "warning") {
mirror_service_health = MIRROR_HEALTH_WARNING;
}
auto& text = callout["text"];
if (!text.is_null()) {
mirror_service.callouts.push_back(text.get_str());
}
}
}
mirror_service.health = mirror_service_health;
}
} catch (std::runtime_error&) {
std::cerr << "rbd: unexpected service status JSON received" << std::endl;
return -EBADMSG;
}
// compute overall daemon health
m_daemon_health = MIRROR_HEALTH_OK;
if (!found_pool) {
// no daemons are reporting status for this pool
m_daemon_health = MIRROR_HEALTH_ERROR;
} else if (!found_leader) {
// no daemons are reporting leader role for this pool
m_daemon_health = MIRROR_HEALTH_WARNING;
}
for (auto& pair : m_mirror_services) {
m_daemon_health = std::max(m_daemon_health, pair.second.health);
}
return 0;
}
} // namespace rbd
|